use of uk.nhs.digital.toolbox.secrets.ApplicationSecrets in project hippo by NHS-digital-website.
the class ReCaptchaValidationPlugin method validate.
@Override
public Map<String, ErrorMessage> validate(HstRequest request, HstResponse response, ComponentConfiguration config, FormBean bean, Form form, FormMap map) {
final String reCaptchaSiteKey = ((ApplicationSecrets) getComponentManager().getComponent("applicationSecrets")).getValue("GOOGLE_CAPTCHA_SITE_KEY");
final String reCaptchaSecretKey = ((ApplicationSecrets) getComponentManager().getComponent("applicationSecrets")).getValue("GOOGLE_CAPTCHA_SECRET");
final Map<String, ErrorMessage> errors = new HashMap<>();
final String clientReCaptchaResponseString = request.getParameter("gRecaptchaResponse");
if (map.getFormMap().size() == 0 || clientReCaptchaResponseString == null && "RENDER_PHASE".equals(request.getLifecyclePhase()) && map.getFormMap().get("eforms_process_done") != null) {
return errors;
}
try {
// to validate response with Google ReCaptcha API
log.debug("***************************** Validate ReCaptcha *****************************");
log.debug("Recaptcha Site Key: " + reCaptchaSiteKey);
log.debug("Recaptcha Secret Key: " + ApplicationSecrets.mask(reCaptchaSecretKey));
log.debug("Recaptcha Response: " + clientReCaptchaResponseString);
Resource gRecaptchaResponse = validateReCaptcha(clientReCaptchaResponseString, reCaptchaSecretKey);
if (gRecaptchaResponse != null) {
if ((boolean) gRecaptchaResponse.getValue("success")) {
log.debug("ReCaptcha succeeded!");
log.debug("ReCaptcha Challenge TTL: " + gRecaptchaResponse.getValue("challenge_ts"));
log.debug("ReCaptcha Hostname: " + gRecaptchaResponse.getValue("hostname"));
} else {
String errorList = getReCaptchaErrors(gRecaptchaResponse);
log.debug("ReCaptcha Failed:" + errorList);
errors.put("ReCaptcha Validation", new ErrorMessage("ReCaptcha validation failed", errorList));
}
}
} catch (MissingResourceException e) {
log.warn(e.getMessage(), e.getClassName(), e.getKey(), e);
}
log.debug("ReCaptcha Error count is: " + errors.size());
log.debug("**************************** End Validate ReCaptcha ****************************");
return errors;
}
Aggregations