验证码优化
This commit is contained in:
1 parent
f489a3553c
commit
4a3bb84aa4
17 files changed
+156
-430
No files matched your search
@@ -63,7 +63,7 @@ public sealed class ArchivesController(AppDbContext db, ICurrentUserDataScope sc
|
||||
}
|
||||
|
||||
public sealed record ArchiveBatchRequest(
|
||||
[property: Required, MinLength(1), MaxLength(100)] Guid[] Ids,
|
||||
[param: Required, MinLength(1), MaxLength(100)] Guid[] Ids,
|
||||
bool Archived,
|
||||
[property: MaxLength(500)] string? Reason = null);
|
||||
[param: MaxLength(500)] string? Reason = null);
|
||||
|
||||
@@ -4,6 +4,7 @@ using Jiaowu.Api.Domain.Academic;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Infrastructure.Auth;
|
||||
using Jiaowu.Api.Infrastructure.Caching;
|
||||
using Lazy.Captcha.Core;
|
||||
using Jiaowu.Api.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
@@ -20,7 +21,7 @@ public sealed class AuthController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
IAuthSessionService authSessionService,
|
||||
ITwoFactorLoginTicketService twoFactorTickets,
|
||||
ILoginCaptchaService loginCaptcha,
|
||||
ICaptcha captcha,
|
||||
IAppCache cache) : ControllerBase
|
||||
{
|
||||
[AllowAnonymous]
|
||||
@@ -139,30 +140,13 @@ public sealed class AuthController(
|
||||
[AllowAnonymous]
|
||||
[EnableRateLimiting("public-auth")]
|
||||
[HttpPost("login/captcha")]
|
||||
public async Task<ActionResult<LoginCaptchaChallenge>> CreateLoginCaptcha(
|
||||
LoginCaptchaCreateRequest request,
|
||||
CancellationToken cancellationToken) =>
|
||||
Ok(await loginCaptcha.CreateAsync(request.DeviceId, HttpContext, cancellationToken));
|
||||
|
||||
[AllowAnonymous]
|
||||
[EnableRateLimiting("public-auth")]
|
||||
[HttpPost("login/captcha/verify")]
|
||||
public async Task<ActionResult<LoginCaptchaProofResponse>> VerifyLoginCaptcha(
|
||||
LoginCaptchaVerifyRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
public ActionResult<LazyLoginCaptchaResponse> CreateLoginCaptcha()
|
||||
{
|
||||
var proof = await loginCaptcha.VerifyAsync(
|
||||
new LoginCaptchaVerification(
|
||||
request.ChallengeId,
|
||||
request.DeviceId,
|
||||
request.Text,
|
||||
request.SliderX,
|
||||
request.Clicks),
|
||||
HttpContext,
|
||||
cancellationToken);
|
||||
return proof is null
|
||||
? Unauthorized(LoginCaptchaProblem())
|
||||
: Ok(new LoginCaptchaProofResponse(proof));
|
||||
var captchaId = Guid.NewGuid().ToString("N");
|
||||
var generated = captcha.Generate(captchaId, expirySeconds: 120);
|
||||
return Ok(new LazyLoginCaptchaResponse(
|
||||
captchaId,
|
||||
$"data:image/gif;base64,{Convert.ToBase64String(generated.Bytes)}"));
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
@@ -172,11 +156,7 @@ public sealed class AuthController(
|
||||
LoginRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (!await loginCaptcha.ConsumeProofAsync(
|
||||
request.CaptchaTicket,
|
||||
request.DeviceId,
|
||||
HttpContext,
|
||||
cancellationToken))
|
||||
if (!captcha.Validate(request.CaptchaId, request.CaptchaCode))
|
||||
return Unauthorized(LoginCaptchaProblem());
|
||||
var user = await userManager.FindByNameAsync(request.UserName);
|
||||
if (user is null || !user.IsEnabled)
|
||||
@@ -350,20 +330,11 @@ public sealed class AuthController(
|
||||
public sealed record LoginRequest(
|
||||
[Required, MaxLength(100)] string UserName,
|
||||
[Required, MaxLength(100)] string Password,
|
||||
[Required, MinLength(32), MaxLength(128)] string CaptchaTicket,
|
||||
[MaxLength(128)] string? DeviceId = null,
|
||||
[Required, MinLength(16), MaxLength(64)] string CaptchaId,
|
||||
[Required, MinLength(3), MaxLength(16)] string CaptchaCode,
|
||||
bool IsNativeApp = false);
|
||||
|
||||
public sealed record LoginCaptchaCreateRequest([MaxLength(128)] string? DeviceId = null);
|
||||
|
||||
public sealed record LoginCaptchaVerifyRequest(
|
||||
[Required, MinLength(32), MaxLength(64)] string ChallengeId,
|
||||
[MaxLength(128)] string? DeviceId,
|
||||
[MaxLength(16)] string? Text,
|
||||
[Range(0, 240)] int? SliderX,
|
||||
[MaxLength(2)] IReadOnlyList<CaptchaPoint>? Clicks);
|
||||
|
||||
public sealed record LoginCaptchaProofResponse(string CaptchaTicket);
|
||||
public sealed record LazyLoginCaptchaResponse(string CaptchaId, string ImageData);
|
||||
|
||||
public sealed record TotpLoginRequest(
|
||||
[Required, MinLength(20), MaxLength(2048)] string TwoFactorTicket,
|
||||
|
||||
@@ -12,6 +12,7 @@ using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Lazy.Captcha.Core;
|
||||
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
|
||||
|
||||
namespace Jiaowu.Api.Controllers;
|
||||
@@ -22,7 +23,7 @@ public sealed class SsoController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
IAuthSessionService authSessionService,
|
||||
IDistributedCache cache,
|
||||
ILoginCaptchaService loginCaptcha,
|
||||
ICaptcha captcha,
|
||||
IOptions<SsoOptions> options,
|
||||
ILogger<SsoController> logger) : ControllerBase
|
||||
{
|
||||
@@ -47,8 +48,8 @@ public sealed class SsoController(
|
||||
[FromQuery] string? bindingIntent = null,
|
||||
[FromQuery] bool nativeApp = false,
|
||||
[FromQuery] string? nativeState = null,
|
||||
[FromQuery] string? captchaTicket = null,
|
||||
[FromQuery] string? deviceId = null,
|
||||
[FromQuery] string? captchaId = null,
|
||||
[FromQuery] string? captchaCode = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!_options.Enabled)
|
||||
@@ -56,8 +57,9 @@ public sealed class SsoController(
|
||||
|
||||
var safeReturnUrl = NormalizeReturnUrl(returnUrl);
|
||||
if (string.IsNullOrWhiteSpace(bindingIntent) &&
|
||||
(string.IsNullOrWhiteSpace(captchaTicket) ||
|
||||
!await loginCaptcha.ConsumeProofAsync(captchaTicket, deviceId, HttpContext, cancellationToken)))
|
||||
(string.IsNullOrWhiteSpace(captchaId) ||
|
||||
string.IsNullOrWhiteSpace(captchaCode) ||
|
||||
!captcha.Validate(captchaId, captchaCode)))
|
||||
{
|
||||
return Unauthorized(AuthController.LoginCaptchaProblem());
|
||||
}
|
||||
@@ -417,8 +419,8 @@ public sealed class SsoController(
|
||||
bindingIntent = intentCode,
|
||||
nativeApp,
|
||||
nativeState,
|
||||
captchaTicket = (string?)null,
|
||||
deviceId = (string?)null
|
||||
captchaId = (string?)null,
|
||||
captchaCode = (string?)null
|
||||
})!;
|
||||
return new SsoBindingStartResponse(loginUrl);
|
||||
}
|
||||
|
||||
Loaded 3 of 17 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user