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

Commit a8bbc94

Browse files
committed
Re-align the events model of the ASP.NET Core and OWIN/Katana versions
1 parent cd3bf3b commit a8bbc94

File tree

15 files changed

+114
-385
lines changed

15 files changed

+114
-385
lines changed

build/common.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<Import Project="version.props" />
55

66
<PropertyGroup>
7+
<LangVersion>latest</LangVersion>
78
<NoWarn>$(NoWarn);CS1591</NoWarn>
89
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
910
<DebugSymbols>true</DebugSymbols>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop
371371

372372
private async Task<JObject> GetIntrospectionPayloadAsync(string token)
373373
{
374-
var configuration = await Options.ConfigurationManager.GetConfigurationAsync(Context.RequestAborted);
374+
var configuration = await Options.ConfigurationManager.GetConfigurationAsync(default);
375375
if (configuration == null)
376376
{
377377
throw new InvalidOperationException("The OAuth2 introspection middleware was unable to retrieve " +
@@ -431,7 +431,7 @@ private async Task<JObject> GetIntrospectionPayloadAsync(string token)
431431
var response = notification.Response;
432432
if (response == null)
433433
{
434-
response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
434+
response = await Options.HttpClient.SendAsync(request);
435435
}
436436

437437
if (!response.IsSuccessStatusCode)
@@ -468,7 +468,7 @@ exception is InvalidCastException ||
468468
exception is JsonReaderException ||
469469
exception is JsonSerializationException)
470470
{
471-
Logger.LogError("An error occurred while deserializing the introspection response: {Exception}.", exception);
471+
Logger.LogError(exception, "An error occurred while deserializing the introspection response.");
472472

473473
return null;
474474
}

src/Owin.Security.OAuth.Introspection/Events/ApplyChallengeContext.cs

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

1212
namespace Owin.Security.OAuth.Introspection
1313
{
1414
/// <summary>
1515
/// Allows customization of the challenge process.
1616
/// </summary>
17-
public class ApplyChallengeContext : BaseNotification<OAuthIntrospectionOptions>
17+
public class ApplyChallengeContext : BaseContext<OAuthIntrospectionOptions>
1818
{
1919
public ApplyChallengeContext(
2020
[NotNull] IOwinContext context,
@@ -61,5 +61,15 @@ public ApplyChallengeContext(
6161
/// the caller as part of the WWW-Authenticate header.
6262
/// </summary>
6363
public string Scope { get; set; }
64+
65+
/// <summary>
66+
/// Gets a boolean indicating if the operation was handled from user code.
67+
/// </summary>
68+
public bool Handled { get; private set; }
69+
70+
/// <summary>
71+
/// Marks the operation as handled to prevent the default logic from being applied.
72+
/// </summary>
73+
public void HandleResponse() => Handled = true;
6474
}
6575
}

src/Owin.Security.OAuth.Introspection/Events/CreateTicketContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
using JetBrains.Annotations;
88
using Microsoft.Owin;
99
using Microsoft.Owin.Security;
10-
using Microsoft.Owin.Security.Notifications;
10+
using Microsoft.Owin.Security.Provider;
1111
using Newtonsoft.Json.Linq;
1212

1313
namespace Owin.Security.OAuth.Introspection
1414
{
1515
/// <summary>
1616
/// Allows interception of the AuthenticationTicket creation process.
1717
/// </summary>
18-
public class CreateTicketContext : BaseNotification<OAuthIntrospectionOptions>
18+
public class CreateTicketContext : BaseContext<OAuthIntrospectionOptions>
1919
{
2020
public CreateTicketContext(
2121
[NotNull] IOwinContext context,

src/Owin.Security.OAuth.Introspection/Events/RetrieveTokenContext.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
using JetBrains.Annotations;
88
using Microsoft.Owin;
99
using Microsoft.Owin.Security;
10-
using Microsoft.Owin.Security.Notifications;
10+
using Microsoft.Owin.Security.Provider;
1111

1212
namespace Owin.Security.OAuth.Introspection
1313
{
1414
/// <summary>
1515
/// Allows custom parsing of access tokens from requests.
1616
/// </summary>
17-
public class RetrieveTokenContext : BaseNotification<OAuthIntrospectionOptions>
17+
public class RetrieveTokenContext : BaseContext<OAuthIntrospectionOptions>
1818
{
1919
public RetrieveTokenContext(
2020
[NotNull] IOwinContext context,
@@ -32,5 +32,25 @@ public RetrieveTokenContext(
3232
/// Gets or sets the <see cref="AuthenticationTicket"/> created by the application.
3333
/// </summary>
3434
public AuthenticationTicket Ticket { get; set; }
35+
36+
/// <summary>
37+
/// Gets a boolean indicating if the operation was handled from user code.
38+
/// </summary>
39+
public bool Handled { get; private set; }
40+
41+
/// <summary>
42+
/// Marks the operation as handled to prevent the default logic from being applied.
43+
/// </summary>
44+
public void HandleValidation() => Handled = true;
45+
46+
/// <summary>
47+
/// Marks the operation as handled to prevent the default logic from being applied.
48+
/// </summary>
49+
/// <param name="ticket">The authentication ticket to use.</param>
50+
public void HandleValidation(AuthenticationTicket ticket)
51+
{
52+
Ticket = ticket;
53+
Handled = true;
54+
}
3555
}
3656
}

src/Owin.Security.OAuth.Introspection/Events/SendIntrospectionRequestContext.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
using System.Net.Http;
88
using JetBrains.Annotations;
99
using Microsoft.Owin;
10-
using Microsoft.Owin.Security.Notifications;
10+
using Microsoft.Owin.Security.Provider;
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 SendIntrospectionRequestContext : BaseNotification<OAuthIntrospectionOptions>
17+
public class SendIntrospectionRequestContext : BaseContext<OAuthIntrospectionOptions>
1818
{
1919
public SendIntrospectionRequestContext(
2020
[NotNull] IOwinContext context,
@@ -46,5 +46,15 @@ public SendIntrospectionRequestContext(
4646
/// The access token parsed from the client request.
4747
/// </summary>
4848
public string Token { get; }
49+
50+
/// <summary>
51+
/// Gets a boolean indicating if the operation was handled from user code.
52+
/// </summary>
53+
public bool Handled { get; private set; }
54+
55+
/// <summary>
56+
/// Marks the operation as handled to prevent the default logic from being applied.
57+
/// </summary>
58+
public void HandleResponse() => Handled = true;
4959
}
5060
}

src/Owin.Security.OAuth.Introspection/Events/ValidateTokenContext.cs

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

1212
namespace Owin.Security.OAuth.Introspection
1313
{
1414
/// <summary>
1515
/// Allows customization of the token validation logic.
1616
/// </summary>
17-
public class ValidateTokenContext : BaseNotification<OAuthIntrospectionOptions>
17+
public class ValidateTokenContext : BaseContext<OAuthIntrospectionOptions>
1818
{
1919
public ValidateTokenContext(
2020
[NotNull] IOwinContext context,

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

Lines changed: 9 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,13 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
3232
var context = new RetrieveTokenContext(Context, Options);
3333
await Options.Events.RetrieveToken(context);
3434

35-
if (context.HandledResponse)
35+
if (context.Handled)
3636
{
37-
// If no ticket has been provided, return a failed result to
38-
// indicate that authentication was rejected by application code.
39-
if (context.Ticket == null)
40-
{
41-
Logger.LogInformation("Authentication was stopped by application code.");
42-
43-
return null;
44-
}
37+
Logger.LogInformation("The default authentication handling was skipped from user code.");
4538

4639
return context.Ticket;
4740
}
4841

49-
else if (context.Skipped)
50-
{
51-
Logger.LogInformation("Authentication was skipped by application code.");
52-
53-
return null;
54-
}
55-
5642
var token = context.Token;
5743

5844
if (string.IsNullOrEmpty(token))
@@ -166,27 +152,6 @@ protected override async Task<AuthenticationTicket> AuthenticateCoreAsync()
166152
var notification = new ValidateTokenContext(Context, Options, ticket);
167153
await Options.Events.ValidateToken(notification);
168154

169-
if (notification.HandledResponse)
170-
{
171-
// If no ticket has been provided, return a failed result to
172-
// indicate that authentication was rejected by application code.
173-
if (notification.Ticket == null)
174-
{
175-
Logger.LogInformation("Authentication was stopped by application code.");
176-
177-
return null;
178-
}
179-
180-
return notification.Ticket;
181-
}
182-
183-
else if (notification.Skipped)
184-
{
185-
Logger.LogInformation("Authentication was skipped by application code.");
186-
187-
return null;
188-
}
189-
190155
// Allow the application code to replace the ticket
191156
// reference from the ValidateToken event.
192157
return notification.Ticket;
@@ -263,7 +228,7 @@ protected override async Task ApplyResponseChallengeAsync()
263228

264229
await Options.Events.ApplyChallenge(notification);
265230

266-
if (notification.HandledResponse || notification.Skipped)
231+
if (notification.Handled)
267232
{
268233
return;
269234
}
@@ -368,7 +333,7 @@ protected override async Task ApplyResponseChallengeAsync()
368333

369334
private async Task<JObject> GetIntrospectionPayloadAsync(string token)
370335
{
371-
var configuration = await Options.ConfigurationManager.GetConfigurationAsync(Request.CallCancelled);
336+
var configuration = await Options.ConfigurationManager.GetConfigurationAsync(default);
372337
if (configuration == null)
373338
{
374339
throw new InvalidOperationException("The OAuth2 introspection middleware was unable to retrieve " +
@@ -418,32 +383,17 @@ private async Task<JObject> GetIntrospectionPayloadAsync(string token)
418383
var notification = new SendIntrospectionRequestContext(Context, Options, request, token);
419384
await Options.Events.SendIntrospectionRequest(notification);
420385

421-
HttpResponseMessage response = null;
422-
423-
if (notification.HandledResponse)
386+
if (notification.Handled)
424387
{
425-
// If no response has been provided, return a failed result to
426-
// indicate that authentication was rejected by application code.
427-
if (notification.Response == null)
428-
{
429-
Logger.LogInformation("Authentication was stopped by application code.");
430-
431-
return null;
432-
}
433-
434-
response = notification.Response;
435-
}
436-
437-
else if (notification.Skipped)
438-
{
439-
Logger.LogInformation("Authentication was skipped by application code.");
388+
Logger.LogInformation("The default challenge handling was skipped from user code.");
440389

441390
return null;
442391
}
443392

393+
var response = notification.Response;
444394
if (response == null)
445395
{
446-
response = await Options.HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Request.CallCancelled);
396+
response = await Options.HttpClient.SendAsync(request);
447397
}
448398

449399
if (!response.IsSuccessStatusCode)
@@ -480,8 +430,7 @@ exception is InvalidCastException ||
480430
exception is JsonReaderException ||
481431
exception is JsonSerializationException)
482432
{
483-
Logger.LogError("An error occurred while deserializing the " +
484-
"introspection response: {Exception}.", exception);
433+
Logger.LogError(exception, "An error occurred while deserializing the introspection response.");
485434

486435
return null;
487436
}
@@ -780,23 +729,6 @@ private async Task<AuthenticationTicket> CreateTicketAsync(string token, JObject
780729
var notification = new CreateTicketContext(Context, Options, ticket, payload);
781730
await Options.Events.CreateTicket(notification);
782731

783-
if (notification.HandledResponse)
784-
{
785-
// If no ticket has been provided, return a failed result to
786-
// indicate that authentication was rejected by application code.
787-
if (notification.Ticket == null)
788-
{
789-
return null;
790-
}
791-
792-
return notification.Ticket;
793-
}
794-
795-
else if (notification.Skipped)
796-
{
797-
return null;
798-
}
799-
800732
return notification.Ticket;
801733
}
802734

src/Owin.Security.OAuth.Validation/Events/ApplyChallengeContext.cs

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

1212
namespace Owin.Security.OAuth.Validation
1313
{
1414
/// <summary>
1515
/// Allows customization of the challenge process.
1616
/// </summary>
17-
public class ApplyChallengeContext : BaseNotification<OAuthValidationOptions>
17+
public class ApplyChallengeContext : BaseContext<OAuthValidationOptions>
1818
{
1919
public ApplyChallengeContext(
2020
[NotNull] IOwinContext context,
@@ -61,5 +61,15 @@ public ApplyChallengeContext(
6161
/// the caller as part of the WWW-Authenticate header.
6262
/// </summary>
6363
public string Scope { get; set; }
64+
65+
/// <summary>
66+
/// Gets a boolean indicating if the operation was handled from user code.
67+
/// </summary>
68+
public bool Handled { get; private set; }
69+
70+
/// <summary>
71+
/// Marks the operation as handled to prevent the default logic from being applied.
72+
/// </summary>
73+
public void HandleResponse() => Handled = true;
6474
}
6575
}

src/Owin.Security.OAuth.Validation/Events/CreateTicketContext.cs

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

1212
namespace Owin.Security.OAuth.Validation
1313
{
1414
/// <summary>
1515
/// Allows interception of the AuthenticationTicket creation process.
1616
/// </summary>
17-
public class CreateTicketContext : BaseNotification<OAuthValidationOptions>
17+
public class CreateTicketContext : BaseContext<OAuthValidationOptions>
1818
{
1919
public CreateTicketContext(
2020
[NotNull] IOwinContext context,

0 commit comments

Comments
 (0)