Skip to content

Commit a58ce66

Browse files
committed
reduce logging
1 parent 1f7d144 commit a58ce66

11 files changed

+23
-24
lines changed

src/AspNetCore.SecurityKey/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public static class ApplicationBuilderExtensions
1717
/// <exception cref="System.ArgumentNullException">builder is null</exception>
1818
public static IApplicationBuilder UseSecurityKey(this IApplicationBuilder builder)
1919
{
20-
if (builder is null)
21-
throw new ArgumentNullException(nameof(builder));
20+
ArgumentNullException.ThrowIfNull(builder);
2221

2322
return builder.UseMiddleware<SecurityKeyMiddleware>();
2423
}

src/AspNetCore.SecurityKey/AuthenticationBuilderExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder bu
2424
/// <param name="authenticationScheme">The authentication scheme.</param>
2525
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
2626
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme)
27-
=> builder.AddSecurityKey(authenticationScheme, configureOptions: null!);
27+
=> builder.AddSecurityKey(authenticationScheme, configureOptions: null);
2828

2929
/// <summary>
3030
/// Adds security API key authentication to <see cref="AuthenticationBuilder"/> using the default scheme.
@@ -33,7 +33,7 @@ public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder bu
3333
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
3434
/// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param>
3535
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
36-
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions)
36+
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, Action<SecurityKeyAuthenticationSchemeOptions>? configureOptions)
3737
=> builder.AddSecurityKey(SecurityKeyAuthenticationDefaults.AuthenticationScheme, configureOptions);
3838

3939
/// <summary>
@@ -43,7 +43,7 @@ public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder bu
4343
/// <param name="authenticationScheme">The authentication scheme.</param>
4444
/// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param>
4545
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
46-
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions)
46+
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme, Action<SecurityKeyAuthenticationSchemeOptions>? configureOptions)
4747
=> builder.AddSecurityKey(authenticationScheme, displayName: null, configureOptions: configureOptions);
4848

4949
/// <summary>
@@ -54,8 +54,10 @@ public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder bu
5454
/// <param name="displayName">A display name for the authentication handler.</param>
5555
/// <param name="configureOptions">A delegate to configure <see cref="SecurityKeyAuthenticationSchemeOptions"/>.</param>
5656
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
57-
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, Action<SecurityKeyAuthenticationSchemeOptions> configureOptions)
57+
public static AuthenticationBuilder AddSecurityKey(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, Action<SecurityKeyAuthenticationSchemeOptions>? configureOptions)
5858
{
59+
ArgumentNullException.ThrowIfNull(builder);
60+
5961
builder.Services.AddOptions<SecurityKeyAuthenticationSchemeOptions>(authenticationScheme);
6062
return builder.AddScheme<SecurityKeyAuthenticationSchemeOptions, SecurityKeyAuthenticationHandler>(authenticationScheme, displayName, configureOptions);
6163
}

src/AspNetCore.SecurityKey/DependencyInjectionExtensions.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ public static class DependencyInjectionExtensions
1818
/// The <see cref="IServiceCollection"/> so that additional calls can be chained.
1919
/// </returns>
2020
public static IServiceCollection AddSecurityKey(this IServiceCollection services, Action<SecurityKeyOptions>? configure = null)
21-
{
22-
return AddSecurityKey<SecurityKeyValidator, SecurityKeyExtractor>(services, configure);
23-
}
21+
=> AddSecurityKey<SecurityKeyValidator, SecurityKeyExtractor>(services, configure);
2422

2523
/// <summary>
2624
/// Adds the security API key services to the specified <see cref="IServiceCollection" />.
@@ -33,9 +31,7 @@ public static IServiceCollection AddSecurityKey(this IServiceCollection services
3331
/// </returns>
3432
public static IServiceCollection AddSecurityKey<TValidator>(this IServiceCollection services, Action<SecurityKeyOptions>? configure = null)
3533
where TValidator : class, ISecurityKeyValidator
36-
{
37-
return AddSecurityKey<TValidator, SecurityKeyExtractor>(services, configure);
38-
}
34+
=> AddSecurityKey<TValidator, SecurityKeyExtractor>(services, configure);
3935

4036
/// <summary>
4137
/// Adds the security API key services to the specified <see cref="IServiceCollection" />.
@@ -51,6 +47,8 @@ public static IServiceCollection AddSecurityKey<TValidator, TExtractor>(this ISe
5147
where TValidator : class, ISecurityKeyValidator
5248
where TExtractor : class, ISecurityKeyExtractor
5349
{
50+
ArgumentNullException.ThrowIfNull(services);
51+
5452
services.AddHttpContextAccessor();
5553

5654
services.AddOptions<SecurityKeyOptions>();

src/AspNetCore.SecurityKey/EndpointFilterExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public static class EndpointFilterExtensions
1818
/// </returns>
1919
public static RouteHandlerBuilder RequireSecurityKey(this RouteHandlerBuilder builder)
2020
{
21+
ArgumentNullException.ThrowIfNull(builder);
22+
2123
builder.AddEndpointFilter<SecurityKeyEndpointFilter>();
2224
return builder;
2325
}

src/AspNetCore.SecurityKey/SecurityKeyAuthenticationHandler.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
4848
var identity = await _securityKeyValidator.Authenticate(securityKey);
4949

5050
if (!identity.IsAuthenticated)
51-
{
52-
SecurityKeyLogger.InvalidSecurityKey(Logger, securityKey);
5351
return AuthenticateResult.Fail("Invalid Security Key");
54-
}
5552

5653
// create a user claim for the security key
5754
var principal = new ClaimsPrincipal(identity);

src/AspNetCore.SecurityKey/SecurityKeyAuthorizationFilter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
4343
if (await _securityKeyValidator.Validate(securityKey))
4444
return;
4545

46-
SecurityKeyLogger.InvalidSecurityKey(_logger, securityKey);
47-
4846
context.Result = new UnauthorizedResult();
4947
}
5048
}

src/AspNetCore.SecurityKey/SecurityKeyEndpointFilter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public SecurityKeyEndpointFilter(
4141
if (await _securityKeyValidator.Validate(securityKey))
4242
return await next(context);
4343

44-
SecurityKeyLogger.InvalidSecurityKey(_logger, securityKey);
45-
4644
return Results.Unauthorized();
4745
}
4846
}

src/AspNetCore.SecurityKey/SecurityKeyExtractor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public class SecurityKeyExtractor : ISecurityKeyExtractor
1818
/// <exception cref="System.ArgumentNullException">securityKeyOptions</exception>
1919
public SecurityKeyExtractor(IOptions<SecurityKeyOptions> securityKeyOptions)
2020
{
21-
if (securityKeyOptions == null)
22-
throw new ArgumentNullException(nameof(securityKeyOptions));
21+
ArgumentNullException.ThrowIfNull(securityKeyOptions);
2322

2423
_securityKeyOptions = securityKeyOptions.Value;
2524
}

src/AspNetCore.SecurityKey/SecurityKeyLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace AspNetCore.Extensions.Authentication;
44

55
internal partial class SecurityKeyLogger
66
{
7-
[LoggerMessage(1001, LogLevel.Error, "Invalid Security Key '{SecurityKey}'")]
7+
[LoggerMessage(1001, LogLevel.Warning, "Invalid Security Key '{SecurityKey}'")]
88
internal static partial void InvalidSecurityKey(ILogger logger, string? securityKey);
99

1010
[LoggerMessage(1002, LogLevel.Debug, "Using Security Keys '{SecurityKey}' from configuration '{ConfigurationName}'")]

src/AspNetCore.SecurityKey/SecurityKeyMiddleware.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public async Task InvokeAsync(HttpContext context)
3939
return;
4040
}
4141

42-
SecurityKeyLogger.InvalidSecurityKey(_logger, securityKey);
43-
4442
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
4543
}
4644
}

0 commit comments

Comments
 (0)