net.tanesha.recaptcha.ReCaptcha Java Examples

The following examples show how to use net.tanesha.recaptcha.ReCaptcha. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReCaptchaService.java    From attic-rave with Apache License 2.0 6 votes vote down vote up
@Override
public String createHtml(HttpServletRequest request) {
    if (captchaEnabled) {
        if (StringUtils.isBlank(privateKey) || StringUtils.isBlank(publicKey)) {
            return invalidConfigurationMessage;
        }
        boolean secure = request.isSecure();

        ReCaptcha captcha;
        if (secure) {
            captcha = ReCaptchaFactory.newSecureReCaptcha(publicKey, privateKey, createNoScript);
        } else {
            captcha = ReCaptchaFactory.newReCaptcha(publicKey, privateKey, createNoScript);
        }

        return captcha.createRecaptchaHtml(null, null);
    }
    return "";
}
 
Example #2
Source File: ReCaptchaService.java    From attic-rave with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isValid(HttpServletRequest request) {
    log.debug("ReCaptcha enabled:  {}", captchaEnabled);
    if (!captchaEnabled) {
        return true;
    }
    if (StringUtils.isBlank(privateKey) || StringUtils.isBlank(publicKey)) {
        log.error("ReCaptcha service is enabled, however, private or public keys are not defined.");
        return true;
    }

    boolean secure = request.isSecure();
    ReCaptcha captcha;
    if (secure) {
        captcha = ReCaptchaFactory.newSecureReCaptcha(publicKey, privateKey, createNoScript);
    } else {
        captcha = ReCaptchaFactory.newReCaptcha(publicKey, privateKey, createNoScript);
    }
    String response = request.getParameter(PARAM_CAPTCHA_RESPONSE);
    String challenge = request.getParameter(PARAM_CAPTCHA_CHALLENGE);
    String remoteAddress = request.getRemoteAddr();
    // validate:
    ReCaptchaResponse captchaResponse = captcha.checkAnswer(remoteAddress, challenge, response);
    boolean valid = captchaResponse.isValid();
    if (valid) {
        return true;
    }
    log.warn("Invalid captcha response:  {}", captchaResponse.getErrorMessage());
    return false;

}
 
Example #3
Source File: AbstractContextResource.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public String getReCaptchaHtml() {
    if (!useReCaptcha()) {
        return "";
    }
    ReCaptcha c = ReCaptchaFactory.newSecureReCaptcha(
        properties.getRecaptchaPublic(), properties.getRecaptchaPrivate(), false);
    return c.createRecaptchaHtml(null, null);
}
 
Example #4
Source File: MultiController.java    From subsonic with GNU General Public License v3.0 4 votes vote down vote up
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);
    }
 
Example #5
Source File: UsersAction.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * Build the context for the create user mode.
 */
private String buildCreateContext(SessionState state, Context context)
{
	// put the service in the context
	context.put("service", userDirectoryService);

	String blurb = (String) state.getAttribute(CONFIG_CREATE_BLURB);
	if (!StringUtils.isEmpty(blurb))
	{
		context.put("createBlurb", blurb);
	}

	// is the type to be pre-set
	context.put("type", state.getAttribute("create-type"));

	boolean isValidatedWithAccountValidator = isValidatedWithAccountValidator(state);
	boolean isEidEditable = isEidEditable(state);

	// if the tool is configured to validate through email, we will use AccountValidator to set name fields, etc. So we indicate this in the context to hide fields that are redundant
	context.put("isValidatedWithAccountValidator", isValidatedWithAccountValidator);

	// If we're using account validator, an email needs to be sent
	// If the eid is not editable, the email will be used as the eid
	context.put("emailRequired", isValidatedWithAccountValidator || !isEidEditable);

	// password is required when using Gateway New Account tool
	// attribute "create-user" is true only for New Account tool
	context.put("pwRequired", state.getAttribute("create-user"));

	context.put("displayEid", isEidEditable);
	String value = (String) state.getAttribute("valueEid");
	if (value != null) context.put("valueEid", value);

	value = (String) state.getAttribute("valueFirstName");
	if (value != null) context.put("valueFirstName", value);

	value = (String) state.getAttribute("valueLastName");
	if (value != null) context.put("valueLastName", value);

	value = (String) state.getAttribute("valueEmail");
	if (value != null) context.put("valueEmail", value);
			
	if ((Boolean)state.getAttribute("user.recaptcha-enabled"))
	{
		ReCaptcha captcha = ReCaptchaFactory.newReCaptcha((String)state.getAttribute("user.recaptcha-public-key"), (String)state.getAttribute("user.recaptcha-private-key"), false);
        String captchaScript = captcha.createRecaptchaHtml((String)state.getAttribute("recaptcha-error"), null);
        state.removeAttribute("recaptcha-error");
        context.put("recaptchaScript", captchaScript);
	}

	return "_create";

}
 
Example #6
Source File: UsersAction.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * Build the context for the create user mode.
 */
private String buildCreateContext(SessionState state, Context context)
{
	// put the service in the context
	context.put("service", userDirectoryService);

	String blurb = (String) state.getAttribute(CONFIG_CREATE_BLURB);
	if (!StringUtils.isEmpty(blurb))
	{
		context.put("createBlurb", blurb);
	}

	// is the type to be pre-set
	context.put("type", state.getAttribute("create-type"));

	boolean isValidatedWithAccountValidator = isValidatedWithAccountValidator(state);
	boolean isEidEditable = isEidEditable(state);

	// if the tool is configured to validate through email, we will use AccountValidator to set name fields, etc. So we indicate this in the context to hide fields that are redundant
	context.put("isValidatedWithAccountValidator", isValidatedWithAccountValidator);

	// If we're using account validator, an email needs to be sent
	// If the eid is not editable, the email will be used as the eid
	context.put("emailRequired", isValidatedWithAccountValidator || !isEidEditable);

	// password is required when using Gateway New Account tool
	// attribute "create-user" is true only for New Account tool
	context.put("pwRequired", state.getAttribute("create-user"));

	context.put("displayEid", isEidEditable);
	String value = (String) state.getAttribute("valueEid");
	if (value != null) context.put("valueEid", value);

	value = (String) state.getAttribute("valueFirstName");
	if (value != null) context.put("valueFirstName", value);

	value = (String) state.getAttribute("valueLastName");
	if (value != null) context.put("valueLastName", value);

	value = (String) state.getAttribute("valueEmail");
	if (value != null) context.put("valueEmail", value);
			
	if ((Boolean)state.getAttribute("user.recaptcha-enabled"))
	{
		ReCaptcha captcha = ReCaptchaFactory.newReCaptcha((String)state.getAttribute("user.recaptcha-public-key"), (String)state.getAttribute("user.recaptcha-private-key"), false);
        String captchaScript = captcha.createRecaptchaHtml((String)state.getAttribute("recaptcha-error"), null);
        state.removeAttribute("recaptcha-error");
        context.put("recaptchaScript", captchaScript);
	}

	return "_create";

}
 
Example #7
Source File: RecaptchaAction.java    From wandora with GNU General Public License v3.0 4 votes vote down vote up
public ReCaptcha makeReCaptcha(){
    return ReCaptchaFactory.newReCaptcha(publicKey, privateKey, false);
}
 
Example #8
Source File: RecaptchaAction.java    From wandora with GNU General Public License v3.0 4 votes vote down vote up
public String createFormHtml(){
    ReCaptcha rc=makeReCaptcha();
    return rc.createRecaptchaHtml(null, null);
}