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

Commit 28d44f7

Browse files
committed
Rename the RequestTokenIntrospection event to SendIntrospectionRequest
1 parent f808e4b commit 28d44f7

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
lines changed

src/AspNet.Security.OAuth.Introspection/Events/RequestTokenIntrospectionContext.cs renamed to src/AspNet.Security.OAuth.Introspection/Events/SendIntrospectionRequestContext.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ namespace AspNet.Security.OAuth.Introspection
1414
/// <summary>
1515
/// Allows for custom handling of the call to the Authorization Server's Introspection endpoint.
1616
/// </summary>
17-
public class RequestTokenIntrospectionContext : BaseContext
17+
public class SendIntrospectionRequestContext : BaseControlContext
1818
{
19-
public RequestTokenIntrospectionContext(
19+
public SendIntrospectionRequestContext(
2020
[NotNull] HttpContext context,
2121
[NotNull] OAuthIntrospectionOptions options,
22-
[NotNull] HttpRequestMessage message,
22+
[NotNull] HttpRequestMessage request,
2323
[NotNull] string token)
2424
: base(context)
2525
{
2626
Options = options;
27-
Message = message;
27+
Request = request;
2828
Token = token;
2929
}
3030

@@ -39,9 +39,14 @@ public RequestTokenIntrospectionContext(
3939
public HttpClient Client => Options.HttpClient;
4040

4141
/// <summary>
42-
/// Gets the HTTP message sent to the introspection endpoint.
42+
/// Gets the HTTP request sent to the introspection endpoint.
4343
/// </summary>
44-
public HttpRequestMessage Message { get; }
44+
public new HttpRequestMessage Request { get; }
45+
46+
/// <summary>
47+
/// Gets or sets the HTTP response returned by the introspection endpoint.
48+
/// </summary>
49+
public new HttpResponseMessage Response { get; set; }
4550

4651
/// <summary>
4752
/// The access token parsed from the client request.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class OAuthIntrospectionEvents
2727
/// <summary>
2828
/// Invoked when a token is to be sent to the authorization server for introspection.
2929
/// </summary>
30-
public Func<RequestTokenIntrospectionContext, Task> OnRequestTokenIntrospection { get; set; } = context => Task.FromResult(0);
30+
public Func<SendIntrospectionRequestContext, Task> OnSendIntrospectionRequest { get; set; } = context => Task.FromResult(0);
3131

3232
/// <summary>
3333
/// Invoked when a token is to be parsed from a newly-received request.
@@ -52,7 +52,7 @@ public class OAuthIntrospectionEvents
5252
/// <summary>
5353
/// Invoked when a token is to be sent to the authorization server for introspection.
5454
/// </summary>
55-
public virtual Task RequestTokenIntrospection(RequestTokenIntrospectionContext context) => OnRequestTokenIntrospection(context);
55+
public virtual Task SendIntrospectionRequest(SendIntrospectionRequestContext context) => OnSendIntrospectionRequest(context);
5656

5757
/// <summary>
5858
/// Invoked when a token is to be parsed from a newly-received request.

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,37 @@ private async Task<JObject> GetIntrospectionPayloadAsync(string token)
409409

410410
request.Content = new FormUrlEncodedContent(parameters);
411411

412-
var notification = new RequestTokenIntrospectionContext(Context, Options, request, token);
413-
await Options.Events.RequestTokenIntrospection(notification);
412+
var notification = new SendIntrospectionRequestContext(Context, Options, request, token);
413+
await Options.Events.SendIntrospectionRequest(notification);
414+
415+
HttpResponseMessage response = null;
416+
417+
if (notification.HandledResponse)
418+
{
419+
// If no response has been provided, return a failed result to
420+
// indicate that authentication was rejected by application code.
421+
if (notification.Response == null)
422+
{
423+
Logger.LogInformation("Authentication was stopped by application code.");
424+
425+
return null;
426+
}
427+
428+
response = notification.Response;
429+
}
430+
431+
else if (notification.Skipped)
432+
{
433+
Logger.LogInformation("Authentication was skipped by application code.");
434+
435+
return null;
436+
}
437+
438+
if (response == null)
439+
{
440+
response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
441+
}
414442

415-
var response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
416443
if (!response.IsSuccessStatusCode)
417444
{
418445
Logger.LogError("An error occurred while validating an access token: the remote server " +

src/Owin.Security.OAuth.Introspection/Events/RequestTokenIntrospectionContext.cs renamed to src/Owin.Security.OAuth.Introspection/Events/SendIntrospectionRequestContext.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
using System.Net.Http;
88
using JetBrains.Annotations;
99
using Microsoft.Owin;
10-
using Microsoft.Owin.Security.Provider;
10+
using Microsoft.Owin.Security.Notifications;
1111

1212
namespace Owin.Security.OAuth.Introspection
1313
{
1414
/// <summary>
1515
/// Allows for custom handling of the call to the Authorization Server's Introspection endpoint.
1616
/// </summary>
17-
public class RequestTokenIntrospectionContext : BaseContext<OAuthIntrospectionOptions>
17+
public class SendIntrospectionRequestContext : BaseNotification<OAuthIntrospectionOptions>
1818
{
19-
public RequestTokenIntrospectionContext(
19+
public SendIntrospectionRequestContext(
2020
[NotNull] IOwinContext context,
2121
[NotNull] OAuthIntrospectionOptions options,
22-
[NotNull] HttpRequestMessage message,
22+
[NotNull] HttpRequestMessage request,
2323
[NotNull] string token)
2424
: base(context, options)
2525
{
26-
Message = message;
26+
Request = request;
2727
Token = token;
2828
}
2929

@@ -33,9 +33,14 @@ public RequestTokenIntrospectionContext(
3333
public HttpClient Client => Options.HttpClient;
3434

3535
/// <summary>
36-
/// Gets the HTTP message sent to the introspection endpoint.
36+
/// Gets the HTTP request sent to the introspection endpoint.
3737
/// </summary>
38-
public HttpRequestMessage Message { get; }
38+
public new HttpRequestMessage Request { get; }
39+
40+
/// <summary>
41+
/// Gets or sets the HTTP response returned by the introspection endpoint.
42+
/// </summary>
43+
public new HttpResponseMessage Response { get; set; }
3944

4045
/// <summary>
4146
/// The access token parsed from the client request.

src/Owin.Security.OAuth.Introspection/OAuthIntrospectionEvents.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class OAuthIntrospectionEvents
3232
/// <summary>
3333
/// Invoked when a token is to be sent to the authorization server for introspection.
3434
/// </summary>
35-
public Func<RequestTokenIntrospectionContext, Task> OnRequestTokenIntrospection { get; set; } = context => Task.FromResult(0);
35+
public Func<SendIntrospectionRequestContext, Task> OnSendIntrospectionRequest { get; set; } = context => Task.FromResult(0);
3636

3737
/// <summary>
3838
/// Invoked when a token is to be validated, before final processing.
@@ -57,7 +57,7 @@ public class OAuthIntrospectionEvents
5757
/// <summary>
5858
/// Invoked when a token is to be sent to the authorization server for introspection.
5959
/// </summary>
60-
public virtual Task RequestTokenIntrospection(RequestTokenIntrospectionContext context) => OnRequestTokenIntrospection(context);
60+
public virtual Task SendIntrospectionRequest(SendIntrospectionRequestContext context) => OnSendIntrospectionRequest(context);
6161

6262
/// <summary>
6363
/// Invoked when a token is to be validated, before final processing.

src/Owin.Security.OAuth.Introspection/OAuthIntrospectionHandler.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,37 @@ private async Task<JObject> GetIntrospectionPayloadAsync(string token)
399399

400400
request.Content = new FormUrlEncodedContent(parameters);
401401

402-
var notification = new RequestTokenIntrospectionContext(Context, Options, request, token);
403-
await Options.Events.RequestTokenIntrospection(notification);
402+
var notification = new SendIntrospectionRequestContext(Context, Options, request, token);
403+
await Options.Events.SendIntrospectionRequest(notification);
404+
405+
HttpResponseMessage response = null;
406+
407+
if (notification.HandledResponse)
408+
{
409+
// If no response has been provided, return a failed result to
410+
// indicate that authentication was rejected by application code.
411+
if (notification.Response == null)
412+
{
413+
Logger.LogInformation("Authentication was stopped by application code.");
414+
415+
return null;
416+
}
417+
418+
response = notification.Response;
419+
}
420+
421+
else if (notification.Skipped)
422+
{
423+
Logger.LogInformation("Authentication was skipped by application code.");
424+
425+
return null;
426+
}
427+
428+
if (response == null)
429+
{
430+
response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Request.CallCancelled);
431+
}
404432

405-
var response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Request.CallCancelled);
406433
if (!response.IsSuccessStatusCode)
407434
{
408435
Logger.LogError("An error occurred while validating an access token: the remote server " +

0 commit comments

Comments
 (0)