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

Commit 7a7fbab

Browse files
Finished initial work on Events for the middlewares.
Squashed commit: [3ff2c22] WHY WON'T MY TESTS WORK AGH [3d7d436] Finished events - Build error fixes and exclusion of .build folder from tracked files. Squashed commits: [6e9a9fe] Finished work on Introspection and Validation events except for tests on a couple of the Introspection events. Also fixed the RootNamespaces of all of the projects. (+3 squashed commit) Squashed commit: [c572f5b] Whitespace changes mostly [c1ea6bc] Wrote some tests but missing AuthenticationFailedContext tests and tests for exceptions thrown. [f8ae71c] Finished events for the Validation middleware. Tests need to be created. (+1 squashed commits) Squashed commits: [36b8003] Finished events for the Validation middleware. Tests need to be created.
1 parent 5a197ba commit 7a7fbab

File tree

47 files changed

+2123
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2123
-226
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ x64/
2121
build/
2222
[Bb]in/
2323
[Oo]bj/
24+
.build/
2425

2526
# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
2627
!packages/*/build/

src/AspNet.Security.OAuth.Introspection/AspNet.Security.OAuth.Introspection.xproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
55
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
66
</PropertyGroup>
7-
87
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
98
<PropertyGroup Label="Globals">
109
<ProjectGuid>a8569260-142c-427a-8b14-a8df56cc15b7</ProjectGuid>
11-
<RootNamespace>AspNet.Security.OpenIdConnect.Introspection</RootNamespace>
10+
<RootNamespace>AspNet.Security.OAuth.Introspection</RootNamespace>
1211
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
1312
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
1413
</PropertyGroup>
15-
1614
<PropertyGroup>
1715
<SchemaVersion>2.0</SchemaVersion>
1816
</PropertyGroup>
1917
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
20-
</Project>
18+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Http;
2+
3+
namespace AspNet.Security.OAuth.Introspection {
4+
public class AccessTokenReceivedContext : BaseIntrospectionContext {
5+
public AccessTokenReceivedContext(
6+
HttpContext context,
7+
OAuthIntrospectionOptions options)
8+
: base(context, options) {
9+
}
10+
11+
/// <summary>
12+
/// Gets or sets the access token.
13+
/// </summary>
14+
public string Token { get; set; }
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
4+
namespace AspNet.Security.OAuth.Introspection {
5+
public class AuthenticationFailedContext : BaseIntrospectionContext {
6+
public AuthenticationFailedContext(
7+
HttpContext context,
8+
OAuthIntrospectionOptions options)
9+
: base(context, options) {
10+
}
11+
12+
public Exception Exception { get; set; }
13+
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Http;
3+
4+
namespace AspNet.Security.OAuth.Introspection
5+
{
6+
public abstract class BaseIntrospectionContext : BaseControlContext
7+
{
8+
public BaseIntrospectionContext(
9+
HttpContext context,
10+
OAuthIntrospectionOptions options)
11+
: base(context) {
12+
Options = options;
13+
}
14+
15+
public OAuthIntrospectionOptions Options { get; }
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Newtonsoft.Json.Linq;
3+
4+
namespace AspNet.Security.OAuth.Introspection {
5+
public class CreateTicketContext : BaseIntrospectionContext {
6+
public CreateTicketContext(
7+
HttpContext context,
8+
OAuthIntrospectionOptions options,
9+
JObject payload)
10+
: base(context, options) {
11+
Payload = payload;
12+
}
13+
14+
public JObject Payload { get; set; }
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Threading.Tasks;
2+
3+
namespace AspNet.Security.OAuth.Introspection {
4+
public interface IOAuthIntrospectionEvents
5+
{
6+
/// <summary>
7+
/// Invoked when an access token is first received.
8+
/// </summary>
9+
Task AccessTokenReceived(AccessTokenReceivedContext context);
10+
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+
16+
/// <summary>
17+
/// Invoked when a ticket is to be created from an introspection response.
18+
/// </summary>
19+
Task CreateTicket(CreateTicketContext context);
20+
21+
/// <summary>
22+
/// Invoked when a token is to be sent to the authorization server for introspection.
23+
/// </summary>
24+
Task RequestTokenIntrospection(RequestTokenIntrospectionContext context);
25+
26+
/// <summary>
27+
/// Invoked after processing, when a token has been validated.
28+
/// </summary>
29+
Task TokenValidated(TokenValidatedContext context);
30+
31+
/// <summary>
32+
/// Invoked when audiences are to be validated for a message.
33+
/// </summary>
34+
Task ValidateAudience(ValidateAudienceContext context);
35+
}
36+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace AspNet.Security.OAuth.Introspection {
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+
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+
17+
/// <summary>
18+
/// Invoked when a ticket is to be created from an introspection response.
19+
/// </summary>
20+
public Func<CreateTicketContext, Task> OnCreateTicket { get; set; } = context => Task.FromResult(0);
21+
22+
/// <summary>
23+
/// Invoked when a token is to be sent to the authorization server for introspection.
24+
/// </summary>
25+
public Func<RequestTokenIntrospectionContext, Task> OnRequestTokenIntrospection { get; set; } = context => Task.FromResult(0);
26+
27+
/// <summary>
28+
/// Invoked after processing, when a token has been validated.
29+
/// </summary>
30+
public Func<TokenValidatedContext, Task> OnTokenValidated { get; set; } = context => Task.FromResult(0);
31+
32+
/// <summary>
33+
/// Invoked when audiences are to be validated for a message.
34+
/// </summary>
35+
public Func<ValidateAudienceContext, Task> OnValidateAudience { get; set; } = context => Task.FromResult(0);
36+
37+
/// <summary>
38+
/// Invoked when an access token is first received.
39+
/// </summary>
40+
public virtual Task AccessTokenReceived(AccessTokenReceivedContext context) => OnAccessTokenReceived(context);
41+
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+
47+
/// <summary>
48+
/// Invoked when a ticket is to be created from an introspection response.
49+
/// </summary>
50+
public virtual Task CreateTicket(CreateTicketContext context) => OnCreateTicket(context);
51+
52+
/// <summary>
53+
/// Invoked when a token is to be sent to the authorization server for introspection.
54+
/// </summary>
55+
public virtual Task RequestTokenIntrospection(RequestTokenIntrospectionContext context) => OnRequestTokenIntrospection(context);
56+
57+
/// <summary>
58+
/// Invoked after processing, when a token has been validated.
59+
/// </summary>
60+
public virtual Task TokenValidated(TokenValidatedContext context) => OnTokenValidated(context);
61+
62+
/// <summary>
63+
/// Invoked when audiences are to be validated for a message.
64+
/// </summary>
65+
public virtual Task ValidateAudience(ValidateAudienceContext context) => OnValidateAudience(context);
66+
}
67+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Newtonsoft.Json.Linq;
3+
4+
namespace AspNet.Security.OAuth.Introspection {
5+
public class RequestTokenIntrospectionContext : BaseIntrospectionContext {
6+
public RequestTokenIntrospectionContext(
7+
HttpContext context,
8+
OAuthIntrospectionOptions options,
9+
string token)
10+
: base(context, options) {
11+
Token = token;
12+
}
13+
14+
public string Token { get; }
15+
16+
public JObject Payload { get; set; }
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Http;
3+
4+
namespace AspNet.Security.OAuth.Introspection {
5+
public class TokenValidatedContext : BaseIntrospectionContext {
6+
public TokenValidatedContext(
7+
HttpContext context,
8+
OAuthIntrospectionOptions options,
9+
AuthenticationTicket ticket)
10+
: base(context, options) {
11+
Ticket = ticket;
12+
}
13+
}
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
namespace AspNet.Security.OAuth.Introspection {
8+
public class ValidateAudienceContext : BaseIntrospectionContext {
9+
public ValidateAudienceContext(
10+
HttpContext context,
11+
OAuthIntrospectionOptions options,
12+
JToken payload)
13+
: base(context, options) {
14+
Payload = payload;
15+
var audiences = payload[OAuthIntrospectionConstants.Claims.Audience];
16+
if(audiences != null) {
17+
if(audiences.Type == JTokenType.String) {
18+
Audiences = new[] { payload.Value<string>(OAuthIntrospectionConstants.Claims.Audience) };
19+
}
20+
else if (audiences.Type == JTokenType.Array) {
21+
Audiences = payload.Value<JArray>(OAuthIntrospectionConstants.Claims.Audience)
22+
.Select(audience => audience.Value<string>());
23+
}
24+
}
25+
Validate();
26+
}
27+
28+
public IEnumerable<string> Audiences { get; set; }
29+
30+
public JToken Payload { get; }
31+
32+
public bool IsValid { get; set; }
33+
34+
public bool Validate() {
35+
return IsValid = Validate(Audiences);
36+
}
37+
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;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)