-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
131 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
sources/Clients.Identity.Blazor.Shared/Authentications/AuthenticationLocalStorage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Blazored.LocalStorage; | ||
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications; | ||
|
||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; | ||
|
||
public class AuthenticationLocalStorage : IAuthenticationStorage | ||
{ | ||
private const string AuthenticationResponseName = "AuthenticationResponse"; | ||
|
||
private readonly ILocalStorageService _localStorage; | ||
|
||
public AuthenticationLocalStorage(ILocalStorageService localStorage) | ||
{ | ||
_localStorage = localStorage; | ||
} | ||
|
||
public async Task SetAccessTokenAsync(LoginProxyResponse response) | ||
{ | ||
await _localStorage.SetItemAsync(AuthenticationResponseName, response); | ||
} | ||
|
||
public async Task<LoginProxyResponse> GetAccessTokenAsync() | ||
{ | ||
return await _localStorage.GetItemAsync<LoginProxyResponse>(AuthenticationResponseName) | ||
?? new LoginProxyResponse(); | ||
} | ||
} |
19 changes: 17 additions & 2 deletions
19
sources/Clients.Identity.Blazor.Shared/Authentications/AuthenticationManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
|
||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; | ||
|
||
public class AuthenticationManager : IAuthenticationManager | ||
{ | ||
private readonly IAuthenticationService _authenticationService; | ||
private readonly IAuthenticationStorage _authenticationStorage; | ||
private readonly AuthenticationStateProvider _authenticationStateProvider; | ||
|
||
public AuthenticationManager(IAuthenticationService authenticationService) | ||
public AuthenticationManager(IAuthenticationService authenticationService, IAuthenticationStorage authenticationStorage, AuthenticationStateProvider authenticationStateProvider) | ||
{ | ||
_authenticationService = authenticationService; | ||
_authenticationStorage = authenticationStorage; | ||
_authenticationStateProvider = authenticationStateProvider; | ||
} | ||
|
||
public async Task<LoginProxyResponse> LoginAsync(LoginProxyRequest request) | ||
{ | ||
return await _authenticationService.LoginAsync(request); | ||
var response = await _authenticationService.LoginAsync(request); | ||
await _authenticationStorage.SetAccessTokenAsync(response); | ||
await _authenticationStateProvider.GetAuthenticationStateAsync(); | ||
|
||
return response; | ||
} | ||
|
||
public async Task LogoutAsync() | ||
{ | ||
await _authenticationStorage.SetAccessTokenAsync(new LoginProxyResponse()); | ||
await _authenticationStateProvider.GetAuthenticationStateAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
sources/Clients.Identity.Blazor.Shared/Authentications/IAuthenticationStorage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications; | ||
|
||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; | ||
|
||
public interface IAuthenticationStorage | ||
{ | ||
Task SetAccessTokenAsync(LoginProxyResponse response); | ||
Task<LoginProxyResponse> GetAccessTokenAsync(); | ||
} |
35 changes: 32 additions & 3 deletions
35
sources/Clients.Identity.Blazor.Shared/Authentications/MyAuthenticationStateProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,44 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
|
||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; | ||
|
||
public class MyAuthenticationStateProvider : AuthenticationStateProvider | ||
{ | ||
public override Task<AuthenticationState> GetAuthenticationStateAsync() | ||
private readonly IAuthenticationStorage _authenticationStorage; | ||
|
||
public MyAuthenticationStateProvider(IAuthenticationStorage authenticationStorage) | ||
{ | ||
_authenticationStorage = authenticationStorage; | ||
} | ||
|
||
public override async Task<AuthenticationState> GetAuthenticationStateAsync() | ||
{ | ||
var state = new AuthenticationState(new ClaimsPrincipal()); | ||
var accessToken = await _authenticationStorage.GetAccessTokenAsync(); | ||
|
||
var identity = new ClaimsIdentity(); | ||
if (accessToken.IsSuccess) | ||
{ | ||
identity = RetrieveUserFromJwt(accessToken.AccessToken); | ||
} | ||
|
||
var state = new AuthenticationState(new ClaimsPrincipal(identity)); | ||
|
||
NotifyAuthenticationStateChanged(Task.FromResult(state)); | ||
return Task.FromResult(state); | ||
return state; | ||
} | ||
|
||
private static ClaimsIdentity RetrieveUserFromJwt(string jwt) | ||
{ | ||
var claims = ParseClaimsFromJwt(jwt).ToList(); | ||
return new ClaimsIdentity(claims, "jwt", "nameid", "role"); | ||
} | ||
|
||
private static IEnumerable<Claim> ParseClaimsFromJwt(string jwt) | ||
{ | ||
var handler = new JwtSecurityTokenHandler(); | ||
var token = handler.ReadJwtToken(jwt); | ||
return token.Claims; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@page "/Logout" | ||
@using MadWorldNL.Clients.Identity.Blazor.Shared.Authentications | ||
<h3>You are logged out now!</h3> | ||
|
||
@code { | ||
[Inject] public IAuthenticationManager AuthenticationManager { get; set; } = null!; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await AuthenticationManager.LogoutAsync(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters