Skip to content

Commit f89274c

Browse files
authored
Merge pull request DuendeArchive#157 from balazsmeszegeto/main
Support modifying underlying TokenIntrospectionRequest
2 parents 62f9a2f + 80d0d41 commit f89274c

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using IdentityModel.Client;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Http;
4+
5+
namespace IdentityModel.AspNetCore.OAuth2Introspection
6+
{
7+
/// <summary>
8+
/// Context for the RequestSending event
9+
/// </summary>
10+
public class RequestSendingContext : BaseContext<OAuth2IntrospectionOptions>
11+
{
12+
/// <summary>
13+
/// ctor
14+
/// </summary>
15+
public RequestSendingContext(
16+
HttpContext context,
17+
AuthenticationScheme scheme,
18+
OAuth2IntrospectionOptions options)
19+
: base(context, scheme, options) { }
20+
21+
/// <summary>
22+
/// The <see cref="TokenIntrospectionRequest"/> request
23+
/// </summary>
24+
public TokenIntrospectionRequest TokenIntrospectionRequest { get; set; }
25+
}
26+
}

src/OAuth2IntrospectionEvents.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public class OAuth2IntrospectionEvents
2626
/// </summary>
2727
public Func<UpdateClientAssertionContext, Task> OnUpdateClientAssertion { get; set; } = context => Task.CompletedTask;
2828

29+
/// <summary>
30+
/// Invoked when sending token introspection request.
31+
/// </summary>
32+
public Func<RequestSendingContext, Task> OnRequestSending { get; set; } = context => Task.CompletedTask;
33+
2934
/// <summary>
3035
/// Invoked if exceptions are thrown during request processing. The exceptions will be re-thrown after this event unless suppressed.
3136
/// </summary>
@@ -40,5 +45,10 @@ public class OAuth2IntrospectionEvents
4045
/// Invoked when client assertion need to be updated.
4146
/// </summary>
4247
public virtual Task UpdateClientAssertion(UpdateClientAssertionContext context) => OnUpdateClientAssertion(context);
48+
49+
/// <summary>
50+
/// Invoked when sending token introspection request.
51+
/// </summary>
52+
public virtual Task RequestSending(RequestSendingContext context) => OnRequestSending(context);
4353
}
44-
}
54+
}

src/OAuth2IntrospectionHandler.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ private static async Task<TokenIntrospectionResponse> LoadClaimsForToken(
179179
{
180180
var introspectionClient = await options.IntrospectionClient.Value.ConfigureAwait(false);
181181
using var request = CreateTokenIntrospectionRequest(token, context, scheme, events, options);
182+
183+
var requestSendingContext = new RequestSendingContext(context, scheme, options)
184+
{
185+
TokenIntrospectionRequest = request,
186+
};
187+
188+
await events.RequestSending(requestSendingContext);
189+
182190
return await introspectionClient.IntrospectTokenAsync(request).ConfigureAwait(false);
183191
}
184192

test/Tests/Introspection.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,31 @@ public async Task ActiveToken_With_Discovery_Unavailable_On_First_Request()
394394
result.StatusCode.Should().Be(HttpStatusCode.OK);
395395
}
396396

397+
[Fact]
398+
public async Task ActiveToken_RequestSending_AdditionalParameter_with_inline_event()
399+
{
400+
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
401+
402+
var client = PipelineFactory.CreateClient(o =>
403+
{
404+
_options(o);
405+
406+
o.Events.OnRequestSending = e =>
407+
{
408+
e.TokenIntrospectionRequest.Parameters = Parameters.FromObject(new { additionalParameter = "42" });
409+
return Task.CompletedTask;
410+
};
411+
412+
}, handler);
413+
414+
client.SetBearerToken("sometoken");
415+
416+
var result = await client.GetAsync("http://test");
417+
result.StatusCode.Should().Be(HttpStatusCode.OK);
418+
419+
handler.LastRequest.Should().Contain(new KeyValuePair<string, string>("additionalParameter", "42"));
420+
}
421+
397422
private void AssertCacheItemExists(TestServer testServer, string cacheKeyPrefix, string token)
398423
{
399424
var cache = testServer.Services.GetService<IDistributedCache>();

0 commit comments

Comments
 (0)