Skip to content

Commit

Permalink
feat(authentication): Update Jwt and Cookie authentication handlers
Browse files Browse the repository at this point in the history
- Updated the `ForwardCookieAuthenticationHandler` constructor to remove the unnecessary `ISystemClock` parameter.
- Updated the `JwtAuthenticationHandler` constructor to remove the unnecessary `ISystemClock` parameter and renamed the `userService` field to `_userService`.
- Refactored the code in the `HandleAuthenticateAsync()` method of `JwtAuthenticationHandler` to improve readability and maintainability.
  • Loading branch information
SakuraIsayeki committed Feb 24, 2024
1 parent 6c8bdb1 commit 167ef62
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public sealed class ForwardCookieAuthenticationHandler : JwtAuthenticationHandle
{
private readonly string _cookieName;

public ForwardCookieAuthenticationHandler(IOptionsMonitor<JwtBearerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, UserService userService, IConfiguration configuration)
: base(options, logger, encoder, clock, userService)
public ForwardCookieAuthenticationHandler(IOptionsMonitor<JwtBearerOptions> options, ILoggerFactory logger, UrlEncoder encoder, UserService userService, IConfiguration configuration)
: base(options, logger, encoder, userService)
{
_cookieName = configuration[$"API:{Startup.ApiRegion.ToRegionString()}:CookieName"]
?? throw new($"Missing configuration value for API:{Startup.ApiRegion.ToRegionString()}:CookieName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace WowsKarma.Api.Services.Authentication.Jwt;

public class JwtAuthenticationHandler : JwtBearerHandler
{
private readonly UserService userService;
private readonly UserService _userService;

public JwtAuthenticationHandler(IOptionsMonitor<JwtBearerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, UserService userService)
: base(options, logger, encoder, clock)
public JwtAuthenticationHandler(IOptionsMonitor<JwtBearerOptions> options, ILoggerFactory logger, UrlEncoder encoder, UserService userService)
: base(options, logger, encoder)
{
this.userService = userService;
_userService = userService;
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
Expand All @@ -26,13 +26,13 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
}

bool isValid = false;
Exception failure = default;
Exception? failure = null;

try
{
if (baseResult.Principal.FindFirstValue("seed") is { } seed
&& uint.TryParse(baseResult.Principal.FindFirstValue(ClaimTypes.NameIdentifier), out uint id)
&& await userService.ValidateUserSeedTokenAsync(id, new(seed)))
&& await _userService.ValidateUserSeedTokenAsync(id, new(seed)))
{
isValid = true;
}
Expand All @@ -45,7 +45,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()

return isValid
? baseResult
: failure == default
: failure is null
? AuthenticateResult.NoResult()
: AuthenticateResult.Fail(failure.Message);
}
Expand Down

0 comments on commit 167ef62

Please sign in to comment.