private Item GetMatchingSetting(string hostName)
        {
            Item toReturn           = null;
            var  foundSettingsItems = GetCandidateSettings();

            if (foundSettingsItems != null && foundSettingsItems.Any())
            {
                foreach (var settingsItem in foundSettingsItems)
                {
                    var validHosts = settingsItem[FormsConstants.Templates.CaptchaV3Environment.Fields.Keys.HostNames]
                                     .Split('|')
                                     .ToList()
                                     .Select(x => x.Trim().ToLower());

                    if (validHosts.Contains(hostName.ToLower()))
                    {
                        toReturn = settingsItem;
                        break;
                    }
                }
            }
            else
            {
                GatedContentLogger.Error("Candidate Settings Not Found", this);
            }

            return(toReturn);
        }
示例#2
0
        public static bool SafeCheckboxValue(this Item item, ID fieldId)
        {
            var toReturn = false;

            if (item != null && !fieldId.IsNull)
            {
                CheckboxField checkboxField = item.Fields[fieldId];
                if (checkboxField != null)
                {
                    toReturn = checkboxField.Checked;
                }
            }
            else
            {
                if (item == null)
                {
                    GatedContentLogger.Error("item is null", new object());
                }

                if (fieldId.IsNull)
                {
                    GatedContentLogger.Error("fieldId is null", new object());
                }
            }
            return(toReturn);
        }
示例#3
0
        public static string SafeLinkFieldFriendlyUrl(this Item item, ID fieldId)
        {
            var toReturn = string.Empty;

            if (item != null && !fieldId.IsNull)
            {
                LinkField linkField = item.Fields[fieldId];
                if (linkField != null)
                {
                    toReturn = linkField.GetFriendlyUrl();
                }
            }
            else
            {
                if (item == null)
                {
                    GatedContentLogger.Error("item is null", new object());
                }
                if (fieldId.IsNull)
                {
                    GatedContentLogger.Error("fieldId is null", new object());
                }
            }

            return(toReturn);
        }
        public async Task<bool> IsCaptchaValid()
        {
            var toReturn = false;

            try
            {
                using (var httpClient = new HttpClient())
                {
                    var values = new Dictionary<string, string>
                {
                    {"secret",  CaptchaV3Environment.SecretKey},
                    {"response", GRecaptchaResponse },
                    {"remoteip", HttpContext.Current.Request.UserHostAddress }
                };

                    var content = new FormUrlEncodedContent(values);
                    var verify = await httpClient.PostAsync(FormsConstants.RecaptchaV3.SiteVerifyEndPoint, content);

                    var captchaResponseStr = await verify.Content.ReadAsStringAsync();

                    GatedContentLogger.Log.Info("Captcha Response:");
                    if (!string.IsNullOrEmpty(captchaResponseStr))
                    {
                        GatedContentLogger.Log.Info(captchaResponseStr);
                    }

                    var captchaResult = JsonConvert.DeserializeObject<RecapatchaResponseViewModel>(captchaResponseStr);

                    toReturn = captchaResult.Success
                        // todo - put back in?  && captchaResult.Action == "todo contact_us"
                        && captchaResult.Score > CaptchaV3Environment.MinPassScore;
                }
            }
            catch (System.Exception ex)
            {
                GatedContentLogger.Error(ex.ToString(), this);
            }

            if (CaptchaTestSettings.TestingForceCaptchaFail)
            {
                toReturn = false;
                GatedContentLogger.Log.Info("Return value has been forced to false from settings");
            }
            else
            if (CaptchaTestSettings.TestingForceCaptchaSuccess)
            {
                toReturn = true;
                GatedContentLogger.Log.Info("Return value has been forced to true from settings");
            }

            return toReturn;
        }
示例#5
0
        public async Task <ActionResult> GatedFormMarkup(FormCollection formCollection)
        {
            GatedContentLogger.Log.Info("[HttpPost] GatedFormMarkup Hit");

            ActionResult toReturn = null;

            var formRelayer = new FormRelayer(formCollection, ModelState.IsValid);

            var captchaDataModel   = new GatedFormDataModel(CaptchaGateSettings, formRelayer.GateItemIdStr);
            var gatedFormViewModel = captchaDataModel.GetViewModel();

            if (formRelayer.DataIsValid && gatedFormViewModel.DataSourceItemProxy.IsValidForRendering)
            {
                formRelayer.TestingFormFormSubmitFail = gatedFormViewModel.DataSourceItemProxy.TestSettings.TestingForceFormSubmitFail;

                var CaptchaTestSettings = new CaptchaTestSettings(
                    gatedFormViewModel.DataSourceItemProxy.TestSettings.TestingForceCaptchaFail,
                    gatedFormViewModel.DataSourceItemProxy.TestSettings.TestingForceCaptchaSuccess
                    );

                var captchaProxy = new CaptchaProxy(
                    formRelayer[FormsConstants.RecaptchaV3.TokenElemId],
                    CaptchaGateSettings.CaptchaV3EnvironmentSettings,
                    CaptchaTestSettings
                    );

                if (await captchaProxy.IsCaptchaValid())
                {
                    toReturn = await GetActionForCaptchaPass(formRelayer, gatedFormViewModel, gatedFormViewModel.FormProxy);
                }
                else
                {
                    toReturn = new RedirectResult(gatedFormViewModel.DataSourceItemProxy.OnCaptchFailUrlFriendly);;
                }
            }
            else
            {
                if (!formRelayer.DataIsValid)
                {
                    GatedContentLogger.Error("formCollection data is invalid", this);
                }

                if (!gatedFormViewModel.DataSourceItemProxy.IsValidForRendering)
                {
                    GatedContentLogger.Error("DataSourceProxy is invalid", this);
                }
            }
            return(toReturn);
        }
        private void LogSettings()
        {
            try
            {
                var recaptchaSettingsSerialized = JsonConvert.SerializeObject(CaptchaV3EnvironmentSettings, Formatting.Indented);
                GatedContentLogger.Log.Debug("Environment: " + recaptchaSettingsSerialized);

                var globalSerialized = JsonConvert.SerializeObject(GatedFormGlobalSettings, Formatting.Indented);
                GatedContentLogger.Log.Debug("Global: " + globalSerialized);
            }
            catch (System.Exception ex)
            {
                GatedContentLogger.Error(ex.ToString(), this);
            }
        }
示例#7
0
        public ActionResult PlainHtmlFormGated()
        {
            ActionResult toReturn  = null;
            var          dataModel = _captchaGateRepository.GetModel() as GatedFormDataModel;

            if (dataModel != null)
            {
                var viewModel = dataModel.GetViewModel();
                toReturn = View(viewModel);
            }
            else
            {
                GatedContentLogger.Error("Error converting view model", this);
            }
            return(toReturn);
        }
        internal CaptchaV3EnvironmentSettings GetEnvironmentSettings()
        {
            var detectedHost = Sitecore.Context.Site != null ? Sitecore.Context.Site.HostName : "{error - no host detected}";
            var settingItem  = GetMatchingSetting(detectedHost);

            CaptchaV3EnvironmentSettings toReturn = null;

            if (settingItem != null)
            {
                toReturn = new CaptchaV3EnvironmentSettings(settingItem, detectedHost);
            }
            else
            {
                GatedContentLogger.Error("Environment setting item is null. Expected a match for '" + detectedHost + "'", this);
                toReturn = new CaptchaV3EnvironmentSettings(null, detectedHost);
            }

            return(toReturn);
        }
示例#9
0
        private void ProcessAttributeAction()
        {
            HtmlAttribute actionAttribute = FormNode.Attributes["action"];

            if (actionAttribute != null)
            {
                OriginalAction = actionAttribute.Value;

                GatedContentLogger.Log.Debug("OriginalAction: " + OriginalAction);

                //AddToEEDiagnostics("Original Action: " + model.OriginalAction);

                actionAttribute.Value = FormsConstants.Endpoints.Nuaire.ApiGateEndpoint;
            }
            else
            {
                GatedContentLogger.Error("Missing Action attribute", this);
            }
        }
        public FormRelayer(FormCollection formCollection, bool isValid)
        {
            FormCollection   = formCollection;
            FormModelIsValid = isValid;

            if (GatedContentLogger.Log.IsInfoEnabled)
            {
                try
                {
                    var receivedFormCollection = JsonConvert.SerializeObject(FormCollection, Formatting.Indented);

                    GatedContentLogger.Log.Info("Received FormCollection: " + receivedFormCollection);
                }
                catch (System.Exception ex)
                {
                    GatedContentLogger.Error(ex.ToString(), this);
                }
            }
        }
        private List <Item> GetCandidateSettings()
        {
            var toReturn = new List <Item>();

            var settingsFolder = Sitecore.Context.Database.GetItem(FormsConstants.Content.CaptchaGateSettings.SettingsFolderItemId);

            if (settingsFolder != null)
            {
                toReturn = settingsFolder
                           .Children
                           .Where(x => x.TemplateID.Equals(FormsConstants.Templates.CaptchaV3Environment.SettingsTemplateID))
                           .ToList();
            }
            else
            {
                GatedContentLogger.Error("Settings folder is null. " + FormsConstants.Content.CaptchaGateSettings.SettingsFolderItemId.ToString(), this);
            }

            return(toReturn);
        }
示例#12
0
        public static string SafeStringGet(this Item item, ID fieldId)
        {
            var toReturn = string.Empty;

            if (item != null && !fieldId.IsNull)
            {
                toReturn = item[fieldId];
            }
            else
            {
                if (item == null)
                {
                    GatedContentLogger.Error("item is null", new object());
                }

                if (fieldId.IsNull)
                {
                    GatedContentLogger.Error("fieldId is null", new object());
                }
            }

            return(toReturn);
        }
示例#13
0
        private async Task <ActionResult> GetActionForCaptchaPass(FormRelayer formCollectionHelper, GatedFormViewModel viewModel, FormProxy formProxy)
        {
            ActionResult toReturn = null;

            if (viewModel != null)
            {
                if (viewModel.FormProxy != null)
                {
                    formCollectionHelper.CleanForm();

                    if (formProxy.OriginalMethod.Equals(FormsConstants.FormsHtml.Attributes.Post, StringComparison.OrdinalIgnoreCase))
                    {
                        var response = await formCollectionHelper.SubmitFormPostAsync(formProxy.OriginalAction);

                        if (response.IsSuccessStatusCode)
                        {
                            GatedContentLogger.Log.Debug("Setting redirect to: " + viewModel.DataSourceItemProxy.OnFormSubmitSuccess);
                            toReturn = new RedirectResult(viewModel.DataSourceItemProxy.OnFormSubmitSuccess);
                        }
                        else
                        {
                            GatedContentLogger.Error(response.ToString(), this);
                            toReturn = new RedirectResult(viewModel.DataSourceItemProxy.OnFormSubmitFailUrlStr);
                        }
                    }
                    else
                    {
                        GatedContentLogger.Error("Unhandled submit method", this);
                    }
                }
                else
                {
                    Sitecore.Diagnostics.Log.Error("Null formProxy", this);
                }
            }
            return(toReturn);
        }