Harden sensitive account security verification
This commit is contained in:
1 parent
c816043537
commit
a2fd08fe7e
5 files changed
+147
-6
No files matched your search
@@ -2,9 +2,11 @@ using System.ComponentModel.DataAnnotations;
|
||||
using System.Security.Claims;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Infrastructure.Branding;
|
||||
using Jiaowu.Api.Infrastructure.Auth;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
|
||||
namespace Jiaowu.Api.Controllers;
|
||||
|
||||
@@ -23,13 +25,16 @@ public sealed class AccountSecurityController(
|
||||
}
|
||||
|
||||
[HttpPost("totp/setup")]
|
||||
[EnableRateLimiting("sensitive-auth")]
|
||||
public async Task<ActionResult<TotpSetupResponse>> SetupTotp(PasswordConfirmationRequest request)
|
||||
{
|
||||
var user = await CurrentUserAsync();
|
||||
if (user is null) return Unauthorized();
|
||||
if (!await userManager.CheckPasswordAsync(user, request.CurrentPassword))
|
||||
if (!await SensitiveOperationVerification.PasswordAsync(userManager, user, request.CurrentPassword))
|
||||
return Unauthorized(PasswordProblem());
|
||||
|
||||
if (user.TwoFactorEnabled) return Conflict("双重验证已启用,请先关闭后再重新设置。");
|
||||
|
||||
await userManager.ResetAuthenticatorKeyAsync(user);
|
||||
var key = await userManager.GetAuthenticatorKeyAsync(user);
|
||||
if (string.IsNullOrWhiteSpace(key)) return Problem("无法创建验证器密钥。");
|
||||
@@ -41,12 +46,13 @@ public sealed class AccountSecurityController(
|
||||
}
|
||||
|
||||
[HttpPost("totp/enable")]
|
||||
[EnableRateLimiting("sensitive-auth")]
|
||||
public async Task<ActionResult<RecoveryCodesResponse>> EnableTotp(TotpEnableRequest request)
|
||||
{
|
||||
var user = await CurrentUserAsync();
|
||||
if (user is null) return Unauthorized();
|
||||
if (!await userManager.VerifyTwoFactorTokenAsync(user,
|
||||
TokenOptions.DefaultAuthenticatorProvider, request.Code.Replace(" ", string.Empty)))
|
||||
if (user.TwoFactorEnabled) return Conflict("双重验证已启用。");
|
||||
if (!await SensitiveOperationVerification.TotpAsync(userManager, user, request.Code.Replace(" ", string.Empty)))
|
||||
{
|
||||
ModelState.AddModelError("code", "验证码不正确或已过期。");
|
||||
return ValidationProblem(ModelState);
|
||||
@@ -58,11 +64,12 @@ public sealed class AccountSecurityController(
|
||||
}
|
||||
|
||||
[HttpPost("totp/disable")]
|
||||
[EnableRateLimiting("sensitive-auth")]
|
||||
public async Task<IActionResult> DisableTotp(PasswordConfirmationRequest request)
|
||||
{
|
||||
var user = await CurrentUserAsync();
|
||||
if (user is null) return Unauthorized();
|
||||
if (!await userManager.CheckPasswordAsync(user, request.CurrentPassword))
|
||||
if (!await SensitiveOperationVerification.PasswordAsync(userManager, user, request.CurrentPassword))
|
||||
return Unauthorized(PasswordProblem());
|
||||
await userManager.SetTwoFactorEnabledAsync(user, false);
|
||||
await userManager.ResetAuthenticatorKeyAsync(user);
|
||||
|
||||
@@ -61,6 +61,7 @@ public sealed class PasskeyController(
|
||||
|
||||
[Authorize]
|
||||
[HttpPost("registration/options")]
|
||||
[EnableRateLimiting("sensitive-auth")]
|
||||
public async Task<ActionResult> RegistrationOptions(
|
||||
PasskeyRegistrationOptionsRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
@@ -68,7 +69,7 @@ public sealed class PasskeyController(
|
||||
if (!_options.Enabled) return PasskeysDisabled();
|
||||
var user = await CurrentUserAsync();
|
||||
if (user is null) return Unauthorized();
|
||||
if (!await userManager.CheckPasswordAsync(user, request.CurrentPassword))
|
||||
if (!await SensitiveOperationVerification.PasswordAsync(userManager, user, request.CurrentPassword))
|
||||
return Unauthorized(PasswordProblem());
|
||||
var credentials = await db.PasskeyCredentials.AsNoTracking()
|
||||
.Where(x => x.UserId == user.Id).ToListAsync(cancellationToken);
|
||||
@@ -278,6 +279,7 @@ public sealed class PasskeyController(
|
||||
|
||||
[Authorize]
|
||||
[HttpDelete("{id:guid}")]
|
||||
[EnableRateLimiting("sensitive-auth")]
|
||||
public async Task<IActionResult> Delete(
|
||||
Guid id,
|
||||
PasswordConfirmationRequest request,
|
||||
@@ -285,7 +287,7 @@ public sealed class PasskeyController(
|
||||
{
|
||||
var user = await CurrentUserAsync();
|
||||
if (user is null) return Unauthorized();
|
||||
if (!await userManager.CheckPasswordAsync(user, request.CurrentPassword))
|
||||
if (!await SensitiveOperationVerification.PasswordAsync(userManager, user, request.CurrentPassword))
|
||||
return Unauthorized(PasswordProblem());
|
||||
var credential = await db.PasskeyCredentials.SingleOrDefaultAsync(
|
||||
x => x.Id == id && x.UserId == user.Id, cancellationToken);
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace Jiaowu.Api.Infrastructure.Auth;
|
||||
|
||||
internal static class SensitiveOperationVerification
|
||||
{
|
||||
public static async Task<bool> PasswordAsync(UserManager<ApplicationUser> manager, ApplicationUser user, string password)
|
||||
{
|
||||
if (!user.IsEnabled || await manager.IsLockedOutAsync(user)) return false;
|
||||
if (!await manager.CheckPasswordAsync(user, password))
|
||||
{
|
||||
await manager.AccessFailedAsync(user);
|
||||
return false;
|
||||
}
|
||||
await manager.ResetAccessFailedCountAsync(user);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static async Task<bool> TotpAsync(UserManager<ApplicationUser> manager, ApplicationUser user, string code)
|
||||
{
|
||||
if (!user.IsEnabled || await manager.IsLockedOutAsync(user)) return false;
|
||||
if (!await manager.VerifyTwoFactorTokenAsync(user, TokenOptions.DefaultAuthenticatorProvider, code))
|
||||
{
|
||||
await manager.AccessFailedAsync(user);
|
||||
return false;
|
||||
}
|
||||
await manager.ResetAccessFailedCountAsync(user);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loaded 3 of 5 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user