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

Commit a1951c7

Browse files
MonkeyJamboreekevinchalet
authored andcommitted
Introduce events support in the introspection/validation middleware
1 parent eb4be02 commit a1951c7

File tree

45 files changed

+2434
-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.

45 files changed

+2434
-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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Extensions for more information
4+
* concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Security.Claims;
8+
using JetBrains.Annotations;
9+
using Microsoft.AspNetCore.Authentication;
10+
using Microsoft.AspNetCore.Http;
11+
using Newtonsoft.Json.Linq;
12+
13+
namespace AspNet.Security.OAuth.Introspection {
14+
/// <summary>
15+
/// Allows interception of the AuthenticationTicket creation process.
16+
/// </summary>
17+
public class CreateTicketContext : BaseControlContext {
18+
public CreateTicketContext(
19+
[NotNull] HttpContext context,
20+
[NotNull] OAuthIntrospectionOptions options,
21+
[NotNull] AuthenticationTicket ticket,
22+
[NotNull] JObject payload)
23+
: base(context) {
24+
Options = options;
25+
Ticket = ticket;
26+
Payload = payload;
27+
}
28+
29+
/// <summary>
30+
/// Gets the options used by the introspection middleware.
31+
/// </summary>
32+
public OAuthIntrospectionOptions Options { get; }
33+
34+
/// <summary>
35+
/// Gets the identity containing the user claims.
36+
/// </summary>
37+
public ClaimsIdentity Identity => Principal?.Identity as ClaimsIdentity;
38+
39+
/// <summary>
40+
/// Gets the principal containing the user claims.
41+
/// </summary>
42+
public ClaimsPrincipal Principal => Ticket?.Principal;
43+
44+
/// <summary>
45+
/// Gets the payload extracted from the introspection response.
46+
/// </summary>
47+
public JObject Payload { get; }
48+
}
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Extensions for more information
4+
* concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Net.Http;
8+
using JetBrains.Annotations;
9+
using Microsoft.AspNetCore.Authentication;
10+
using Microsoft.AspNetCore.Http;
11+
12+
namespace AspNet.Security.OAuth.Introspection {
13+
/// <summary>
14+
/// Allows for custom handling of the call to the Authorization Server's Introspection endpoint.
15+
/// </summary>
16+
public class RequestTokenIntrospectionContext : BaseContext {
17+
public RequestTokenIntrospectionContext(
18+
[NotNull] HttpContext context,
19+
[NotNull] OAuthIntrospectionOptions options,
20+
[NotNull] HttpRequestMessage message,
21+
[NotNull] string token)
22+
: base(context) {
23+
Options = options;
24+
Message = message;
25+
Token = token;
26+
}
27+
28+
/// <summary>
29+
/// Gets the options used by the introspection middleware.
30+
/// </summary>
31+
public OAuthIntrospectionOptions Options { get; }
32+
33+
/// <summary>
34+
/// An <see cref="HttpClient"/> for use by the application to call the authorization server.
35+
/// </summary>
36+
public HttpClient Client => Options.HttpClient;
37+
38+
/// <summary>
39+
/// Gets the HTTP message sent to the introspection endpoint.
40+
/// </summary>
41+
public HttpRequestMessage Message { get; }
42+
43+
/// <summary>
44+
/// The access token parsed from the client request.
45+
/// </summary>
46+
public string Token { get; }
47+
}
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Extensions for more information
4+
* concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using JetBrains.Annotations;
8+
using Microsoft.AspNetCore.Authentication;
9+
using Microsoft.AspNetCore.Http;
10+
11+
namespace AspNet.Security.OAuth.Introspection {
12+
/// <summary>
13+
/// Allows custom parsing of access tokens from requests.
14+
/// </summary>
15+
public class RetrieveTokenContext : BaseControlContext {
16+
public RetrieveTokenContext(
17+
[NotNull] HttpContext context,
18+
[NotNull] OAuthIntrospectionOptions options)
19+
: base(context) {
20+
Options = options;
21+
}
22+
23+
/// <summary>
24+
/// Gets the options used by the introspection middleware.
25+
/// </summary>
26+
public OAuthIntrospectionOptions Options { get; }
27+
28+
/// <summary>
29+
/// Gets or sets the access token.
30+
/// </summary>
31+
public string Token { get; set; }
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Extensions for more information
4+
* concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using JetBrains.Annotations;
8+
using Microsoft.AspNetCore.Authentication;
9+
using Microsoft.AspNetCore.Http;
10+
11+
namespace AspNet.Security.OAuth.Introspection {
12+
/// <summary>
13+
/// Allows customization of the token validation logic.
14+
/// </summary>
15+
public class ValidateTokenContext : BaseControlContext {
16+
public ValidateTokenContext(
17+
[NotNull] HttpContext context,
18+
[NotNull] OAuthIntrospectionOptions options,
19+
[NotNull] AuthenticationTicket ticket)
20+
: base(context) {
21+
Options = options;
22+
Ticket = ticket;
23+
}
24+
25+
/// <summary>
26+
/// Gets the options used by the introspection middleware.
27+
/// </summary>
28+
public OAuthIntrospectionOptions Options { get; }
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Extensions for more information
4+
* concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System.Threading.Tasks;
8+
9+
namespace AspNet.Security.OAuth.Introspection {
10+
/// <summary>
11+
/// Allows customization of introspection handling within the middleware.
12+
/// </summary>
13+
public interface IOAuthIntrospectionEvents {
14+
/// <summary>
15+
/// Invoked when a token is to be parsed from a newly-received request.
16+
/// </summary>
17+
Task RetrieveToken(RetrieveTokenContext context);
18+
19+
/// <summary>
20+
/// Invoked when a ticket is to be created from an introspection response.
21+
/// </summary>
22+
Task CreateTicket(CreateTicketContext context);
23+
24+
/// <summary>
25+
/// Invoked when a token is to be sent to the authorization server for introspection.
26+
/// </summary>
27+
Task RequestTokenIntrospection(RequestTokenIntrospectionContext context);
28+
29+
/// <summary>
30+
/// Invoked when a token is to be validated, before final processing.
31+
/// </summary>
32+
Task ValidateToken(ValidateTokenContext context);
33+
}
34+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public static class Parameters {
2727
public const string TokenTypeHint = "token_type_hint";
2828
}
2929

30+
public static class Properties {
31+
public const string Audiences = ".audiences";
32+
}
33+
3034
public static class TokenTypes {
3135
public const string AccessToken = "access_token";
3236
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Extensions for more information
4+
* concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System;
8+
using System.Threading.Tasks;
9+
10+
namespace AspNet.Security.OAuth.Introspection {
11+
/// <summary>
12+
/// Allows customization of introspection handling within the middleware.
13+
/// </summary>
14+
public class OAuthIntrospectionEvents : IOAuthIntrospectionEvents {
15+
/// <summary>
16+
/// Invoked when a ticket is to be created from an introspection response.
17+
/// </summary>
18+
public Func<CreateTicketContext, Task> OnCreateTicket { get; set; } = context => Task.FromResult(0);
19+
20+
/// <summary>
21+
/// Invoked when a token is to be parsed from a newly-received request.
22+
/// </summary>
23+
public Func<RetrieveTokenContext, Task> OnRetrieveToken { get; set; } = context => Task.FromResult(0);
24+
25+
/// <summary>
26+
/// Invoked when a token is to be sent to the authorization server for introspection.
27+
/// </summary>
28+
public Func<RequestTokenIntrospectionContext, Task> OnRequestTokenIntrospection { get; set; } = context => Task.FromResult(0);
29+
30+
/// <summary>
31+
/// Invoked when a token is to be validated, before final processing.
32+
/// </summary>
33+
public Func<ValidateTokenContext, Task> OnValidateToken { get; set; } = context => Task.FromResult(0);
34+
35+
/// <summary>
36+
/// Invoked when a ticket is to be created from an introspection response.
37+
/// </summary>
38+
public virtual Task CreateTicket(CreateTicketContext context) => OnCreateTicket(context);
39+
40+
/// <summary>
41+
/// Invoked when a token is to be parsed from a newly-received request.
42+
/// </summary>
43+
public virtual Task RetrieveToken(RetrieveTokenContext context) => OnRetrieveToken(context);
44+
45+
/// <summary>
46+
/// Invoked when a token is to be sent to the authorization server for introspection.
47+
/// </summary>
48+
public virtual Task RequestTokenIntrospection(RequestTokenIntrospectionContext context) => OnRequestTokenIntrospection(context);
49+
50+
/// <summary>
51+
/// Invoked when a token is to be validated, before final processing.
52+
/// </summary>
53+
public virtual Task ValidateToken(ValidateTokenContext context) => OnValidateToken(context);
54+
}
55+
}

0 commit comments

Comments
 (0)