2018-09-14 17:50:27 -07:00
|
|
|
package com.wisemapping.service;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2022-02-12 07:52:16 -08:00
|
|
|
import com.wisemapping.validator.Messages;
|
2018-09-14 17:50:27 -07:00
|
|
|
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;
|
2018-09-14 17:50:27 -07:00
|
|
|
|
2021-12-24 18:03:23 -08:00
|
|
|
import javax.validation.constraints.NotNull;
|
2018-09-14 17:50:27 -07:00
|
|
|
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";
|
2018-09-14 17:50:27 -07:00
|
|
|
private String recaptchaSecret;
|
|
|
|
|
2022-02-03 21:27:43 -08:00
|
|
|
@Nullable
|
2021-12-24 18:03:23 -08:00
|
|
|
public String verifyRecaptcha(@NotNull String ip, @NotNull String recaptcha) {
|
2018-09-14 17:50:27 -07:00
|
|
|
|
|
|
|
final List<NameValuePair> build = Form.form()
|
|
|
|
.add("secret", recaptchaSecret)
|
2021-12-24 18:03:23 -08:00
|
|
|
.add("response", recaptcha)
|
2018-09-14 17:50:27 -07:00
|
|
|
.add("remoteip", ip)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Add logs ...
|
|
|
|
logger.debug("Response from remoteip: " + ip);
|
|
|
|
logger.debug("Response from recaptchaSecret: " + recaptchaSecret);
|
2021-12-24 18:03:23 -08:00
|
|
|
logger.debug("Response from recaptcha: " + recaptcha);
|
2018-09-14 17:50:27 -07:00
|
|
|
|
|
|
|
String result = StringUtils.EMPTY;
|
|
|
|
try {
|
|
|
|
final byte[] body = Request
|
|
|
|
.Post(GOOGLE_RECAPTCHA_VERIFY_URL)
|
|
|
|
.bodyForm(build)
|
|
|
|
.execute()
|
|
|
|
.returnContent()
|
|
|
|
.asBytes();
|
|
|
|
|
2021-12-24 18:03:23 -08:00
|
|
|
final Map responseBody = objectMapper.readValue(body, HashMap.class);
|
|
|
|
logger.warn("Response from recaptcha after parse: " + responseBody);
|
2018-09-14 17:50:27 -07:00
|
|
|
|
2021-12-24 18:03:23 -08:00
|
|
|
final Boolean success = (Boolean) responseBody.get("success");
|
2022-02-12 07:52:16 -08:00
|
|
|
if (success != null && !success) {
|
2021-12-24 18:03:23 -08:00
|
|
|
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) || errorCodes.equals(CATCHA_ERROR_CODE_INPUT_RESPONSE)) {
|
2022-02-12 07:52:16 -08:00
|
|
|
result = Messages.CAPTCHA_TIMEOUT_OUT_DUPLICATE;
|
|
|
|
} else {
|
|
|
|
result = Messages.CAPTCHA_LOADING_ERROR;
|
|
|
|
logger.error("Unexpected error during catch resolution:" + errorCodes);
|
|
|
|
}
|
2018-09-14 17:50:27 -07:00
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|