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

Read recaptcha secret from web.config #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 12 additions & 5 deletions Perplex.Umbraco.Forms/FieldTypes/PerplexRecaptcha.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using Newtonsoft.Json.Linq;
using PerplexUmbraco.Forms.Code.Configuration;
using System;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Web;
using System.Linq;

using Newtonsoft.Json.Linq;

using PerplexUmbraco.Forms.Code.Configuration;

using Umbraco.Core.Logging;
using Umbraco.Forms.Core;

using static PerplexUmbraco.Forms.Code.Constants;


namespace PerplexUmbraco.Forms.Code.Recaptcha
{
public class PerplexRecaptcha : FieldType
Expand All @@ -32,21 +38,22 @@ public PerplexRecaptcha()
public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContextBase context)
{
var secretKey = Umbraco.Forms.Core.Configuration.GetSetting("RecaptchaPrivateKey");
var recaptchaPrivateKey = ConfigurationManager.AppSettings["RecaptchaSecretKey"] ?? secretKey;

// Get configured error message, either from this field or the XML configuration file.
// The ErrorMessage property is empty here, for some reason.
string fieldError = FieldTypeHelpers.GetSettingValue(field, nameof(ErrorMessage));
string errorMsg = fieldError?.Length > 0 ? fieldError: PerplexUmbracoFormsConfig.Get.PerplexRecaptchaConfig?.ErrorMessage;

if (string.IsNullOrEmpty(secretKey))
if (string.IsNullOrEmpty(recaptchaPrivateKey))
{
// just return the error message
LogHelper.Warn<UmbracoEvents>("ERROR: ReCaptcha v.2 is missing the Secret Key - Please update the '/app_plugins/umbracoforms/umbracoforms.config' to include 'key=\"RecaptchaPrivateKey\"'");
return new[] { errorMsg };
}

var reCaptchaResponse = context.Request["g-recaptcha-response"];
var url = $"https://www.google.com/recaptcha/api/siteverify?secret={secretKey}&response={reCaptchaResponse}";
var url = $"https://www.google.com/recaptcha/api/siteverify?secret={recaptchaPrivateKey}&response={reCaptchaResponse}";

var isSuccess = false;
var errorCodes = new List<string>();
Expand Down
1 change: 1 addition & 0 deletions Perplex.Umbraco.Forms/Perplex.Umbraco.Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<HintPath>..\packages\UmbracoCms.Core.7.6.0\lib\net45\SQLCE4Umbraco.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SqlServerCe, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<HintPath>..\packages\UmbracoCms.Core.7.6.0\lib\net45\System.Data.SqlServerCe.dll</HintPath>
Expand Down