Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
AwesomeNipun committed Apr 25, 2023
1 parent 419168c commit 070fb0e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,6 @@ private static Properties validateCaptchaConfigs(Properties properties) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_VERIFY_URL),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}

if (reCaptchaEnabled && reCaptchaEnterpriseEnabled &&
StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_API_KEY))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants
.RE_CAPTCHA_API_KEY), Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (reCaptchaEnabled && reCaptchaEnterpriseEnabled &&
StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_PROJECT_ID))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants
Expand All @@ -503,37 +497,38 @@ public static HttpResponse makeCaptchaVerificationHttpRequest(ReCaptchaResponseT
boolean reCaptchaEnterpriseEnabled =
Boolean.valueOf(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENTERPRISE_ENABLED));
CloseableHttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build();
HttpPost httppost;

if (!reCaptchaEnterpriseEnabled){ // for Recaptcha V2 and V3
String reCaptchaSecretKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY);
HttpPost httpPost;

httppost = new HttpPost(reCaptchaVerifyUrl);
List<BasicNameValuePair> params = Arrays.asList(new BasicNameValuePair("secret", reCaptchaSecretKey),
new BasicNameValuePair("response", reCaptchaResponse.getToken()));
httppost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
if (reCaptchaEnterpriseEnabled) {
// For ReCaptcha Enterprise.
String projectID = properties.getProperty(CaptchaConstants.RE_CAPTCHA_PROJECT_ID);
String secretKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY);
String siteKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SITE_KEY);
String verifyUrl = reCaptchaVerifyUrl + "/v1/projects/" + projectID + "/assessments?key=" + secretKey;
httpPost = new HttpPost(verifyUrl);
httpPost.setHeader(Constants.HEADER_CONTENT_TYPE, Constants.APPLICATION_JSON);
String json = String.format("{ \"event\": { \"token\": \"%s\", \"siteKey\": \"%s\" } }", reCaptchaResponse,
siteKey);
StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8);
httpPost.setEntity(entity);

try {
response = httpclient.execute(httppost);
response = httpclient.execute(httpPost);
} catch (IOException e) {
RecoveryUtil.handleBadRequest(String.format("Unable to get the verification response : %s",
e.getMessage()), Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
} else {
// For ReCaptcha v2 and v3.
String reCaptchaSecretKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY);

} else{ // for Recaptcha Enterprise
String projectID = properties.getProperty(CaptchaConstants.RE_CAPTCHA_PROJECT_ID);
String APIKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_API_KEY);
String siteKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SITE_KEY);
String verifyUrl = reCaptchaVerifyUrl + "/v1/projects/" + projectID + "/assessments?key=" + APIKey;
httppost = new HttpPost(verifyUrl);
httppost.setHeader("Content-Type", "application/json");
String json = String.format("{ \"event\": { \"token\": \"%s\", \"siteKey\": \"%s\" } }", reCaptchaResponse,
siteKey);
StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8);
httppost.setEntity(entity);
httpPost = new HttpPost(reCaptchaVerifyUrl);
List<BasicNameValuePair> params = Arrays.asList(new BasicNameValuePair("secret", reCaptchaSecretKey),
new BasicNameValuePair("response", reCaptchaResponse.getToken()));
httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

try {
response = httpclient.execute(httppost);
response = httpclient.execute(httpPost);
} catch (IOException e) {
RecoveryUtil.handleBadRequest(String.format("Unable to get the verification response : %s",
e.getMessage()), Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,36 +95,34 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
HttpEntity entity = response.getEntity();
ReCaptchaVerificationResponseDTO reCaptchaVerificationResponseDTO = new ReCaptchaVerificationResponseDTO();

if (!reCaptchaEnterpriseEnabled) {

if (reCaptchaEnterpriseEnabled) {
// For ReCaptcha Enterprise.
if (entity == null) {
RecoveryUtil.handleBadRequest("ReCaptcha Enterprise verification response is not received.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
try {
if (entity == null) {
RecoveryUtil.handleBadRequest("ReCaptcha verification response is not received.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
} else {
try (InputStream in = entity.getContent()) {
JsonObject verificationResponse = new JsonParser().parse(IOUtils.toString(in)).getAsJsonObject();
reCaptchaVerificationResponseDTO.setSuccess(verificationResponse.get(SUCCESS).getAsBoolean());
}
try (InputStream in = entity.getContent()) {
JsonObject verificationResponse = new JsonParser().parse(IOUtils.toString(in)).getAsJsonObject();
JsonObject tokenProperties = verificationResponse.get("tokenProperties").getAsJsonObject();
boolean success = tokenProperties.get(VALID).getAsBoolean();
reCaptchaVerificationResponseDTO.setSuccess(success);
}
} catch (IOException e) {
log.error("Unable to read the verification response.", e);
RecoveryUtil.handleBadRequest("Unable to read the verification response.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
} else {

if (entity == null) {
RecoveryUtil.handleBadRequest("ReCaptcha Enterprise verification response is not received.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}

// For ReCaptcha v2 and v3.
try {
if (entity == null) {
RecoveryUtil.handleBadRequest("ReCaptcha verification response is not received.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
try (InputStream in = entity.getContent()) {
JsonObject verificationResponse = new JsonParser().parse(IOUtils.toString(in)).getAsJsonObject();
JsonObject tokenProperties = verificationResponse.get("tokenProperties").getAsJsonObject();
boolean success = tokenProperties.get(VALID).getAsBoolean();
reCaptchaVerificationResponseDTO.setSuccess(success);
reCaptchaVerificationResponseDTO.setSuccess(verificationResponse.get(SUCCESS).getAsBoolean());
}
} catch (IOException e) {
log.error("Unable to read the verification response.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public class CaptchaDataHolder {

private String reCaptchaSecretKey;

private String reCaptchaAPIKey;

private String reCaptchaProjectID;

private String reCaptchaErrorRedirectUrls;
Expand Down Expand Up @@ -100,16 +98,6 @@ public void setReCaptchaEnterpriseEnabled(boolean reCaptchaEnterpriseEnabled) {
this.reCaptchaEnterpriseEnabled = reCaptchaEnterpriseEnabled;
}

public String getReCaptchaAPIKey() {

return reCaptchaAPIKey;
}

public void setReCaptchaAPIKey(String reCaptchaAPIKey) {

this.reCaptchaAPIKey = reCaptchaAPIKey;
}

public String getReCaptchaProjectID() {

return reCaptchaProjectID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class CaptchaConstants {

public static final String RE_CAPTCHA_SITE_KEY = "recaptcha.site.key";

public static final String RE_CAPTCHA_API_KEY = "recaptcha.api.key";

public static final String RE_CAPTCHA_PROJECT_ID = "recaptcha.project.id";

public static final String RE_CAPTCHA_SECRET_KEY = "recaptcha.secret.key";
Expand All @@ -66,6 +64,8 @@ public class CaptchaConstants {

public static final String CAPTCHA_SUCCESS = "success";

public static final String CAPTCHA_VALID = "valid";

// Default value for threshold for score in reCAPTCHA v3.
public static final double CAPTCHA_V3_DEFAULT_THRESHOLD = 0.5;

Expand Down
Loading

0 comments on commit 070fb0e

Please sign in to comment.