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

Commit 0115d43

Browse files
committed
Introduce complete challenge support and add a new event to control how the challenge is returned to the caller
1 parent 12bc12d commit 0115d43

35 files changed

Lines changed: 2258 additions & 220 deletions
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
using Microsoft.AspNetCore.Http.Authentication;
11+
12+
namespace AspNet.Security.OAuth.Introspection
13+
{
14+
/// <summary>
15+
/// Allows customization of the challenge process.
16+
/// </summary>
17+
public class ApplyChallengeContext : BaseControlContext
18+
{
19+
public ApplyChallengeContext(
20+
[NotNull] HttpContext context,
21+
[NotNull] OAuthIntrospectionOptions options,
22+
[NotNull] AuthenticationProperties properties)
23+
: base(context)
24+
{
25+
Options = options;
26+
Properties = properties;
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 authentication properties associated with the challenge.
36+
/// </summary>
37+
public AuthenticationProperties Properties { get; }
38+
39+
/// <summary>
40+
/// Gets or sets the "error" value returned to the caller as part
41+
/// of the WWW-Authenticate header. This property may be null when
42+
/// <see cref="OAuthIntrospectionOptions.IncludeErrorDetails"/> is set to <c>false</c>.
43+
/// </summary>
44+
public string Error { get; set; }
45+
46+
/// <summary>
47+
/// Gets or sets the "error_description" value returned to the caller as part
48+
/// of the WWW-Authenticate header. This property may be null when
49+
/// <see cref="OAuthIntrospectionOptions.IncludeErrorDetails"/> is set to <c>false</c>.
50+
/// </summary>
51+
public string ErrorDescription { get; set; }
52+
53+
/// <summary>
54+
/// Gets or sets the "error_uri" value returned to the caller as part of the
55+
/// WWW-Authenticate header. This property is always null unless explicitly set.
56+
/// </summary>
57+
public string ErrorUri { get; set; }
58+
59+
/// <summary>
60+
/// Gets or sets the "realm" value returned to
61+
/// the caller as part of the WWW-Authenticate header.
62+
/// </summary>
63+
public string Realm { get; set; }
64+
65+
/// <summary>
66+
/// Gets or sets the "scope" value returned to
67+
/// the caller as part of the WWW-Authenticate header.
68+
/// </summary>
69+
public string Scope { get; set; }
70+
}
71+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,47 @@ public static class Claims
2222
public const string Username = "username";
2323
}
2424

25+
public static class Errors
26+
{
27+
public const string InsufficientScope = "insufficient_scope";
28+
public const string InvalidRequest = "invalid_request";
29+
public const string InvalidToken = "invalid_token";
30+
}
31+
2532
public static class Metadata
2633
{
2734
public const string IntrospectionEndpoint = "introspection_endpoint";
2835
}
2936

3037
public static class Parameters
3138
{
39+
public const string Error = "error";
40+
public const string ErrorDescription = "error_description";
41+
public const string ErrorUri = "error_uri";
42+
public const string Realm = "realm";
43+
public const string Scope = "scope";
3244
public const string Token = "token";
3345
public const string TokenTypeHint = "token_type_hint";
3446
}
3547

3648
public static class Properties
3749
{
3850
public const string Audiences = ".audiences";
51+
public const string Error = ".error";
52+
public const string ErrorDescription = ".error_description";
53+
public const string ErrorUri = ".error_uri";
54+
public const string Realm = ".realm";
55+
public const string Scope = ".scope";
3956
public const string Scopes = ".scopes";
4057
public const string TicketId = ".ticket_id";
4158
public const string Token = "access_token";
4259
}
4360

61+
public static class Schemes
62+
{
63+
public const string Bearer = "Bearer";
64+
}
65+
4466
public static class TokenTypes
4567
{
4668
public const string AccessToken = "access_token";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace AspNet.Security.OAuth.Introspection
2+
{
3+
/// <summary>
4+
/// Represents an OAuth2 introspection error.
5+
/// </summary>
6+
public class OAuthIntrospectionError
7+
{
8+
/// <summary>
9+
/// Gets or sets the error code.
10+
/// </summary>
11+
public string Error { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets the error_description.
15+
/// </summary>
16+
public string ErrorDescription { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets the error_uri.
20+
/// </summary>
21+
public string ErrorUri { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets the realm.
25+
/// </summary>
26+
public string Realm { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets the scope.
30+
/// </summary>
31+
public string Scope { get; set; }
32+
}
33+
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,50 @@ namespace AspNet.Security.OAuth.Introspection
1515
public class OAuthIntrospectionEvents
1616
{
1717
/// <summary>
18-
/// Invoked when a ticket is to be created from an introspection response.
18+
/// Invoked when a challenge response is returned to the caller.
1919
/// </summary>
20-
public Func<CreateTicketContext, Task> OnCreateTicket { get; set; } = context => Task.FromResult(0);
20+
public Func<ApplyChallengeContext, Task> OnApplyChallenge { get; set; } = context => Task.FromResult(0);
2121

2222
/// <summary>
23-
/// Invoked when a token is to be parsed from a newly-received request.
23+
/// Invoked when a ticket is to be created from an introspection response.
2424
/// </summary>
25-
public Func<RetrieveTokenContext, Task> OnRetrieveToken { get; set; } = context => Task.FromResult(0);
25+
public Func<CreateTicketContext, Task> OnCreateTicket { get; set; } = context => Task.FromResult(0);
2626

2727
/// <summary>
2828
/// Invoked when a token is to be sent to the authorization server for introspection.
2929
/// </summary>
3030
public Func<RequestTokenIntrospectionContext, Task> OnRequestTokenIntrospection { get; set; } = context => Task.FromResult(0);
3131

32+
/// <summary>
33+
/// Invoked when a token is to be parsed from a newly-received request.
34+
/// </summary>
35+
public Func<RetrieveTokenContext, Task> OnRetrieveToken { get; set; } = context => Task.FromResult(0);
36+
3237
/// <summary>
3338
/// Invoked when a token is to be validated, before final processing.
3439
/// </summary>
3540
public Func<ValidateTokenContext, Task> OnValidateToken { get; set; } = context => Task.FromResult(0);
3641

3742
/// <summary>
38-
/// Invoked when a ticket is to be created from an introspection response.
43+
/// Invoked when a challenge response is returned to the caller.
3944
/// </summary>
40-
public virtual Task CreateTicket(CreateTicketContext context) => OnCreateTicket(context);
45+
public virtual Task ApplyChallenge(ApplyChallengeContext context) => OnApplyChallenge(context);
4146

4247
/// <summary>
43-
/// Invoked when a token is to be parsed from a newly-received request.
48+
/// Invoked when a ticket is to be created from an introspection response.
4449
/// </summary>
45-
public virtual Task RetrieveToken(RetrieveTokenContext context) => OnRetrieveToken(context);
50+
public virtual Task CreateTicket(CreateTicketContext context) => OnCreateTicket(context);
4651

4752
/// <summary>
4853
/// Invoked when a token is to be sent to the authorization server for introspection.
4954
/// </summary>
5055
public virtual Task RequestTokenIntrospection(RequestTokenIntrospectionContext context) => OnRequestTokenIntrospection(context);
5156

57+
/// <summary>
58+
/// Invoked when a token is to be parsed from a newly-received request.
59+
/// </summary>
60+
public virtual Task RetrieveToken(RetrieveTokenContext context) => OnRetrieveToken(context);
61+
5262
/// <summary>
5363
/// Invoked when a token is to be validated, before final processing.
5464
/// </summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Builder
1818
public static class OAuthIntrospectionExtensions
1919
{
2020
/// <summary>
21-
/// Adds a new instance of the OAuth2 introspection middleware in the ASP.NET 5 pipeline.
21+
/// Adds a new instance of the OAuth2 introspection middleware in the ASP.NET Core pipeline.
2222
/// </summary>
2323
/// <param name="app">The application builder.</param>
2424
/// <param name="configuration">The delegate used to configure the introspection options.</param>
@@ -44,7 +44,7 @@ public static IApplicationBuilder UseOAuthIntrospection(
4444
}
4545

4646
/// <summary>
47-
/// Adds a new instance of the OAuth2 introspection middleware in the ASP.NET 5 pipeline.
47+
/// Adds a new instance of the OAuth2 introspection middleware in the ASP.NET Core pipeline.
4848
/// </summary>
4949
/// <param name="app">The application builder.</param>
5050
/// <param name="options">The options used to configure the introspection middleware.</param>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace AspNet.Security.OAuth.Introspection
2+
{
3+
/// <summary>
4+
/// Exposes the OAuth2 introspection details
5+
/// associated with the current request.
6+
/// </summary>
7+
public class OAuthIntrospectionFeature
8+
{
9+
/// <summary>
10+
/// Gets or sets the error details returned
11+
/// as part of the challenge response.
12+
/// </summary>
13+
public OAuthIntrospectionError Error { get; set; }
14+
}
15+
}

0 commit comments

Comments
 (0)