Základní backend: migrace, autentizace JWT, role, Swagger
- Helpdesk.Migrator (DbUp) aplikuje db/migrations, evidence v schema_version - Domain/Application/Infrastructure/Api podle Clean Architecture - registrace, ověření e-mailu, login, refresh s rotací, logout, reset hesla - JWT podle RFC 7519 evidované jen podle jti v auth_token - správa uživatelů a rolí, číselníky, ProblemDetails, Swagger UI - Docker Compose (db, migrator, api), oddělené DB účty - notifikace mají cizí klíče na issue a proposal - unit testy domény a zabezpečení Closes #1, closes #2, closes #3, closes #4, closes #5, closes #9
This commit is contained in:
1 parent
fc76be7d69
commit
a51d386278
64 files changed
+4394
-1
No files matched your search
@@ -0,0 +1,45 @@
|
||||
using Helpdesk.Application.Abstractions;
|
||||
using Helpdesk.Domain.Auth;
|
||||
using Helpdesk.Infrastructure.Security;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
|
||||
namespace Helpdesk.Api.Infrastructure;
|
||||
|
||||
public static class AuthenticationSetup
|
||||
{
|
||||
/// <summary>
|
||||
/// Ověření access tokenu podle RFC 7519, kap. 7.2 (kap. 10.1 návrhu):
|
||||
/// body 1–4 (podpis HS256, <c>typ</c> = <c>at+jwt</c>, <c>iss</c>/<c>aud</c>, <c>exp</c>) zajistí JwtBearer,
|
||||
/// bod 5 (zneplatněné <c>jti</c>) kontroluje <see cref="JwtBearerEvents.OnTokenValidated"/>.
|
||||
/// </summary>
|
||||
public static IServiceCollection AddHelpdeskAuthentication(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var jwt = configuration.GetSection(JwtOptions.Section).Get<JwtOptions>() ?? new JwtOptions();
|
||||
|
||||
services
|
||||
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.MapInboundClaims = false; // claimy zůstanou jako "sub", "role", "jti"
|
||||
options.TokenValidationParameters = JwtTokenService.CreateValidationParameters(jwt, TokenType.Access);
|
||||
options.Events = new JwtBearerEvents
|
||||
{
|
||||
OnTokenValidated = context =>
|
||||
{
|
||||
var store = context.HttpContext.RequestServices.GetRequiredService<IRevokedTokenStore>();
|
||||
if (context.SecurityToken is not JsonWebToken token || string.IsNullOrEmpty(token.Id) || store.IsRevoked(token.Id))
|
||||
context.Fail("Token byl zneplatněn.");
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RateLimitPolicies
|
||||
{
|
||||
public const string Auth = "auth";
|
||||
}
|
||||
Reference in new issue
Block a user