Skip to content

Commit e29771f

Browse files
authored
5.1.0 changes (#22)
- WWW-Authenticate challenge header now returns SchemeName as scheme part instead of ApiKeyOptions.KeyName - WWW-Authenticate challenge header now has 2 new parameters 'in' and 'key_name' in value part - ForLegacyUseKeyNameAsSchemeNameOnWWWAuthenticateHeader added to the ApiKeyOptions - In Authorization Header now able to use either SchemeName or ApiKeyOptions.KeyName when matching AuthorizationHeader Scheme - Visibility of all the handlers changed to public - Tests added - Readme updated - Copyright year updated on License
1 parent 9a482c1 commit e29771f

35 files changed

+4320
-63
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Mihir Dilip
3+
Copyright (c) 2021 Mihir Dilip
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 142 additions & 41 deletions
Large diffs are not rendered by default.

samples/SampleWebApi.Shared/Services/ApiKeyProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public ApiKeyProvider(ILogger<IApiKeyProvider> logger, IApiKeyRepository apiKeyR
1616
_apiKeyRepository = apiKeyRepository;
1717
}
1818

19-
public Task<IApiKey> ProvideAsync(string key)
19+
public async Task<IApiKey> ProvideAsync(string key)
2020
{
2121
try
2222
{
23-
return _apiKeyRepository.GetApiKeyAsync(key);
23+
return await _apiKeyRepository.GetApiKeyAsync(key);
2424
}
2525
catch (System.Exception exception)
2626
{

samples/SampleWebApi_2_0/SampleWebApi_2_0.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.0.0" />
15+
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.1.0" />
1616
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
1717
</ItemGroup>
1818

samples/SampleWebApi_2_2/SampleWebApi_2_2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.0.0" />
9+
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.1.0" />
1010
<PackageReference Include="Microsoft.AspNetCore.App" />
1111
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
1212
</ItemGroup>

samples/SampleWebApi_3_1/SampleWebApi_3_1.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />
88

99
<ItemGroup>
10-
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.0.0" />
10+
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.1.0" />
1111
</ItemGroup>
1212

1313
<!--<ItemGroup>

samples/SampleWebApi_5_0/SampleWebApi_5_0.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Import Project="..\SampleWebApi.Shared\SampleWebApi.Shared.projitems" Label="Shared" />
88

99
<ItemGroup>
10-
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.0.0" />
10+
<PackageReference Include="AspNetCore.Authentication.ApiKey" Version="5.1.0" />
1111
</ItemGroup>
1212

1313
<!--<ItemGroup>

src/AspNetCore.Authentication.ApiKey.sln

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_2_2", "..\samp
2424
EndProject
2525
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_3_1", "..\samples\SampleWebApi_3_1\SampleWebApi_3_1.csproj", "{CABEEEAE-3974-4CC4-97F1-18C8D2188DAF}"
2626
EndProject
27-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_5_0", "..\samples\SampleWebApi_5_0\SampleWebApi_5_0.csproj", "{1E1E202B-EFB2-40FD-8271-659F36084916}"
27+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_5_0", "..\samples\SampleWebApi_5_0\SampleWebApi_5_0.csproj", "{1E1E202B-EFB2-40FD-8271-659F36084916}"
28+
EndProject
29+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.Authentication.ApiKey.Tests", "..\test\AspNetCore.Authentication.ApiKey.Tests\AspNetCore.Authentication.ApiKey.Tests.csproj", "{EA2A367F-2D2D-4C20-8C32-C19F67E73187}"
30+
EndProject
31+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{A15FB7AB-5B7A-4428-BEBA-32DEE3C88C39}"
2832
EndProject
2933
Global
3034
GlobalSection(SharedMSBuildProjectFiles) = preSolution
@@ -59,6 +63,10 @@ Global
5963
{1E1E202B-EFB2-40FD-8271-659F36084916}.Debug|Any CPU.Build.0 = Debug|Any CPU
6064
{1E1E202B-EFB2-40FD-8271-659F36084916}.Release|Any CPU.ActiveCfg = Release|Any CPU
6165
{1E1E202B-EFB2-40FD-8271-659F36084916}.Release|Any CPU.Build.0 = Release|Any CPU
66+
{EA2A367F-2D2D-4C20-8C32-C19F67E73187}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67+
{EA2A367F-2D2D-4C20-8C32-C19F67E73187}.Debug|Any CPU.Build.0 = Debug|Any CPU
68+
{EA2A367F-2D2D-4C20-8C32-C19F67E73187}.Release|Any CPU.ActiveCfg = Release|Any CPU
69+
{EA2A367F-2D2D-4C20-8C32-C19F67E73187}.Release|Any CPU.Build.0 = Release|Any CPU
6270
EndGlobalSection
6371
GlobalSection(SolutionProperties) = preSolution
6472
HideSolutionNode = FALSE
@@ -69,6 +77,7 @@ Global
6977
{FD2DF3AB-05C1-4145-827A-482C539B2491} = {3C777BBB-7464-43FB-A046-EA465791AB0C}
7078
{CABEEEAE-3974-4CC4-97F1-18C8D2188DAF} = {3C777BBB-7464-43FB-A046-EA465791AB0C}
7179
{1E1E202B-EFB2-40FD-8271-659F36084916} = {3C777BBB-7464-43FB-A046-EA465791AB0C}
80+
{EA2A367F-2D2D-4C20-8C32-C19F67E73187} = {A15FB7AB-5B7A-4428-BEBA-32DEE3C88C39}
7281
EndGlobalSection
7382
GlobalSection(ExtensibilityGlobals) = postSolution
7483
SolutionGuid = {70815049-1680-480A-BF5A-00536D6C9C20}

src/AspNetCore.Authentication.ApiKey/ApiKeyHandlerBase.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ namespace AspNetCore.Authentication.ApiKey
1616
/// <summary>
1717
/// Inherited from <see cref="AuthenticationHandler{TOptions}"/> for api key authentication.
1818
/// </summary>
19-
internal abstract class ApiKeyHandlerBase : AuthenticationHandler<ApiKeyOptions>
19+
public abstract class ApiKeyHandlerBase : AuthenticationHandler<ApiKeyOptions>
2020
{
2121
protected ApiKeyHandlerBase(IOptionsMonitor<ApiKeyOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
2222
: base(options, logger, encoder, clock)
2323
{
2424
}
2525

26-
private string Challenge => $"{Options.KeyName} realm=\"{Options.Realm}\", charset=\"UTF-8\"";
26+
private string Challenge => $"{GetWwwAuthenticateSchemeName()} realm=\"{Options.Realm}\", charset=\"UTF-8\", in=\"{GetWwwAuthenticateInParameter()}\", key_name=\"{Options.KeyName}\"";
2727

2828
/// <summary>
2929
/// Get or set <see cref="ApiKeyEvents"/>.
@@ -209,6 +209,29 @@ private async Task<IApiKey> ValidateUsingApiKeyProviderAsync(string apiKey)
209209
}
210210
}
211211

212+
private string GetWwwAuthenticateSchemeName()
213+
{
214+
return Options.ForLegacyUseKeyNameAsSchemeNameOnWWWAuthenticateHeader
215+
? Options.KeyName
216+
: Scheme.Name;
217+
}
218+
219+
private string GetWwwAuthenticateInParameter()
220+
{
221+
var handlerType = this.GetType();
222+
223+
if (handlerType == typeof(ApiKeyInAuthorizationHeaderHandler))
224+
return "authorization_header";
225+
if (handlerType == typeof(ApiKeyInHeaderHandler))
226+
return "header";
227+
if (handlerType == typeof(ApiKeyInQueryParamsHandler))
228+
return "query_params";
229+
if (handlerType == typeof(ApiKeyInHeaderOrQueryParamsHandler))
230+
return "header_or_query_params";
231+
232+
throw new NotImplementedException($"No parameter name defined for {handlerType.FullName}.");
233+
}
234+
212235
private bool IgnoreAuthenticationIfAllowAnonymous()
213236
{
214237
#if (NET461 || NETSTANDARD2_0)

src/AspNetCore.Authentication.ApiKey/ApiKeyInAuthorizationHeaderHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace AspNetCore.Authentication.ApiKey
1414
{
15-
internal class ApiKeyInAuthorizationHeaderHandler : ApiKeyHandlerBase
15+
public class ApiKeyInAuthorizationHeaderHandler : ApiKeyHandlerBase
1616
{
1717
public ApiKeyInAuthorizationHeaderHandler(IOptionsMonitor<ApiKeyOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
1818
: base(options, logger, encoder, clock)
@@ -23,7 +23,9 @@ protected override Task<string> ParseApiKeyAsync()
2323
{
2424
if (Request.Headers.ContainsKey(HeaderNames.Authorization)
2525
&& AuthenticationHeaderValue.TryParse(Request.Headers[HeaderNames.Authorization], out var headerValue)
26-
&& headerValue.Scheme.Equals(Options.KeyName, StringComparison.OrdinalIgnoreCase)
26+
&& (headerValue.Scheme.Equals(Scheme.Name, StringComparison.OrdinalIgnoreCase)
27+
|| headerValue.Scheme.Equals(Options.KeyName, StringComparison.OrdinalIgnoreCase)
28+
)
2729
)
2830
{
2931
return Task.FromResult(headerValue.Parameter);

0 commit comments

Comments
 (0)