Search in sources :

Example 1 with ReCaptchaResponse

use of net.tanesha.recaptcha.ReCaptchaResponse in project libresonic by Libresonic.

the class RecoverController method recover.

@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView recover(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    String usernameOrEmail = StringUtils.trimToNull(request.getParameter("usernameOrEmail"));
    ReCaptcha captcha = ReCaptchaFactory.newSecureReCaptcha("6LcZ3OMSAAAAANkKMdFdaNopWu9iS03V-nLOuoiH", "6LcZ3OMSAAAAAPaFg89mEzs-Ft0fIu7wxfKtkwmQ", false);
    boolean showCaptcha = true;
    if (usernameOrEmail != null) {
        map.put("usernameOrEmail", usernameOrEmail);
        User user = getUserByUsernameOrEmail(usernameOrEmail);
        String challenge = request.getParameter("recaptcha_challenge_field");
        String uresponse = request.getParameter("recaptcha_response_field");
        ReCaptchaResponse captchaResponse = captcha.checkAnswer(request.getRemoteAddr(), challenge, uresponse);
        if (!captchaResponse.isValid()) {
            map.put("error", "recover.error.invalidcaptcha");
        } else if (user == null) {
            map.put("error", "recover.error.usernotfound");
        } else if (user.getEmail() == null) {
            map.put("error", "recover.error.noemail");
        } else {
            String password = RandomStringUtils.randomAlphanumeric(8);
            if (emailPassword(password, user.getUsername(), user.getEmail())) {
                map.put("sentTo", user.getEmail());
                user.setLdapAuthenticated(false);
                user.setPassword(password);
                securityService.updateUser(user);
                showCaptcha = false;
            } else {
                map.put("error", "recover.error.sendfailed");
            }
        }
    }
    if (showCaptcha) {
        map.put("captcha", captcha.createRecaptchaHtml(null, null));
    }
    return new ModelAndView("recover", "model", map);
}
Also used : User(org.libresonic.player.domain.User) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ReCaptchaResponse(net.tanesha.recaptcha.ReCaptchaResponse) ReCaptcha(net.tanesha.recaptcha.ReCaptcha) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

HashMap (java.util.HashMap)1 ReCaptcha (net.tanesha.recaptcha.ReCaptcha)1 ReCaptchaResponse (net.tanesha.recaptcha.ReCaptchaResponse)1 User (org.libresonic.player.domain.User)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1