Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding RecaptchaApiSource to RecaptchaConfiguration. #61

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Recaptcha.Web-net45/Configuration/RecaptchaConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Recaptcha.Web.Configuration
/// </summary>
public class RecaptchaConfiguration
{
private const string DEFAULT_API_SOURCE = "www.google.com/recaptcha";

/// <summary>
/// Creates an instance of the <see cref="RecaptchaConfiguration"/> class.
/// </summary>
Expand All @@ -20,8 +22,9 @@ public class RecaptchaConfiguration
/// <param name="language">Forces the reCAPTCHA widget to render in a specific language. By default, the user's language is used.</param>
/// <param name="size">The size of the reCAPTCHA widget.</param>
/// <param name="useSsl">Determines if SSL is to be used in Google reCAPTCHA API calls.</param>
/// <param name="apiSource">The reCAPTCHA source url used by the widget. Default is "www.google.com/recaptcha". Do not include schema or api.js , use UseSsl to specify https://.
///
public RecaptchaConfiguration(string siteKey, string secretKey, string apiVersion, string language = null, RecaptchaTheme theme = RecaptchaTheme.Default, RecaptchaSize size= RecaptchaSize.Default, RecaptchaSslBehavior useSsl = RecaptchaSslBehavior.AlwaysUseSsl)
public RecaptchaConfiguration(string siteKey, string secretKey, string apiVersion, string language = null, RecaptchaTheme theme = RecaptchaTheme.Default, RecaptchaSize size = RecaptchaSize.Default, RecaptchaSslBehavior useSsl = RecaptchaSslBehavior.AlwaysUseSsl, string apiSource = DEFAULT_API_SOURCE)
{
SiteKey = siteKey;
SecretKey = secretKey;
Expand All @@ -30,6 +33,7 @@ public RecaptchaConfiguration(string siteKey, string secretKey, string apiVersio
Theme = theme;
Size = size;
UseSsl = useSsl;
ApiSource = apiSource;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line must be updated because otherwise a null value will be used when the appsetting not present in the configuration.

ApiSource = string.IsNullOrEmpty(apiSource) ? DEFAULT_API_SOURCE : apiSource;

}

/// <summary>
Expand Down Expand Up @@ -94,5 +98,14 @@ public RecaptchaSize Size
get;
private set;
}

/// <summary>
/// The reCAPTCHA source url used by the widget. Default is "www.google.com/recaptcha". Do not include schema or api.js , use UseSsl to specify https://.
/// </summary>
public string ApiSource
{
get;
private set;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public static class RecaptchaConfigurationManager
/// <returns>Returns configuration as an instance of the <see cref="RecaptchaConfiguration"/> class.</returns>
public static RecaptchaConfiguration GetConfiguration()
{
string siteKey = null, secretKey = null, language=null, apiVersion="2";
string siteKey = null, secretKey = null, language = null, apiVersion = "2", apiSource = null;
RecaptchaSize size = RecaptchaSize.Default;
RecaptchaTheme theme = RecaptchaTheme.Default;
RecaptchaSslBehavior useSsl = RecaptchaSslBehavior.AlwaysUseSsl;

if(ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaSiteKey"))
if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaSiteKey"))
{
siteKey = ConfigurationManager.AppSettings["RecaptchaSiteKey"];
}
Expand Down Expand Up @@ -63,7 +63,12 @@ public static RecaptchaConfiguration GetConfiguration()
Enum.TryParse<RecaptchaSslBehavior>(ConfigurationManager.AppSettings["RecaptchaUseSsl"], out useSsl);
}

return new RecaptchaConfiguration(siteKey, secretKey, apiVersion, language, theme, size, useSsl);
if (ConfigurationManager.AppSettings.AllKeys.Contains("RecaptchaApiSource"))
{
apiSource = ConfigurationManager.AppSettings["RecaptchaApiSource"];
}

return new RecaptchaConfiguration(siteKey, secretKey, apiVersion, language, theme, size, useSsl, apiSource);
}
}
}
5 changes: 3 additions & 2 deletions src/Recaptcha.Web-net45/Recaptcha2HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* =========================================================================================================================== */

using Recaptcha.Web.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -92,7 +93,7 @@ public string CreateWidgetHtml(bool renderApiScript, RecaptchaTheme theme, strin
}

var sbAttributes = new StringBuilder();
foreach(var key in dictAttributes.Keys)
foreach (var key in dictAttributes.Keys)
{
sbAttributes.Append($"{key}=\"{dictAttributes[key]}\" ");
}
Expand Down Expand Up @@ -140,7 +141,7 @@ public string CreateApiScripttHtml(string language, RecaptchaSslBehavior useSsl)
}

var dictQS = new Dictionary<string, string>();
var url = $"{protocol}www.google.com/recaptcha/api.js";
var url = $"{protocol}{RecaptchaConfigurationManager.GetConfiguration().ApiSource}/api.js";

if (!string.IsNullOrEmpty(language))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Recaptcha.Web-net45/RecaptchaVerificationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private Task<RecaptchaVerificationResult> VerifyRecpatcha2ResponseTaskAsync(stri

Uri verifyUri = null;

verifyUri = new Uri("https://www.google.com/recaptcha/api/siteverify", UriKind.Absolute);
verifyUri = new Uri($"https://{RecaptchaConfigurationManager.GetConfiguration().ApiSource}/api/siteverify", UriKind.Absolute);

try
{
Expand Down Expand Up @@ -195,7 +195,7 @@ private RecaptchaVerificationResult VerifyRecpatcha2Response(string secretKey)
string postData = String.Format("secret={0}&response={1}&remoteip={2}", secretKey, this.Response, this.UserHostAddress);

byte[] postDataBuffer = System.Text.Encoding.ASCII.GetBytes(postData);
Uri verifyUri = new Uri("https://www.google.com/recaptcha/api/siteverify", UriKind.Absolute);
Uri verifyUri = new Uri($"https://{RecaptchaConfigurationManager.GetConfiguration().ApiSource}/api/siteverify", UriKind.Absolute);
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(verifyUri);
Expand Down
3 changes: 2 additions & 1 deletion src/Recaptcha.Web-netcoreapp3.1/Recaptcha2HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* =========================================================================================================================== */

using Microsoft.AspNetCore.Http;
using Recaptcha.Web.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -157,7 +158,7 @@ public string CreateApiScripttHtml(string language, RecaptchaSslBehavior useSsl)
}

var dictQS = new Dictionary<string, string>();
var url = $"{protocol}www.google.com/recaptcha/api.js";
var url = $"{protocol}{RecaptchaConfigurationManager.GetConfiguration().ApiSource}/api.js";

if (!string.IsNullOrEmpty(language))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private Task<RecaptchaVerificationResult> VerifyRecpatcha2ResponseTaskAsync(stri

Uri verifyUri = null;

verifyUri = new Uri("https://www.google.com/recaptcha/api/siteverify", UriKind.Absolute);
verifyUri = new Uri($"https://{RecaptchaConfigurationManager.GetConfiguration().ApiSource}/siteverify", UriKind.Absolute);

try
{
Expand Down Expand Up @@ -196,7 +196,7 @@ private RecaptchaVerificationResult VerifyRecpatcha2Response(string secretKey)
string postData = String.Format("secret={0}&response={1}&remoteip={2}", secretKey, this.Response, this.UserHostAddress);

byte[] postDataBuffer = System.Text.Encoding.ASCII.GetBytes(postData);
Uri verifyUri = new Uri("https://www.google.com/recaptcha/api/siteverify", UriKind.Absolute);
Uri verifyUri = new Uri($"https://{RecaptchaConfigurationManager.GetConfiguration().ApiSource}/siteverify", UriKind.Absolute);
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(verifyUri);
Expand Down