-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to have ApiKey in request route identified by route pat…
…tern key from netcoreapp3.0 onwards
- Loading branch information
1 parent
066f2f3
commit e639bcb
Showing
13 changed files
with
491 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/AspNetCore.Authentication.ApiKey/ApiKeyInRouteValuesHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Mihir Dilip. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE file in the project root for license information. | ||
#if NETCOREAPP3_0_OR_GREATER | ||
|
||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
|
||
namespace AspNetCore.Authentication.ApiKey | ||
{ | ||
public class ApiKeyInRouteValuesHandler : ApiKeyHandlerBase | ||
{ | ||
private const string WwwAuthenticateInParameter = "route_values"; | ||
protected override string GetWwwAuthenticateInParameter() => WwwAuthenticateInParameter; | ||
|
||
#if NET8_0_OR_GREATER | ||
protected ApiKeyInRouteValuesHandler(IOptionsMonitor<ApiKeyOptions> options, ILoggerFactory logger, UrlEncoder encoder) | ||
: base(options, logger, encoder) | ||
{ | ||
} | ||
|
||
[Obsolete("ISystemClock is obsolete, use TimeProvider on AuthenticationSchemeOptions instead.")] | ||
#endif | ||
public ApiKeyInRouteValuesHandler(IOptionsMonitor<ApiKeyOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) | ||
: base(options, logger, encoder, clock) | ||
{ | ||
} | ||
|
||
protected override Task<string> ParseApiKeyAsync() | ||
{ | ||
if (Request.RouteValues.TryGetValue(Options.KeyName, out var value) && value != null && value.GetType() == typeof(string)) | ||
{ | ||
return Task.FromResult(value.ToString()); | ||
} | ||
|
||
// No ApiKey query parameter found | ||
return Task.FromResult(string.Empty); | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.