Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarciarubio committed Oct 17, 2024
1 parent 312c9d1 commit 773501b
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 37 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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" },
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@

namespace AspNetCore.Examples.Auth.Api.Tests.TestDoubles;

public class TestAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
public class TestAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{
public TestAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder)
: base(options, logger, encoder)
{
}

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var authHeader = Request.Headers.Authorization.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 6 additions & 14 deletions AspNetCore.Examples.Auth.Api/Extensions/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiscoveryDocumentProvider>()
Expand Down Expand Up @@ -53,18 +53,12 @@ private static bool IsAuthorizedOperation(this ActionDescriptor actionDescriptor
return authorize && !allowAnonymous;
}

private class DiscoveryDocumentProvider
private class DiscoveryDocumentProvider(IHttpClientFactory httpClientFactory, IOptionsMonitor<JwtBearerOptions> jwtBearerOptions)
{
private readonly IOptionsMonitor<JwtBearerOptions> _jwtBearerOptions;
private readonly HttpClient _httpClient;
private readonly IOptionsMonitor<JwtBearerOptions> _jwtBearerOptions = jwtBearerOptions;
private readonly HttpClient _httpClient = httpClientFactory.CreateClient(nameof(DiscoveryDocumentProvider));
private DiscoveryDocumentResponse? _value;

public DiscoveryDocumentProvider(IHttpClientFactory httpClientFactory, IOptionsMonitor<JwtBearerOptions> jwtBearerOptions)
{
_jwtBearerOptions = jwtBearerOptions;
_httpClient = httpClientFactory.CreateClient(nameof(DiscoveryDocumentProvider));
}

public async Task<DiscoveryDocumentResponse?> GetDiscoveryDocument()
{
if (_value is not null)
Expand All @@ -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);
}

Expand Down Expand Up @@ -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." },
Expand Down
3 changes: 1 addition & 2 deletions AspNetCore.Examples.Auth.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
.AddHttpClient()
.AddCustomAuthentication(builder.Configuration)
.AddCustomAuthorization()
.AddCustomOpenApi(builder.Configuration);

.AddCustomOpenApi();

var app = builder.Build();

Expand Down
1 change: 1 addition & 0 deletions AspNetCore.Examples.Auth.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 773501b

Please sign in to comment.