diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..0a57c19 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +[*.cs] + +# IDE0008: Use explicit type +csharp_style_var_elsewhere = true + +# IDE0008: Use explicit type +csharp_style_var_for_built_in_types = true + +# IDE0160: Convert to file scoped namespace +csharp_style_namespace_declarations = file_scoped:suggestion \ No newline at end of file diff --git a/AspNetCore.Examples.Auth.Api.Tests/Extensions/RequestBuilderExtensions.cs b/AspNetCore.Examples.Auth.Api.Tests/Extensions/RequestBuilderExtensions.cs index bf57803..d9e6762 100644 --- a/AspNetCore.Examples.Auth.Api.Tests/Extensions/RequestBuilderExtensions.cs +++ b/AspNetCore.Examples.Auth.Api.Tests/Extensions/RequestBuilderExtensions.cs @@ -1,6 +1,8 @@ using System.Text.Json; +#pragma warning disable IDE0130 // Namespace does not match folder structure namespace Microsoft.AspNetCore.TestHost; +#pragma warning restore IDE0130 // Namespace does not match folder structure public static class RequestBuilderExtensions { diff --git a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_claims_endpoint_sould.cs b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_claims_endpoint_should.cs similarity index 87% rename from AspNetCore.Examples.Auth.Api.Tests/Requests_to_claims_endpoint_sould.cs rename to AspNetCore.Examples.Auth.Api.Tests/Requests_to_claims_endpoint_should.cs index 328cc60..c2c9d6c 100644 --- a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_claims_endpoint_sould.cs +++ b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_claims_endpoint_should.cs @@ -1,14 +1,12 @@ - - namespace AspNetCore.Examples.Auth.Api.Tests; [Collection(nameof(HostCollectionFixture))] -public class Requests_to_claims_endpoint_sould(HostFixture hostFixture) +public class Requests_to_claims_endpoint_should(HostFixture hostFixture) { private readonly TestServer _server = hostFixture.Server; [Fact] - public async Task Succceed_when_authenticated() + public async Task Succeed_when_authenticated() { var claims = new[] { new Claim { Type = "TestClaim1", Value = "Value1" }, diff --git a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyadmin_endpoint_sould.cs b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_admin_endpoint_should.cs similarity index 94% rename from AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyadmin_endpoint_sould.cs rename to AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_admin_endpoint_should.cs index ab21d71..9cf19a9 100644 --- a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyadmin_endpoint_sould.cs +++ b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_admin_endpoint_should.cs @@ -1,7 +1,7 @@ namespace AspNetCore.Examples.Auth.Api.Tests; [Collection(nameof(HostCollectionFixture))] -public class Requests_to_onlyadmin_endpoint_sould(HostFixture hostFixture) +public class Requests_to_only_admin_endpoint_should(HostFixture hostFixture) { private readonly TestServer _server = hostFixture.Server; diff --git a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyalice_endpoint_sould.cs b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_alice_endpoint_should.cs similarity index 94% rename from AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyalice_endpoint_sould.cs rename to AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_alice_endpoint_should.cs index aa6c0e1..5c02998 100644 --- a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyalice_endpoint_sould.cs +++ b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_alice_endpoint_should.cs @@ -1,7 +1,7 @@ namespace AspNetCore.Examples.Auth.Api.Tests; [Collection(nameof(HostCollectionFixture))] -public class Requests_to_onlyalice_endpoint_sould(HostFixture hostFixture) +public class Requests_to_only_alice_endpoint_should(HostFixture hostFixture) { private readonly TestServer _server = hostFixture.Server; diff --git a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlybob_endpoint_sould.cs b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_bob_endpoint_should.cs similarity index 94% rename from AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlybob_endpoint_sould.cs rename to AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_bob_endpoint_should.cs index 19efdf9..93ae1c3 100644 --- a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlybob_endpoint_sould.cs +++ b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_bob_endpoint_should.cs @@ -1,7 +1,7 @@ namespace AspNetCore.Examples.Auth.Api.Tests; [Collection(nameof(HostCollectionFixture))] -public class Requests_to_onlybob_endpoint_sould(HostFixture hostFixture) +public class Requests_to_only_bob_endpoint_should(HostFixture hostFixture) { private readonly TestServer _server = hostFixture.Server; diff --git a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyrecentlogin_endpoint_sould.cs b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_recent_login_endpoint_should.cs similarity index 95% rename from AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyrecentlogin_endpoint_sould.cs rename to AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_recent_login_endpoint_should.cs index 9ca2aab..4890dca 100644 --- a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlyrecentlogin_endpoint_sould.cs +++ b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_recent_login_endpoint_should.cs @@ -1,7 +1,7 @@ namespace AspNetCore.Examples.Auth.Api.Tests; [Collection(nameof(HostCollectionFixture))] -public class Requests_to_onlyrecentlogin_endpoint_sould(HostFixture hostFixture) +public class Requests_to_only_recent_login_endpoint_should(HostFixture hostFixture) { private readonly TestServer _server = hostFixture.Server; diff --git a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlysameauthor_endpoint_sould.cs b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_same_author_endpoint_should.cs similarity index 94% rename from AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlysameauthor_endpoint_sould.cs rename to AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_same_author_endpoint_should.cs index 02d1eeb..309042e 100644 --- a/AspNetCore.Examples.Auth.Api.Tests/Requests_to_onlysameauthor_endpoint_sould.cs +++ b/AspNetCore.Examples.Auth.Api.Tests/Requests_to_only_same_author_endpoint_should.cs @@ -1,7 +1,7 @@ namespace AspNetCore.Examples.Auth.Api.Tests; [Collection(nameof(HostCollectionFixture))] -public class Requests_to_onlysameauthor_endpoint_sould(HostFixture hostFixture) +public class Requests_to_only_same_author_endpoint_should(HostFixture hostFixture) { private readonly TestServer _server = hostFixture.Server; diff --git a/AspNetCore.Examples.Auth.Api.Tests/TestDoubles/TestAuthenticationHandler.cs b/AspNetCore.Examples.Auth.Api.Tests/TestDoubles/TestAuthenticationHandler.cs index 96d2501..20bce91 100644 --- a/AspNetCore.Examples.Auth.Api.Tests/TestDoubles/TestAuthenticationHandler.cs +++ b/AspNetCore.Examples.Auth.Api.Tests/TestDoubles/TestAuthenticationHandler.cs @@ -6,16 +6,12 @@ namespace AspNetCore.Examples.Auth.Api.Tests.TestDoubles; -public class TestAuthenticationHandler : AuthenticationHandler +public class TestAuthenticationHandler( + IOptionsMonitor options, + ILoggerFactory logger, + UrlEncoder encoder +) : AuthenticationHandler(options, logger, encoder) { - public TestAuthenticationHandler( - IOptionsMonitor options, - ILoggerFactory logger, - UrlEncoder encoder) - : base(options, logger, encoder) - { - } - protected override Task HandleAuthenticateAsync() { var authHeader = Request.Headers.Authorization.ToString(); diff --git a/AspNetCore.Examples.Auth.Api/Extensions/AuthorizationExtensions.cs b/AspNetCore.Examples.Auth.Api/Extensions/AuthorizationExtensions.cs index 5fcb61a..8653ddf 100644 --- a/AspNetCore.Examples.Auth.Api/Extensions/AuthorizationExtensions.cs +++ b/AspNetCore.Examples.Auth.Api/Extensions/AuthorizationExtensions.cs @@ -43,10 +43,10 @@ public static WebApplication UseCustomAuthorization(this WebApplication app) { if (context.User.FindFirstValue(ClaimTypes.Email) == AliceEmail) { - context.User.AddIdentity(new ClaimsIdentity(new[] - { + context.User.AddIdentity(new ClaimsIdentity( + [ new System.Security.Claims.Claim(ClaimTypes.Role, Roles.Admin), - })); + ])); } return next(); diff --git a/AspNetCore.Examples.Auth.Api/Extensions/OpenApiExtensions.cs b/AspNetCore.Examples.Auth.Api/Extensions/OpenApiExtensions.cs index 71345fd..ed81f95 100644 --- a/AspNetCore.Examples.Auth.Api/Extensions/OpenApiExtensions.cs +++ b/AspNetCore.Examples.Auth.Api/Extensions/OpenApiExtensions.cs @@ -11,7 +11,7 @@ namespace AspNetCore.Examples.Auth.Api.Extensions; public static class OpenApiExtensions { - public static IServiceCollection AddCustomOpenApi(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddCustomOpenApi(this IServiceCollection services) { return services .AddSingleton() @@ -53,18 +53,12 @@ private static bool IsAuthorizedOperation(this ActionDescriptor actionDescriptor return authorize && !allowAnonymous; } - private class DiscoveryDocumentProvider + private class DiscoveryDocumentProvider(IHttpClientFactory httpClientFactory, IOptionsMonitor jwtBearerOptions) { - private readonly IOptionsMonitor _jwtBearerOptions; - private readonly HttpClient _httpClient; + private readonly IOptionsMonitor _jwtBearerOptions = jwtBearerOptions; + private readonly HttpClient _httpClient = httpClientFactory.CreateClient(nameof(DiscoveryDocumentProvider)); private DiscoveryDocumentResponse? _value; - public DiscoveryDocumentProvider(IHttpClientFactory httpClientFactory, IOptionsMonitor jwtBearerOptions) - { - _jwtBearerOptions = jwtBearerOptions; - _httpClient = httpClientFactory.CreateClient(nameof(DiscoveryDocumentProvider)); - } - public async Task GetDiscoveryDocument() { if (_value is not null) @@ -86,9 +80,7 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf if (securityScheme is null) return; - if (document.Components is null) - document.Components = new OpenApiComponents(); - + document.Components ??= new OpenApiComponents(); document.Components.SecuritySchemes.Add(securityScheme.Name, _securityScheme); } @@ -128,7 +120,7 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf private class OpenApiAuthOperationTransformer : IOpenApiOperationTransformer { - private static readonly OpenApiResponses _authorizedResponses = new OpenApiResponses() + private static readonly OpenApiResponses _authorizedResponses = new() { ["401"] = new OpenApiResponse { Description = "User not authenticated." }, ["403"] = new OpenApiResponse { Description = "User not authorized to perform this action." }, diff --git a/AspNetCore.Examples.Auth.Api/Program.cs b/AspNetCore.Examples.Auth.Api/Program.cs index 4a5f1a2..fa6dbab 100644 --- a/AspNetCore.Examples.Auth.Api/Program.cs +++ b/AspNetCore.Examples.Auth.Api/Program.cs @@ -7,8 +7,7 @@ .AddHttpClient() .AddCustomAuthentication(builder.Configuration) .AddCustomAuthorization() - .AddCustomOpenApi(builder.Configuration); - + .AddCustomOpenApi(); var app = builder.Build(); diff --git a/AspNetCore.Examples.Auth.sln b/AspNetCore.Examples.Auth.sln index 754b858..7f6367f 100644 --- a/AspNetCore.Examples.Auth.sln +++ b/AspNetCore.Examples.Auth.sln @@ -9,6 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Examples.Auth.Ap EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{64154395-42D7-495D-A517-6B979C66EB8E}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig global.json = global.json README.md = README.md EndProjectSection