2018-09-14 17:50:27 -07:00
|
|
|
package com.wisemapping.service;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
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;
|
|
|
|
|
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();
|
|
|
|
private String recaptchaSecret;
|
|
|
|
|
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");
|
|
|
|
if (success!=null && !success) {
|
|
|
|
final List<String> errorCodes = (List<String>) responseBody.get("error-codes");
|
|
|
|
result = RecaptchaUtil.codeToDescription(errorCodes.get(0));
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RecaptchaUtil {
|
|
|
|
|
2021-12-24 18:03:23 -08:00
|
|
|
private static final Map<String, String>
|
2018-09-14 17:50:27 -07:00
|
|
|
RECAPTCHA_ERROR_CODE = new HashMap<>();
|
|
|
|
|
2021-12-24 18:03:23 -08:00
|
|
|
static String codeToDescription(final String code)
|
|
|
|
{
|
|
|
|
return RECAPTCHA_ERROR_CODE.getOrDefault(code,"Unexpected error validating code. Please, refresh the page and try again.");
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:50:27 -07:00
|
|
|
static {
|
|
|
|
RECAPTCHA_ERROR_CODE.put("missing-input-secret",
|
|
|
|
"The secret parameter is missing");
|
|
|
|
RECAPTCHA_ERROR_CODE.put("invalid-input-secret",
|
|
|
|
"The secret parameter is invalid or malformed");
|
|
|
|
RECAPTCHA_ERROR_CODE.put("missing-input-response",
|
|
|
|
"The response parameter is missing");
|
|
|
|
RECAPTCHA_ERROR_CODE.put("invalid-input-response",
|
|
|
|
"The response parameter is invalid or malformed");
|
|
|
|
RECAPTCHA_ERROR_CODE.put("bad-request",
|
|
|
|
"The request is invalid or malformed");
|
2021-12-24 18:03:23 -08:00
|
|
|
RECAPTCHA_ERROR_CODE.put("timeout-or-duplicate",
|
|
|
|
"Please, refresh the page and try again.");
|
2018-09-14 17:50:27 -07:00
|
|
|
}
|
|
|
|
}
|