Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add swagger support for JWT #22

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
<PackageVersion Include="throw" Version="1.4.0" />
<PackageVersion Include="xunit" Version="2.6.5" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
Expand All @@ -41,4 +42,4 @@
<PackageVersion Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
</ItemGroup>
</Project>
</Project>
14 changes: 3 additions & 11 deletions src/CleanArchitecture.Api/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@ public class ApiController : ControllerBase
{
protected ActionResult Problem(List<Error> errors)
{
if (errors.Count is 0)
{
return Problem();
}

if (errors.All(error => error.Type == ErrorType.Validation))
{
return ValidationProblem(errors);
}

return Problem(errors[0]);
return errors.Count is 0
? Problem()
: errors.All(error => error.Type == ErrorType.Validation) ? ValidationProblem(errors) : Problem(errors[0]);
}

private ObjectResult Problem(Error error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Identity.Core" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
Expand Down
2 changes: 2 additions & 0 deletions src/CleanArchitecture.Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using CleanArchitecture.Infrastructure.Security;
using CleanArchitecture.Infrastructure.Security.CurrentUserProvider;
using CleanArchitecture.Infrastructure.Security.PolicyEnforcer;
using CleanArchitecture.Infrastructure.Security.SwaggerSupport;
using CleanArchitecture.Infrastructure.Security.TokenGenerator;
using CleanArchitecture.Infrastructure.Security.TokenValidation;
using CleanArchitecture.Infrastructure.Services;
Expand Down Expand Up @@ -103,6 +104,7 @@ private static IServiceCollection AddAuthentication(this IServiceCollection serv

services
.ConfigureOptions<JwtBearerTokenValidationConfiguration>()
.ConfigureOptions<SwaggerJwtSupportConfiguration>()
.AddAuthentication(defaultScheme: JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace CleanArchitecture.Infrastructure.Security.SwaggerSupport;

public class SwaggerJwtSupportConfiguration : IConfigureOptions<SwaggerGenOptions>
{
public void Configure(SwaggerGenOptions options)
{
options.AddSecurityDefinition("Bearer", new()
{
In = ParameterLocation.Header,
Description = "Please provide a valid jwt token.",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "Bearer",
});

options.AddSecurityRequirement(new()
{
{
new()
{
Reference = new()
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer",
},
},
Array.Empty<string>()
},
});
}
}
Loading