Ajout de l'authentification (compte unique, JWT, reset par email)
Application mono-utilisateur : inscription unique, connexion par JWT (12 h), protection de toutes les routes de données, réinitialisation de mot de passe par email avec repli de journalisation, onboarding réorganisé en deux étapes (inscription → connexion → profil + véhicule). Backend (SchotzIadKmTracker.Api) : - Entités Compte (singleton) et ResetPasswordToken + migration AjoutAuthentification. - Hachage PasswordHasher, JwtService (HS256, durée configurable), IEmailService (MailKit + repli LoggingEmailService selon Smtp:Host). - AuthService (register 409 si compte existant, login 401 générique, forgot-password non-divulgation, reset à usage unique + expiration), AuthController (5 endpoints publics), DTOs + validateurs FluentValidation. - Sécurité pipeline : JwtBearer + filtre d'autorisation global, UseAuthentication avant UseAuthorization ; /health et /api/auth/* publics. Frontend (schotziad-km-tracker, zoneless + signals) : - AuthService (localStorage), authInterceptor (Bearer + purge/redirection 401), authGuard, écrans Login/Register/ForgotPassword/ResetPassword, routes publiques et protection [authGuard, profilConfigureGuard]. Configuration : .env.example et docker-compose.yml (JWT__SecretKey, durées, Smtp__*). Tests : 54 tests backend (propriétés FsCheck P1–P14, exemples, intégration WebApplicationFactory) + tests frontend (AuthService, authGuard, authInterceptor, ResetPasswordComponent). Suite complète au vert. Spec : .kiro/specs/authentification (requirements, design, tasks).
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
using System.Text;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using SchotzIadKmTracker.Api.Data;
|
||||
using SchotzIadKmTracker.Api.Models.Entities;
|
||||
using SchotzIadKmTracker.Api.Validators;
|
||||
|
||||
using SchotzIadKmTracker.Api.Configuration;
|
||||
@@ -20,6 +25,12 @@ builder.Services.Configure<BANOptions>(
|
||||
builder.Configuration.GetSection(BANOptions.SectionName));
|
||||
builder.Services.Configure<ORSOptions>(
|
||||
builder.Configuration.GetSection(ORSOptions.SectionName));
|
||||
builder.Services.Configure<JwtOptions>(
|
||||
builder.Configuration.GetSection(JwtOptions.SectionName));
|
||||
builder.Services.Configure<AuthOptions>(
|
||||
builder.Configuration.GetSection(AuthOptions.SectionName));
|
||||
builder.Services.Configure<SmtpOptions>(
|
||||
builder.Configuration.GetSection(SmtpOptions.SectionName));
|
||||
|
||||
// --- Client typé BAN (Base Adresses Nationale) ---
|
||||
// BaseAddress et Timeout proviennent de BANOptions (appsettings.json).
|
||||
@@ -70,7 +81,15 @@ builder.Services.AddScoped<IAdresseCacheRepository, AdresseCacheRepository>();
|
||||
builder.Services.AddScoped<IAdresseCacheService, AdresseCacheService>();
|
||||
|
||||
// --- Services de base ---
|
||||
builder.Services.AddControllers();
|
||||
// Autorisation GLOBALE par défaut : toutes les routes MVC exigent un utilisateur authentifié.
|
||||
// AuthController porte [AllowAnonymous] ; /health (endpoint minimal) n'est pas couvert par les filtres MVC.
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
var policy = new Microsoft.AspNetCore.Authorization.AuthorizationPolicyBuilder()
|
||||
.RequireAuthenticatedUser()
|
||||
.Build();
|
||||
options.Filters.Add(new Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter(policy));
|
||||
});
|
||||
|
||||
// --- Health checks (endpoint GET /health, utilisé par Docker Compose) ---
|
||||
builder.Services.AddHealthChecks();
|
||||
@@ -87,7 +106,39 @@ var connectionString = builder.Configuration.GetConnectionString("Default")
|
||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||
options.UseSqlite(connectionString));
|
||||
|
||||
// --- Hachage des mots de passe (ASP.NET Core Identity PasswordHasher) ---
|
||||
builder.Services.AddSingleton<IPasswordHasher<Compte>, PasswordHasher<Compte>>();
|
||||
|
||||
// --- Repositories & services métier ---
|
||||
builder.Services.AddScoped<IJwtService, JwtService>();
|
||||
|
||||
// --- Sélection de l'implémentation Email selon la présence de config SMTP ---
|
||||
var smtpOptions = builder.Configuration.GetSection(SmtpOptions.SectionName).Get<SmtpOptions>() ?? new SmtpOptions();
|
||||
if (string.IsNullOrWhiteSpace(smtpOptions.Host))
|
||||
builder.Services.AddScoped<IEmailService, LoggingEmailService>(); // repli journalisation (Exigence 6.5)
|
||||
else
|
||||
builder.Services.AddScoped<IEmailService, SmtpEmailService>(); // MailKit (Exigence 6.4)
|
||||
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
|
||||
// --- Authentification JWT Bearer ---
|
||||
var jwtOptions = builder.Configuration.GetSection(JwtOptions.SectionName).Get<JwtOptions>() ?? new JwtOptions();
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true, // rejette les jetons expirés (Exigence 5.2)
|
||||
ValidateIssuerSigningKey = true, // rejette les signatures invalides (Exigence 5.3)
|
||||
ValidIssuer = jwtOptions.Issuer,
|
||||
ValidAudience = jwtOptions.Audience,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecretKey)),
|
||||
ClockSkew = TimeSpan.FromSeconds(30) // tolérance d'horloge réduite
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<SchotzIadKmTracker.Api.Repositories.IVehiculeRepository,
|
||||
SchotzIadKmTracker.Api.Repositories.VehiculeRepository>();
|
||||
builder.Services.AddScoped<IVehiculeService, VehiculeService>();
|
||||
@@ -152,6 +203,8 @@ app.UseHttpsRedirection();
|
||||
|
||||
app.UseCors(CorsPolicyName);
|
||||
|
||||
app.UseAuthentication();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
Reference in New Issue
Block a user