Changes
This commit is contained in:
@@ -37,11 +37,7 @@ namespace Api.Controllers
|
||||
/// the reCAPTCHA widget and obtain client-side tokens.
|
||||
/// </summary>
|
||||
/// <returns>200 OK with the public site key as a string.</returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetReCaptchaSiteKey(CancellationToken ct)
|
||||
{
|
||||
return Ok(_captchaSettings.PublicKey);
|
||||
}
|
||||
// ReCaptcha endpoints have been extracted to CaptchaController
|
||||
|
||||
/// <summary>
|
||||
/// Validates the provided reCAPTCHA token and sends a contact message
|
||||
@@ -62,9 +58,8 @@ namespace Api.Controllers
|
||||
return ValidationProblem(ModelState);
|
||||
|
||||
var userIp = HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
|
||||
var res = await ValidateCaptcha(req.CaptchaToken, ct);
|
||||
if (!res.Verdict.Success) return BadRequest("Captcha verification failed.");
|
||||
var verdict = await _captcha.VerifyAsync(req.CaptchaToken, userIp, ct);
|
||||
if (!verdict.Success) return BadRequest("Captcha verification failed.");
|
||||
|
||||
try
|
||||
{
|
||||
@@ -73,7 +68,7 @@ namespace Api.Controllers
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogError(ex, "Contact send failed. ip={Ip} from={From}", res.UserIp, req.Email);
|
||||
_log.LogError(ex, "Contact send failed. ip={Ip} from={From}", userIp, req.Email);
|
||||
return StatusCode(500, "Could not send message.");
|
||||
}
|
||||
}
|
||||
@@ -96,8 +91,9 @@ namespace Api.Controllers
|
||||
if (!ModelState.IsValid)
|
||||
return ValidationProblem(ModelState);
|
||||
|
||||
var res = await ValidateCaptcha(req.CaptchaToken, ct);
|
||||
if (!res.Verdict.Success) return BadRequest("Captcha verification failed.");
|
||||
var userIp = HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
var verdict = await _captcha.VerifyAsync(req.CaptchaToken, userIp, ct);
|
||||
if (!verdict.Success) return BadRequest("Captcha verification failed.");
|
||||
|
||||
try
|
||||
{
|
||||
@@ -106,29 +102,12 @@ namespace Api.Controllers
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.LogError(ex, "Subscription failed. ip={Ip} eMail={eMail}", res.UserIp, req.Email);
|
||||
_log.LogError(ex, "Subscription failed. ip={Ip} eMail={eMail}", userIp, req.Email);
|
||||
return StatusCode(500, "Failed.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper that runs reCAPTCHA verification for the supplied token and
|
||||
/// returns the verdict along with the resolved user IP address.
|
||||
/// </summary>
|
||||
/// <param name="token">Client-provided reCAPTCHA token.</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
/// <returns>Tuple containing the verification verdict and user IP.</returns>
|
||||
private async Task<(CaptchaVerdictModel Verdict, string? UserIp)> ValidateCaptcha(string token, CancellationToken ct)
|
||||
{
|
||||
var userIp = HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
var verdict = await _captcha.VerifyAsync(token, userIp, ct);
|
||||
if (!verdict.Success)
|
||||
{
|
||||
_log.LogWarning("Captcha failed. ip={Ip} score={Score} err={Err}",
|
||||
userIp, verdict.Score, verdict.Error);
|
||||
}
|
||||
return (verdict, userIp);
|
||||
}
|
||||
// Captcha verification helper was moved to CaptchaController; ContactController calls _captcha.VerifyAsync directly.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user