-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from DetNordjyskeMediehus/refactorings
Add minimal service implementation and split ApiKeyAuthenticationExtensions
- Loading branch information
Showing
12 changed files
with
181 additions
and
63 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 was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
Source/AuthenticationHandler/ApiKeyAuthenticationExtensions.DefaultScheme.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,34 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
|
||
namespace Dnmh.Security.ApiKeyAuthentication.AuthenticationHandler; | ||
|
||
public static partial class ApiKeyAuthenticationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a default authentication scheme named <c>"ApiKey"</c> and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler | ||
/// with a given valid api key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="validApiKey">The valid api key that the api keys provided in the requests are validated against.</param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication(this AuthenticationBuilder builder, string validApiKey) => | ||
builder.AddApiKeyAuthentication(_ => validApiKey); | ||
|
||
/// <summary> | ||
/// Adds a default authentication scheme named <c>"ApiKey"</c> and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler | ||
/// with a function to provide a valid api key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="validApiKeyFunction">A function that returns a valid api key. The valid api key is validated against the api keys provided in the requests.</param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication(this AuthenticationBuilder builder, Func<IServiceProvider, string> validApiKeyFunction) => | ||
builder.AddApiKeyAuthentication(sp => new SimpleApiKeyAuthenticationService(validApiKeyFunction?.Invoke(sp) ?? throw new ArgumentNullException(nameof(validApiKeyFunction)))); | ||
|
||
/// <summary> | ||
/// Adds a default authentication scheme named <c>"ApiKey"</c> and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler. | ||
/// </summary> | ||
/// <typeparam name="TAuthService">The implementing class of interface <see cref="IApiKeyAuthenticationService"/></typeparam> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="serviceImplementationFactory">Optional implementation factory for the registration of <typeparamref name="TAuthService"/></param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication<TAuthService>(this AuthenticationBuilder builder, Func<IServiceProvider, TAuthService>? serviceImplementationFactory = null) | ||
where TAuthService : class, IApiKeyAuthenticationService => | ||
builder.AddApiKeyAuthentication("ApiKey", _ => { }, serviceImplementationFactory); | ||
} |
37 changes: 37 additions & 0 deletions
37
.../AuthenticationHandler/ApiKeyAuthenticationExtensions.DefaultSchemeConfigurableOptions.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,37 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
|
||
namespace Dnmh.Security.ApiKeyAuthentication.AuthenticationHandler; | ||
|
||
public static partial class ApiKeyAuthenticationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a default authentication scheme named <c>"ApiKey"</c> and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler | ||
/// with a given valid api key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="validApiKey">The valid api key that the api keys provided in the requests are validated against.</param> | ||
/// <param name="configureOptions">The action used to configure options</param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication(this AuthenticationBuilder builder, string validApiKey, Action<ApiKeyAuthenticationOptions> configureOptions) => | ||
builder.AddApiKeyAuthentication(_ => validApiKey, configureOptions); | ||
|
||
/// <summary> | ||
/// Adds a default authentication scheme named <c>"ApiKey"</c> and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler | ||
/// with a function to provide a valid api key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="validApiKeyFunction">A function that returns a valid api key. The valid api key is validated against the api keys provided in the requests.</param> | ||
/// <param name="configureOptions">The action used to configure options</param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication(this AuthenticationBuilder builder, Func<IServiceProvider, string> validApiKeyFunction, Action<ApiKeyAuthenticationOptions> configureOptions) => | ||
builder.AddApiKeyAuthentication(configureOptions, sp => new SimpleApiKeyAuthenticationService(validApiKeyFunction?.Invoke(sp) ?? throw new ArgumentNullException(nameof(validApiKeyFunction)))); | ||
|
||
/// <summary> | ||
/// Adds a default authentication scheme named <c>"ApiKey"</c> and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler. | ||
/// </summary> | ||
/// <typeparam name="TAuthService">The implementing class of interface <see cref="IApiKeyAuthenticationService"/></typeparam> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="configureOptions">The action used to configure options</param> | ||
/// <param name="serviceImplementationFactory">Optional implementation factory for the registration of <typeparamref name="TAuthService"/></param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication<TAuthService>(this AuthenticationBuilder builder, Action<ApiKeyAuthenticationOptions> configureOptions, Func<IServiceProvider, TAuthService>? serviceImplementationFactory = null) | ||
where TAuthService : class, IApiKeyAuthenticationService => | ||
builder.AddApiKeyAuthentication("ApiKey", configureOptions, serviceImplementationFactory); | ||
} |
37 changes: 37 additions & 0 deletions
37
Source/AuthenticationHandler/ApiKeyAuthenticationExtensions.NamedScheme.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,37 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
|
||
namespace Dnmh.Security.ApiKeyAuthentication.AuthenticationHandler; | ||
|
||
public static partial class ApiKeyAuthenticationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a given authentication scheme name and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler | ||
/// with a given valid api key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="authenticationScheme">The authentication scheme name</param> | ||
/// <param name="validApiKey">The valid api key that the api keys provided in the requests are validated against.</param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication(this AuthenticationBuilder builder, string authenticationScheme, string validApiKey) => | ||
builder.AddApiKeyAuthentication(authenticationScheme, _ => validApiKey); | ||
|
||
/// <summary> | ||
/// Adds a given authentication scheme name and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler | ||
/// with a function to provide a valid api key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="authenticationScheme">The authentication scheme name</param> | ||
/// <param name="validApiKeyFunction">A function that returns a valid api key. The valid api key is validated against the api keys provided in the requests.</param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication(this AuthenticationBuilder builder, string authenticationScheme, Func<IServiceProvider, string> validApiKeyFunction) => | ||
builder.AddApiKeyAuthentication(authenticationScheme, sp => new SimpleApiKeyAuthenticationService(validApiKeyFunction?.Invoke(sp) ?? throw new ArgumentNullException(nameof(validApiKeyFunction)))); | ||
|
||
/// <summary> | ||
/// Adds a given authentication scheme name and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler. | ||
/// </summary> | ||
/// <typeparam name="TAuthService">The implementing class of interface <see cref="IApiKeyAuthenticationService"/></typeparam> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="authenticationScheme">The authentication scheme name</param> | ||
/// <param name="serviceImplementationFactory">Optional implementation factory for the registration of <typeparamref name="TAuthService"/></param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication<TAuthService>(this AuthenticationBuilder builder, string authenticationScheme, Func<IServiceProvider, TAuthService>? serviceImplementationFactory = null) | ||
where TAuthService : class, IApiKeyAuthenticationService => | ||
builder.AddApiKeyAuthentication(authenticationScheme, _ => { }, serviceImplementationFactory); | ||
} |
28 changes: 28 additions & 0 deletions
28
...ce/AuthenticationHandler/ApiKeyAuthenticationExtensions.NamedSchemeConfigurableOptions.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,28 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
|
||
namespace Dnmh.Security.ApiKeyAuthentication.AuthenticationHandler; | ||
|
||
public static partial class ApiKeyAuthenticationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a given authentication scheme name and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler | ||
/// with a given valid api key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="authenticationScheme">The authentication scheme name</param> | ||
/// <param name="validApiKey">The valid api key that the api keys provided in the requests are validated against.</param> | ||
/// <param name="configureOptions">The action used to configure options</param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication(this AuthenticationBuilder builder, string authenticationScheme, string validApiKey, Action<ApiKeyAuthenticationOptions> configureOptions) => | ||
builder.AddApiKeyAuthentication(authenticationScheme, _ => validApiKey, configureOptions); | ||
|
||
/// <summary> | ||
/// Adds a given authentication scheme name and registers the <see cref="ApiKeyAuthenticationHandler"/> as the authentication handler | ||
/// with a function to provide a valid api key. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/></param> | ||
/// <param name="authenticationScheme">The authentication scheme name</param> | ||
/// <param name="validApiKeyFunction">A function that returns a valid api key. The valid api key is validated against the api keys provided in the requests.</param> | ||
/// <param name="configureOptions">The action used to configure options</param> | ||
public static ApiKeyAuthenticationBuilder AddApiKeyAuthentication(this AuthenticationBuilder builder, string authenticationScheme, Func<IServiceProvider, string> validApiKeyFunction, Action<ApiKeyAuthenticationOptions> configureOptions) => | ||
builder.AddApiKeyAuthentication(authenticationScheme, configureOptions, sp => new SimpleApiKeyAuthenticationService(validApiKeyFunction?.Invoke(sp) ?? throw new ArgumentNullException(nameof(validApiKeyFunction)))); | ||
} |
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
Oops, something went wrong.