Skip to content
This repository was archived by the owner on Dec 24, 2020. It is now read-only.

Commit 87b3dc5

Browse files
Removed the AuthenticationFailedContext events and the related try/catch blocks in all of the middleware.
1 parent 7a7fbab commit 87b3dc5

16 files changed

+361
-502
lines changed

src/AspNet.Security.OAuth.Introspection/Events/AuthenticationFailedContext.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/AspNet.Security.OAuth.Introspection/Events/IOAuthIntrospectionEvents.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ public interface IOAuthIntrospectionEvents
88
/// </summary>
99
Task AccessTokenReceived(AccessTokenReceivedContext context);
1010

11-
/// <summary>
12-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
13-
/// </summary>
14-
Task AuthenticationFailed(AuthenticationFailedContext context);
15-
1611
/// <summary>
1712
/// Invoked when a ticket is to be created from an introspection response.
1813
/// </summary>

src/AspNet.Security.OAuth.Introspection/Events/OAuthIntrospectionEvents.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents
99
/// </summary>
1010
public Func<AccessTokenReceivedContext, Task> OnAccessTokenReceived { get; set; } = context => Task.FromResult(0);
1111

12-
/// <summary>
13-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
14-
/// </summary>
15-
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0);
16-
1712
/// <summary>
1813
/// Invoked when a ticket is to be created from an introspection response.
1914
/// </summary>
@@ -39,11 +34,6 @@ public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents
3934
/// </summary>
4035
public virtual Task AccessTokenReceived(AccessTokenReceivedContext context) => OnAccessTokenReceived(context);
4136

42-
/// <summary>
43-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
44-
/// </summary>
45-
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);
46-
4737
/// <summary>
4838
/// Invoked when a ticket is to be created from an introspection response.
4939
/// </summary>

src/AspNet.Security.OAuth.Introspection/OAuthIntrospectionHandler.cs

Lines changed: 103 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -22,139 +22,139 @@
2222

2323
namespace AspNet.Security.OAuth.Introspection {
2424
public class OAuthIntrospectionHandler : AuthenticationHandler<OAuthIntrospectionOptions> {
25-
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
26-
try {
27-
// Give application opportunity to find from a different location, adjust, or reject token
28-
var accessTokenReceivedContext = new AccessTokenReceivedContext(Context, Options);
29-
30-
// event can set the token
31-
await Options.Events.AccessTokenReceived(accessTokenReceivedContext);
32-
if (accessTokenReceivedContext.HandledResponse) {
33-
return AuthenticateResult.Success(accessTokenReceivedContext.Ticket);
34-
}
35-
if (accessTokenReceivedContext.Skipped) {
36-
Logger.LogInformation("Authentication was skipped by event processing.");
37-
38-
return AuthenticateResult.Skip();
39-
}
25+
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
26+
{
27+
// Give application opportunity to find from a different location, adjust, or reject token
28+
var accessTokenReceivedContext = new AccessTokenReceivedContext(Context, Options);
29+
30+
// event can set the token
31+
await Options.Events.AccessTokenReceived(accessTokenReceivedContext);
32+
if (accessTokenReceivedContext.HandledResponse)
33+
{
34+
return AuthenticateResult.Success(accessTokenReceivedContext.Ticket);
35+
}
4036

41-
// If application retrieved token from somewhere else, use that.
42-
string token = accessTokenReceivedContext.Token;
37+
if (accessTokenReceivedContext.Skipped)
38+
{
39+
Logger.LogInformation("Authentication was skipped by event processing.");
4340

44-
if (string.IsNullOrWhiteSpace(token)) {
45-
string header = Request.Headers[HeaderNames.Authorization];
46-
if (string.IsNullOrEmpty(header)) {
47-
return AuthenticateResult.Fail("Authentication failed because the bearer token " +
48-
"was missing from the 'Authorization' header.");
49-
}
41+
return AuthenticateResult.Skip();
42+
}
5043

51-
// Ensure that the authorization header contains the mandatory "Bearer" scheme.
52-
// See https://tools.ietf.org/html/rfc6750#section-2.1
53-
if (!header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) {
54-
return AuthenticateResult.Fail("Authentication failed because an invalid scheme " +
55-
"was used in the 'Authorization' header.");
56-
}
44+
// If application retrieved token from somewhere else, use that.
45+
string token = accessTokenReceivedContext.Token;
5746

58-
token = header.Substring("Bearer ".Length);
59-
}
60-
if (string.IsNullOrWhiteSpace(token)) {
47+
if (string.IsNullOrWhiteSpace(token))
48+
{
49+
string header = Request.Headers[HeaderNames.Authorization];
50+
if (string.IsNullOrEmpty(header))
51+
{
6152
return AuthenticateResult.Fail("Authentication failed because the bearer token " +
6253
"was missing from the 'Authorization' header.");
6354
}
6455

65-
// Try to resolve the authentication ticket from the distributed cache. If none
66-
// can be found, a new introspection request is sent to the authorization server.
67-
var ticket = await RetrieveTicketAsync(token);
68-
if (ticket == null) {
69-
JObject payload;
70-
// Allow interception of the introspection retrieval process via events
71-
var requestTokenIntrospectionContext = new RequestTokenIntrospectionContext(Context, Options, token);
72-
await Options.Events.RequestTokenIntrospection(requestTokenIntrospectionContext);
73-
if (requestTokenIntrospectionContext.HandledResponse) {
74-
return AuthenticateResult.Success(requestTokenIntrospectionContext.Ticket);
75-
}
76-
else if (requestTokenIntrospectionContext.Skipped) {
77-
return AuthenticateResult.Skip();
78-
}
79-
else {
80-
// Return a failed authentication result if the introspection
81-
// request failed or if the "active" claim was false.
82-
payload = requestTokenIntrospectionContext.Payload ?? await GetIntrospectionPayloadAsync(token);
83-
}
56+
// Ensure that the authorization header contains the mandatory "Bearer" scheme.
57+
// See https://tools.ietf.org/html/rfc6750#section-2.1
58+
if (!header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
59+
{
60+
return AuthenticateResult.Fail("Authentication failed because an invalid scheme " +
61+
"was used in the 'Authorization' header.");
62+
}
8463

85-
if (payload == null || !payload.Value<bool>(OAuthIntrospectionConstants.Claims.Active)) {
86-
return AuthenticateResult.Fail("Authentication failed because the authorization " +
87-
"server rejected the access token.");
88-
}
64+
token = header.Substring("Bearer ".Length);
65+
}
66+
if (string.IsNullOrWhiteSpace(token))
67+
{
68+
return AuthenticateResult.Fail("Authentication failed because the bearer token " +
69+
"was missing from the 'Authorization' header.");
70+
}
8971

90-
// Ensure that the access token was issued
91-
// to be used with this resource server.
92-
if (!await ValidateAudienceAsync(payload)) {
93-
return AuthenticateResult.Fail("Authentication failed because the access token " +
94-
"was not valid for this resource server.");
95-
}
72+
// Try to resolve the authentication ticket from the distributed cache. If none
73+
// can be found, a new introspection request is sent to the authorization server.
74+
var ticket = await RetrieveTicketAsync(token);
75+
if (ticket == null)
76+
{
77+
JObject payload;
78+
// Allow interception of the introspection retrieval process via events
79+
var requestTokenIntrospectionContext = new RequestTokenIntrospectionContext(Context, Options, token);
9680

97-
// Allow interception of the ticket creation process via events
98-
var createTicketContext = new CreateTicketContext(Context, Options, payload);
99-
await Options.Events.CreateTicket(createTicketContext);
100-
if (createTicketContext.HandledResponse) {
101-
return AuthenticateResult.Success(createTicketContext.Ticket);
102-
}
103-
if (createTicketContext.Skipped) {
104-
return AuthenticateResult.Skip();
105-
}
81+
await Options.Events.RequestTokenIntrospection(requestTokenIntrospectionContext);
82+
83+
if (requestTokenIntrospectionContext.HandledResponse)
84+
{
85+
return AuthenticateResult.Success(requestTokenIntrospectionContext.Ticket);
86+
}
10687

107-
// Create a new authentication ticket from the introspection
108-
// response returned by the authorization server.
109-
ticket = createTicketContext.Ticket ?? await CreateTicketAsync(payload);
110-
Debug.Assert(ticket != null);
88+
else if (requestTokenIntrospectionContext.Skipped)
89+
{
90+
return AuthenticateResult.Skip();
91+
}
11192

112-
await StoreTicketAsync(token, ticket);
93+
else {
94+
// Return a failed authentication result if the introspection
95+
// request failed or if the "active" claim was false.
96+
payload = requestTokenIntrospectionContext.Payload ?? await GetIntrospectionPayloadAsync(token);
11397
}
11498

115-
// Allow for interception and handling of the token validated event.
116-
var tokenValidatedContext = new TokenValidatedContext(Context, Options, ticket);
117-
await Options.Events.TokenValidated(tokenValidatedContext);
118-
if (tokenValidatedContext.HandledResponse)
99+
if (payload == null || !payload.Value<bool>(OAuthIntrospectionConstants.Claims.Active))
119100
{
120-
return AuthenticateResult.Success(tokenValidatedContext.Ticket);
101+
return AuthenticateResult.Fail("Authentication failed because the authorization " +
102+
"server rejected the access token.");
121103
}
122-
if (tokenValidatedContext.Skipped)
104+
105+
// Ensure that the access token was issued
106+
// to be used with this resource server.
107+
if (!await ValidateAudienceAsync(payload))
123108
{
124-
Logger.LogInformation("Authentication was skipped by event processing.");
109+
return AuthenticateResult.Fail("Authentication failed because the access token " +
110+
"was not valid for this resource server.");
111+
}
125112

113+
// Allow interception of the ticket creation process via events
114+
var createTicketContext = new CreateTicketContext(Context, Options, payload);
115+
await Options.Events.CreateTicket(createTicketContext);
116+
if (createTicketContext.HandledResponse)
117+
{
118+
return AuthenticateResult.Success(createTicketContext.Ticket);
119+
}
120+
if (createTicketContext.Skipped)
121+
{
126122
return AuthenticateResult.Skip();
127123
}
128124

129-
// Flow the ticket changes
130-
ticket = tokenValidatedContext.Ticket;
125+
// Create a new authentication ticket from the introspection
126+
// response returned by the authorization server.
127+
ticket = createTicketContext.Ticket ?? await CreateTicketAsync(payload);
128+
Debug.Assert(ticket != null);
131129

132-
// Ensure that the authentication ticket is still valid.
133-
if (ticket.Properties.ExpiresUtc.HasValue &&
134-
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow) {
135-
return AuthenticateResult.Fail("Authentication failed because the access token was expired.");
136-
}
130+
await StoreTicketAsync(token, ticket);
131+
}
137132

138-
return AuthenticateResult.Success(ticket);
133+
// Allow for interception and handling of the token validated event.
134+
var tokenValidatedContext = new TokenValidatedContext(Context, Options, ticket);
135+
await Options.Events.TokenValidated(tokenValidatedContext);
136+
if (tokenValidatedContext.HandledResponse)
137+
{
138+
return AuthenticateResult.Success(tokenValidatedContext.Ticket);
139139
}
140-
catch (Exception exception)
140+
if (tokenValidatedContext.Skipped)
141141
{
142-
var authenticationFailedContext = new AuthenticationFailedContext(Context, Options) {
143-
Exception = exception
144-
};
142+
Logger.LogInformation("Authentication was skipped by event processing.");
145143

146-
await Options.Events.AuthenticationFailed(authenticationFailedContext);
147-
if (authenticationFailedContext.HandledResponse) {
148-
return AuthenticateResult.Success(authenticationFailedContext.Ticket);
149-
}
144+
return AuthenticateResult.Skip();
145+
}
150146

151-
if (authenticationFailedContext.Skipped) {
152-
return AuthenticateResult.Skip();
153-
}
147+
// Flow the ticket changes
148+
ticket = tokenValidatedContext.Ticket;
154149

155-
Logger.LogInformation("Exception occurred while processing message", authenticationFailedContext.Exception);
156-
throw authenticationFailedContext.Exception ?? exception;
150+
// Ensure that the authentication ticket is still valid.
151+
if (ticket.Properties.ExpiresUtc.HasValue &&
152+
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow)
153+
{
154+
return AuthenticateResult.Fail("Authentication failed because the access token was expired.");
157155
}
156+
157+
return AuthenticateResult.Success(ticket);
158158
}
159159

160160
protected virtual async Task<string> ResolveIntrospectionEndpointAsync(string issuer) {

src/AspNet.Security.OAuth.Validation/Events/AuthenticationFailedContext.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/AspNet.Security.OAuth.Validation/Events/IOAuthValidationEvents.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ public interface IOAuthValidationEvents
99
/// </summary>
1010
Task AccessTokenReceived(AccessTokenReceivedContext context);
1111

12-
/// <summary>
13-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
14-
/// </summary>
15-
Task AuthenticationFailed(AuthenticationFailedContext context);
16-
1712
/// <summary>
1813
/// Invoked after processing, when a token has been validated.
1914
/// </summary>

src/AspNet.Security.OAuth.Validation/Events/OAuthValidationEvents.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ public class OAuthValidationEvents : IOAuthValidationEvents
99
/// </summary>
1010
public Func<AccessTokenReceivedContext, Task> OnAccessTokenReceived { get; set; } = context => Task.FromResult(0);
1111

12-
/// <summary>
13-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
14-
/// </summary>
15-
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0);
16-
1712
/// <summary>
1813
/// Invoked after processing, when a token has been validated.
1914
/// </summary>
@@ -29,11 +24,6 @@ public class OAuthValidationEvents : IOAuthValidationEvents
2924
/// </summary>
3025
public virtual Task AccessTokenReceived(AccessTokenReceivedContext context) => OnAccessTokenReceived(context);
3126

32-
/// <summary>
33-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
34-
/// </summary>
35-
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);
36-
3727
/// <summary>
3828
/// Invoked after processing, when a token has been validated.
3929
/// </summary>

0 commit comments

Comments
 (0)