|
9 | 9 | using System.Security.Claims; |
10 | 10 | using System.Text.Encodings.Web; |
11 | 11 | using System.Text.Json; |
12 | | -using System.Threading.Tasks; |
13 | | -using JetBrains.Annotations; |
14 | | -using Microsoft.AspNetCore.Authentication; |
15 | | -using Microsoft.AspNetCore.Authentication.OAuth; |
16 | 12 | using Microsoft.AspNetCore.WebUtilities; |
17 | 13 | using Microsoft.Extensions.Logging; |
18 | 14 | using Microsoft.Extensions.Options; |
19 | 15 |
|
20 | | -namespace AspNet.Security.OAuth.<%= name %> |
| 16 | +namespace AspNet.Security.OAuth.<%= name %>; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Defines a handler for authentication using <%= name %>. |
| 20 | +/// </summary> |
| 21 | +public class <%= name %>AuthenticationHandler : OAuthHandler<<%= name %>AuthenticationOptions> |
21 | 22 | { |
22 | 23 | /// <summary> |
23 | | - /// Defines a handler for authentication using <%= name %>. |
| 24 | + /// Initializes a new instance of the <see cref="<%= name %>AuthenticationHandler"/> class. |
24 | 25 | /// </summary> |
25 | | - public class <%= name %>AuthenticationHandler : OAuthHandler<<%= name %>AuthenticationOptions> |
| 26 | + /// <param name="options">The authentication options.</param> |
| 27 | + /// <param name="logger">The logger to use.</param> |
| 28 | + /// <param name="encoder">The URL encoder to use.</param> |
| 29 | + /// <param name="clock">The system clock to use.</param> |
| 30 | + public <%= name %>AuthenticationHandler( |
| 31 | + [NotNull] IOptionsMonitor<<%= name %>AuthenticationOptions> options, |
| 32 | + [NotNull] ILoggerFactory logger, |
| 33 | + [NotNull] UrlEncoder encoder, |
| 34 | + [NotNull] ISystemClock clock) |
| 35 | + : base(options, logger, encoder, clock) |
26 | 36 | { |
27 | | - /// <summary> |
28 | | - /// Initializes a new instance of the <see cref="<%= name %>AuthenticationHandler"/> class. |
29 | | - /// </summary> |
30 | | - /// <param name="options">The authentication options.</param> |
31 | | - /// <param name="logger">The logger to use.</param> |
32 | | - /// <param name="encoder">The URL encoder to use.</param> |
33 | | - /// <param name="clock">The system clock to use.</param> |
34 | | - public <%= name %>AuthenticationHandler( |
35 | | - [NotNull] IOptionsMonitor<<%= name %>AuthenticationOptions> options, |
36 | | - [NotNull] ILoggerFactory logger, |
37 | | - [NotNull] UrlEncoder encoder, |
38 | | - [NotNull] ISystemClock clock) |
39 | | - : base(options, logger, encoder, clock) |
40 | | - { |
41 | | - } |
| 37 | + } |
42 | 38 |
|
43 | | - /// <inheritdoc /> |
44 | | - protected override async Task<AuthenticationTicket> CreateTicketAsync( |
45 | | - [NotNull] ClaimsIdentity identity, |
46 | | - [NotNull] AuthenticationProperties properties, |
47 | | - [NotNull] OAuthTokenResponse tokens) |
48 | | - { |
49 | | - var endpoint = Options.UserInformationEndpoint; |
| 39 | + /// <inheritdoc /> |
| 40 | + protected override async Task<AuthenticationTicket> CreateTicketAsync( |
| 41 | + [NotNull] ClaimsIdentity identity, |
| 42 | + [NotNull] AuthenticationProperties properties, |
| 43 | + [NotNull] OAuthTokenResponse tokens) |
| 44 | + { |
| 45 | + var endpoint = Options.UserInformationEndpoint; |
50 | 46 |
|
51 | | - // TODO Append any additional query string parameters required |
52 | | - //endpoint = QueryHelpers.AddQueryString(endpoint, "token", tokens.AccessToken); |
| 47 | + // TODO Append any additional query string parameters required |
| 48 | + //endpoint = QueryHelpers.AddQueryString(endpoint, "token", tokens.AccessToken); |
53 | 49 |
|
54 | | - using var request = new HttpRequestMessage(HttpMethod.Get, endpoint); |
55 | | - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 50 | + using var request = new HttpRequestMessage(HttpMethod.Get, endpoint); |
| 51 | + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
56 | 52 |
|
57 | | - // TODO Add any HTTP request headers required |
58 | | - //request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); |
| 53 | + // TODO Add any HTTP request headers required |
| 54 | + //request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); |
59 | 55 |
|
60 | | - using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted); |
61 | | - if (!response.IsSuccessStatusCode) |
62 | | - { |
63 | | - Logger.LogError("An error occurred while retrieving the user profile: the remote server " + |
64 | | - "returned a {Status} response with the following payload: {Headers} {Body}.", |
65 | | - /* Status: */ response.StatusCode, |
66 | | - /* Headers: */ response.Headers.ToString(), |
67 | | - /* Body: */ await response.Content.ReadAsStringAsync()); |
| 56 | + using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted); |
| 57 | + if (!response.IsSuccessStatusCode) |
| 58 | + { |
| 59 | + Logger.LogError("An error occurred while retrieving the user profile: the remote server " + |
| 60 | + "returned a {Status} response with the following payload: {Headers} {Body}.", |
| 61 | + /* Status: */ response.StatusCode, |
| 62 | + /* Headers: */ response.Headers.ToString(), |
| 63 | + /* Body: */ await response.Content.ReadAsStringAsync()); |
68 | 64 |
|
69 | | - throw new HttpRequestException("An error occurred while retrieving the user profile from <%= name %>."); |
70 | | - } |
| 65 | + throw new HttpRequestException("An error occurred while retrieving the user profile from <%= name %>."); |
| 66 | + } |
71 | 67 |
|
72 | | - using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); |
| 68 | + using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); |
73 | 69 |
|
74 | | - var principal = new ClaimsPrincipal(identity); |
75 | | - var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement); |
76 | | - context.RunClaimActions(); |
| 70 | + var principal = new ClaimsPrincipal(identity); |
| 71 | + var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement); |
| 72 | + context.RunClaimActions(); |
77 | 73 |
|
78 | | - await Events.CreatingTicket(context); |
79 | | - return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name); |
80 | | - } |
| 74 | + await Events.CreatingTicket(context); |
| 75 | + return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name); |
81 | 76 | } |
82 | 77 | } |
0 commit comments