Skip to content

Commit

Permalink
Update to .NET 9
Browse files Browse the repository at this point in the history
  • Loading branch information
ktos committed Nov 12, 2024
1 parent e527652 commit d4f8bbb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
45 changes: 33 additions & 12 deletions src/ApiKeyHeaderAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ namespace Ktos.AspNetCore.Authentication.ApiKeyHeader
/// <summary>
/// Handles ApiKeyHeader authentication scheme
/// </summary>
public class ApiKeyHeaderAuthenticationHandler : AuthenticationHandler<ApiKeyHeaderAuthenticationOptions>
public class ApiKeyHeaderAuthenticationHandler
: AuthenticationHandler<ApiKeyHeaderAuthenticationOptions>
{
/// <summary>
/// Initializes a new instance of ApiKeyHeaderAuthenticationHandler
Expand All @@ -59,25 +60,36 @@ public class ApiKeyHeaderAuthenticationHandler : AuthenticationHandler<ApiKeyHea
/// <param name="logger"></param>
/// <param name="encoder"></param>
/// <param name="clock"></param>
public ApiKeyHeaderAuthenticationHandler(IOptionsMonitor<ApiKeyHeaderAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
public ApiKeyHeaderAuthenticationHandler(
IOptionsMonitor<ApiKeyHeaderAuthenticationOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock
)
: base(options, logger, encoder, clock) { }

/// <summary>
/// Handles authentication by checking if there is proper api key set in HTTP header
/// </summary>
/// <returns>Returns Claim with name if authentication was successful or NoResult of not</returns>
protected override async Task<AuthenticateResult> 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);

Expand All @@ -92,7 +104,9 @@ protected override async Task<AuthenticateResult> 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)
{
Expand All @@ -105,7 +119,9 @@ protected override async Task<AuthenticateResult> 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)
{
Expand All @@ -117,14 +133,19 @@ protected override async Task<AuthenticateResult> 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;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Ktos.AspNetCore.Authentication.ApiKeyHeader.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<PackageId>Ktos.AspNetCore.Authentication.ApiKeyHeader</PackageId>
<Authors>Marcin Badurowicz</Authors>
<NoWarn>$(NoWarn);CS1998</NoWarn>
<Description>Api Key in HTTP Header Authentication Scheme for ASP.NET Core</Description>
<PackageVersion>7.0.0</PackageVersion>
<PackageVersion>9.0.0</PackageVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>api;apikey;aspnetcore;authentication;security</PackageTags>
<PackageProjectUrl>https://github.com/ktos/Ktos.AspNetCore.Authentication.ApiKeyHeader</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<IsPackable>false</IsPackable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand Down

0 comments on commit d4f8bbb

Please sign in to comment.