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

Commit 3d7d436

Browse files
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 3d7d436

File tree

46 files changed

+2100
-197
lines changed

Some content is hidden

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

46 files changed

+2100
-197
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: 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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Threading.Tasks;
2+
3+
namespace AspNet.Security.OAuth.Introspection {
4+
public interface IOAuthIntrospectionEvents {
5+
/// <summary>
6+
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
7+
/// </summary>
8+
Task AuthenticationFailed(AuthenticationFailedContext context);
9+
10+
/// <summary>
11+
/// Invoked when a ticket is to be created from an introspection response.
12+
/// </summary>
13+
Task CreateTicket(CreateTicketContext context);
14+
15+
/// <summary>
16+
/// Invoked when a token is to be sent to the authorization server for introspection.
17+
/// </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+
25+
/// <summary>
26+
/// Invoked after processing, when a token has been validated.
27+
/// </summary>
28+
Task TokenValidated(TokenValidatedContext context);
29+
30+
/// <summary>
31+
/// Invoked when audiences are to be validated for a message.
32+
/// </summary>
33+
Task ValidateAudience(ValidateAudienceContext context);
34+
}
35+
}
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 IntrospectTokenContext : BaseIntrospectionContext {
6+
public IntrospectTokenContext(
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: 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 MessageReceivedContext : BaseIntrospectionContext {
5+
public MessageReceivedContext(
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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace AspNet.Security.OAuth.Introspection {
5+
public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents {
6+
/// <summary>
7+
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
8+
/// </summary>
9+
public Func<AuthenticationFailedContext, Task> OnAuthenticationFailed { get; set; } = context => Task.FromResult(0);
10+
11+
/// <summary>
12+
/// Invoked when a ticket is to be created from an introspection response.
13+
/// </summary>
14+
public Func<CreateTicketContext, Task> OnCreateTicket { get; set; } = context => Task.FromResult(0);
15+
16+
/// <summary>
17+
/// Invoked when a token is to be sent to the authorization server for introspection.
18+
/// </summary>
19+
public Func<IntrospectTokenContext, Task> OnIntrospectToken { get; set; } = context => Task.FromResult(0);
20+
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+
26+
/// <summary>
27+
/// Invoked after processing, when a token has been validated.
28+
/// </summary>
29+
public Func<TokenValidatedContext, Task> OnTokenValidated { get; set; } = context => Task.FromResult(0);
30+
31+
/// <summary>
32+
/// Invoked when audiences are to be validated for a message.
33+
/// </summary>
34+
public Func<ValidateAudienceContext, Task> OnValidateAudience { get; set; } = context => Task.FromResult(0);
35+
36+
/// <summary>
37+
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
38+
/// </summary>
39+
public virtual Task AuthenticationFailed(AuthenticationFailedContext context) => OnAuthenticationFailed(context);
40+
41+
/// <summary>
42+
/// Invoked when a ticket is to be created from an introspection response.
43+
/// </summary>
44+
public virtual Task CreateTicket(CreateTicketContext context) => OnCreateTicket(context);
45+
46+
/// <summary>
47+
/// Invoked when a token is to be sent to the authorization server for introspection.
48+
/// </summary>
49+
public virtual Task IntrospectToken(IntrospectTokenContext context) => OnIntrospectToken(context);
50+
51+
/// <summary>
52+
/// Invoked when a protocol message is first received.
53+
/// </summary>
54+
public virtual Task MessageRecieved(MessageReceivedContext context) => OnMessageReceived(context);
55+
56+
/// <summary>
57+
/// Invoked after processing, when a token has been validated.
58+
/// </summary>
59+
public virtual Task TokenValidated(TokenValidatedContext context) => OnTokenValidated(context);
60+
61+
/// <summary>
62+
/// Invoked when audiences are to be validated for a message.
63+
/// </summary>
64+
public virtual Task ValidateAudience(ValidateAudienceContext context) => OnValidateAudience(context);
65+
}
66+
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 = payload.Value<JArray>(OAuthIntrospectionConstants.Claims.Audience)
19+
.Select(audience => audience.Value<string>());
20+
}
21+
else if (audiences.Type == JTokenType.Array) {
22+
Audiences = new[] { payload.Value<string>(OAuthIntrospectionConstants.Claims.Audience) };
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+
return IsValid = Options.Audiences.Count == 0 || !audiences.Intersect(Options.Audiences, StringComparer.Ordinal).Any();
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)