-
-
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.
- Loading branch information
Showing
17 changed files
with
227 additions
and
25 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
56 changes: 52 additions & 4 deletions
56
src/AspNetCore.SecurityKey/AuthenticationBuilderExtensions.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 |
---|---|---|
@@ -1,14 +1,62 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace AspNetCore.SecurityKey; | ||
|
||
/// <summary> | ||
/// Extension methods to configure security API key authentication. | ||
/// </summary> | ||
public static class AuthenticationBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the default scheme. | ||
/// The default scheme is specified by <see cref="SecurityKeyAuthenticationDefaults.AuthenticationScheme"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> | ||
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> | ||
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder) | ||
=> builder.AddSecurityKey(SecurityKeyAuthenticationDefaults.AuthenticationScheme); | ||
|
||
/// <summary> | ||
/// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> | ||
/// <param name="authenticationScheme">The authentication scheme.</param> | ||
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> | ||
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme) | ||
=> builder.AddSecurityKey(authenticationScheme, configureOptions: null!); | ||
|
||
/// <summary> | ||
/// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the default scheme. | ||
/// The default scheme is specified by <see cref="SecurityKeyAuthenticationDefaults.AuthenticationScheme"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> | ||
/// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param> | ||
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> | ||
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions) | ||
=> builder.AddSecurityKey(SecurityKeyAuthenticationDefaults.AuthenticationScheme, configureOptions); | ||
|
||
/// <summary> | ||
/// Adds security api key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> | ||
/// <param name="authenticationScheme">The authentication scheme.</param> | ||
/// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param> | ||
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> | ||
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions) | ||
=> builder.AddSecurityKey(authenticationScheme, displayName: null, configureOptions: configureOptions); | ||
|
||
/// <summary> | ||
/// Adds security api key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param> | ||
/// <param name="authenticationScheme">The authentication scheme.</param> | ||
/// <param name="displayName">A display name for the authentication handler.</param> | ||
/// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param> | ||
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns> | ||
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions) | ||
{ | ||
return builder.AddScheme<SecurityKeyAuthenticationSchemeOptions, SecurityKeyAuthenticationHandler>( | ||
SecurityKeyAuthenticationDefaults.AuthenticationScheme, | ||
options => { } | ||
); | ||
builder.Services.AddOptions<SecurityKeyAuthenticationSchemeOptions>(authenticationScheme); | ||
return builder.AddScheme<SecurityKeyAuthenticationSchemeOptions, SecurityKeyAuthenticationHandler>(authenticationScheme, displayName, configureOptions); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
#if NET7_0_OR_GREATER | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace AspNetCore.SecurityKey; | ||
|
||
/// <summary> | ||
/// Extension methods for adding <see cref="IEndpointFilter" /> to a route handler. | ||
/// </summary> | ||
public static class EndpointFilterExtensions | ||
{ | ||
#if NET7_0_OR_GREATER | ||
/// <summary> | ||
/// Registers an endpoint filter requiring security API key for the specified route handler. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="RouteHandlerBuilder"/>.</param> | ||
/// <returns> | ||
/// A <see cref="RouteHandlerBuilder"/> that can be used to further customize the route handler. | ||
/// </returns> | ||
public static RouteHandlerBuilder RequireSecurityKey(this RouteHandlerBuilder builder) | ||
{ | ||
builder.AddEndpointFilter<SecurityKeyEndpointFilter>(); | ||
return builder; | ||
} | ||
#endif | ||
} | ||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
namespace AspNetCore.SecurityKey; | ||
|
||
/// <summary> | ||
/// An interface for validating the security API key | ||
/// </summary> | ||
public interface ISecurityKeyValidator | ||
{ | ||
/// <summary> | ||
/// Validates the specified security API key. | ||
/// </summary> | ||
/// <param name="value">The security API key to validate.</param> | ||
/// <returns>true if security API key is valid; otherwise false</returns> | ||
bool Validate(string? value); | ||
} |
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
8 changes: 7 additions & 1 deletion
8
src/AspNetCore.SecurityKey/SecurityKeyAuthenticationDefaults.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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
namespace AspNetCore.SecurityKey; | ||
namespace AspNetCore.SecurityKey; | ||
|
||
/// <summary> | ||
/// Default values related to security API key authentication | ||
/// </summary> | ||
public static class SecurityKeyAuthenticationDefaults | ||
{ | ||
/// <summary> | ||
/// The default authentication scheme | ||
/// </summary> | ||
public const string AuthenticationScheme = "SecurityKey"; | ||
} |
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
6 changes: 5 additions & 1 deletion
6
src/AspNetCore.SecurityKey/SecurityKeyAuthenticationSchemeOptions.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
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
Oops, something went wrong.