com.textrecruit.ustackserverapi.util.ReCaptchaUtil Maven / Gradle / Ivy
package com.textrecruit.ustackserverapi.util;
import com.textrecruit.ustackserverapi.exceptions.BadRequestException;
import com.textrecruit.ustackserverapi.params.ParamNames;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import com.textrecruit.ustackserverapi.CallParameters;
public class ReCaptchaUtil {
/**
* Validates the user provided Re-Captcha with the remote server
*
* @param privateKey
* @param callParams
* @throws BadRequestException
*/
public static void validateReCaptcha(String privateKey, CallParameters callParams) throws BadRequestException
{
if ("PASS".equals(System.getProperty("ReCaptchaTest")))
return;
if ("FAIL".equals(System.getProperty("ReCaptchaTest")))
throw new BadRequestException("Invalid Captcha");
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
reCaptcha.setPrivateKey(privateKey);
String challenge = callParams.get(ParamNames.recaptcha_challenge_field);
String uresponse = callParams.get(ParamNames.recaptcha_response_field);
if (challenge == null || uresponse == null)
throw new BadRequestException("'recaptcha_challenge_field' and 'recaptcha_response_field' parameters are required");
System.out.println(String.format("ReCaptcha from '%s' | Challenge [%s], Response [%s]", callParams.getRemoteIpAddress(), challenge, uresponse));
ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(callParams.getRemoteIpAddress(), challenge, uresponse);
if (!reCaptchaResponse.isValid())
throw new BadRequestException("Invalid Captcha", new HttpResponseStatus(418, "Invalid Recaptcha"));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy