-
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
20 changed files
with
229 additions
and
14 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
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
6 changes: 6 additions & 0 deletions
6
sources/Clients.Identity.Api.Contracts/Authentications/LogoutProxyResponse.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,6 @@ | ||
namespace MadWorldNL.Clients.Identity.Api.Contracts.Authentications; | ||
|
||
public class LogoutProxyResponse | ||
{ | ||
public bool IsSuccess { get; set; } | ||
} |
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
29 changes: 29 additions & 0 deletions
29
sources/Clients.Identity.Blazor.Shared/Accounts/AccountManager.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,29 @@ | ||
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications; | ||
using MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
|
||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Accounts; | ||
|
||
public class AccountManager : IAccountManager | ||
{ | ||
private readonly IAccountService _accountService; | ||
private readonly IAuthenticationStorage _authenticationStorage; | ||
private readonly AuthenticationStateProvider _authenticationStateProvider; | ||
|
||
public AccountManager( | ||
IAccountService accountService, | ||
IAuthenticationStorage authenticationStorage, | ||
AuthenticationStateProvider authenticationStateProvider) | ||
{ | ||
_accountService = accountService; | ||
_authenticationStorage = authenticationStorage; | ||
_authenticationStateProvider = authenticationStateProvider; | ||
} | ||
|
||
public async Task LogoutAsync() | ||
{ | ||
await _accountService.LogoutAsync(); | ||
await _authenticationStorage.SetAccessTokenAsync(new LoginProxyResponse()); | ||
await _authenticationStateProvider.GetAuthenticationStateAsync(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
sources/Clients.Identity.Blazor.Shared/Accounts/AccountService.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,23 @@ | ||
using System.Net.Http.Json; | ||
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications; | ||
using MadWorldNL.Clients.Identity.Blazor.Shared.Settings; | ||
|
||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Accounts; | ||
|
||
public class AccountService : IAccountService | ||
{ | ||
private const string Endpoint = "Authentication"; | ||
|
||
private readonly HttpClient _httpClient; | ||
|
||
public AccountService(IHttpClientFactory clientFactory) | ||
{ | ||
_httpClient = clientFactory.CreateClient(ApiTypes.Identity); | ||
} | ||
|
||
public async Task<LogoutProxyResponse> LogoutAsync() | ||
{ | ||
return await _httpClient.GetFromJsonAsync<LogoutProxyResponse>($"{Endpoint}/Logout") | ||
?? new LogoutProxyResponse(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
sources/Clients.Identity.Blazor.Shared/Accounts/IAccountManager.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,6 @@ | ||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Accounts; | ||
|
||
public interface IAccountManager | ||
{ | ||
Task LogoutAsync(); | ||
} |
8 changes: 8 additions & 0 deletions
8
sources/Clients.Identity.Blazor.Shared/Accounts/IAccountService.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,8 @@ | ||
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications; | ||
|
||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Accounts; | ||
|
||
public interface IAccountService | ||
{ | ||
Task<LogoutProxyResponse> 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
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
65 changes: 65 additions & 0 deletions
65
sources/Clients.Identity.Blazor.Shared/Authentications/MyHttpMessageHandler.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,65 @@ | ||
using System.Net; | ||
using System.Net.Http.Headers; | ||
|
||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; | ||
|
||
public class MyHttpMessageHandler : DelegatingHandler | ||
{ | ||
private readonly IAuthenticationManager _authenticationManager; | ||
|
||
public MyHttpMessageHandler(IAuthenticationManager authenticationManager) | ||
{ | ||
_authenticationManager = authenticationManager; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
await AddAuthorizationHeader(request); | ||
return await base.SendAsync(request, cancellationToken); | ||
} | ||
catch (RefreshTokenInvalidException) | ||
{ | ||
return CreateExceptionMessage(); | ||
} | ||
} | ||
|
||
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
AddAuthorizationHeader(request).Wait(cancellationToken); | ||
return base.Send(request, cancellationToken); | ||
} | ||
catch (RefreshTokenInvalidException) | ||
{ | ||
return CreateExceptionMessage(); | ||
} | ||
} | ||
|
||
private async Task AddAuthorizationHeader(HttpRequestMessage request) | ||
{ | ||
var accessToken = await _authenticationManager.GetActiveTokenFromSession(); | ||
|
||
if (!accessToken.IsSuccess) | ||
{ | ||
throw new RefreshTokenInvalidException(); | ||
} | ||
|
||
AddBearerToken(request, accessToken.AccessToken); | ||
} | ||
|
||
private static void AddBearerToken(HttpRequestMessage request, string token) | ||
{ | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); | ||
} | ||
|
||
private static HttpResponseMessage CreateExceptionMessage() | ||
{ | ||
return new HttpResponseMessage() | ||
{ | ||
StatusCode = HttpStatusCode.Unauthorized, | ||
}; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
sources/Clients.Identity.Blazor.Shared/Authentications/RefreshTokenInvalidException.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,6 @@ | ||
namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications; | ||
|
||
public class RefreshTokenInvalidException : Exception | ||
{ | ||
|
||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
@page "/Logout" | ||
@using MadWorldNL.Clients.Identity.Blazor.Shared.Authentications | ||
@using MadWorldNL.Clients.Identity.Blazor.Shared.Accounts | ||
|
||
<h3>You are logged out now!</h3> | ||
|
||
@code { | ||
[Inject] public IAuthenticationManager AuthenticationManager { get; set; } = null!; | ||
[Inject] public IAccountManager AccountManager { get; set; } = null!; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
await AuthenticationManager.LogoutAsync(); | ||
await AccountManager.LogoutAsync(); | ||
} | ||
|
||
} |
Oops, something went wrong.