U3 password-reset (backend): wachtwoord vergeten en opnieuw instellen
POST auth/forgot-password antwoordt altijd hetzelfde (202) en handelt de mail na het antwoord af; POST auth/reset-password zet een nieuw wachtwoord met de link uit de mail. Alleen een voltooide reset trekt alle refresh tokens in (en beeindigt eerdere access tokens via de security stamp); het aanvragen raakt niets. Link eenmalig en tijdsbeperkt (standaard 60 minuten), cooldown van 5 minuten per adres in het geheugen, beide endpoints onder de login-limiter. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Email;
|
||||
using SlpModularCms.Core.Identity.Models;
|
||||
using SlpModularCms.Core.Identity.Services;
|
||||
|
||||
@@ -15,11 +16,22 @@ public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly IAuthService _authService;
|
||||
private readonly JwtSettings _jwtSettings;
|
||||
private readonly IPasswordResetService _passwordReset;
|
||||
private readonly IPasswordResetDispatcher _dispatcher;
|
||||
private readonly IIdentityMailService _mail;
|
||||
|
||||
public AuthController(IAuthService authService, IOptions<JwtSettings> jwtSettings)
|
||||
public AuthController(
|
||||
IAuthService authService,
|
||||
IOptions<JwtSettings> jwtSettings,
|
||||
IPasswordResetService passwordReset,
|
||||
IPasswordResetDispatcher dispatcher,
|
||||
IIdentityMailService mail)
|
||||
{
|
||||
_authService = authService;
|
||||
_jwtSettings = jwtSettings.Value;
|
||||
_passwordReset = passwordReset;
|
||||
_dispatcher = dispatcher;
|
||||
_mail = mail;
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
@@ -76,6 +88,47 @@ public class AuthController : ControllerBase
|
||||
return Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Anonymous. Always answers 202 with the same body for a well-formed address, whether the account
|
||||
/// exists or not; the mail (if any) is handled after the response. 404 on a site without mail.
|
||||
/// </summary>
|
||||
[HttpPost("forgot-password")]
|
||||
[AllowAnonymous]
|
||||
[EnableRateLimiting("login")]
|
||||
public IActionResult ForgotPassword([FromBody] ForgotPasswordRequest request)
|
||||
{
|
||||
if (!_mail.IsEnabled) return NotFound();
|
||||
|
||||
var email = request?.Email?.Trim();
|
||||
if (!EmailAddressRules.IsValidAddress(email))
|
||||
return BadRequest(new { code = "invalid_email", detail = "Enter a valid e-mail address." });
|
||||
|
||||
_dispatcher.Dispatch(email!);
|
||||
return Accepted(new { message = "If this address belongs to an account, an e-mail with a link has been sent." });
|
||||
}
|
||||
|
||||
/// <summary>Anonymous. Sets a new password with the link from the mail; only a completed reset ends sessions.</summary>
|
||||
[HttpPost("reset-password")]
|
||||
[AllowAnonymous]
|
||||
[EnableRateLimiting("login")]
|
||||
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordRequest request, CancellationToken ct)
|
||||
{
|
||||
if (!_mail.IsEnabled) return NotFound();
|
||||
|
||||
if (request is null || request.UserId == Guid.Empty
|
||||
|| string.IsNullOrWhiteSpace(request.Token) || request.Token.Length > 2048
|
||||
|| string.IsNullOrEmpty(request.NewPassword) || request.NewPassword.Length > 128)
|
||||
return BadRequest(new { code = "invalid_link", detail = "This link is invalid or has expired." });
|
||||
|
||||
var result = await _passwordReset.ResetAsync(request.UserId, request.Token, request.NewPassword, ct);
|
||||
return result.Status switch
|
||||
{
|
||||
PasswordResetStatus.Succeeded => Ok(new { message = "Password set. You can sign in now." }),
|
||||
PasswordResetStatus.PasswordRejected => BadRequest(new { code = "password_rejected", detail = "The password does not meet the requirements.", problems = result.Problems }),
|
||||
_ => BadRequest(new { code = "invalid_link", detail = "This link is invalid or has expired." })
|
||||
};
|
||||
}
|
||||
|
||||
private void SetTokenCookie(string refreshToken)
|
||||
{
|
||||
Response.Cookies.Append("refreshToken", refreshToken, GetCookieOptions());
|
||||
|
||||
Reference in New Issue
Block a user