Search in sources :

Example 1 with ErrorMessage

use of com.onehippo.cms7.eforms.hst.model.ErrorMessage 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;
}
Also used : HashMap(java.util.HashMap) ApplicationSecrets(uk.nhs.digital.toolbox.secrets.ApplicationSecrets) MissingResourceException(java.util.MissingResourceException) Resource(org.onehippo.cms7.crisp.api.resource.Resource) ErrorMessage(com.onehippo.cms7.eforms.hst.model.ErrorMessage)

Aggregations

ErrorMessage (com.onehippo.cms7.eforms.hst.model.ErrorMessage)1 HashMap (java.util.HashMap)1 MissingResourceException (java.util.MissingResourceException)1 Resource (org.onehippo.cms7.crisp.api.resource.Resource)1 ApplicationSecrets (uk.nhs.digital.toolbox.secrets.ApplicationSecrets)1