From ff4c94506f3d2af58b39a08c44429f69d3af98fb Mon Sep 17 00:00:00 2001 From: Fuxin Hao Date: Fri, 1 Nov 2019 14:41:45 +0800 Subject: [PATCH] use reCAPTCHA globally --- .../forms/RegistrationRecaptcha.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/services/src/main/java/org/keycloak/authentication/forms/RegistrationRecaptcha.java b/services/src/main/java/org/keycloak/authentication/forms/RegistrationRecaptcha.java index 360b1346fd..e1cea92e10 100755 --- a/services/src/main/java/org/keycloak/authentication/forms/RegistrationRecaptcha.java +++ b/services/src/main/java/org/keycloak/authentication/forms/RegistrationRecaptcha.java @@ -53,6 +53,7 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Optional; /** * @author Bill Burke @@ -63,6 +64,7 @@ public class RegistrationRecaptcha implements FormAction, FormActionFactory, Con public static final String RECAPTCHA_REFERENCE_CATEGORY = "recaptcha"; public static final String SITE_KEY = "site.key"; public static final String SITE_SECRET = "secret"; + public static final String USE_RECAPTCHA_NET = "useRecaptchaNet"; private static final Logger logger = Logger.getLogger(RegistrationRecaptcha.class); public static final String PROVIDER_ID = "registration-recaptcha-action"; @@ -104,7 +106,7 @@ public class RegistrationRecaptcha implements FormAction, FormActionFactory, Con String siteKey = captchaConfig.getConfig().get(SITE_KEY); form.setAttribute("recaptchaRequired", true); form.setAttribute("recaptchaSiteKey", siteKey); - form.addScript("https://www.google.com/recaptcha/api.js?hl=" + userLanguageTag); + form.addScript("https://www." + getRecaptchaDomain(captchaConfig) + "/recaptcha/api.js?hl=" + userLanguageTag); } @Override @@ -135,9 +137,21 @@ public class RegistrationRecaptcha implements FormAction, FormActionFactory, Con } } + private String getRecaptchaDomain(AuthenticatorConfigModel config) { + Boolean useRecaptcha = Optional.ofNullable(config) + .map(configModel -> configModel.getConfig()) + .map(cfg -> Boolean.valueOf(cfg.get(USE_RECAPTCHA_NET))) + .orElse(false); + if (useRecaptcha) { + return "recaptcha.net"; + } + + return "google.com"; + } + protected boolean validateRecaptcha(ValidationContext context, boolean success, String captcha, String secret) { HttpClient httpClient = context.getSession().getProvider(HttpClientProvider.class).getHttpClient(); - HttpPost post = new HttpPost("https://www.google.com/recaptcha/api/siteverify"); + HttpPost post = new HttpPost("https://www." + getRecaptchaDomain(context.getAuthenticatorConfig()) + "/recaptcha/api/siteverify"); List formparams = new LinkedList<>(); formparams.add(new BasicNameValuePair("secret", secret)); formparams.add(new BasicNameValuePair("response", captcha)); @@ -233,6 +247,12 @@ public class RegistrationRecaptcha implements FormAction, FormActionFactory, Con property.setHelpText("Google Recaptcha Secret"); CONFIG_PROPERTIES.add(property); + property = new ProviderConfigProperty(); + property.setName(USE_RECAPTCHA_NET); + property.setLabel("use recaptcha.net"); + property.setType(ProviderConfigProperty.BOOLEAN_TYPE); + property.setHelpText("Use recaptcha.net? (or else google.com)"); + CONFIG_PROPERTIES.add(property); }