Support for reCaptcha v2

UI Components for JSF
Post Reply
Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

27 Jul 2015, 20:45

Hi folks,

i would like to know, if there are any plans to support the newer reCaptcha (v2) version from google, which should be more sophisticated than the previous version. Would be nice, if the p:captcha could be also used against the second version, so a developer can choose.

For end users, the input shall be much simpler, especially if google "thinks" you are not a bot at all (Google is an "intelligent agent" :mrgreen: ). So with version 2 sometimes only a simple checkbox must be clicked instead of looking at a picture and try to read the right value out of it.

MfG

Hamsterbau
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

gregor
Posts: 36
Joined: 25 Mar 2014, 12:28

15 Aug 2015, 13:17

hi,

any news here?

cheers

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

15 Aug 2015, 18:47

Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

15 Aug 2015, 19:28

Thanks for this hint. Would be great, if supported directly by Primefaces.

But, am i the only one, who must laugh about this "Feature": Update reCAPTCHA to suport NoCaptcha :mrgreen:
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

rkrenn
Posts: 33
Joined: 26 Jul 2011, 03:55
Location: Austria

11 Feb 2018, 01:57

here is an example how to implement reCAPTCHA v2 in legacy PF versions.
https://github.com/phoenixctms/ctsms/co ... 8e11c50eb9
Mojarra 2.1.28, PrimeFaces 3.3.1, Primefaces Extensions 0.7.0, libservlet3.1-java tomcat8, openjdk-8-jdk

Topaazy
Posts: 2
Joined: 11 Feb 2018, 12:58
Contact:

11 Feb 2018, 14:32

Hi,

I have implemented this with reCAPTCHA4net from http://recaptcha4net.codeplex.com/
Demo at shop.getyournet.ch

Step 1
In Nop.Web.Framework, add a reference to the assembly Recaptcha.Web
Also add a reference to System.Net.Http

Step 2
In Nop.Web.Framework > UI > Captcha, change the file HtmlExtensions.cs

Code: Select all

using System.IO;
using System.Web.Mvc;
using System.Web.UI;
using Nop.Core.Infrastructure;

namespace Nop.Web.Framework.UI.Captcha
{
    public static class HtmlExtensions
    {
        public static string GenerateCaptcha(this HtmlHelper helper)
        {
            var captchaSettings = EngineContext.Current.Resolve<CaptchaSettings>();

            var theme = !string.IsNullOrEmpty(captchaSettings.ReCaptchaTheme) ? captchaSettings.ReCaptchaTheme : "white";
            var captchaControl = new Recaptcha.Web.UI.Controls.Recaptcha
            {
                ID = "recaptcha",
                ColorTheme = Recaptcha.Web.ColorTheme.Light,
                PublicKey = captchaSettings.ReCaptchaPublicKey,
                PrivateKey = captchaSettings.ReCaptchaPrivateKey
            };

            var htmlWriter = new HtmlTextWriter(new StringWriter());

            captchaControl.RenderControl(htmlWriter);

            return htmlWriter.InnerWriter.ToString();
        }
    }
}

Step 3
In Nop.Web.Framework > UI > Captcha, change the file CaptchaValidatorAttribute.cs

Code: Select all

using System.Web.Mvc;
using Nop.Core.Infrastructure;
using System;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Nop.Services.Tasks;
using System.Threading.Tasks;

namespace Nop.Web.Framework.UI.Captcha
{
    public class CaptchaValidatorAttribute : ActionFilterAttribute
    {
        private const string RESPONSE_FIELD_KEY = "g-reCAPTCHA-response";

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var valid = false;
            var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
            if (!string.IsNullOrEmpty(captchaResponseValue))
            {
                var captchaSettings = EngineContext.Current.Resolve<CaptchaSettings>();
                if (captchaSettings.Enabled)
                    valid = System.Threading.Tasks.Task.Factory.StartNew(async () => await ValidateResponse(captchaResponseValue, captchaSettings.ReCaptchaPrivateKey).ConfigureAwait(false)).Unwrap().Result;
            }

            //this will push the result value into a parameter in our Action  
            filterContext.ActionParameters["captchaValid"] = valid;

            base.OnActionExecuting(filterContext);
        }

        private async Task<bool> ValidateResponse(string captchaResponseValue, string key)
        {
            var valid = false;
            var uribuilder = new UriBuilder("https://www.google.com/recaptcha/api/siteverify")
            {
                Query = string.Format("secret={0}&response={1}", key, captchaResponseValue)
            };
            using (var httpClient = new HttpClient())
            {
                var async = await httpClient.GetAsync(uribuilder.Uri);
                async.EnsureSuccessStatusCode();
                var readstring = await async.Content.ReadAsStringAsync();
                var resultobject = JObject.Parse(readstring);
                valid = resultobject.Value<bool>("success");
            }
            return valid;
        }
    }
}

Step 4
Compile the project and copy Nop.Web.Framework.dll and Recaptcha.Web.dll to the bin folder of your production site.

Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

13 Feb 2018, 11:41

rkrenn wrote:
11 Feb 2018, 01:57
here is an example how to implement reCAPTCHA v2 in legacy PF versions.
https://github.com/phoenixctms/ctsms/co ... 8e11c50eb9
Thanks a lot, this is really nice for legacy support. But i think you can use the same key for reCAPTCHA v1 and v2. There is no need to have a new one for the newer version ;)
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

13 Feb 2018, 18:11

All you posted below is for .NOT eeh... .Net and not for Java
Topaazy wrote:
11 Feb 2018, 14:32
Hi,

I have implemented this with reCAPTCHA4net from http://recaptcha4net.codeplex.com/
Demo at shop.getyournet.ch

Step 1
In Nop.Web.Framework, add a reference to the assembly Recaptcha.Web
Also add a reference to System.Net.Http

Step 2
In Nop.Web.Framework > UI > Captcha, change the file HtmlExtensions.cs

Code: Select all

using System.IO;
using System.Web.Mvc;
using System.Web.UI;
using Nop.Core.Infrastructure;

namespace Nop.Web.Framework.UI.Captcha
{
    public static class HtmlExtensions
    {
        public static string GenerateCaptcha(this HtmlHelper helper)
        {
            var captchaSettings = EngineContext.Current.Resolve<CaptchaSettings>();

            var theme = !string.IsNullOrEmpty(captchaSettings.ReCaptchaTheme) ? captchaSettings.ReCaptchaTheme : "white";
            var captchaControl = new Recaptcha.Web.UI.Controls.Recaptcha
            {
                ID = "recaptcha",
                ColorTheme = Recaptcha.Web.ColorTheme.Light,
                PublicKey = captchaSettings.ReCaptchaPublicKey,
                PrivateKey = captchaSettings.ReCaptchaPrivateKey
            };

            var htmlWriter = new HtmlTextWriter(new StringWriter());

            captchaControl.RenderControl(htmlWriter);

            return htmlWriter.InnerWriter.ToString();
        }
    }
}

Step 3
In Nop.Web.Framework > UI > Captcha, change the file CaptchaValidatorAttribute.cs

Code: Select all

using System.Web.Mvc;
using Nop.Core.Infrastructure;
using System;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Nop.Services.Tasks;
using System.Threading.Tasks;

namespace Nop.Web.Framework.UI.Captcha
{
    public class CaptchaValidatorAttribute : ActionFilterAttribute
    {
        private const string RESPONSE_FIELD_KEY = "g-reCAPTCHA-response";

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var valid = false;
            var captchaResponseValue = filterContext.HttpContext.Request.Form[RESPONSE_FIELD_KEY];
            if (!string.IsNullOrEmpty(captchaResponseValue))
            {
                var captchaSettings = EngineContext.Current.Resolve<CaptchaSettings>();
                if (captchaSettings.Enabled)
                    valid = System.Threading.Tasks.Task.Factory.StartNew(async () => await ValidateResponse(captchaResponseValue, captchaSettings.ReCaptchaPrivateKey).ConfigureAwait(false)).Unwrap().Result;
            }

            //this will push the result value into a parameter in our Action  
            filterContext.ActionParameters["captchaValid"] = valid;

            base.OnActionExecuting(filterContext);
        }

        private async Task<bool> ValidateResponse(string captchaResponseValue, string key)
        {
            var valid = false;
            var uribuilder = new UriBuilder("https://www.google.com/recaptcha/api/siteverify")
            {
                Query = string.Format("secret={0}&response={1}", key, captchaResponseValue)
            };
            using (var httpClient = new HttpClient())
            {
                var async = await httpClient.GetAsync(uribuilder.Uri);
                async.EnsureSuccessStatusCode();
                var readstring = await async.Content.ReadAsStringAsync();
                var resultobject = JObject.Parse(readstring);
                valid = resultobject.Value<bool>("success");
            }
            return valid;
        }
    }
}

Step 4
Compile the project and copy Nop.Web.Framework.dll and Recaptcha.Web.dll to the bin folder of your production site.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 60 guests