Skip to content

Commit

Permalink
fix JWT auth validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dolifer authored Jun 22, 2022
1 parent 47a3abe commit 99c66dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
41 changes: 10 additions & 31 deletions src/NFixtures.WebApi/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,30 @@ public static class ServiceCollectionExtensions
/// but success only for a given collection of <see cref="TestUser"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="users">The collection of <see cref="TestUser"/> to use.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection ConfigureTestAuthentication(this IServiceCollection services, params TestUser[] users)
=> services.ConfigureTestAuthentication(JwtBearerDefaults.AuthenticationScheme, users);
public static IServiceCollection ConfigureTestAuthentication(this IServiceCollection services)
=> services.ConfigureTestAuthentication(JwtBearerDefaults.AuthenticationScheme);

/// <summary>
/// Configures <see cref="JwtBearerOptions"/> to allow any JWT token,
/// but success only for a given collection of <see cref="TestUser"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="schema">The name of the authentication schema.</param>
/// <param name="users">The collection of <see cref="TestUser"/> to use.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection ConfigureTestAuthentication(this IServiceCollection services, string schema, params TestUser[] users)
public static IServiceCollection ConfigureTestAuthentication(this IServiceCollection services, string schema)
{
services
.PostConfigure<JwtBearerOptions>(schema, o =>
{
o.TokenValidationParameters.SignatureValidator = (token, _) => new JwtSecurityToken(token);
o.TokenValidationParameters.ValidateAudience = false;
o.TokenValidationParameters.ValidateIssuer = false;
o.Events = new JwtBearerEvents
o.TokenValidationParameters = new()
{
OnTokenValidated = context =>
{
var token = context.SecurityToken as JwtSecurityToken;
var claim = token?.Claims.FirstOrDefault(c => c.Type == TestUser.IdClaim);
if (claim is null || !Guid.TryParse(claim.Value, out var id))
{
context.Fail(FormatStrings.Authorization_ClaimNotFound);
return Task.CompletedTask;
}
if (users.Any(x => x.Id == id))
{
context.Success();
return Task.CompletedTask;
}
context.Fail(string.Format(FormatStrings.Authorization_UserNotFound, id));
return Task.CompletedTask;
}
IssuerSigningKeyResolver = null,
SignatureValidator = (token, _) => new JwtSecurityToken(token),
ValidateIssuer = false,
ValidateAudience = false,
RequireSignedTokens = false,
ValidateIssuerSigningKey = false
};
});

Expand Down
2 changes: 1 addition & 1 deletion tests/NFixtures.WebApi.Tests/Fixtures/ApiFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
protected override void ConfigureTestServices(IServiceCollection services)
{
services
.ConfigureTestAuthentication(FirstUser);
.ConfigureTestAuthentication();
}
}
}

0 comments on commit 99c66dd

Please sign in to comment.