♻️ Optimized auth service

This commit is contained in:
2025-11-02 14:26:07 +08:00
parent a377ca2072
commit 5445df3b61
5 changed files with 296 additions and 134 deletions

View File

@@ -1,10 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
namespace DysonNetwork.Pass.Auth;
[ApiController]
[Route("/api/captcha")]
public class CaptchaController(IConfiguration configuration) : ControllerBase
public class CaptchaController(
IConfiguration configuration,
AuthService authService,
ILogger<CaptchaController> logger
) : ControllerBase
{
[HttpGet]
public IActionResult GetConfiguration()
@@ -15,4 +20,43 @@ public class CaptchaController(IConfiguration configuration) : ControllerBase
apiKey = configuration["Captcha:ApiKey"],
});
}
}
[HttpPost("verify")]
[EnableRateLimiting("captcha")]
public async Task<IActionResult> Verify([FromBody] CaptchaVerifyRequest request)
{
if (string.IsNullOrWhiteSpace(request.Token))
{
logger.LogWarning("Captcha verification failed: empty token from {IpAddress}",
HttpContext.Connection.RemoteIpAddress?.ToString());
return BadRequest("Token is required");
}
try
{
var isValid = await authService.ValidateCaptcha(request.Token);
if (!isValid)
{
logger.LogWarning("Captcha verification failed: invalid token from {IpAddress}",
HttpContext.Connection.RemoteIpAddress?.ToString());
return BadRequest("Invalid captcha token");
}
logger.LogInformation("Captcha verification successful from {IpAddress}",
HttpContext.Connection.RemoteIpAddress?.ToString());
return Ok();
}
catch (Exception ex)
{
logger.LogError(ex, "Error during captcha verification from {IpAddress}",
HttpContext.Connection.RemoteIpAddress?.ToString());
return StatusCode(500, "Internal server error");
}
}
public class CaptchaVerifyRequest
{
public string Token { get; set; } = string.Empty;
}
}