Skip to content

Commit

Permalink
Merge pull request #30 from michaelvs97/fix-recaptcha-net
Browse files Browse the repository at this point in the history
Fix url for recaptcha.net and fix the generated js url to use recaptc…
  • Loading branch information
sleeuwen committed Feb 3, 2022
2 parents b17f6ae + 66346b0 commit 6109fe3
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
9 changes: 5 additions & 4 deletions AspNetCore.ReCaptcha.Tests/ReCaptchaGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Html;
using System;
using Microsoft.AspNetCore.Html;
using Xunit;

namespace AspNetCore.ReCaptcha.Tests
Expand All @@ -8,23 +9,23 @@ public class ReCaptchaGeneratorTests
[Fact]
public void ReCaptchaGeneratorReturnsReCaptchaForV2()
{
var result = ReCaptchaGenerator.ReCaptchaV2("test", "test", "test", "test", "test", "test", "test");
var result = ReCaptchaGenerator.ReCaptchaV2(new Uri("https://www.google.com/recaptcha/"), "test", "test", "test", "test", "test", "test", "test");

Assert.NotNull(result);
}

[Fact]
public void ReCaptchaGeneratorReturnsReCaptchaForV2Invisible()
{
var result = ReCaptchaGenerator.ReCaptchaV2Invisible("test", "test", "test", "test", "test", "test");
var result = ReCaptchaGenerator.ReCaptchaV2Invisible(new Uri("https://www.google.com/recaptcha/"), "test", "test", "test", "test", "test", "test");

Assert.NotNull(result);
}

[Fact]
public void ReCaptchaGeneratorReturnsReCaptchaForV3()
{
var result = ReCaptchaGenerator.ReCaptchaV3("test", "test", "test", "test");
var result = ReCaptchaGenerator.ReCaptchaV3(new Uri("https://www.google.com/recaptcha/"), "test", "test", "test", "test");

Assert.NotNull(result);
}
Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha.Tests/ReCaptchaHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void AddReCaptcha_UseRecaptchaNet()

Assert.Equal(ReCaptchaVersion.V2, settings.Version);
Assert.True(settings.UseRecaptchaNet);
Assert.Equal("https://www.recaptcha.net/", settings.RecaptchaBaseUrl.ToString());
Assert.Equal("https://www.recaptcha.net/recaptcha/", settings.RecaptchaBaseUrl.ToString());
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha/AspNetCore.ReCaptcha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<PackageId>AspNetCore.ReCaptcha</PackageId>
<Version>1.5.0</Version>
<Version>1.5.1</Version>
<Authors>Michaelvs97,sleeuwen</Authors>
<Description>Google ReCAPTCHA v2/v3 Library for .NET Core 3.1 and .NET 5.0/6.0</Description>
<PackageDescription>Google ReCAPTCHA v2/v3 Library for .NET Core 3.1 and .NET 5.0/6.0</PackageDescription>
Expand Down
13 changes: 7 additions & 6 deletions AspNetCore.ReCaptcha/ReCaptchaGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using Microsoft.AspNetCore.Html;

namespace AspNetCore.ReCaptcha
{
internal static class ReCaptchaGenerator
{
public static IHtmlContent ReCaptchaV2(string siteKey, string size, string theme, string language, string callback, string errorCallback, string expiredCallback)
public static IHtmlContent ReCaptchaV2(Uri baseUrl, string siteKey, string size, string theme, string language, string callback, string errorCallback, string expiredCallback)
{
var content = new HtmlContentBuilder();
content.AppendFormat(@"<div class=""g-recaptcha"" data-sitekey=""{0}""", siteKey);
Expand All @@ -22,12 +23,12 @@ public static IHtmlContent ReCaptchaV2(string siteKey, string size, string theme

content.AppendFormat("></div>");
content.AppendLine();
content.AppendFormat(@"<script src=""https://www.google.com/recaptcha/api.js?hl={0}"" defer></script>", language);
content.AppendFormat(@"<script src=""{0}api.js?hl={1}"" defer></script>", baseUrl, language);

return content;
}

public static IHtmlContent ReCaptchaV2Invisible(string siteKey, string text, string className, string language, string callback, string badge)
public static IHtmlContent ReCaptchaV2Invisible(Uri baseUrl, string siteKey, string text, string className, string language, string callback, string badge)
{
var content = new HtmlContentBuilder();
content.AppendFormat(@"<button class=""g-recaptcha {0}""", className);
Expand All @@ -37,16 +38,16 @@ public static IHtmlContent ReCaptchaV2Invisible(string siteKey, string text, str
content.AppendFormat(@">{0}</button>", text);
content.AppendLine();

content.AppendFormat(@"<script src=""https://www.google.com/recaptcha/api.js?hl={0}"" defer></script>", language);
content.AppendFormat(@"<script src=""{0}api.js?hl={1}"" defer></script>", baseUrl, language);

return content;
}

public static IHtmlContent ReCaptchaV3(string siteKey, string action, string language, string callBack)
public static IHtmlContent ReCaptchaV3(Uri baseUrl, string siteKey, string action, string language, string callBack)
{
var content = new HtmlContentBuilder();
content.AppendHtml(@"<input id=""g-recaptcha-response"" name=""g-recaptcha-response"" type=""hidden"" value="""" />");
content.AppendFormat(@"<script src=""https://www.google.com/recaptcha/api.js?render={0}&hl={1}""></script>", siteKey, language);
content.AppendFormat(@"<script src=""{0}api.js?render={1}&hl={2}""></script>", baseUrl, siteKey, language);
content.AppendHtml("<script>");
content.AppendHtml("function updateReCaptcha() {");
content.AppendFormat("grecaptcha.execute('{0}', {{action: '{1}'}}).then(function(token){{", siteKey, action);
Expand Down
6 changes: 3 additions & 3 deletions AspNetCore.ReCaptcha/ReCaptchaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ public static IHtmlContent ReCaptcha(
{
default:
case ReCaptchaVersion.V2:
return ReCaptchaGenerator.ReCaptchaV2(settings.SiteKey, size, theme, language, callback, errorCallback, expiredCallback);
return ReCaptchaGenerator.ReCaptchaV2(settings.RecaptchaBaseUrl, settings.SiteKey, size, theme, language, callback, errorCallback, expiredCallback);
case ReCaptchaVersion.V2Invisible:
return ReCaptchaGenerator.ReCaptchaV2Invisible(settings.SiteKey, text, className, language, callback, badge);
return ReCaptchaGenerator.ReCaptchaV2Invisible(settings.RecaptchaBaseUrl, settings.SiteKey, text, className, language, callback, badge);
case ReCaptchaVersion.V3:
return ReCaptchaGenerator.ReCaptchaV3(settings.SiteKey, action, language, callback);
return ReCaptchaGenerator.ReCaptchaV3(settings.RecaptchaBaseUrl, settings.SiteKey, action, language, callback);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha/ReCaptchaSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace AspNetCore.ReCaptcha
public class ReCaptchaSettings
{
internal static readonly Uri GoogleReCaptchaBaseUrl = new Uri("https://www.google.com/recaptcha/");
internal static readonly Uri RecaptchaNetBaseUrl = new Uri("https://www.recaptcha.net/");
internal static readonly Uri RecaptchaNetBaseUrl = new Uri("https://www.recaptcha.net/recaptcha/");

public string SiteKey { get; set; }
public string SecretKey { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions AspNetCore.ReCaptcha/ReCaptchaTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public override void Process(TagHelperContext context, TagHelperOutput output)

var content = settings.Version switch
{
ReCaptchaVersion.V2Invisible => ReCaptchaGenerator.ReCaptchaV2Invisible(settings.SiteKey, Text, ClassName, Language, Callback, Badge),
ReCaptchaVersion.V3 => ReCaptchaGenerator.ReCaptchaV3(settings.SiteKey, Action, Language, Callback),
_ => ReCaptchaGenerator.ReCaptchaV2(settings.SiteKey, Size, Theme, Language, Callback, ErrorCallback, ExpiredCallback),
ReCaptchaVersion.V2Invisible => ReCaptchaGenerator.ReCaptchaV2Invisible(settings.RecaptchaBaseUrl, settings.SiteKey, Text, ClassName, Language, Callback, Badge),
ReCaptchaVersion.V3 => ReCaptchaGenerator.ReCaptchaV3(settings.RecaptchaBaseUrl, settings.SiteKey, Action, Language, Callback),
_ => ReCaptchaGenerator.ReCaptchaV2(settings.RecaptchaBaseUrl, settings.SiteKey, Size, Theme, Language, Callback, ErrorCallback, ExpiredCallback),
};

output.Content.AppendHtml(content);
Expand Down

0 comments on commit 6109fe3

Please sign in to comment.