Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Minor improvements with GoogleReCaptchaValidator

KrzysztofPajak37268873dd8e
diff --git a/src/Web/Grand.Web.Common/Filters/ValidateCaptchaAttribute.cs b/src/Web/Grand.Web.Common/Filters/ValidateCaptchaAttribute.cs
index c806ade1a..4faa9da05 100644
--- a/src/Web/Grand.Web.Common/Filters/ValidateCaptchaAttribute.cs
+++ b/src/Web/Grand.Web.Common/Filters/ValidateCaptchaAttribute.cs
@@ -17,7 +17,7 @@ namespace Grand.Web.Common.Filters
/// <param name="actionParameterName">The name of the action parameter to which the result will be passed</param>
public ValidateCaptchaAttribute(string actionParameterName = "captchaValid") : base(typeof(ValidateCaptchaFilter))
{
- this.Arguments = new object[] { actionParameterName };
+ Arguments = new object[] { actionParameterName };
}
#region Filter
@@ -40,15 +40,17 @@ namespace Grand.Web.Common.Filters
private readonly string _actionParameterName;
private readonly CaptchaSettings _captchaSettings;
+ private readonly GoogleReCaptchaValidator _googleReCaptchaValidator;
#endregion
#region Ctor
- public ValidateCaptchaFilter(string actionParameterName, CaptchaSettings captchaSettings)
+ public ValidateCaptchaFilter(string actionParameterName, CaptchaSettings captchaSettings, GoogleReCaptchaValidator googleReCaptchaValidator)
{
_actionParameterName = actionParameterName;
_captchaSettings = captchaSettings;
+ _googleReCaptchaValidator = googleReCaptchaValidator;
}
#endregion
@@ -80,17 +82,8 @@ namespace Grand.Web.Common.Filters
if ((!StringValues.IsNullOrEmpty(captchaChallengeValue) && !StringValues.IsNullOrEmpty(captchaResponseValue)) || !string.IsNullOrEmpty(gCaptchaResponseValue))
{
- //create CAPTCHA validator
- var captchaValidtor = new GoogleReCaptchaValidator(_captchaSettings.ReCaptchaVersion)
- {
- SecretKey = _captchaSettings.ReCaptchaPrivateKey,
- RemoteIp = context.HttpContext.Connection.RemoteIpAddress?.ToString(),
- Response = !StringValues.IsNullOrEmpty(captchaResponseValue) ? captchaResponseValue.ToString() : gCaptchaResponseValue,
- Challenge = captchaChallengeValue
- };
-
- //validate request
- var recaptchaResponse = await captchaValidtor.Validate();
+ //Captcha validate request
+ var recaptchaResponse = await _googleReCaptchaValidator.Validate(!StringValues.IsNullOrEmpty(captchaResponseValue) ? captchaResponseValue.ToString() : gCaptchaResponseValue);
isValid = recaptchaResponse.IsValid;
if (!isValid)
foreach (var error in recaptchaResponse.ErrorCodes)
diff --git a/src/Web/Grand.Web.Common/Security/Captcha/GoogleReCaptchaValidator.cs b/src/Web/Grand.Web.Common/Security/Captcha/GoogleReCaptchaValidator.cs
index 04e073146..4de200958 100644
--- a/src/Web/Grand.Web.Common/Security/Captcha/GoogleReCaptchaValidator.cs
+++ b/src/Web/Grand.Web.Common/Security/Captcha/GoogleReCaptchaValidator.cs
@@ -1,4 +1,5 @@
-using Newtonsoft.Json.Linq;
+using Microsoft.AspNetCore.Http;
+using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Net.Http;
@@ -10,29 +11,30 @@ namespace Grand.Web.Common.Security.Captcha
{
private const string RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}&remoteip={2}";
- public string SecretKey { get; set; }
- public string RemoteIp { get; set; }
- public string Response { get; set; }
- public string Challenge { get; set; }
+ private readonly CaptchaSettings _captchaSettings;
+ private readonly IHttpContextAccessor _httpContextAccessor;
+ private readonly HttpClient _httpClient;
- private readonly GoogleReCaptchaVersion _version;
-
- public GoogleReCaptchaValidator(GoogleReCaptchaVersion version = GoogleReCaptchaVersion.V2)
+ public GoogleReCaptchaValidator(CaptchaSettings captchaSettings,
+ IHttpContextAccessor httpContextAccessor,
+ HttpClient httpClient)
{
- _version = version;
+ _captchaSettings = captchaSettings;
+ _httpContextAccessor = httpContextAccessor;
+ _httpClient = httpClient;
}
- public async Task<GoogleReCaptchaResponse> Validate()
+ public async Task<GoogleReCaptchaResponse> Validate(string response)
{
GoogleReCaptchaResponse result = null;
- var httpClient = new HttpClient();
- var requestUri = string.Format(RECAPTCHA_VERIFY_URL, SecretKey, Response, RemoteIp);
+ var requestUri = string.Format(RECAPTCHA_VERIFY_URL, _captchaSettings.ReCaptchaPrivateKey, response,
+ _httpContextAccessor.HttpContext.Connection.RemoteIpAddress?.ToString());
try
{
- var response = await httpClient.GetAsync(requestUri);
- response.EnsureSuccessStatusCode();
- var contentresult = await response.Content.ReadAsStringAsync();
+ var responseMessage = await _httpClient.GetAsync(requestUri);
+ responseMessage.EnsureSuccessStatusCode();
+ var contentresult = await responseMessage.Content.ReadAsStringAsync();
result = ParseResponseResult(contentresult);
}
@@ -41,11 +43,7 @@ namespace Grand.Web.Common.Security.Captcha
result = new GoogleReCaptchaResponse { IsValid = false };
result.ErrorCodes.Add("Unknown error" + exc.Message);
}
- finally
- {
- httpClient.Dispose();
- }
-
+
return result;
}
diff --git a/src/Web/Grand.Web.Common/Startup/StartupApplication.cs b/src/Web/Grand.Web.Common/Startup/StartupApplication.cs
index a4a48f6a7..dafb4ae8e 100644
--- a/src/Web/Grand.Web.Common/Startup/StartupApplication.cs
+++ b/src/Web/Grand.Web.Common/Startup/StartupApplication.cs
@@ -12,6 +12,7 @@ using Grand.Web.Common.Localization;
using Grand.Web.Common.Middleware;
using Grand.Web.Common.Page;
using Grand.Web.Common.Routing;
+using Grand.Web.Common.Security.Captcha;
using Grand.Web.Common.TagHelpers;
using Grand.Web.Common.Themes;
using Microsoft.AspNetCore.Builder;
@@ -138,6 +139,9 @@ namespace Grand.Web.Common.Startup
//powered by
serviceCollection.AddSingleton<IPoweredByMiddlewareOptions, PoweredByMiddlewareOptions>();
+
+ //request reCAPTCHA service
+ serviceCollection.AddHttpClient<GoogleReCaptchaValidator>();
}
}