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

Commit 3ff2c22

Browse files
WHY WON'T MY TESTS WORK AGH
1 parent 3d7d436 commit 3ff2c22

29 files changed

+237
-239
lines changed

src/AspNet.Security.OAuth.Introspection/Events/MessageReceivedContext.cs renamed to src/AspNet.Security.OAuth.Introspection/Events/AccessTokenReceivedContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Microsoft.AspNetCore.Http;
22

33
namespace AspNet.Security.OAuth.Introspection {
4-
public class MessageReceivedContext : BaseIntrospectionContext {
5-
public MessageReceivedContext(
4+
public class AccessTokenReceivedContext : BaseIntrospectionContext {
5+
public AccessTokenReceivedContext(
66
HttpContext context,
77
OAuthIntrospectionOptions options)
88
: base(context, options) {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
using System.Threading.Tasks;
22

33
namespace AspNet.Security.OAuth.Introspection {
4-
public interface IOAuthIntrospectionEvents {
4+
public interface IOAuthIntrospectionEvents
5+
{
6+
/// <summary>
7+
/// Invoked when an access token is first received.
8+
/// </summary>
9+
Task AccessTokenReceived(AccessTokenReceivedContext context);
10+
511
/// <summary>
612
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
713
/// </summary>
@@ -15,12 +21,7 @@ public interface IOAuthIntrospectionEvents {
1521
/// <summary>
1622
/// Invoked when a token is to be sent to the authorization server for introspection.
1723
/// </summary>
18-
Task IntrospectToken(IntrospectTokenContext context);
19-
20-
/// <summary>
21-
/// Invoked when a protocol message is first received.
22-
/// </summary>
23-
Task MessageRecieved(MessageReceivedContext context);
24+
Task RequestTokenIntrospection(RequestTokenIntrospectionContext context);
2425

2526
/// <summary>
2627
/// Invoked after processing, when a token has been validated.

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
using System.Threading.Tasks;
33

44
namespace AspNet.Security.OAuth.Introspection {
5-
public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents {
5+
public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents
6+
{
7+
/// <summary>
8+
/// Invoked when an access token is first received.
9+
/// </summary>
10+
public Func<AccessTokenReceivedContext, Task> OnAccessTokenReceived { get; set; } = context => Task.FromResult(0);
11+
612
/// <summary>
713
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
814
/// </summary>
@@ -16,13 +22,8 @@ public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents {
1622
/// <summary>
1723
/// Invoked when a token is to be sent to the authorization server for introspection.
1824
/// </summary>
19-
public Func<IntrospectTokenContext, Task> OnIntrospectToken { get; set; } = context => Task.FromResult(0);
25+
public Func<RequestTokenIntrospectionContext, Task> OnRequestTokenIntrospection { get; set; } = context => Task.FromResult(0);
2026

21-
/// <summary>
22-
/// Invoked when a protocol message is first received.
23-
/// </summary>
24-
public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.FromResult(0);
25-
2627
/// <summary>
2728
/// Invoked after processing, when a token has been validated.
2829
/// </summary>
@@ -33,6 +34,11 @@ public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents {
3334
/// </summary>
3435
public Func<ValidateAudienceContext, Task> OnValidateAudience { get; set; } = context => Task.FromResult(0);
3536

37+
/// <summary>
38+
/// Invoked when an access token is first received.
39+
/// </summary>
40+
public virtual Task AccessTokenReceived(AccessTokenReceivedContext context) => OnAccessTokenReceived(context);
41+
3642
/// <summary>
3743
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
3844
/// </summary>
@@ -46,13 +52,8 @@ public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents {
4652
/// <summary>
4753
/// Invoked when a token is to be sent to the authorization server for introspection.
4854
/// </summary>
49-
public virtual Task IntrospectToken(IntrospectTokenContext context) => OnIntrospectToken(context);
55+
public virtual Task RequestTokenIntrospection(RequestTokenIntrospectionContext context) => OnRequestTokenIntrospection(context);
5056

51-
/// <summary>
52-
/// Invoked when a protocol message is first received.
53-
/// </summary>
54-
public virtual Task MessageRecieved(MessageReceivedContext context) => OnMessageReceived(context);
55-
5657
/// <summary>
5758
/// Invoked after processing, when a token has been validated.
5859
/// </summary>

src/AspNet.Security.OAuth.Introspection/Events/IntrospectTokenContext.cs renamed to src/AspNet.Security.OAuth.Introspection/Events/RequestTokenIntrospectionContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using Newtonsoft.Json.Linq;
33

44
namespace AspNet.Security.OAuth.Introspection {
5-
public class IntrospectTokenContext : BaseIntrospectionContext {
6-
public IntrospectTokenContext(
5+
public class RequestTokenIntrospectionContext : BaseIntrospectionContext {
6+
public RequestTokenIntrospectionContext(
77
HttpContext context,
88
OAuthIntrospectionOptions options,
99
string token)

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public ValidateAudienceContext(
1515
var audiences = payload[OAuthIntrospectionConstants.Claims.Audience];
1616
if(audiences != null) {
1717
if(audiences.Type == JTokenType.String) {
18-
Audiences = payload.Value<JArray>(OAuthIntrospectionConstants.Claims.Audience)
19-
.Select(audience => audience.Value<string>());
18+
Audiences = new[] { payload.Value<string>(OAuthIntrospectionConstants.Claims.Audience) };
2019
}
2120
else if (audiences.Type == JTokenType.Array) {
22-
Audiences = new[] { payload.Value<string>(OAuthIntrospectionConstants.Claims.Audience) };
21+
Audiences = payload.Value<JArray>(OAuthIntrospectionConstants.Claims.Audience)
22+
.Select(audience => audience.Value<string>());
2323
}
2424
}
2525
Validate();
@@ -35,8 +35,11 @@ public bool Validate() {
3535
return IsValid = Validate(Audiences);
3636
}
3737

38-
public bool Validate(IEnumerable<string> audiences) {
39-
return IsValid = Options.Audiences.Count == 0 || !audiences.Intersect(Options.Audiences, StringComparer.Ordinal).Any();
38+
public bool Validate(IEnumerable<string> audiences)
39+
{
40+
IsValid = (audiences == null && Options.Audiences.Count > 0) || Options.Audiences.Count == 0 ||
41+
!audiences.Intersect(Options.Audiences, StringComparer.Ordinal).Any();
42+
return IsValid;
4043
}
4144
}
4245
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ public class OAuthIntrospectionHandler : AuthenticationHandler<OAuthIntrospectio
2525
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
2626
try {
2727
// Give application opportunity to find from a different location, adjust, or reject token
28-
var messageReceivedContext = new MessageReceivedContext(Context, Options);
28+
var accessTokenReceivedContext = new AccessTokenReceivedContext(Context, Options);
2929

3030
// event can set the token
31-
await Options.Events.MessageRecieved(messageReceivedContext);
32-
if (messageReceivedContext.HandledResponse) {
33-
return AuthenticateResult.Success(messageReceivedContext.Ticket);
31+
await Options.Events.AccessTokenReceived(accessTokenReceivedContext);
32+
if (accessTokenReceivedContext.HandledResponse) {
33+
return AuthenticateResult.Success(accessTokenReceivedContext.Ticket);
3434
}
35-
if (messageReceivedContext.Skipped) {
35+
if (accessTokenReceivedContext.Skipped) {
3636
Logger.LogInformation("Authentication was skipped by event processing.");
3737

38-
return null;
38+
return AuthenticateResult.Skip();
3939
}
4040

4141
// If application retrieved token from somewhere else, use that.
42-
string token = messageReceivedContext.Token;
42+
string token = accessTokenReceivedContext.Token;
4343

4444
if (string.IsNullOrWhiteSpace(token)) {
4545
string header = Request.Headers[HeaderNames.Authorization];
@@ -68,18 +68,18 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
6868
if (ticket == null) {
6969
JObject payload;
7070
// Allow interception of the introspection retrieval process via events
71-
var introspectTokenContext = new IntrospectTokenContext(Context, Options, token);
72-
await Options.Events.IntrospectToken(introspectTokenContext);
73-
if (introspectTokenContext.HandledResponse) {
74-
return AuthenticateResult.Success(introspectTokenContext.Ticket);
71+
var requestTokenIntrospectionContext = new RequestTokenIntrospectionContext(Context, Options, token);
72+
await Options.Events.RequestTokenIntrospection(requestTokenIntrospectionContext);
73+
if (requestTokenIntrospectionContext.HandledResponse) {
74+
return AuthenticateResult.Success(requestTokenIntrospectionContext.Ticket);
7575
}
76-
else if (introspectTokenContext.Skipped) {
76+
else if (requestTokenIntrospectionContext.Skipped) {
7777
return AuthenticateResult.Skip();
7878
}
7979
else {
8080
// Return a failed authentication result if the introspection
8181
// request failed or if the "active" claim was false.
82-
payload = introspectTokenContext.Payload ?? await GetIntrospectionPayloadAsync(token);
82+
payload = requestTokenIntrospectionContext.Payload ?? await GetIntrospectionPayloadAsync(token);
8383
}
8484

8585
if (payload == null || !payload.Value<bool>(OAuthIntrospectionConstants.Claims.Active)) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.AspNetCore.Http;
2+
3+
namespace AspNet.Security.OAuth.Validation
4+
{
5+
public class AccessTokenReceivedContext : BaseOAuthValidationContext
6+
{
7+
public AccessTokenReceivedContext(HttpContext context, OAuthValidationOptions options)
8+
: base(context, options)
9+
{
10+
}
11+
12+
/// <summary>
13+
/// Bearer Token. This will give application an opportunity to retrieve token from an alternation location.
14+
/// </summary>
15+
public string Token { get; set; }
16+
}
17+
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
using Microsoft.AspNetCore.Authentication;
2-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Http;
32
using System;
43

54
namespace AspNet.Security.OAuth.Validation
65
{
7-
public class AuthenticationFailedContext : BaseControlContext
6+
public class AuthenticationFailedContext : BaseOAuthValidationContext
87
{
98
public AuthenticationFailedContext(HttpContext context, OAuthValidationOptions options)
10-
: base(context)
9+
: base(context, options)
1110
{
12-
Options = options;
1311
}
14-
public OAuthValidationOptions Options { get; }
1512
public Exception Exception { get; set; }
1613
}
1714
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ namespace AspNet.Security.OAuth.Validation
55
public interface IOAuthValidationEvents
66
{
77
/// <summary>
8-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
8+
/// Invoked when an access token is first received.
99
/// </summary>
10-
Task AuthenticationFailed(AuthenticationFailedContext context);
10+
Task AccessTokenReceived(AccessTokenReceivedContext context);
1111

1212
/// <summary>
13-
/// Invoked when a protocol message is first received.
13+
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
1414
/// </summary>
15-
Task MessageRecieved(MessageReceivedContext context);
15+
Task AuthenticationFailed(AuthenticationFailedContext context);
1616

1717
/// <summary>
1818
/// Invoked after processing, when a token has been validated.

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

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

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
using System.Threading.Tasks;
33

44
namespace AspNet.Security.OAuth.Validation {
5-
public class OAuthValidationEvents : IOAuthValidationEvents {
5+
public class OAuthValidationEvents : IOAuthValidationEvents
6+
{
67
/// <summary>
7-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
8+
/// Invoked when an access token is first received.
89
/// </summary>
9-
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0);
10+
public Func<AccessTokenReceivedContext, Task> OnAccessTokenReceived { get; set; } = context => Task.FromResult(0);
1011

1112
/// <summary>
12-
/// Invoked when a protocol message is first received.
13+
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
1314
/// </summary>
14-
public Func<MessageReceivedContext, Task> OnMessageReceived { get; set; } = context => Task.FromResult(0);
15-
15+
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0);
16+
1617
/// <summary>
1718
/// Invoked after processing, when a token has been validated.
1819
/// </summary>
@@ -24,15 +25,15 @@ public class OAuthValidationEvents : IOAuthValidationEvents {
2425
public Func<ValidateAudienceContext, Task> OnValidateAudience { get; set; } = context => Task.FromResult(0);
2526

2627
/// <summary>
27-
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
28+
/// Invoked when an access token is first received.
2829
/// </summary>
29-
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);
30+
public virtual Task AccessTokenReceived(AccessTokenReceivedContext context) => OnAccessTokenReceived(context);
3031

3132
/// <summary>
32-
/// Invoked when a protocol message is first received.
33+
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
3334
/// </summary>
34-
public virtual Task MessageRecieved(MessageReceivedContext context) => OnMessageReceived(context);
35-
35+
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);
36+
3637
/// <summary>
3738
/// Invoked after processing, when a token has been validated.
3839
/// </summary>
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
using Microsoft.AspNetCore.Http;
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Http;
23

34
namespace AspNet.Security.OAuth.Validation
45
{
56
public class TokenValidatedContext : BaseOAuthValidationContext
67
{
78
public TokenValidatedContext(
89
HttpContext context,
9-
OAuthValidationOptions options)
10-
: base(context, options) {
10+
OAuthValidationOptions options,
11+
AuthenticationTicket ticket)
12+
: base(context, options)
13+
{
14+
Ticket = ticket;
1115
}
1216
}
1317
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@
66

77
namespace AspNet.Security.OAuth.Validation
88
{
9-
public class ValidateAudienceContext : BaseControlContext
9+
public class ValidateAudienceContext : BaseOAuthValidationContext
1010
{
1111
public ValidateAudienceContext(
12-
HttpContext context,
12+
HttpContext context,
1313
OAuthValidationOptions options,
1414
AuthenticationTicket ticket)
15-
: base(context) {
16-
Options = options;
15+
: base(context, options) {
1716
Ticket = ticket;
1817
string audiences;
19-
if (!ticket.Properties.Items.TryGetValue(OAuthValidationConstants.Properties.Audiences, out audiences))
18+
if (ticket.Properties.Items.TryGetValue(OAuthValidationConstants.Properties.Audiences, out audiences))
2019
{
2120
Audiences = audiences.Split(' ');
2221
}
2322
Validate();
2423
}
2524

26-
public OAuthValidationOptions Options { get; set; }
27-
2825
public IEnumerable<string> Audiences { get; set; }
2926

3027
public bool IsValid { get; set; }
@@ -36,7 +33,8 @@ public bool Validate()
3633

3734
public bool Validate(IEnumerable<string> audiences)
3835
{
39-
IsValid = Options.Audiences.Count == 0 || !audiences.Intersect(Options.Audiences, StringComparer.Ordinal).Any();
36+
IsValid = (audiences == null && Options.Audiences.Count > 0) || Options.Audiences.Count == 0 ||
37+
!audiences.Intersect(Options.Audiences, StringComparer.Ordinal).Any();
4038
return IsValid;
4139
}
4240
}

0 commit comments

Comments
 (0)