Skip to content

Commit a36e21e

Browse files
Use file-scoped namespaces
Switch to using file-scoped namespaces. Remove using statements covered by global using statements.
1 parent b33d929 commit a36e21e

File tree

4 files changed

+158
-174
lines changed

4 files changed

+158
-174
lines changed

Diff for: generators/app/templates/AuthenticationDefaults.cs

+37-41
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,45 @@
44
* for more information concerning the license and the contributors participating to this project.
55
*/
66

7-
using Microsoft.AspNetCore.Authentication;
8-
using Microsoft.AspNetCore.Authentication.OAuth;
7+
namespace AspNet.Security.OAuth.<%= name %>;
98

10-
namespace AspNet.Security.OAuth.<%= name %>
9+
/// <summary>
10+
/// Default values used by the <%= name %> authentication provider.
11+
/// </summary>
12+
public static class <%= name %>AuthenticationDefaults
1113
{
1214
/// <summary>
13-
/// Default values used by the <%= name %> authentication provider.
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
1416
/// </summary>
15-
public static class <%= name %>AuthenticationDefaults
16-
{
17-
/// <summary>
18-
/// Default value for <see cref="AuthenticationScheme.Name"/>.
19-
/// </summary>
20-
public const string AuthenticationScheme = "<%= name %>";
21-
22-
/// <summary>
23-
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
24-
/// </summary>
25-
public static readonly string DisplayName = "<%= name %>";
26-
27-
/// <summary>
28-
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
29-
/// </summary>
30-
public static readonly string Issuer = "<%= name %>";
31-
32-
/// <summary>
33-
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
34-
/// </summary>
35-
public static readonly string CallbackPath = "/signin-<%= name.toLowerCase() %>";
36-
37-
/// <summary>
38-
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
39-
/// </summary>
40-
public static readonly string AuthorizationEndpoint = "<%= authorizationendpoint %>";
41-
42-
/// <summary>
43-
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
44-
/// </summary>
45-
public static readonly string TokenEndpoint = "<%= tokenendpoint %>";
46-
47-
/// <summary>
48-
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
49-
/// </summary>
50-
public static readonly string UserInformationEndpoint = "<%= userinformationendpoint %>";
51-
}
17+
public const string AuthenticationScheme = "<%= name %>";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "<%= name %>";
23+
24+
/// <summary>
25+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
26+
/// </summary>
27+
public static readonly string Issuer = "<%= name %>";
28+
29+
/// <summary>
30+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
31+
/// </summary>
32+
public static readonly string CallbackPath = "/signin-<%= name.toLowerCase() %>";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public static readonly string AuthorizationEndpoint = "<%= authorizationendpoint %>";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public static readonly string TokenEndpoint = "<%= tokenendpoint %>";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public static readonly string UserInformationEndpoint = "<%= userinformationendpoint %>";
5248
}

Diff for: generators/app/templates/AuthenticationExtensions.cs

+57-61
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,71 @@
44
* for more information concerning the license and the contributors participating to this project.
55
*/
66

7-
using System;
87
using AspNet.Security.OAuth.<%= name %>;
9-
using JetBrains.Annotations;
10-
using Microsoft.AspNetCore.Authentication;
118

12-
namespace Microsoft.Extensions.DependencyInjection
9+
namespace Microsoft.Extensions.DependencyInjection;
10+
11+
/// <summary>
12+
/// Extension methods to add <%= name %> authentication capabilities to an HTTP application pipeline.
13+
/// </summary>
14+
public static class <%= name %>AuthenticationExtensions
1315
{
1416
/// <summary>
15-
/// Extension methods to add <%= name %> authentication capabilities to an HTTP application pipeline.
17+
/// Adds <see cref="<%= name %>AuthenticationHandler"/> to the specified
18+
/// <see cref="AuthenticationBuilder"/>, which enables <%= name %> authentication capabilities.
1619
/// </summary>
17-
public static class <%= name %>AuthenticationExtensions
20+
/// <param name="builder">The authentication builder.</param>
21+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
22+
public static AuthenticationBuilder Add<%= name %>([NotNull] this AuthenticationBuilder builder)
1823
{
19-
/// <summary>
20-
/// Adds <see cref="<%= name %>AuthenticationHandler"/> to the specified
21-
/// <see cref="AuthenticationBuilder"/>, which enables <%= name %> authentication capabilities.
22-
/// </summary>
23-
/// <param name="builder">The authentication builder.</param>
24-
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
25-
public static AuthenticationBuilder Add<%= name %>([NotNull] this AuthenticationBuilder builder)
26-
{
27-
return builder.Add<%= name %>(<%= name %>AuthenticationDefaults.AuthenticationScheme, options => { });
28-
}
24+
return builder.Add<%= name %>(<%= name %>AuthenticationDefaults.AuthenticationScheme, options => { });
25+
}
2926

30-
/// <summary>
31-
/// Adds <see cref="<%= name %>AuthenticationHandler"/> to the specified
32-
/// <see cref="AuthenticationBuilder"/>, which enables <%= name %> authentication capabilities.
33-
/// </summary>
34-
/// <param name="builder">The authentication builder.</param>
35-
/// <param name="configuration">The delegate used to configure the <%= name %> options.</param>
36-
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
37-
public static AuthenticationBuilder Add<%= name %>(
38-
[NotNull] this AuthenticationBuilder builder,
39-
[NotNull] Action<<%= name %>AuthenticationOptions> configuration)
40-
{
41-
return builder.Add<%= name %>(<%= name %>AuthenticationDefaults.AuthenticationScheme, configuration);
42-
}
27+
/// <summary>
28+
/// Adds <see cref="<%= name %>AuthenticationHandler"/> to the specified
29+
/// <see cref="AuthenticationBuilder"/>, which enables <%= name %> authentication capabilities.
30+
/// </summary>
31+
/// <param name="builder">The authentication builder.</param>
32+
/// <param name="configuration">The delegate used to configure the <%= name %> options.</param>
33+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
34+
public static AuthenticationBuilder Add<%= name %>(
35+
[NotNull] this AuthenticationBuilder builder,
36+
[NotNull] Action<<%= name %>AuthenticationOptions> configuration)
37+
{
38+
return builder.Add<%= name %>(<%= name %>AuthenticationDefaults.AuthenticationScheme, configuration);
39+
}
4340

44-
/// <summary>
45-
/// Adds <see cref="<%= name %>AuthenticationHandler"/> to the specified
46-
/// <see cref="AuthenticationBuilder"/>, which enables <%= name %> authentication capabilities.
47-
/// </summary>
48-
/// <param name="builder">The authentication builder.</param>
49-
/// <param name="scheme">The authentication scheme associated with this instance.</param>
50-
/// <param name="configuration">The delegate used to configure the <%= name %> options.</param>
51-
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
52-
public static AuthenticationBuilder Add<%= name %>(
53-
[NotNull] this AuthenticationBuilder builder,
54-
[NotNull] string scheme,
55-
[NotNull] Action<<%= name %>AuthenticationOptions> configuration)
56-
{
57-
return builder.Add<%= name %>(scheme, <%= name %>AuthenticationDefaults.DisplayName, configuration);
58-
}
41+
/// <summary>
42+
/// Adds <see cref="<%= name %>AuthenticationHandler"/> to the specified
43+
/// <see cref="AuthenticationBuilder"/>, which enables <%= name %> authentication capabilities.
44+
/// </summary>
45+
/// <param name="builder">The authentication builder.</param>
46+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
47+
/// <param name="configuration">The delegate used to configure the <%= name %> options.</param>
48+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
49+
public static AuthenticationBuilder Add<%= name %>(
50+
[NotNull] this AuthenticationBuilder builder,
51+
[NotNull] string scheme,
52+
[NotNull] Action<<%= name %>AuthenticationOptions> configuration)
53+
{
54+
return builder.Add<%= name %>(scheme, <%= name %>AuthenticationDefaults.DisplayName, configuration);
55+
}
5956

60-
/// <summary>
61-
/// Adds <see cref="<%= name %>AuthenticationHandler"/> to the specified
62-
/// <see cref="AuthenticationBuilder"/>, which enables <%= name %> authentication capabilities.
63-
/// </summary>
64-
/// <param name="builder">The authentication builder.</param>
65-
/// <param name="scheme">The authentication scheme associated with this instance.</param>
66-
/// <param name="caption">The optional display name associated with this instance.</param>
67-
/// <param name="configuration">The delegate used to configure the <%= name %> options.</param>
68-
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
69-
public static AuthenticationBuilder Add<%= name %>(
70-
[NotNull] this AuthenticationBuilder builder,
71-
[NotNull] string scheme,
72-
[CanBeNull] string caption,
73-
[NotNull] Action<<%= name %>AuthenticationOptions> configuration)
74-
{
75-
return builder.AddOAuth<<%= name %>AuthenticationOptions, <%= name %>AuthenticationHandler>(scheme, caption, configuration);
76-
}
57+
/// <summary>
58+
/// Adds <see cref="<%= name %>AuthenticationHandler"/> to the specified
59+
/// <see cref="AuthenticationBuilder"/>, which enables <%= name %> authentication capabilities.
60+
/// </summary>
61+
/// <param name="builder">The authentication builder.</param>
62+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
63+
/// <param name="caption">The optional display name associated with this instance.</param>
64+
/// <param name="configuration">The delegate used to configure the <%= name %> options.</param>
65+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
66+
public static AuthenticationBuilder Add<%= name %>(
67+
[NotNull] this AuthenticationBuilder builder,
68+
[NotNull] string scheme,
69+
[CanBeNull] string caption,
70+
[NotNull] Action<<%= name %>AuthenticationOptions> configuration)
71+
{
72+
return builder.AddOAuth<<%= name %>AuthenticationOptions, <%= name %>AuthenticationHandler>(scheme, caption, configuration);
7773
}
7874
}

Diff for: generators/app/templates/AuthenticationHandler.cs

+47-52
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,69 @@
99
using System.Security.Claims;
1010
using System.Text.Encodings.Web;
1111
using System.Text.Json;
12-
using System.Threading.Tasks;
13-
using JetBrains.Annotations;
14-
using Microsoft.AspNetCore.Authentication;
15-
using Microsoft.AspNetCore.Authentication.OAuth;
1612
using Microsoft.AspNetCore.WebUtilities;
1713
using Microsoft.Extensions.Logging;
1814
using Microsoft.Extensions.Options;
1915

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>
2122
{
2223
/// <summary>
23-
/// Defines a handler for authentication using <%= name %>.
24+
/// Initializes a new instance of the <see cref="<%= name %>AuthenticationHandler"/> class.
2425
/// </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)
2636
{
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+
}
4238

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;
5046

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);
5349

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"));
5652

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);
5955

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());
6864

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+
}
7167

72-
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
68+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
7369

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();
7773

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);
8176
}
8277
}

Diff for: generators/app/templates/AuthenticationOptions.cs

+17-20
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,30 @@
55
*/
66

77
using System.Security.Claims;
8-
using Microsoft.AspNetCore.Authentication;
9-
using Microsoft.AspNetCore.Authentication.OAuth;
108

11-
namespace AspNet.Security.OAuth.<%= name %>
9+
namespace AspNet.Security.OAuth.<%= name %>;
10+
11+
/// <summary>
12+
/// Defines a set of options used by <see cref="<%= name %>AuthenticationHandler"/>.
13+
/// </summary>
14+
public class <%= name %>AuthenticationOptions : OAuthOptions
1215
{
1316
/// <summary>
14-
/// Defines a set of options used by <see cref="<%= name %>AuthenticationHandler"/>.
17+
/// Initializes a new instance of the <see cref="<%= name %>AuthenticationOptions"/> class.
1518
/// </summary>
16-
public class <%= name %>AuthenticationOptions : OAuthOptions
19+
public <%= name %>AuthenticationOptions()
1720
{
18-
/// <summary>
19-
/// Initializes a new instance of the <see cref="<%= name %>AuthenticationOptions"/> class.
20-
/// </summary>
21-
public <%= name %>AuthenticationOptions()
22-
{
23-
ClaimsIssuer = <%= name %>AuthenticationDefaults.Issuer;
24-
CallbackPath = <%= name %>AuthenticationDefaults.CallbackPath;
21+
ClaimsIssuer = <%= name %>AuthenticationDefaults.Issuer;
22+
CallbackPath = <%= name %>AuthenticationDefaults.CallbackPath;
2523

26-
AuthorizationEndpoint = <%= name %>AuthenticationDefaults.AuthorizationEndpoint;
27-
TokenEndpoint = <%= name %>AuthenticationDefaults.TokenEndpoint;
28-
UserInformationEndpoint = <%= name %>AuthenticationDefaults.UserInformationEndpoint;
24+
AuthorizationEndpoint = <%= name %>AuthenticationDefaults.AuthorizationEndpoint;
25+
TokenEndpoint = <%= name %>AuthenticationDefaults.TokenEndpoint;
26+
UserInformationEndpoint = <%= name %>AuthenticationDefaults.UserInformationEndpoint;
2927

30-
// TODO Add any required scopes
31-
// Scope.Add("?");
28+
// TODO Add any required scopes
29+
// Scope.Add("?");
3230

33-
// TODO Map any claims
34-
// ClaimActions.MapJsonKey(ClaimTypes.Email, "?");
35-
}
31+
// TODO Map any claims
32+
// ClaimActions.MapJsonKey(ClaimTypes.Email, "?");
3633
}
3734
}

0 commit comments

Comments
 (0)