fix(identity): DB refresh tokens, auto-refresh handler, 2FA endpoints, JWT fix
- DB-backed RevokedRefreshToken entity + migration (remplace ConcurrentDictionary\n en memoire, fonctionne en multi-instance et survive aux redemarrages)\n- TokenRefreshHandler: auto-refresh des JWT 401 via DelegatingHandler\n (avec SemaphoreSlim pour eviter les refresh storms)\n- Fix: GenerateRefreshToken genere maintenant un JWT valide au lieu de\n bytes aleatoires (ValidateRefreshToken attendait un JWT - flux casse)\n- Fix: Login.razor gere Requires2fa correctement (affiche champ code 2FA)\n- Ajout: endpoints POST /api/auth/2fa/enable et /2fa/disable\n- Ajout: Disable2faAsync dans IAuthService\n- Fix: GenerateTemporaryPassword garantit les 4 categories de charactere\n (uppercase, lowercase, digit, non-alphanumeric)\n- Fix: TestFactory ajoute Jwt:RefreshKey manquant\n\nBuild: 0 errors, 0 warnings\nTests: 226/226 passed
This commit is contained in:
1 parent
940e826865
commit
7be5d42e85
16 files changed
+2102
-66
No files matched your search
@@ -19,22 +19,38 @@
|
||||
</div>
|
||||
|
||||
<div class="auth-form">
|
||||
<form @onsubmit="LoginAsync">
|
||||
<MudTextField @bind-Value="_email"
|
||||
Label="Email"
|
||||
Variant="Variant.Outlined"
|
||||
Class="mb-4"
|
||||
Required="true"
|
||||
Immediate="true"
|
||||
Autocomplete="email" />
|
||||
<MudTextField @bind-Value="_password"
|
||||
Label="Mot de passe"
|
||||
Variant="Variant.Outlined"
|
||||
InputType="InputType.Password"
|
||||
Class="mb-4"
|
||||
Required="true"
|
||||
Immediate="true"
|
||||
Autocomplete="current-password" />
|
||||
<form @onsubmit="HandleSubmitAsync">
|
||||
@if (!_requires2fa)
|
||||
{
|
||||
<MudTextField @bind-Value="_email"
|
||||
Label="Email"
|
||||
Variant="Variant.Outlined"
|
||||
Class="mb-4"
|
||||
Required="true"
|
||||
Immediate="true"
|
||||
Autocomplete="email" />
|
||||
<MudTextField @bind-Value="_password"
|
||||
Label="Mot de passe"
|
||||
Variant="Variant.Outlined"
|
||||
InputType="InputType.Password"
|
||||
Class="mb-4"
|
||||
Required="true"
|
||||
Immediate="true"
|
||||
Autocomplete="current-password" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudAlert Severity="Severity.Info" Class="mb-4">
|
||||
Authentification à deux facteurs requise. Saisissez le code généré par votre application d'authentification.
|
||||
</MudAlert>
|
||||
<MudTextField @bind-Value="_twoFactorCode"
|
||||
Label="Code 2FA"
|
||||
Variant="Variant.Outlined"
|
||||
Class="mb-4"
|
||||
Required="true"
|
||||
Immediate="true"
|
||||
Autocomplete="one-time-code" />
|
||||
}
|
||||
|
||||
<div class="auth-error">
|
||||
@if (!string.IsNullOrEmpty(_errorMessage))
|
||||
@@ -53,7 +69,7 @@
|
||||
{
|
||||
<MudProgressCircular Size="Size.Small" Class="mr-2" Color="Color.Inherit" />
|
||||
}
|
||||
@(_loading ? "Connexion en cours…" : "Se connecter")
|
||||
@(_loading ? "Connexion en cours…" : (_requires2fa ? "Vérifier" : "Se connecter"))
|
||||
</MudButton>
|
||||
</form>
|
||||
</div>
|
||||
@@ -70,10 +86,15 @@
|
||||
@code {
|
||||
private string _email = "";
|
||||
private string _password = "";
|
||||
private string _twoFactorCode = "";
|
||||
private bool _requires2fa;
|
||||
private bool _loading;
|
||||
private string _errorMessage = "";
|
||||
private string _returnUrl = "/dashboard";
|
||||
|
||||
private Task HandleSubmitAsync() =>
|
||||
_requires2fa ? Verify2faAsync() : LoginAsync();
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
if (Auth.IsLoggedIn)
|
||||
@@ -103,26 +124,15 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// Update Blazor-level auth state
|
||||
await AuthStateProvider.NotifyLoginAsync(result);
|
||||
|
||||
// Create middleware-level auth cookie so the ASP.NET Core
|
||||
// middleware recognizes the user on subsequent requests.
|
||||
var cookieOk = true;
|
||||
if (!string.IsNullOrEmpty(result.AccessToken))
|
||||
// Check if 2FA is required
|
||||
if (result.Requires2fa)
|
||||
{
|
||||
cookieOk = await AuthHelper.SignInAsync(JsRuntime,
|
||||
AuthHelper.CreateSignInPayload(result.AccessToken));
|
||||
}
|
||||
|
||||
if (!cookieOk)
|
||||
{
|
||||
_errorMessage = "Erreur lors de la création de la session. Veuillez réessayer.";
|
||||
_requires2fa = true;
|
||||
_loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
Snackbar.Add("Connexion réussie", Severity.Success);
|
||||
Navigation.NavigateTo(_returnUrl);
|
||||
await CompleteAuthAsync(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -130,4 +140,55 @@
|
||||
}
|
||||
finally { _loading = false; }
|
||||
}
|
||||
|
||||
private async Task Verify2faAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_twoFactorCode))
|
||||
{
|
||||
_errorMessage = "Le code 2FA est requis";
|
||||
return;
|
||||
}
|
||||
|
||||
_loading = true;
|
||||
_errorMessage = "";
|
||||
try
|
||||
{
|
||||
var result = await Api.Verify2faAsync(_email, _twoFactorCode.Trim());
|
||||
if (result is null)
|
||||
{
|
||||
_errorMessage = "Code 2FA invalide. Veuillez réessayer.";
|
||||
return;
|
||||
}
|
||||
|
||||
await CompleteAuthAsync(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_errorMessage = $"Erreur : {ex.Message}";
|
||||
}
|
||||
finally { _loading = false; }
|
||||
}
|
||||
|
||||
private async Task CompleteAuthAsync(AuthResponse result)
|
||||
{
|
||||
// Update Blazor-level auth state
|
||||
await AuthStateProvider.NotifyLoginAsync(result);
|
||||
|
||||
// Create middleware-level auth cookie
|
||||
var cookieOk = true;
|
||||
if (!string.IsNullOrEmpty(result.AccessToken))
|
||||
{
|
||||
cookieOk = await AuthHelper.SignInAsync(JsRuntime,
|
||||
AuthHelper.CreateSignInPayload(result.AccessToken));
|
||||
}
|
||||
|
||||
if (!cookieOk)
|
||||
{
|
||||
_errorMessage = "Erreur lors de la création de la session. Veuillez réessayer.";
|
||||
return;
|
||||
}
|
||||
|
||||
Snackbar.Add("Connexion réussie", Severity.Success);
|
||||
Navigation.NavigateTo(_returnUrl);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user