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

Add expired-callback and error-callback to the V2 invisible widget #69

Merged
merged 2 commits into from
Jul 20, 2024
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
11 changes: 10 additions & 1 deletion AspNetCore.ReCaptcha.Tests/ReCaptchaGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ public void ReCaptchaGeneratorReturnsReCaptchaForV2()
var result = ReCaptchaGenerator.ReCaptchaV2(new Uri("https://www.google.com/recaptcha/"), "test", "test", "test", "test", "test", "test", "test", false, "nonce");

Assert.NotNull(result);
Assert.Equal(@"<div class=""g-recaptcha"" data-sitekey=""test"" data-size=""test"" data-theme=""test"" data-callback=""test"" data-error-callback=""test"" data-expired-callback=""test""></div>
<script src=""https://www.google.com/recaptcha/api.js?hl=test"" defer></script>",
result.ToHtmlString());
}

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

Assert.NotNull(result);
Assert.Equal(@"<button class=""g-recaptcha test"" data-sitekey=""test"" data-badge=""test"" data-callback=""test"" data-expired-callback=""test"" data-error-callback=""test"">test</button>
<script src=""https://www.google.com/recaptcha/api.js?hl=test"" defer></script>", result.ToHtmlString());
}

[Fact]
Expand All @@ -28,6 +33,10 @@ public void ReCaptchaGeneratorReturnsReCaptchaForV3()
var result = ReCaptchaGenerator.ReCaptchaV3(new Uri("https://www.google.com/recaptcha/"), "test", "test", "test", "test", 1, "nonce");

Assert.NotNull(result);
Assert.Equal(
@"<input id=""g-recaptcha-response-1"" name=""g-recaptcha-response"" type=""hidden"" value="""" /><script src=""https://www.google.com/recaptcha/api.js?render=test&hl=test""></script><script nonce=""nonce"">function updateReCaptcha1() {grecaptcha.execute('test', {action: 'test'}).then(function(token){document.getElementById('g-recaptcha-response-1').value = token;});}grecaptcha.ready(function() {setInterval(updateReCaptcha1, 100000); updateReCaptcha1()});</script>
",
result.ToHtmlString());
}

[Theory]
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>
<TargetFramework>net6.0</TargetFramework>
<PackageId>AspNetCore.ReCaptcha</PackageId>
<Version>1.8.1</Version>
<Version>1.8.2</Version>
<Authors>Michaelvs97,sleeuwen</Authors>
<Description>Google ReCAPTCHA v2/v3 Library for .NET 6 and above</Description>
<PackageDescription>Google ReCAPTCHA v2/v3 Library for .NET 6 and above</PackageDescription>
Expand Down
14 changes: 11 additions & 3 deletions AspNetCore.ReCaptcha/ReCaptchaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,21 @@ public static IHtmlContent ReCaptchaV2(Uri baseUrl, string siteKey, string size,
return content;
}

public static IHtmlContent ReCaptchaV2Invisible(Uri baseUrl, 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 errorCallback, string expiredCallback, string badge)
{
var content = new HtmlContentBuilder();
content.AppendFormat(@"<button class=""g-recaptcha {0}""", className);
content.AppendFormat(@" data-sitekey=""{0}""", siteKey);
content.AppendFormat(@" data-callback=""{0}""", callback);
content.AppendFormat(@" data-badge=""{0}""", badge);

if (!string.IsNullOrEmpty(badge))
content.AppendFormat(@" data-badge=""{0}""", badge);
if (!string.IsNullOrEmpty(callback))
content.AppendFormat(@" data-callback=""{0}""", callback);
if (!string.IsNullOrEmpty(expiredCallback))
content.AppendFormat(@" data-expired-callback=""{0}""", expiredCallback);
if (!string.IsNullOrEmpty(errorCallback))
content.AppendFormat(@" data-error-callback=""{0}""", errorCallback);

content.AppendFormat(@">{0}</button>", text);
content.AppendLine();

Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha/ReCaptchaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static IHtmlContent ReCaptcha(
case ReCaptchaVersion.V2:
return ReCaptchaGenerator.ReCaptchaV2(staticSettings.RecaptchaBaseUrl, settings.SiteKey, size, theme, language, callback, errorCallback, expiredCallback, autoTheme, nonce);
case ReCaptchaVersion.V2Invisible:
return ReCaptchaGenerator.ReCaptchaV2Invisible(staticSettings.RecaptchaBaseUrl, settings.SiteKey, text, className, language, callback, badge);
return ReCaptchaGenerator.ReCaptchaV2Invisible(staticSettings.RecaptchaBaseUrl, settings.SiteKey, text, className, language, callback, errorCallback, expiredCallback, badge);
case ReCaptchaVersion.V3:
return ReCaptchaGenerator.ReCaptchaV3(staticSettings.RecaptchaBaseUrl, settings.SiteKey, action, language, callback, ReCaptchaGenerator.GenerateId(helper.ViewContext), nonce);
}
Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha/ReCaptchaTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)

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