Skip to content

Commit 3f97600

Browse files
committed
make ISecurityKeyValidator async
1 parent e2d1960 commit 3f97600

15 files changed

+104
-53
lines changed

samples/Sample.Controllers/Controllers.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@HostAddress = http://localhost:5154
1+
@HostAddress = https://localhost:7236
22

33
###
44

samples/Sample.Middleware/Middleware.http

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@HostAddress = http://localhost:5284
1+
@HostAddress = https://localhost:7006
22

33
###
44

@@ -17,3 +17,9 @@ x-api-key: 01HSGVAH2M5WVQYG4YPT7FNK4K8
1717
GET {{HostAddress}}/addresses/
1818
Accept: application/json
1919
X-API-KEY: 01hsgvah2m5wvqyg4ypt7fnk4k8
20+
21+
###
22+
23+
GET {{HostAddress}}/addresses/
24+
Accept: application/json
25+
X-API-KEY: badkey

samples/Sample.MinimalApi/MinimalApi.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@HostAddress = http://localhost:5009
1+
@HostAddress = https://localhost:7216
22

33
###
44

samples/Sample.MinimalApi/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ public static void Main(string[] args)
4949

5050
app.MapGet("/current", (ClaimsPrincipal? principal) =>
5151
{
52-
var p = principal;
53-
return WeatherFaker.Instance.Generate(5);
52+
return new
53+
{
54+
Name = principal?.Identity?.Name,
55+
Data = WeatherFaker.Instance.Generate(5)
56+
};
5457
})
5558
.WithName("GetCurrentUser")
5659
.WithOpenApi();

src/AspNetCore.SecurityKey/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class ApplicationBuilderExtensions
1212
/// </summary>
1313
/// <param name="builder">The <see cref="IApplicationBuilder" /> instance this method extends</param>
1414
/// <returns>
15-
/// The <see cref="IApplicationBuilder" /> for requirirng security API keys
15+
/// The <see cref="IApplicationBuilder" /> for requiring security API keys
1616
/// </returns>
1717
/// <exception cref="System.ArgumentNullException">builder is null</exception>
1818
public static IApplicationBuilder UseSecurityKey(this IApplicationBuilder builder)

src/AspNetCore.SecurityKey/AuthenticationBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder bu
3737
=> builder.AddSecurityKey(SecurityKeyAuthenticationDefaults.AuthenticationScheme, configureOptions);
3838

3939
/// <summary>
40-
/// Adds security api key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme.
40+
/// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme.
4141
/// </summary>
4242
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
4343
/// <param name="authenticationScheme">The authentication scheme.</param>
@@ -47,7 +47,7 @@ public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder bu
4747
=> builder.AddSecurityKey(authenticationScheme, displayName: null, configureOptions: configureOptions);
4848

4949
/// <summary>
50-
/// Adds security api key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme.
50+
/// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the specified scheme.
5151
/// </summary>
5252
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
5353
/// <param name="authenticationScheme">The authentication scheme.</param>

src/AspNetCore.SecurityKey/ISecurityKeyValidator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ public interface ISecurityKeyValidator
1212
/// </summary>
1313
/// <param name="value">The security API key to validate.</param>
1414
/// <returns>true if security API key is valid; otherwise false</returns>
15-
bool Validate(string? value);
15+
ValueTask<bool> Validate(string? value);
1616

1717
/// <summary>
18-
/// Validates the specified security API key.
18+
/// Authenticate the specified security API key.
1919
/// </summary>
2020
/// <param name="value">The security API key to validate.</param>
21-
/// <param name="claims">The claims associated with the security API key.</param>
2221
/// <returns>
23-
/// true if security API key is valid; otherwise false
22+
/// <see cref="ClaimsIdentity"/> result of the authentication
2423
/// </returns>
25-
bool Validate(string? value, out Claim[] claims);
24+
ValueTask<ClaimsIdentity> Authenticate(string? value);
2625
}

src/AspNetCore.SecurityKey/SecurityKeyAuthenticationHandler.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,21 @@ public SecurityKeyAuthenticationHandler(
4242
#pragma warning restore CS0618
4343

4444
/// <inheritdoc />
45-
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
45+
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
4646
{
4747
var securityKey = _securityKeyExtractor.GetKey(Context);
48+
var identity = await _securityKeyValidator.Authenticate(securityKey);
4849

49-
if (!_securityKeyValidator.Validate(securityKey, out var claims))
50+
if (!identity.IsAuthenticated)
5051
{
5152
SecurityKeyLogger.InvalidSecurityKey(Logger, securityKey);
52-
return Task.FromResult(AuthenticateResult.Fail("Invalid Security Key"));
53+
return AuthenticateResult.Fail("Invalid Security Key");
5354
}
5455

5556
// create a user claim for the security key
56-
var identity = new ClaimsIdentity(claims, SecurityKeyAuthenticationDefaults.AuthenticationScheme);
5757
var principal = new ClaimsPrincipal(identity);
58-
5958
var ticket = new AuthenticationTicket(principal, Scheme.Name);
6059

61-
return Task.FromResult(AuthenticateResult.Success(ticket));
60+
return AuthenticateResult.Success(ticket);
6261
}
6362
}

src/AspNetCore.SecurityKey/SecurityKeyAuthorizationFilter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
using AspNetCore.Extensions.Authentication;
23

34
using Microsoft.AspNetCore.Mvc;
@@ -9,8 +10,8 @@ namespace AspNetCore.SecurityKey;
910
/// <summary>
1011
/// A filter that requiring security API key authorization.
1112
/// </summary>
12-
/// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.IAuthorizationFilter" />
13-
public class SecurityKeyAuthorizationFilter : IAuthorizationFilter
13+
/// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.IAsyncAuthorizationFilter" />
14+
public class SecurityKeyAuthorizationFilter : IAsyncAuthorizationFilter
1415
{
1516
private readonly ISecurityKeyExtractor _securityKeyExtractor;
1617
private readonly ISecurityKeyValidator _securityKeyValidator;
@@ -33,14 +34,13 @@ public SecurityKeyAuthorizationFilter(
3334
}
3435

3536
/// <inheritdoc />
36-
public void OnAuthorization(AuthorizationFilterContext context)
37+
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
3738
{
38-
if (context is null)
39-
throw new ArgumentNullException(nameof(context));
39+
ArgumentNullException.ThrowIfNull(context);
4040

4141
var securityKey = _securityKeyExtractor.GetKey(context.HttpContext);
4242

43-
if (_securityKeyValidator.Validate(securityKey))
43+
if (await _securityKeyValidator.Validate(securityKey))
4444
return;
4545

4646
SecurityKeyLogger.InvalidSecurityKey(_logger, securityKey);

src/AspNetCore.SecurityKey/SecurityKeyEndpointFilter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class SecurityKeyEndpointFilter : IEndpointFilter
1313
{
1414
private readonly ISecurityKeyExtractor _securityKeyExtractor;
1515
private readonly ISecurityKeyValidator _securityKeyValidator;
16-
private readonly ILogger<SecurityKeyAuthorizationFilter> _logger;
16+
private readonly ILogger<SecurityKeyEndpointFilter> _logger;
1717

1818
/// <summary>
1919
/// Initializes a new instance of the <see cref="SecurityKeyEndpointFilter"/> class.
@@ -24,7 +24,7 @@ public class SecurityKeyEndpointFilter : IEndpointFilter
2424
public SecurityKeyEndpointFilter(
2525
ISecurityKeyExtractor securityKeyExtractor,
2626
ISecurityKeyValidator securityKeyValidator,
27-
ILogger<SecurityKeyAuthorizationFilter> logger)
27+
ILogger<SecurityKeyEndpointFilter> logger)
2828
{
2929
_securityKeyExtractor = securityKeyExtractor ?? throw new ArgumentNullException(nameof(securityKeyExtractor));
3030
_securityKeyValidator = securityKeyValidator ?? throw new ArgumentNullException(nameof(securityKeyValidator));
@@ -34,12 +34,11 @@ public SecurityKeyEndpointFilter(
3434
/// <inheritdoc />
3535
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
3636
{
37-
if (context is null)
38-
throw new ArgumentNullException(nameof(context));
37+
ArgumentNullException.ThrowIfNull(context);
3938

4039
var securityKey = _securityKeyExtractor.GetKey(context.HttpContext);
4140

42-
if (_securityKeyValidator.Validate(securityKey))
41+
if (await _securityKeyValidator.Validate(securityKey))
4342
return await next(context);
4443

4544
SecurityKeyLogger.InvalidSecurityKey(_logger, securityKey);

0 commit comments

Comments
 (0)