From 167ef624b31d99f3283fbe13e541af3e53f99fa0 Mon Sep 17 00:00:00 2001 From: Sakura Akeno Isayeki Date: Sat, 24 Feb 2024 11:37:20 +0100 Subject: [PATCH] feat(authentication): Update Jwt and Cookie authentication handlers - 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. --- .../Cookie/ForwardCookieAuthenticationHandler.cs | 4 ++-- .../Authentication/Jwt/JwtAuthenticationHandler.cs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/WowsKarma.Api/Services/Authentication/Cookie/ForwardCookieAuthenticationHandler.cs b/WowsKarma.Api/Services/Authentication/Cookie/ForwardCookieAuthenticationHandler.cs index d660660..1654761 100644 --- a/WowsKarma.Api/Services/Authentication/Cookie/ForwardCookieAuthenticationHandler.cs +++ b/WowsKarma.Api/Services/Authentication/Cookie/ForwardCookieAuthenticationHandler.cs @@ -15,8 +15,8 @@ public sealed class ForwardCookieAuthenticationHandler : JwtAuthenticationHandle { private readonly string _cookieName; - public ForwardCookieAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, UserService userService, IConfiguration configuration) - : base(options, logger, encoder, clock, userService) + public ForwardCookieAuthenticationHandler(IOptionsMonitor 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"); diff --git a/WowsKarma.Api/Services/Authentication/Jwt/JwtAuthenticationHandler.cs b/WowsKarma.Api/Services/Authentication/Jwt/JwtAuthenticationHandler.cs index 51fc1a1..1820e7b 100644 --- a/WowsKarma.Api/Services/Authentication/Jwt/JwtAuthenticationHandler.cs +++ b/WowsKarma.Api/Services/Authentication/Jwt/JwtAuthenticationHandler.cs @@ -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 options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, UserService userService) - : base(options, logger, encoder, clock) + public JwtAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, UserService userService) + : base(options, logger, encoder) { - this.userService = userService; + _userService = userService; } protected override async Task HandleAuthenticateAsync() @@ -26,13 +26,13 @@ protected override async Task 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; } @@ -45,7 +45,7 @@ protected override async Task HandleAuthenticateAsync() return isValid ? baseResult - : failure == default + : failure is null ? AuthenticateResult.NoResult() : AuthenticateResult.Fail(failure.Message); }