-
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
9 changed files
with
133 additions
and
11 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
50 changes: 50 additions & 0 deletions
50
sources/Clients.Admin/Application/Authentications/GrpcHttpMessageHandler.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,50 @@ | ||
using MadWorldNL.Clients.Admin.Domain.Authentications; | ||
|
||
namespace MadWorldNL.Clients.Admin.Application.Authentications; | ||
|
||
public class GrpcHttpMessageHandler : DelegatingHandler | ||
{ | ||
private readonly IAuthenticationManager _authenticationManager; | ||
|
||
public GrpcHttpMessageHandler(IAuthenticationManager authenticationManager) | ||
{ | ||
_authenticationManager = authenticationManager; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var token = await GetJwtTokenAsync(); | ||
request.Headers.Add("Authorization", $"Bearer {token}"); | ||
|
||
return await base.SendAsync(request, cancellationToken); | ||
} | ||
|
||
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
return SendAsync(request, cancellationToken).GetAwaiter().GetResult(); | ||
} | ||
|
||
private async Task<string> GetJwtTokenAsync() | ||
{ | ||
var authenticationToken = await _authenticationManager.GetCurrentAuthenticationTokenAsync(); | ||
|
||
if (!authenticationToken.IsSuccess) | ||
{ | ||
throw new AuthenticationTokenException(); | ||
} | ||
|
||
if (authenticationToken.Expires >= DateTime.UtcNow.AddMinutes(5)) | ||
{ | ||
return authenticationToken.AccessToken; | ||
} | ||
|
||
authenticationToken = await _authenticationManager.RefreshTokenAsync(); | ||
|
||
if (!authenticationToken.IsSuccess) | ||
{ | ||
throw new AuthenticationTokenException(); | ||
} | ||
|
||
return authenticationToken.AccessToken; | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
sources/Clients.Admin/Application/Authentications/IAuthenticationManager.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,11 +1,13 @@ | ||
using MadWorldNL.Clients.Admin.Domain; | ||
using MadWorldNL.Clients.Admin.Domain.Authentications; | ||
|
||
namespace MadWorldNL.Clients.Admin.Application.Authentications; | ||
|
||
public interface IAuthenticationManager | ||
{ | ||
Task<AuthenticationToken> GetCurrentAuthenticationTokenAsync(); | ||
Task<AuthenticationToken> LoginFromSessionAsync(); | ||
Task<AuthenticationToken> LoginAsync(string username, string password); | ||
|
||
Task LogoutAsync(); | ||
Task<AuthenticationToken> RefreshTokenAsync(); | ||
} |
2 changes: 1 addition & 1 deletion
2
...ients.Admin/Domain/AuthenticationToken.cs → ...in/Authentications/AuthenticationToken.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
6 changes: 6 additions & 0 deletions
6
sources/Clients.Admin/Domain/Authentications/AuthenticationTokenException.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.Admin.Domain.Authentications; | ||
|
||
public class AuthenticationTokenException : 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
3 changes: 2 additions & 1 deletion
3
sources/Clients.Admin/Services/Authentications/IAuthenticationService.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,8 +1,9 @@ | ||
using MadWorldNL.Clients.Admin.Domain; | ||
using MadWorldNL.Clients.Admin.Domain.Authentications; | ||
|
||
namespace MadWorldNL.Clients.Admin.Services.Authentications; | ||
|
||
public interface IAuthenticationService | ||
{ | ||
AuthenticationToken Login(string username, string password, string audience); | ||
AuthenticationToken RefreshToken(string refreshToken); | ||
} |
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