Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit e529bbd

Browse files
Merge pull request #32 from Jarga/dev
Add OAuth2Introspection Events
2 parents ce37b0f + 2a54819 commit e529bbd

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Security.Claims;
3+
using System.Threading.Tasks;
4+
5+
namespace IdentityModel.AspNetCore.OAuth2Introspection
6+
{
7+
/// <summary>
8+
/// Default implementation.
9+
/// </summary>
10+
public class OAuth2IntrospectionEvents
11+
{
12+
13+
/// <summary>
14+
/// Gets or sets the function that is invoked when the CreatingTicket method is invoked.
15+
/// </summary>
16+
public Func<ClaimsPrincipal, Task> OnCreatingTicket { get; set; } = context => Task.CompletedTask;
17+
18+
/// <summary>
19+
/// Invoked after the provider successfully authenticates a user.
20+
/// </summary>
21+
/// <param name="principal">Contains claims set hydtrated from the introspection response <see cref="System.Security.Claims.ClaimsPrincipal"/>.</param>
22+
/// <returns>A <see cref="Task"/> representing the completed operation.</returns>
23+
public virtual Task CreatingTicket(ClaimsPrincipal principal) => OnCreatingTicket(principal);
24+
}
25+
}

src/IdentityModel.AspNetCore.OAuth2Introspection/OAuth2IntrospectionHandler.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public OAuth2IntrospectionHandler(
4444
_cache = cache;
4545
}
4646

47+
48+
/// <summary>
49+
/// The handler calls methods on the events which give the application control at certain points where processing is occurring.
50+
/// If it is not provided a default instance is supplied which does nothing when the methods are called.
51+
/// </summary>
52+
protected new OAuth2IntrospectionEvents Events
53+
{
54+
get { return (OAuth2IntrospectionEvents)base.Events; }
55+
set { base.Events = value; }
56+
}
57+
4758
/// <summary>
4859
/// Tries to authenticate a reference token on the current request
4960
/// </summary>
@@ -68,7 +79,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
6879
var claims = await _cache.GetClaimsAsync(token).ConfigureAwait(false);
6980
if (claims != null)
7081
{
71-
var ticket = CreateTicket(claims);
82+
var ticket = await CreateTicket(claims);
7283

7384
_logger.LogTrace("Token found in cache.");
7485

@@ -101,7 +112,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
101112

102113
if (response.IsActive)
103114
{
104-
var ticket = CreateTicket(response.Claims);
115+
var ticket = await CreateTicket(response.Claims);
105116

106117
if (Options.SaveToken)
107118
{
@@ -150,11 +161,13 @@ private async Task<IntrospectionResponse> LoadClaimsForToken(string token)
150161
}).ConfigureAwait(false);
151162
}
152163

153-
private AuthenticationTicket CreateTicket(IEnumerable<Claim> claims)
164+
private async Task<AuthenticationTicket> CreateTicket(IEnumerable<Claim> claims)
154165
{
155166
var id = new ClaimsIdentity(claims, Scheme.Name, Options.NameClaimType, Options.RoleClaimType);
156167
var principal = new ClaimsPrincipal(id);
157168

169+
await Events.CreatingTicket(principal);
170+
158171
return new AuthenticationTicket(principal, new AuthenticationProperties(), Scheme.Name);
159172
}
160173
}

src/IdentityModel.AspNetCore.OAuth2Introspection/OAuth2IntrospectionOptions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ namespace Microsoft.AspNetCore.Builder
1818
/// </summary>
1919
public class OAuth2IntrospectionOptions : AuthenticationSchemeOptions
2020
{
21+
/// <summary>
22+
/// Dafault options constructor
23+
/// </summary>
24+
public OAuth2IntrospectionOptions()
25+
{
26+
Events = new OAuth2IntrospectionEvents();
27+
}
2128
/// <summary>
2229
/// Sets the base-path of the token provider.
2330
/// If set, the OpenID Connect discovery document will be used to find the introspection endpoint.
@@ -113,6 +120,15 @@ public class OAuth2IntrospectionOptions : AuthenticationSchemeOptions
113120
/// </summary>
114121
public Func<HttpRequest, string> TokenRetriever { get; set; } = TokenRetrieval.FromAuthorizationHeader();
115122

123+
/// <summary>
124+
/// Gets or sets the <see cref="OAuth2IntrospectionEvents"/> used to handle authentication events.
125+
/// </summary>
126+
public new OAuth2IntrospectionEvents Events
127+
{
128+
get { return (OAuth2IntrospectionEvents)base.Events; }
129+
set { base.Events = value; }
130+
}
131+
116132
internal AsyncLazy<IntrospectionClient> IntrospectionClient { get; set; }
117133
internal ConcurrentDictionary<string, AsyncLazy<IntrospectionResponse>> LazyIntrospections { get; set; }
118134

0 commit comments

Comments
 (0)