Skip to content

Commit

Permalink
Make configuration simpler.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarciarubio committed Nov 6, 2024
1 parent b700bf1 commit f905293
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0-rc.2.24474.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-rc.2.24474.3" />
<PackageReference Include="Scalar.AspNetCore" Version="1.2.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.9.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ public static class AuthenticationExtensions
{
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
{
var authConfig = configuration.GetSection("Authentication");
if (!authConfig.Exists())
return services;
var bearerConfig = configuration
.GetSection($"Authentication:{JwtBearerDefaults.AuthenticationScheme}");

var builder = services.AddAuthentication();

var bearerConfig = authConfig.GetSection(JwtBearerDefaults.AuthenticationScheme);
if (!bearerConfig.Exists())
return services;

return builder
return services
.AddAuthentication()
.AddJwtBearer(bearerConfig.Bind)
.Services;
}
Expand Down
14 changes: 2 additions & 12 deletions AspNetCore.Examples.Auth.Api/Extensions/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,8 @@ public static IServiceCollection AddCustomOpenApi(this IServiceCollection servic
public static WebApplication UseCustomOpenApi(this WebApplication app, IConfiguration configuration)
{
app.MapOpenApi();

var swaggerUIConfig = configuration.GetSection("OpenApi:SwaggerUI");
if (swaggerUIConfig.Exists())
{
app.UseSwaggerUI(swaggerUIConfig.Bind);
}

var scalarConfig = configuration.GetSection("OpenApi:Scalar");
if (scalarConfig.Exists())
{
app.MapScalarApiReference(scalarConfig.Bind);
}
app.UseSwaggerUI(configuration.GetSection("OpenApi:SwaggerUI").Bind);
app.MapScalarApiReference(configuration.GetSection("OpenApi:Scalar").Bind);

return app;
}
Expand Down
5 changes: 4 additions & 1 deletion AspNetCore.Examples.Auth.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

var app = builder.Build();

app.UseCustomOpenApi(builder.Configuration);
if (app.Environment.IsDevelopment())
{
app.UseCustomOpenApi(builder.Configuration);
}

app.UseAuthentication();
app.UseCustomAuthorization();
Expand Down
33 changes: 33 additions & 0 deletions AspNetCore.Examples.Auth.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Authentication": {
"Bearer": {
"Authority": "https://demo.duendesoftware.com",
"Audience": "api"
}
},
"OpenApi": {
"SwaggerUI": {
"OAuthConfigObject": {
"ClientId": "interactive.public",
"ScopeSeparator": " ",
"UsePkceWithAuthorizationCodeGrant": true,
"Scopes": [ "api" ]
}
},
"Scalar": {
// Does not yet fully support OAuth
"Authentication": {
"OAuth2": {
"ClientId": "interactive.public",
"Scopes": [ "api" ]
}
}
}
}
}
14 changes: 7 additions & 7 deletions AspNetCore.Examples.Auth.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"AllowedHosts": "*",
"Authentication": {
"Bearer": {
"Authority": "https://demo.duendesoftware.com",
"Audience": "api"
"Authority": "",
"Audience": ""
}
},
"OpenApi": {
Expand All @@ -23,18 +23,18 @@
]
},
"OAuthConfigObject": {
"ClientId": "interactive.public",
"ScopeSeparator": " ",
"ClientId": "",
"ScopeSeparator": "",
"UsePkceWithAuthorizationCodeGrant": true,
"Scopes": [ "api" ]
"Scopes": []
}
},
"Scalar": {
// Does not yet fully support OAuth
"Authentication": {
"OAuth2": {
"ClientId": "interactive.public",
"Scopes": [ "api" ]
"ClientId": "",
"Scopes": [ "" ]
}
}
}
Expand Down

0 comments on commit f905293

Please sign in to comment.