Skip to content

Commit 3b3c61b

Browse files
committed
feat: recaptcha support for VueForms
1 parent bee2956 commit 3b3c61b

File tree

6 files changed

+38
-11
lines changed

6 files changed

+38
-11
lines changed

src/Lib/StatCan.OrchardCore.Security/SecurityExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static IApplicationBuilder UseStatCanSecurityHeaders(this IApplicationBui
4040
.UnsafeEval() // for vue-js in oc admin
4141
.UnsafeInline() // for oc admin
4242
.Self()
43+
.From("https://www.google.com/recaptcha/").From("https://www.gstatic.com/recaptcha/") //recaptcha
4344
.From("cdn.jsdelivr.net")
4445
.From("code.jquery.com")
4546
.From("ajax.googleapis.com")
@@ -56,7 +57,9 @@ public static IApplicationBuilder UseStatCanSecurityHeaders(this IApplicationBui
5657
.From("*.demdex.net") // adobe analytics
5758
.From("cm.everesttech.net") // adobe analytics
5859
.From("*.adobe.com"); // adobe analytics
59-
builder.AddFrameSource().Self().From("canada.demdex.net"); // adobe analytics
60+
builder.AddFrameSource().Self()
61+
.From("canada.demdex.net")// adobe analytics
62+
.From("https://www.google.com/recaptcha/");
6063
});
6164

6265
if (!env.IsDevelopment())

src/Modules/StatCan.OrchardCore.Scripting/FormsGlobalMethodsProvider.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
using Newtonsoft.Json.Linq;
77
using OrchardCore.Infrastructure.Html;
88
using OrchardCore.Scripting;
9+
using OrchardCore.ReCaptcha.Services;
910

1011
namespace StatCan.OrchardCore.Scripting
1112
{
1213
public class FormsGlobalMethodsProvider : IGlobalMethodProvider
1314
{
1415
private readonly GlobalMethod _formAsJsonObject;
16+
private readonly GlobalMethod _validateReCaptcha;
1517

1618
public FormsGlobalMethodsProvider()
1719
{
@@ -37,11 +39,19 @@ public FormsGlobalMethodsProvider()
3739
}
3840
)
3941
};
42+
_validateReCaptcha = new GlobalMethod
43+
{
44+
Name = "validateReCaptcha",
45+
Method = serviceProvider => (Func<string, bool>)((reCaptchaResponse) => {
46+
var recaptchaService = serviceProvider.GetRequiredService<ReCaptchaService>();
47+
return recaptchaService.VerifyCaptchaResponseAsync(reCaptchaResponse).GetAwaiter().GetResult();
48+
})
49+
};
4050
}
4151

4252
public IEnumerable<GlobalMethod> GetMethods()
4353
{
44-
return new[] { _formAsJsonObject };
54+
return new[] { _formAsJsonObject, _validateReCaptcha };
4555
}
4656
}
4757
}

src/Modules/StatCan.OrchardCore.Scripting/StatCan.OrchardCore.Scripting.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="OrchardCore.ReCaptcha.Core" Version="$(OrchardCoreVersion)" />
1011
<PackageReference Include="OrchardCore.Abstractions" Version="$(OrchardCoreVersion)" />
1112
<PackageReference Include="OrchardCore.Infrastructure.Abstractions" Version="$(OrchardCoreVersion)" />
1213
<PackageReference Include="OrchardCore.ContentManagement" Version="$(OrchardCoreVersion)" />

src/Modules/StatCan.OrchardCore.VueForms/Assets/vue-forms.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,19 @@ function initForm(app) {
6262
observer.validate().then((valid) => {
6363
if (valid) {
6464
const action = vm.$refs.form.getAttribute("action");
65-
var token = vm.$refs.form.querySelector('input[name="__RequestVerificationToken"]');;
65+
let frmData = {...vm.$data};
66+
frmData.__RequestVerificationToken = vm.$refs.form.querySelector('input[name="__RequestVerificationToken"]').value;
67+
if(typeof(grecaptcha) == 'object')
68+
{
69+
frmData.recaptcha = grecaptcha.getResponse();
70+
}
71+
6672
vm.form.submitting = true;
6773

6874
$.ajax({
6975
type: "POST",
7076
url: action,
71-
data: {...vm.$data, __RequestVerificationToken: token.value },
77+
data: frmData,
7278
cache: false,
7379
dataType: "json",
7480
success: function (data) {

src/Modules/StatCan.OrchardCore.VueForms/wwwroot/Scripts/vue-forms.js

Lines changed: 13 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Modules/StatCan.OrchardCore.VueForms/wwwroot/Scripts/vue-forms.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)