use reCAPTCHA globally

This commit is contained in:
Fuxin Hao 2019-11-01 14:41:45 +08:00 committed by Stian Thorgersen
parent 532436dfad
commit ff4c94506f

View file

@ -53,6 +53,7 @@ import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** /**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
@ -63,6 +64,7 @@ public class RegistrationRecaptcha implements FormAction, FormActionFactory, Con
public static final String RECAPTCHA_REFERENCE_CATEGORY = "recaptcha"; public static final String RECAPTCHA_REFERENCE_CATEGORY = "recaptcha";
public static final String SITE_KEY = "site.key"; public static final String SITE_KEY = "site.key";
public static final String SITE_SECRET = "secret"; public static final String SITE_SECRET = "secret";
public static final String USE_RECAPTCHA_NET = "useRecaptchaNet";
private static final Logger logger = Logger.getLogger(RegistrationRecaptcha.class); private static final Logger logger = Logger.getLogger(RegistrationRecaptcha.class);
public static final String PROVIDER_ID = "registration-recaptcha-action"; 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); String siteKey = captchaConfig.getConfig().get(SITE_KEY);
form.setAttribute("recaptchaRequired", true); form.setAttribute("recaptchaRequired", true);
form.setAttribute("recaptchaSiteKey", siteKey); 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 @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) { protected boolean validateRecaptcha(ValidationContext context, boolean success, String captcha, String secret) {
HttpClient httpClient = context.getSession().getProvider(HttpClientProvider.class).getHttpClient(); 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<NameValuePair> formparams = new LinkedList<>(); List<NameValuePair> formparams = new LinkedList<>();
formparams.add(new BasicNameValuePair("secret", secret)); formparams.add(new BasicNameValuePair("secret", secret));
formparams.add(new BasicNameValuePair("response", captcha)); formparams.add(new BasicNameValuePair("response", captcha));
@ -233,6 +247,12 @@ public class RegistrationRecaptcha implements FormAction, FormActionFactory, Con
property.setHelpText("Google Recaptcha Secret"); property.setHelpText("Google Recaptcha Secret");
CONFIG_PROPERTIES.add(property); 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);
} }