-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,117 @@ | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
using Microsoft.IdentityModel.Logging; | ||
using Microsoft.OpenApi.Models; | ||
using Serilog; | ||
|
||
namespace Api; | ||
|
||
internal static class HostingExtensions | ||
{ | ||
private static IWebHostEnvironment? _env; | ||
public static WebApplication ConfigureServices(this WebApplicationBuilder builder) | ||
{ | ||
var services = builder.Services; | ||
var configuration = builder.Configuration; | ||
_env = builder.Environment; | ||
|
||
var stsServer = configuration["StsServer"]; | ||
|
||
services.AddAuthentication("dpoptokenscheme") | ||
.AddJwtBearer("dpoptokenscheme", options => | ||
{ | ||
options.Authority = stsServer; | ||
options.TokenValidationParameters.ValidateAudience = false; | ||
options.MapInboundClaims = false; | ||
|
||
options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" }; | ||
}); | ||
|
||
services.ConfigureDPoPTokensForScheme("dpoptokenscheme"); | ||
|
||
services.AddAuthorization(options => | ||
options.AddPolicy("protectedScope", policy => | ||
{ | ||
policy.RequireClaim("scope", "scope-dpop"); | ||
}) | ||
); | ||
|
||
services.AddSwaggerGen(c => | ||
{ | ||
// add JWT Authentication | ||
var securityScheme = new OpenApiSecurityScheme | ||
{ | ||
Name = "JWT Authentication", | ||
Description = "Enter JWT Bearer token **_only_**", | ||
In = ParameterLocation.Header, | ||
Type = SecuritySchemeType.Http, | ||
Scheme = "bearer", // must be lower case | ||
BearerFormat = "JWT", | ||
Reference = new OpenApiReference | ||
{ | ||
Id = JwtBearerDefaults.AuthenticationScheme, | ||
Type = ReferenceType.SecurityScheme | ||
} | ||
}; | ||
c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme); | ||
c.AddSecurityRequirement(new OpenApiSecurityRequirement | ||
{ | ||
{securityScheme, Array.Empty<string>()} | ||
}); | ||
|
||
c.SwaggerDoc("v1", new OpenApiInfo | ||
{ | ||
Title = "DPOP API", | ||
Version = "v1", | ||
Description = "User API", | ||
Contact = new OpenApiContact | ||
{ | ||
Name = "damienbod", | ||
Email = string.Empty, | ||
Url = new Uri("https://damienbod.com/"), | ||
}, | ||
}); | ||
}); | ||
|
||
services.AddControllers(); | ||
|
||
return builder.Build(); | ||
} | ||
|
||
public static WebApplication ConfigurePipeline(this WebApplication app) | ||
{ | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
using Microsoft.IdentityModel.Logging; | ||
using Microsoft.OpenApi.Models; | ||
using Serilog; | ||
|
||
namespace Api; | ||
|
||
internal static class HostingExtensions | ||
{ | ||
private static IWebHostEnvironment? _env; | ||
public static WebApplication ConfigureServices(this WebApplicationBuilder builder) | ||
{ | ||
var services = builder.Services; | ||
var configuration = builder.Configuration; | ||
_env = builder.Environment; | ||
|
||
var stsServer = configuration["StsServer"]; | ||
|
||
services.AddAuthentication("dpoptokenscheme") | ||
.AddJwtBearer("dpoptokenscheme", options => | ||
{ | ||
options.Authority = stsServer; | ||
options.TokenValidationParameters.ValidateAudience = false; | ||
options.MapInboundClaims = false; | ||
|
||
options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" }; | ||
}); | ||
|
||
services.ConfigureDPoPTokensForScheme("dpoptokenscheme"); | ||
|
||
services.AddAuthorization(options => | ||
options.AddPolicy("protectedScope", policy => | ||
{ | ||
policy.RequireClaim("scope", "scope-dpop"); | ||
}) | ||
); | ||
|
||
services.AddSwaggerGen(c => | ||
{ | ||
// add JWT Authentication | ||
var securityScheme = new OpenApiSecurityScheme | ||
{ | ||
Name = "JWT Authentication", | ||
Description = "Enter JWT Bearer token **_only_**", | ||
In = ParameterLocation.Header, | ||
Type = SecuritySchemeType.Http, | ||
Scheme = "bearer", // must be lower case | ||
BearerFormat = "JWT", | ||
Reference = new OpenApiReference | ||
{ | ||
Id = JwtBearerDefaults.AuthenticationScheme, | ||
Type = ReferenceType.SecurityScheme | ||
} | ||
}; | ||
c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme); | ||
c.AddSecurityRequirement(new OpenApiSecurityRequirement | ||
{ | ||
{securityScheme, Array.Empty<string>()} | ||
}); | ||
|
||
c.SwaggerDoc("v1", new OpenApiInfo | ||
{ | ||
Title = "DPOP API", | ||
Version = "v1", | ||
Description = "User API", | ||
Contact = new OpenApiContact | ||
{ | ||
Name = "damienbod", | ||
Email = string.Empty, | ||
Url = new Uri("https://damienbod.com/"), | ||
}, | ||
}); | ||
}); | ||
|
||
services.AddControllers(); | ||
|
||
return builder.Build(); | ||
} | ||
|
||
public static WebApplication ConfigurePipeline(this WebApplication app) | ||
{ | ||
IdentityModelEventSource.ShowPII = true; | ||
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear(); | ||
|
||
app.UseSerilogRequestLogging(); | ||
|
||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseSecurityHeaders(SecurityHeadersDefinitions | ||
.GetHeaderPolicyCollection(_env!.IsDevelopment())); | ||
|
||
if (_env!.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
|
||
app.UseSwagger(); | ||
app.UseSwaggerUI(c => | ||
{ | ||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"); | ||
}); | ||
} | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.MapControllers() | ||
.RequireAuthorization(); | ||
|
||
return app; | ||
} | ||
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear(); | ||
|
||
app.UseSerilogRequestLogging(); | ||
|
||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseSecurityHeaders(SecurityHeadersDefinitions | ||
.GetHeaderPolicyCollection(_env!.IsDevelopment())); | ||
|
||
if (_env!.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
|
||
app.UseSwagger(); | ||
app.UseSwaggerUI(c => | ||
{ | ||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"); | ||
}); | ||
} | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
app.MapControllers() | ||
.RequireAuthorization(); | ||
|
||
return app; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters