mirror of
https://github.com/bitwarden/server
synced 2025-12-25 04:33:26 +00:00
* Revert "Add git blame entry (#2226)" This reverts commit239286737d. * Revert "Turn on file scoped namespaces (#2225)" This reverts commit34fb4cca2a.
38 lines
1.6 KiB
C#
38 lines
1.6 KiB
C#
using Bit.Core.Context;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Models.Api;
|
|
using Bit.Core.Services;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Bit.Core.Utilities
|
|
{
|
|
public class CaptchaProtectedAttribute : ActionFilterAttribute
|
|
{
|
|
public string ModelParameterName { get; set; } = "model";
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
|
{
|
|
var currentContext = context.HttpContext.RequestServices.GetRequiredService<ICurrentContext>();
|
|
var captchaValidationService = context.HttpContext.RequestServices.GetRequiredService<ICaptchaValidationService>();
|
|
|
|
if (captchaValidationService.RequireCaptchaValidation(currentContext, null))
|
|
{
|
|
var captchaResponse = (context.ActionArguments[ModelParameterName] as ICaptchaProtectedModel)?.CaptchaResponse;
|
|
|
|
if (string.IsNullOrWhiteSpace(captchaResponse))
|
|
{
|
|
throw new BadRequestException(captchaValidationService.SiteKeyResponseKeyName, captchaValidationService.SiteKey);
|
|
}
|
|
|
|
var captchaValidationResponse = captchaValidationService.ValidateCaptchaResponseAsync(captchaResponse,
|
|
currentContext.IpAddress, null).GetAwaiter().GetResult();
|
|
if (!captchaValidationResponse.Success || captchaValidationResponse.IsBot)
|
|
{
|
|
throw new BadRequestException("Captcha is invalid. Please refresh and try again");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|