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

Commit ebecb8b

Browse files
committed
Update the validation middleware to validate the expiration date before the audience to be consistent with the introspection middleware
1 parent 28d44f7 commit ebecb8b

File tree

8 files changed

+28
-29
lines changed

8 files changed

+28
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
126126
Error = new OAuthIntrospectionError
127127
{
128128
Error = OAuthIntrospectionConstants.Errors.InvalidToken,
129-
ErrorDescription = "The access token is expired."
129+
ErrorDescription = "The access token is no longer valid."
130130
}
131131
});
132132

src/AspNet.Security.OAuth.Validation/OAuthValidationHandler.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,37 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
9696
return AuthenticateResult.Fail("Authentication failed because the access token was invalid.");
9797
}
9898

99-
// Ensure that the access token was issued
100-
// to be used with this resource server.
101-
if (!ValidateAudience(ticket))
99+
// Ensure that the authentication ticket is still valid.
100+
if (ticket.Properties.ExpiresUtc.HasValue &&
101+
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow)
102102
{
103103
Context.Features.Set(new OAuthValidationFeature
104104
{
105105
Error = new OAuthValidationError
106106
{
107107
Error = OAuthValidationConstants.Errors.InvalidToken,
108-
ErrorDescription = "The access token is not valid for this resource server."
108+
ErrorDescription = "The access token is no longer valid."
109109
}
110110
});
111111

112-
return AuthenticateResult.Fail("Authentication failed because the access token " +
113-
"was not valid for this resource server.");
112+
return AuthenticateResult.Fail("Authentication failed because the access token was expired.");
114113
}
115114

116-
// Ensure that the authentication ticket is still valid.
117-
if (ticket.Properties.ExpiresUtc.HasValue &&
118-
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow)
115+
// Ensure that the access token was issued
116+
// to be used with this resource server.
117+
if (!ValidateAudience(ticket))
119118
{
120119
Context.Features.Set(new OAuthValidationFeature
121120
{
122121
Error = new OAuthValidationError
123122
{
124123
Error = OAuthValidationConstants.Errors.InvalidToken,
125-
ErrorDescription = "The access token is expired."
124+
ErrorDescription = "The access token is not valid for this resource server."
126125
}
127126
});
128127

129-
return AuthenticateResult.Fail("Authentication failed because the access token was expired.");
128+
return AuthenticateResult.Fail("Authentication failed because the access token " +
129+
"was not valid for this resource server.");
130130
}
131131

132132
var notification = new ValidateTokenContext(Context, Options, ticket);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using System.Security.Claims;
1515
using System.Text;
1616
using System.Threading.Tasks;
17-
using Microsoft.Extensions.Caching.Distributed;
1817
using Microsoft.Extensions.Logging;
1918
using Microsoft.Owin.Security;
2019
using Microsoft.Owin.Security.Infrastructure;
@@ -125,7 +124,7 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
125124
Context.Set(typeof(OAuthIntrospectionError).FullName, new OAuthIntrospectionError
126125
{
127126
Error = OAuthIntrospectionConstants.Errors.InvalidToken,
128-
ErrorDescription = "The access token is expired."
127+
ErrorDescription = "The access token is no longer valid."
129128
});
130129

131130
return null;

src/Owin.Security.OAuth.Validation/OAuthValidationHandler.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,32 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
9494
return null;
9595
}
9696

97-
// Ensure that the access token was issued
98-
// to be used with this resource server.
99-
if (!ValidateAudience(ticket))
97+
// Ensure that the authentication ticket is still valid.
98+
if (ticket.Properties.ExpiresUtc.HasValue &&
99+
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow)
100100
{
101-
Logger.LogError("Authentication failed because the access token " +
102-
"was not valid for this resource server.");
101+
Logger.LogError("Authentication failed because the access token was expired.");
103102

104103
Context.Set(typeof(OAuthValidationError).FullName, new OAuthValidationError
105104
{
106105
Error = OAuthValidationConstants.Errors.InvalidToken,
107-
ErrorDescription = "The access token is not valid for this resource server."
106+
ErrorDescription = "The access token is no longer valid."
108107
});
109108

110109
return null;
111110
}
112111

113-
// Ensure that the authentication ticket is still valid.
114-
if (ticket.Properties.ExpiresUtc.HasValue &&
115-
ticket.Properties.ExpiresUtc.Value < Options.SystemClock.UtcNow)
112+
// Ensure that the access token was issued
113+
// to be used with this resource server.
114+
if (!ValidateAudience(ticket))
116115
{
117-
Logger.LogError("Authentication failed because the access token was expired.");
116+
Logger.LogError("Authentication failed because the access token " +
117+
"was not valid for this resource server.");
118118

119119
Context.Set(typeof(OAuthValidationError).FullName, new OAuthValidationError
120120
{
121121
Error = OAuthValidationConstants.Errors.InvalidToken,
122-
ErrorDescription = "The access token is expired."
122+
ErrorDescription = "The access token is not valid for this resource server."
123123
});
124124

125125
return null;

test/AspNet.Security.OAuth.Introspection.Tests/OAuthIntrospectionHandlerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ public async Task HandleUnauthorizedAsync_ErrorDetailsAreResolvedFromChallengeCo
571571

572572
[Theory]
573573
[InlineData("invalid-token", OAuthIntrospectionConstants.Errors.InvalidToken, "The access token is not valid.")]
574-
[InlineData("expired-token", OAuthIntrospectionConstants.Errors.InvalidToken, "The access token is expired.")]
574+
[InlineData("expired-token", OAuthIntrospectionConstants.Errors.InvalidToken, "The access token is no longer valid.")]
575575
public async Task HandleUnauthorizedAsync_ErrorDetailsAreInferredFromAuthenticationFailure(
576576
string token, string error, string description)
577577
{

test/AspNet.Security.OAuth.Validation.Tests/OAuthValidationHandlerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ public async Task HandleUnauthorizedAsync_ErrorDetailsAreResolvedFromChallengeCo
558558

559559
[Theory]
560560
[InlineData("invalid-token", OAuthValidationConstants.Errors.InvalidToken, "The access token is not valid.")]
561-
[InlineData("expired-token", OAuthValidationConstants.Errors.InvalidToken, "The access token is expired.")]
561+
[InlineData("expired-token", OAuthValidationConstants.Errors.InvalidToken, "The access token is no longer valid.")]
562562
public async Task HandleUnauthorizedAsync_ErrorDetailsAreInferredFromAuthenticationFailure(
563563
string token, string error, string description)
564564
{

test/Owin.Security.OAuth.Introspection.Tests/OAuthIntrospectionHandlerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public async Task HandleUnauthorizedAsync_ErrorDetailsAreResolvedFromChallengeCo
553553

554554
[Theory]
555555
[InlineData("invalid-token", OAuthIntrospectionConstants.Errors.InvalidToken, "The access token is not valid.")]
556-
[InlineData("expired-token", OAuthIntrospectionConstants.Errors.InvalidToken, "The access token is expired.")]
556+
[InlineData("expired-token", OAuthIntrospectionConstants.Errors.InvalidToken, "The access token is no longer valid.")]
557557
public async Task HandleUnauthorizedAsync_ErrorDetailsAreInferredFromAuthenticationFailure(
558558
string token, string error, string description)
559559
{

test/Owin.Security.OAuth.Validation.Tests/OAuthValidationHandlerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ public async Task HandleUnauthorizedAsync_ErrorDetailsAreResolvedFromChallengeCo
542542

543543
[Theory]
544544
[InlineData("invalid-token", OAuthValidationConstants.Errors.InvalidToken, "The access token is not valid.")]
545-
[InlineData("expired-token", OAuthValidationConstants.Errors.InvalidToken, "The access token is expired.")]
545+
[InlineData("expired-token", OAuthValidationConstants.Errors.InvalidToken, "The access token is no longer valid.")]
546546
public async Task HandleUnauthorizedAsync_ErrorDetailsAreInferredFromAuthenticationFailure(
547547
string token, string error, string description)
548548
{

0 commit comments

Comments
 (0)