Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
AwesomeNipun committed May 29, 2023
1 parent aaa8aec commit 27a285a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ private static Properties validateCaptchaConfigs(Properties properties) {
boolean reCaptchaEnterpriseEnabled = Boolean.valueOf(properties.getProperty(CaptchaConstants
.RE_CAPTCHA_ENTERPRISE_ENABLED));

if (reCaptchaEnabled || reCaptchaEnterpriseEnabled) {
if (reCaptchaEnabled) {
if (StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_SITE_KEY))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_SITE_KEY),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
Expand Down Expand Up @@ -495,6 +495,7 @@ public static HttpResponse makeCaptchaVerificationHttpRequest(ReCaptchaResponseT

HttpResponse response = null;
String reCaptchaVerifyUrl = properties.getProperty(CaptchaConstants.RE_CAPTCHA_VERIFY_URL);
String reCaptchaSecretKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY);
boolean reCaptchaEnterpriseEnabled =
Boolean.valueOf(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENTERPRISE_ENABLED));
CloseableHttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build();
Expand All @@ -503,9 +504,9 @@ public static HttpResponse makeCaptchaVerificationHttpRequest(ReCaptchaResponseT
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;
String verifyUrl = reCaptchaVerifyUrl + "/v1/projects/" + projectID +
"/assessments?key=" + reCaptchaSecretKey;
httpPost = new HttpPost(verifyUrl);
httpPost.setHeader(Constants.HEADER_CONTENT_TYPE, Constants.APPLICATION_JSON);
String json = String.format("{ \"event\": { \"token\": \"%s\", \"siteKey\": \"%s\" } }", reCaptchaResponse,
Expand All @@ -521,8 +522,6 @@ public static HttpResponse makeCaptchaVerificationHttpRequest(ReCaptchaResponseT
}
} else {
// For ReCaptcha v2 and v3.
String reCaptchaSecretKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY);

httpPost = new HttpPost(reCaptchaVerifyUrl);
List<BasicNameValuePair> params = Arrays.asList(new BasicNameValuePair("secret", reCaptchaSecretKey),
new BasicNameValuePair("response", reCaptchaResponse.getToken()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Response getCaptcha(String captchaType, String recoveryType, String tenan
Boolean.valueOf(properties.getProperty(CaptchaConstants.FORCEFULLY_ENABLED_RECAPTCHA_FOR_ALL_TENANTS));
ReCaptchaPropertiesDTO reCaptchaPropertiesDTO = new ReCaptchaPropertiesDTO();

if ((reCaptchaEnabled || reCaptchaEnterpriseEnabled) && (forcefullyEnabledRecaptchaForAllTenants ||
if (reCaptchaEnabled && (forcefullyEnabledRecaptchaForAllTenants ||
RecoveryUtil.checkCaptchaEnabledResidentIdpConfiguration(tenantDomain, recoveryType))) {
reCaptchaPropertiesDTO.setReCaptchaEnabled(reCaptchaEnabled);
reCaptchaPropertiesDTO.setReCaptchaKey(properties.getProperty(CaptchaConstants.RE_CAPTCHA_SITE_KEY));
Expand All @@ -88,7 +88,7 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
boolean reCaptchaEnterpriseEnabled =
Boolean.valueOf(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENTERPRISE_ENABLED));

if (!reCaptchaEnabled && !reCaptchaEnterpriseEnabled) {
if (!reCaptchaEnabled) {
RecoveryUtil.handleBadRequest("ReCaptcha is disabled", Constants.INVALID);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,18 @@ public static void buildReCaptchaFilterProperties() throws CaptchaServerExceptio
CaptchaDataHolder.getInstance().setReCaptchaErrorRedirectUrls(reCaptchaFailedRedirectUrls);
}

if (reCaptchaEnabled && reCaptchaEnterpriseEnabled) {
throw new CaptchaServerException("Both reCaptcha and reCaptchaEnterprise " +
"cannot be enabled at the same time.");
if (!reCaptchaEnabled && reCaptchaEnterpriseEnabled) {
throw new CaptchaServerException("reCaptcha is not enabled. Please enable reCaptcha to use " +
"reCaptcha Enterprise.");
}

if (reCaptchaEnabled) {
CaptchaDataHolder.getInstance().setReCaptchaEnabled(true);
CaptchaDataHolder.getInstance().setReCaptchaEnterpriseEnabled(false);
resolveSecrets(properties);
setReCaptchaConfigs(properties);
//setSSOLoginConnectorConfigs(properties);
//setPathBasedConnectorConfigs(properties);
} else if (reCaptchaEnterpriseEnabled) {
CaptchaDataHolder.getInstance().setReCaptchaEnabled(false);
CaptchaDataHolder.getInstance().setReCaptchaEnterpriseEnabled(true);
if (reCaptchaEnterpriseEnabled) {
CaptchaDataHolder.getInstance().setReCaptchaEnterpriseEnabled(true);
} else {
CaptchaDataHolder.getInstance().setReCaptchaEnterpriseEnabled(false);
}
resolveSecrets(properties);
setReCaptchaConfigs(properties);
//setSSOLoginConnectorConfigs(properties);
Expand Down Expand Up @@ -535,10 +532,7 @@ private static UserStoreManager getUserStoreManagerForUser(String userName,

private static void setReCaptchaConfigs(Properties properties) {

boolean recaptchaEnterpriseEnabled =
Boolean.parseBoolean(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENTERPRISE_ENABLED));

CaptchaDataHolder.getInstance().setReCaptchaEnterpriseEnabled(recaptchaEnterpriseEnabled);
boolean recaptchaEnterpriseEnabled = CaptchaDataHolder.getInstance().isReCaptchaEnterpriseEnabled();

if (recaptchaEnterpriseEnabled){
// ReCaptcha Enterprise require Project ID.
Expand Down

0 comments on commit 27a285a

Please sign in to comment.