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 { /// /// Ověření access tokenu podle RFC 7519, kap. 7.2 (kap. 10.1 návrhu): /// body 1–4 (podpis HS256, typ = at+jwt, iss/aud, exp) zajistí JwtBearer, /// bod 5 (zneplatněné jti) kontroluje . /// public static IServiceCollection AddHelpdeskAuthentication(this IServiceCollection services, IConfiguration configuration) { var jwt = configuration.GetSection(JwtOptions.Section).Get() ?? 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(); 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"; public const string IssueWrites = "issue-writes"; }