From d4f8bbb78feffdeb3c7443c17eaffac4d97347d6 Mon Sep 17 00:00:00 2001 From: Marcin Badurowicz Date: Tue, 12 Nov 2024 22:43:12 +0100 Subject: [PATCH] Update to .NET 9 --- src/ApiKeyHeaderAuthenticationHandler.cs | 45 ++++++++++++++----- ...NetCore.Authentication.ApiKeyHeader.csproj | 4 +- ...e.Authentication.ApiKeyHeader.Tests.csproj | 2 +- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/ApiKeyHeaderAuthenticationHandler.cs b/src/ApiKeyHeaderAuthenticationHandler.cs index bd95a17..d2fcb6e 100644 --- a/src/ApiKeyHeaderAuthenticationHandler.cs +++ b/src/ApiKeyHeaderAuthenticationHandler.cs @@ -50,7 +50,8 @@ namespace Ktos.AspNetCore.Authentication.ApiKeyHeader /// /// Handles ApiKeyHeader authentication scheme /// - public class ApiKeyHeaderAuthenticationHandler : AuthenticationHandler + public class ApiKeyHeaderAuthenticationHandler + : AuthenticationHandler { /// /// Initializes a new instance of ApiKeyHeaderAuthenticationHandler @@ -59,9 +60,13 @@ public class ApiKeyHeaderAuthenticationHandler : AuthenticationHandler /// /// - public ApiKeyHeaderAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock) - { - } + public ApiKeyHeaderAuthenticationHandler( + IOptionsMonitor options, + ILoggerFactory logger, + UrlEncoder encoder, + ISystemClock clock + ) + : base(options, logger, encoder, clock) { } /// /// Handles authentication by checking if there is proper api key set in HTTP header @@ -69,15 +74,22 @@ public ApiKeyHeaderAuthenticationHandler(IOptionsMonitorReturns Claim with name if authentication was successful or NoResult of not protected override async Task HandleAuthenticateAsync() { - var registeredHandler = Context.RequestServices.GetService(typeof(IApiKeyCustomAuthenticator)); - var registeredHandler2 = Context.RequestServices.GetService(typeof(IApiKeyCustomAuthenticationTicketHandler)); + var registeredHandler = Context.RequestServices.GetService( + typeof(IApiKeyCustomAuthenticator) + ); + var registeredHandler2 = Context.RequestServices.GetService( + typeof(IApiKeyCustomAuthenticationTicketHandler) + ); var headerKey = Context.Request.Headers[Options.Header].FirstOrDefault(); if (headerKey == null) { return AuthenticateResult.NoResult(); } - else if (Options.CustomAuthenticationHandler != null && !Options.UseRegisteredAuthenticationHandler) + else if ( + Options.CustomAuthenticationHandler != null + && !Options.UseRegisteredAuthenticationHandler + ) { var (result, claimName) = Options.CustomAuthenticationHandler(headerKey); @@ -92,7 +104,9 @@ protected override async Task HandleAuthenticateAsync() } else if (registeredHandler != null && Options.UseRegisteredAuthenticationHandler) { - var (result, claimName) = (registeredHandler as IApiKeyCustomAuthenticator).CustomAuthenticationHandler(headerKey); + var (result, claimName) = ( + registeredHandler as IApiKeyCustomAuthenticator + ).CustomAuthenticationHandler(headerKey); if (result) { @@ -105,7 +119,9 @@ protected override async Task HandleAuthenticateAsync() } else if (registeredHandler2 != null && Options.UseRegisteredAuthenticationHandler) { - return (registeredHandler2 as IApiKeyCustomAuthenticationTicketHandler).CustomAuthenticationHandler(headerKey); + return ( + registeredHandler2 as IApiKeyCustomAuthenticationTicketHandler + ).CustomAuthenticationHandler(headerKey); } else if (headerKey == Options.ApiKey && !Options.UseRegisteredAuthenticationHandler) { @@ -117,14 +133,19 @@ protected override async Task HandleAuthenticateAsync() } } - private AuthenticationTicket CreateAuthenticationTicket(string claimName = ApiKeyHeaderAuthenticationDefaults.AuthenticationClaimName) + private AuthenticationTicket CreateAuthenticationTicket( + string claimName = ApiKeyHeaderAuthenticationDefaults.AuthenticationClaimName + ) { var claims = new[] { new Claim(ClaimTypes.Name, claimName) }; var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); - var at = new AuthenticationTicket(principal, ApiKeyHeaderAuthenticationDefaults.AuthenticationScheme); + var at = new AuthenticationTicket( + principal, + ApiKeyHeaderAuthenticationDefaults.AuthenticationScheme + ); //Context.User.AddIdentity(new ClaimsIdentity(ApiKeyHeaderAuthenticationDefaults.AuthenticationScheme)); return at; } } -} \ No newline at end of file +} diff --git a/src/Ktos.AspNetCore.Authentication.ApiKeyHeader.csproj b/src/Ktos.AspNetCore.Authentication.ApiKeyHeader.csproj index 9c57e5e..9a1f4e8 100644 --- a/src/Ktos.AspNetCore.Authentication.ApiKeyHeader.csproj +++ b/src/Ktos.AspNetCore.Authentication.ApiKeyHeader.csproj @@ -1,12 +1,12 @@  - net7.0 + net9.0 Ktos.AspNetCore.Authentication.ApiKeyHeader Marcin Badurowicz $(NoWarn);CS1998 Api Key in HTTP Header Authentication Scheme for ASP.NET Core - 7.0.0 + 9.0.0 true api;apikey;aspnetcore;authentication;security https://github.com/ktos/Ktos.AspNetCore.Authentication.ApiKeyHeader diff --git a/test/Ktos.AspNetCore.Authentication.ApiKeyHeader.Tests.csproj b/test/Ktos.AspNetCore.Authentication.ApiKeyHeader.Tests.csproj index 6a88b9a..f47433d 100644 --- a/test/Ktos.AspNetCore.Authentication.ApiKeyHeader.Tests.csproj +++ b/test/Ktos.AspNetCore.Authentication.ApiKeyHeader.Tests.csproj @@ -1,7 +1,7 @@  - net7.0 + net9.0 $(NoWarn);CS1591 false true