Skip to content

Raise authenticationFailed event when cert validation fails #31370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
}

var result = await ValidateCertificateAsync(clientCertificate);

// Invoke the failed handler if validation failed, before updating the cache
if (result.Failure != null)
{
var authenticationFailedContext = await HandleFailureAsync(result.Failure);
if (authenticationFailedContext.Result != null)
{
result = authenticationFailedContext.Result;
}
}

if (_cache != null)
{
_cache.Put(Context, clientCertificate, result);
Expand All @@ -87,12 +98,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
}
catch (Exception ex)
{
var authenticationFailedContext = new CertificateAuthenticationFailedContext(Context, Scheme, Options)
{
Exception = ex
};

await Events.AuthenticationFailed(authenticationFailedContext);
var authenticationFailedContext = await HandleFailureAsync(ex);
if (authenticationFailedContext.Result != null)
{
return authenticationFailedContext.Result;
Expand All @@ -102,6 +108,17 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
}
}

private async Task<CertificateAuthenticationFailedContext> HandleFailureAsync(Exception error)
{
var authenticationFailedContext = new CertificateAuthenticationFailedContext(Context, Scheme, Options)
{
Exception = error
};

await Events.AuthenticationFailed(authenticationFailedContext);
return authenticationFailedContext;
}

private async Task<AuthenticateResult> ValidateCertificateAsync(X509Certificate2 clientCertificate)
{
// If we have a self signed cert, and they're not allowed, exit early and not bother with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer
public class JwtBearerEvents
{
/// <summary>
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// Invoked if authentication fails during request processing. The exceptions will be re-thrown after this event unless suppressed.
/// </summary>
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.CompletedTask;

Expand Down
25 changes: 24 additions & 1 deletion src/Security/Authentication/test/CertificateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,30 @@ public async Task VerifyUntrustedClientCertEndsUpInForbidden()
var response = await server.CreateClient().GetAsync("https://example.com/");
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
}

[Fact]
public async Task VerifyValidationFailureCanBeHandled()
{
var failCalled = false;
using var host = await CreateHost(
new CertificateAuthenticationOptions
{
Events = new CertificateAuthenticationEvents()
{
OnAuthenticationFailed = context =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What caused the initial validation failure in this test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure exactly, I copied the VerifyUntrustedClientCertEndsUpInForbidden(), so I guess its the signedclient cert doesn't validate?

https://github.com/dotnet/aspnetcore/pull/31370/files#diff-0ca82662fe7488251aa5453edd2bb21a0844e259d1cae0f3b35c1e7520cec1a7R275

{
context.Fail("Validation failed: " + context.Exception);
failCalled = true;
return Task.CompletedTask;
}
}
}, Certificates.SignedClient);

using var server = host.GetTestServer();
var response = await server.CreateClient().GetAsync("https://example.com/");
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
Assert.True(failCalled);
}

[Fact]
public async Task VerifyClientCertWithUntrustedRootAndTrustedChainEndsUpInForbidden()
Expand Down Expand Up @@ -752,7 +776,6 @@ private static async Task<IHost> CreateHost(

await host.StartAsync();


var server = host.GetTestServer();
server.BaseAddress = baseAddress;
return host;
Expand Down