83 lines
3.1 KiB
Java
Raw Normal View History

package com.wisemapping.service;
import com.fasterxml.jackson.databind.ObjectMapper;
2022-02-12 07:52:16 -08:00
import com.wisemapping.validator.Messages;
import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.apache.log4j.Logger;
2022-02-03 21:27:43 -08:00
import org.jetbrains.annotations.Nullable;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RecaptchaService {
final private static Logger logger = Logger.getLogger(RecaptchaService.class);
final private static String GOOGLE_RECAPTCHA_VERIFY_URL =
"https://www.google.com/recaptcha/api/siteverify";
private final static ObjectMapper objectMapper = new ObjectMapper();
2022-02-12 08:29:53 -08:00
public static final String CATCH_ERROR_CODE_TIMEOUT_OR_DUPLICATE = "timeout-or-duplicate";
public static final String CATCHA_ERROR_CODE_INPUT_RESPONSE = "invalid-input-response";
private String recaptchaSecret;
2022-02-03 21:27:43 -08:00
@Nullable
public String verifyRecaptcha(@NotNull String ip, @NotNull String recaptcha) {
final List<NameValuePair> build = Form.form()
.add("secret", recaptchaSecret)
.add("response", recaptcha)
.add("remoteip", ip)
.build();
// Add logs ...
logger.debug("Response from remoteip: " + ip);
logger.debug("Response from recaptchaSecret: " + recaptchaSecret);
logger.debug("Response from recaptcha: " + recaptcha);
String result = StringUtils.EMPTY;
try {
final byte[] body = Request
.Post(GOOGLE_RECAPTCHA_VERIFY_URL)
.bodyForm(build)
.execute()
.returnContent()
.asBytes();
final Map responseBody = objectMapper.readValue(body, HashMap.class);
2022-02-13 08:25:09 -08:00
logger.debug("Response from recaptcha after parse: " + responseBody);
final Boolean success = (Boolean) responseBody.get("success");
2022-02-12 07:52:16 -08:00
if (success != null && !success) {
final List<String> errorCodes = (List<String>) responseBody.get("error-codes");
2022-02-12 08:29:53 -08:00
String errorCode = errorCodes.get(0);
if (errorCode.equals(CATCH_ERROR_CODE_TIMEOUT_OR_DUPLICATE)) {
2022-02-12 07:52:16 -08:00
result = Messages.CAPTCHA_TIMEOUT_OUT_DUPLICATE;
} else if (errorCode.equals("invalid-input-response")) {
result = Messages.CAPTCHA_INVALID_INPUT_RESPONSE;
2022-02-12 07:52:16 -08:00
} else {
result = Messages.CAPTCHA_LOADING_ERROR;
logger.error("Unexpected error during catch resolution:" + errorCodes);
}
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
result = e.getMessage();
}
logger.debug("Captcha Result:" + result);
return result;
}
public void setRecaptchaSecret(String recaptchaSecret) {
this.recaptchaSecret = recaptchaSecret;
}
}