@POST @Path("resetpw") @Consumes("application/x-www-form-urlencoded") @Produces(MediaType.TEXT_HTML) public Viewable handlePasswordResetForm( @Context UriInfo ui, @FormParam("token") String token, @FormParam("password1") String password1, @FormParam("password2") String password2, @FormParam("recaptcha_challenge_field") String challenge, @FormParam("recaptcha_response_field") String uresponse) { try { this.token = token; if ((password1 != null) || (password2 != null)) { if (management.checkPasswordResetTokenForAdminUser(user.getUuid(), token)) { if ((password1 != null) && password1.equals(password2)) { management.setAdminUserPassword(user.getUuid(), password1); return handleViewable("resetpw_set_success", this); } else { errorMsg = "Passwords didn't match, let's try again..."; return handleViewable("resetpw_set_form", this); } } else { errorMsg = "Something odd happened, let's try again..."; return handleViewable("resetpw_email_form", this); } } if (!useReCaptcha()) { management.startAdminUserPasswordResetFlow(user); return handleViewable("resetpw_email_success", this); } ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPrivateKey(properties.getRecaptchaPrivate()); ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(httpServletRequest.getRemoteAddr(), challenge, uresponse); if (reCaptchaResponse.isValid()) { management.startAdminUserPasswordResetFlow(user); return handleViewable("resetpw_email_success", this); } else { errorMsg = "Incorrect Captcha"; return handleViewable("resetpw_email_form", this); } } catch (RedirectionException e) { throw e; } catch (Exception e) { return handleViewable("error", e); } }
public boolean isSuccessful(String challenge, String response) { ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPrivateKey(PRIVATE_KEY); ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(CAPTCHA_URL, challenge, response); if (reCaptchaResponse.isValid()) { return true; } else { return false; } }
public static boolean checkCaptcha(HttpServletRequest request) { String remoteAddr = request.getRemoteAddr(); ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPrivateKey("6LcryM4SAAAAAKHGFwoD1t-tQsWB_QGuNInVNYbp"); String challenge = request.getParameter("recaptcha_challenge_field"); String uresponse = request.getParameter("recaptcha_response_field"); ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse); if (reCaptchaResponse.isValid()) { return true; } else { return false; } }
public void validateCaptcha(FacesContext context, UIComponent toValidate, Object value) { if (c != null) { Map map = context.getExternalContext().getRequestParameterMap(); String challenge = map.get("recaptcha_challenge_field").toString(); String response = map.get("recaptcha_response_field").toString(); HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); ReCaptchaResponse resp = r.checkAnswer(req.getRemoteAddr(), challenge, response); if (!resp.isValid() || hasValidationErrors) { Logger.getLogger(ContactUsPage.class.getName()) .info("INVALID RESPONSE: " + resp.getErrorMessage()); ((UIInput) toValidate).setValid(false); if (hasValidationErrors) { context.addMessage( toValidate.getClientId(context), new FacesMessage( "Some required information was entered incorrectly. Please press refresh below to get a new challenge, then correct the issue.")); hasValidationErrors = false; } else { context.addMessage( toValidate.getClientId(context), new FacesMessage("Press refresh below to get a new challenge.")); hasValidationErrors = false; } } } }
@POST @Path("resetpw") @Consumes("application/x-www-form-urlencoded") @Produces(MediaType.TEXT_HTML) public Viewable handlePasswordResetForm( @Context UriInfo ui, @FormParam("email") String email, @FormParam("recaptcha_challenge_field") String challenge, @FormParam("recaptcha_response_field") String uresponse) { try { if (isBlank(email)) { errorMsg = "No email provided, try again..."; return handleViewable("resetpw_email_form", this); } ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPrivateKey(properties.getRecaptchaPrivate()); ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(httpServletRequest.getRemoteAddr(), challenge, uresponse); if (!useReCaptcha() || reCaptchaResponse.isValid()) { user = management.findAdminUser(email); if (user != null) { management.startAdminUserPasswordResetFlow(user); return handleViewable("resetpw_email_success", this); } else { errorMsg = "We don't recognize that email, try again..."; return handleViewable("resetpw_email_form", this); } } else { errorMsg = "Incorrect Captcha, try again..."; return handleViewable("resetpw_email_form", this); } } catch (RedirectionException e) { throw e; } catch (Exception e) { return handleViewable("error", e); } }
public String getCaptcha() { c = captchService.findCaptcha(); String retVal = null; if (c != null) { dbgLog.info("ContactUsPage: captcha service returned non-null value"); r = new ReCaptchaImpl(); l = new SimpleHttpLoader(); r.setIncludeNoscript(true); r.setRecaptchaServer(ReCaptchaImpl.HTTPS_SERVER); r.setHttpLoader(l); r.setPrivateKey(c.getPrivateKey()); r.setPublicKey(c.getPublicKey()); Logger.getLogger(ContactUsPage.class.getName()) .info( "PUBLIC: " + c.getPublicKey() + " -- PRIVATE: " + c.getPrivateKey() + " -- HOST: " + c.getHost()); retVal = r.createRecaptchaHtml(null, null); } else { retVal = ""; } return retVal; }
@RequestMapping(value = "/register.html", method = RequestMethod.POST) public String registerCustomer( @Valid @ModelAttribute("customer") SecuredShopPersistableCustomer customer, BindingResult bindingResult, Model model, HttpServletRequest request, final Locale locale) throws Exception { MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE); Language language = super.getLanguage(request); ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPublicKey(coreConfiguration.getProperty(Constants.RECAPATCHA_PUBLIC_KEY)); reCaptcha.setPrivateKey(coreConfiguration.getProperty(Constants.RECAPATCHA_PRIVATE_KEY)); String userName = null; String password = null; model.addAttribute( "recapatcha_public_key", coreConfiguration.getProperty(Constants.RECAPATCHA_PUBLIC_KEY)); if (StringUtils.isNotBlank(customer.getRecaptcha_challenge_field()) && StringUtils.isNotBlank(customer.getRecaptcha_response_field())) { ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer( request.getRemoteAddr(), customer.getRecaptcha_challenge_field(), customer.getRecaptcha_response_field()); if (!reCaptchaResponse.isValid()) { LOGGER.debug("Captcha response does not matched"); FieldError error = new FieldError( "recaptcha_challenge_field", "recaptcha_challenge_field", messages.getMessage("validaion.recaptcha.not.matched", locale)); bindingResult.addError(error); } } if (StringUtils.isNotBlank(customer.getUserName())) { if (customerFacade.checkIfUserExists(customer.getUserName(), merchantStore)) { LOGGER.debug( "Customer with username {} already exists for this store ", customer.getUserName()); FieldError error = new FieldError( "userName", "userName", messages.getMessage("registration.username.already.exists", locale)); bindingResult.addError(error); } userName = customer.getUserName(); } if (StringUtils.isNotBlank(customer.getPassword()) && StringUtils.isNotBlank(customer.getCheckPassword())) { if (!customer.getPassword().equals(customer.getCheckPassword())) { FieldError error = new FieldError( "password", "password", messages.getMessage("message.password.checkpassword.identical", locale)); bindingResult.addError(error); } password = customer.getPassword(); } if (bindingResult.hasErrors()) { LOGGER.debug( "found {} validation error while validating in customer registration ", bindingResult.getErrorCount()); StringBuilder template = new StringBuilder() .append(ControllerConstants.Tiles.Customer.register) .append(".") .append(merchantStore.getStoreTemplate()); return template.toString(); } @SuppressWarnings("unused") CustomerEntity customerData = null; try { customerData = customerFacade.registerCustomer(customer, merchantStore, language); } catch (CustomerRegistrationException cre) { LOGGER.error("Error while registering customer.. ", cre); ObjectError error = new ObjectError("registration", messages.getMessage("registration.failed", locale)); bindingResult.addError(error); StringBuilder template = new StringBuilder() .append(ControllerConstants.Tiles.Customer.register) .append(".") .append(merchantStore.getStoreTemplate()); return template.toString(); } catch (Exception e) { LOGGER.error("Error while registering customer.. ", e); ObjectError error = new ObjectError("registration", messages.getMessage("registration.failed", locale)); bindingResult.addError(error); StringBuilder template = new StringBuilder() .append(ControllerConstants.Tiles.Customer.register) .append(".") .append(merchantStore.getStoreTemplate()); return template.toString(); } /** Send registration email */ emailTemplatesUtils.sendRegistrationEmail( customer, merchantStore, locale, request.getContextPath()); /** Login user */ try { // refresh customer Customer c = customerFacade.getCustomerByUserName(customer.getUserName(), merchantStore); // authenticate customerFacade.authenticate(c, userName, password); super.setSessionAttribute(Constants.CUSTOMER, c, request); return "redirect:/shop/customer/dashboard.html"; } catch (Exception e) { LOGGER.error("Cannot authenticate user ", e); ObjectError error = new ObjectError("registration", messages.getMessage("registration.failed", locale)); bindingResult.addError(error); } StringBuilder template = new StringBuilder() .append(ControllerConstants.Tiles.Customer.register) .append(".") .append(merchantStore.getStoreTemplate()); return template.toString(); }
@RequestMapping( value = {"/shop/store/{storeCode}/contact"}, method = RequestMethod.POST) public @ResponseBody String sendEmail( @ModelAttribute(value = "contact") ContactForm contact, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception { AjaxResponse ajaxResponse = new AjaxResponse(); MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE); try { if (StringUtils.isBlank(contact.getCaptchaResponseField())) { FieldError error = new FieldError( "captchaResponseField", "captchaResponseField", messages.getMessage("NotEmpty.contact.captchaResponseField", locale)); bindingResult.addError(error); ajaxResponse.setErrorString(bindingResult.getAllErrors().get(0).getDefaultMessage()); ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE); return ajaxResponse.toJSONString(); } ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPublicKey(coreConfiguration.getProperty(Constants.RECAPATCHA_PUBLIC_KEY)); reCaptcha.setPrivateKey(coreConfiguration.getProperty(Constants.RECAPATCHA_PRIVATE_KEY)); if (StringUtils.isNotBlank(contact.getCaptchaChallengeField()) && StringUtils.isNotBlank(contact.getCaptchaResponseField())) { ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer( request.getRemoteAddr(), contact.getCaptchaChallengeField(), contact.getCaptchaResponseField()); if (!reCaptchaResponse.isValid()) { LOGGER.debug("Captcha response does not matched"); FieldError error = new FieldError( "captchaChallengeField", "captchaChallengeField", messages.getMessage("validaion.recaptcha.not.matched", locale)); bindingResult.addError(error); } } if (bindingResult.hasErrors()) { LOGGER.debug( "found {} validation error while validating in customer registration ", bindingResult.getErrorCount()); ajaxResponse.setErrorString(bindingResult.getAllErrors().get(0).getDefaultMessage()); ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE); return ajaxResponse.toJSONString(); } emailTemplatesUtils.sendContactEmail( contact, store, LocaleUtils.getLocale(store.getDefaultLanguage()), request.getContextPath()); ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_SUCCESS); } catch (Exception e) { LOGGER.error("An error occured while trying to send an email", e); ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE); } return ajaxResponse.toJSONString(); }
@RequestMapping(value = CONTROLLER_MAPPING + "submit", method = RequestMethod.POST) public ModelAndView formSubmission( HttpServletRequest request, String confirmationUrl, String usersEmail, String name, String fromEmail, String toEmail, String userMsg) { ModelAndView modelAndView = new ModelAndView(confirmationUrl); if (log.isDebugEnabled()) log.debug("Contact form submitted"); if ((StringUtils.isBlank(confirmationUrl)) || (StringUtils.isBlank(fromEmail)) || (StringUtils.isBlank(toEmail))) { log.error( String.format( "Contact form not configured properly, confirmationUrl: %s, fromEmail: %s, toEmail: %s", confirmationUrl, fromEmail, toEmail)); modelAndView = new ModelAndView("system-error", "errorMsg", "This page has not been configure"); ContactUsResponse contactUsResponse = contactUsResponseService.findContactUsResponse(); if (contactUsResponse != null) { modelAndView.addObject("fromEmail", contactUsResponse.getFromEmailAddress()); modelAndView.addObject("toEmail", contactUsResponse.getToEmailAddresses()); } else { modelAndView = new ModelAndView("system-error", "errorMsg", "This page has not been configure"); } } else if ((StringUtils.isBlank(usersEmail)) || (StringUtils.isBlank(name)) || (StringUtils.isBlank(userMsg))) { log.warn( String.format( "Contact form was not submitted properly, usersEmail: %s, name: %s, userMsg: %s", usersEmail, name, userMsg)); modelAndView = new ModelAndView( "contact/form", "errorMsg", "You must provide entries for all required fields"); ContactUsResponse contactUsResponse = contactUsResponseService.findContactUsResponse(); if (contactUsResponse != null) { modelAndView.addObject("fromEmail", contactUsResponse.getFromEmailAddress()); modelAndView.addObject("toEmail", contactUsResponse.getToEmailAddresses()); modelAndView.addObject("name", name); modelAndView.addObject("userMsg", userMsg); modelAndView.addObject("usersEmail", usersEmail); } else { modelAndView = new ModelAndView("system-error", "errorMsg", "This page has not been configure"); } } else if (!EmailUtil.isValidEmailAddress(usersEmail)) { modelAndView = new ModelAndView("contact/form", "errorMsg", "You must provide a valid email address"); ContactUsResponse contactUsResponse = contactUsResponseService.findContactUsResponse(); if (contactUsResponse != null) { modelAndView.addObject("fromEmail", contactUsResponse.getFromEmailAddress()); modelAndView.addObject("toEmail", contactUsResponse.getToEmailAddresses()); modelAndView.addObject("name", name); modelAndView.addObject("userMsg", userMsg); modelAndView.addObject("usersEmail", usersEmail); } else { modelAndView = new ModelAndView("system-error", "errorMsg", "This page has not been configure"); } } else { String challenge = (String) request.getParameter("recaptcha_challenge_field"); String response = (String) request.getParameter("recaptcha_response_field"); String remoteAddr = request.getRemoteAddr(); ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPrivateKey("6Lco2cUSAAAAAO3-55M3iPPOfr3FIe_4n5XYsFBW"); ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, response); if (reCaptchaResponse.isValid()) { log.debug( String.format( "Contact form submitted properly, confirmationUrl: %s, fromEmail: %s, toEmail: %s, name: %s, usersEmail: %s, userMsg: %s", confirmationUrl, fromEmail, toEmail, name, usersEmail, userMsg)); if (!EmailUtil.send( fromEmail, toEmail, name, "Website Contact Request from " + name + " (" + usersEmail + ")", userMsg)) { log.error(String.format("An error occurred sending the email", "")); modelAndView = new ModelAndView( "contact/form", "errorMsg", "An error occurred sending your request, please try again later."); } } else { ContactUsResponse contactUsResponse = contactUsResponseService.findContactUsResponse(); if (contactUsResponse != null) { modelAndView = new ModelAndView("contact/form", "errorMsg", "Invalid captcha"); modelAndView.addObject("fromEmail", contactUsResponse.getFromEmailAddress()); modelAndView.addObject("toEmail", contactUsResponse.getToEmailAddresses()); modelAndView.addObject("name", name); modelAndView.addObject("userMsg", userMsg); modelAndView.addObject("usersEmail", usersEmail); } else { modelAndView = new ModelAndView("system-error", "errorMsg", "This page has not been configure"); } } } log.debug("Finishing off by loading navigation"); loadConfig(modelAndView); loadNavigation(modelAndView); modelAndView.addObject("currentUser", siteUserService.getCurrentUser()); return modelAndView; }