From 070fb0e51bb91abe9fcff78e332b4bf671ce00a5 Mon Sep 17 00:00:00 2001 From: AwesomeNipun Date: Tue, 25 Apr 2023 02:58:27 +0530 Subject: [PATCH] Refactor code --- .../recovery/endpoint/Utils/RecoveryUtil.java | 49 +++--- .../endpoint/impl/CaptchaApiServiceImpl.java | 36 ++-- .../captcha/internal/CaptchaDataHolder.java | 12 -- .../captcha/util/CaptchaConstants.java | 4 +- .../identity/captcha/util/CaptchaUtil.java | 155 +++++++++--------- .../conf/captcha-config.properties.j2 | 6 + 6 files changed, 122 insertions(+), 140 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.user.recovery/src/main/java/org/wso2/carbon/identity/recovery/endpoint/Utils/RecoveryUtil.java b/components/org.wso2.carbon.identity.api.user.recovery/src/main/java/org/wso2/carbon/identity/recovery/endpoint/Utils/RecoveryUtil.java index 1b0b23ebcf..b5df0c669a 100644 --- a/components/org.wso2.carbon.identity.api.user.recovery/src/main/java/org/wso2/carbon/identity/recovery/endpoint/Utils/RecoveryUtil.java +++ b/components/org.wso2.carbon.identity.api.user.recovery/src/main/java/org/wso2/carbon/identity/recovery/endpoint/Utils/RecoveryUtil.java @@ -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 @@ -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 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 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); diff --git a/components/org.wso2.carbon.identity.api.user.recovery/src/main/java/org/wso2/carbon/identity/recovery/endpoint/impl/CaptchaApiServiceImpl.java b/components/org.wso2.carbon.identity.api.user.recovery/src/main/java/org/wso2/carbon/identity/recovery/endpoint/impl/CaptchaApiServiceImpl.java index 0e3afef249..69b05831e6 100644 --- a/components/org.wso2.carbon.identity.api.user.recovery/src/main/java/org/wso2/carbon/identity/recovery/endpoint/impl/CaptchaApiServiceImpl.java +++ b/components/org.wso2.carbon.identity.api.user.recovery/src/main/java/org/wso2/carbon/identity/recovery/endpoint/impl/CaptchaApiServiceImpl.java @@ -95,17 +95,18 @@ 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); @@ -113,18 +114,15 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin 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); diff --git a/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/internal/CaptchaDataHolder.java b/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/internal/CaptchaDataHolder.java index 57a6198930..f55f563301 100644 --- a/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/internal/CaptchaDataHolder.java +++ b/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/internal/CaptchaDataHolder.java @@ -47,8 +47,6 @@ public class CaptchaDataHolder { private String reCaptchaSecretKey; - private String reCaptchaAPIKey; - private String reCaptchaProjectID; private String reCaptchaErrorRedirectUrls; @@ -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; diff --git a/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/util/CaptchaConstants.java b/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/util/CaptchaConstants.java index 66f18a4025..2c3605157f 100644 --- a/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/util/CaptchaConstants.java +++ b/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/util/CaptchaConstants.java @@ -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"; @@ -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; diff --git a/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/util/CaptchaUtil.java b/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/util/CaptchaUtil.java index 88a258435e..07971de6cd 100644 --- a/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/util/CaptchaUtil.java +++ b/components/org.wso2.carbon.identity.captcha/src/main/java/org/wso2/carbon/identity/captcha/util/CaptchaUtil.java @@ -18,15 +18,16 @@ package org.wso2.carbon.identity.captcha.util; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.commons.collections.MapUtils; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.entity.StringEntity; @@ -36,7 +37,6 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator; import org.wso2.carbon.identity.application.authentication.framework.config.ConfigurationFacade; @@ -274,55 +274,67 @@ public static boolean isValidCaptcha(String reCaptchaResponse) throws CaptchaExc final double scoreThreshold = CaptchaDataHolder.getInstance().getReCaptchaScoreThreshold(); boolean isRecaptchaEnterpriseEnabled = CaptchaDataHolder.getInstance().isReCaptchaEnterpriseEnabled(); - HttpPost httppost; - if (!isRecaptchaEnterpriseEnabled) { // for recaptcha v2 and v3 + HttpPost httpPost; + if (isRecaptchaEnterpriseEnabled) { + // For ReCaptcha Enterprise. + String recaptchaUrl = CaptchaDataHolder.getInstance().getReCaptchaVerifyUrl(); + String projectID = CaptchaDataHolder.getInstance().getReCaptchaProjectID(); + String siteKey = CaptchaDataHolder.getInstance().getReCaptchaSiteKey(); + String secretKey = CaptchaDataHolder.getInstance().getReCaptchaSecretKey(); - httppost = new HttpPost(CaptchaDataHolder.getInstance().getReCaptchaVerifyUrl()); - List params = Arrays.asList(new BasicNameValuePair("secret", CaptchaDataHolder - .getInstance().getReCaptchaSecretKey()), new BasicNameValuePair("response", reCaptchaResponse)); - httppost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8)); + String verifyUrl = recaptchaUrl + "/v1/projects/" + projectID + "/assessments?key=" + secretKey; + httpPost = new HttpPost(verifyUrl); + + httpPost.setHeader(HttpHeaders.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); HttpResponse response; try { - response = httpclient.execute(httppost); + response = httpclient.execute(httpPost); } catch (IOException e) { - throw new CaptchaServerException("Unable to get the verification response.", e); + throw new CaptchaServerException("Unable to get the verification response."); } - HttpEntity entity = response.getEntity(); - if (entity == null) { + HttpEntity responseEntity = response.getEntity(); + + if (responseEntity == null) { throw new CaptchaServerException("reCaptcha verification response is not received."); } try { - try (InputStream in = entity.getContent()) { - JsonObject verificationResponse = new JsonParser().parse(IOUtils.toString(in)).getAsJsonObject(); + try (InputStream in = responseEntity.getContent()) { + JsonElement jsonElement = JsonParser.parseReader(new InputStreamReader(in, StandardCharsets.UTF_8)); + JsonObject verificationResponse = jsonElement.getAsJsonObject(); if (verificationResponse == null) { throw new CaptchaClientException("Error receiving reCaptcha response from the server"); } - boolean success = verificationResponse.get(CaptchaConstants.CAPTCHA_SUCCESS) != null - && verificationResponse.get(CaptchaConstants.CAPTCHA_SUCCESS).getAsBoolean(); + JsonObject tokenProperties = verificationResponse.get("tokenProperties").getAsJsonObject(); + boolean success = tokenProperties.get(CaptchaConstants.CAPTCHA_VALID).getAsBoolean(); + + JsonObject riskAnalysis = verificationResponse.get("riskAnalysis").getAsJsonObject(); + // Whether this request was a valid reCAPTCHA token. if (!success) { throw new CaptchaClientException("reCaptcha token is invalid. Error:" + verificationResponse.get("error-codes")); } - if (verificationResponse.get(CaptchaConstants.CAPTCHA_SCORE) != null) { - double score = verificationResponse.get(CaptchaConstants.CAPTCHA_SCORE).getAsDouble(); - // reCAPTCHA v3 response contains score + if (riskAnalysis.get(CaptchaConstants.CAPTCHA_SCORE) != null) { + double score = riskAnalysis.get(CaptchaConstants.CAPTCHA_SCORE).getAsDouble(); + // reCAPTCHA enterprise response contains score. if (log.isDebugEnabled()) { - log.debug("reCAPTCHA v3 response { timestamp:" + - verificationResponse.get("challenge_ts") + ", action: " + - verificationResponse.get("action") + ", score: " + score + " }"); + log.debug("reCAPTCHA Enterprise response { timestamp:" + + tokenProperties.get("createTime") + ", action: " + + tokenProperties.get("action") + ", score: " + score + " }"); } if (score < scoreThreshold) { throw new CaptchaClientException("reCaptcha score is less than the threshold."); } - } else { - if (log.isDebugEnabled()) { - log.debug("reCAPTCHA v2 response { timestamp:" + - verificationResponse.get("challenge_ts") + " }"); - } } } } catch (IOException e) { @@ -330,66 +342,56 @@ public static boolean isValidCaptcha(String reCaptchaResponse) throws CaptchaExc } catch (ClassCastException e) { throw new CaptchaServerException("Unable to cast the response value.", e); } - - } else { // for recaptcha enterprise - - String recaptchaUrl = CaptchaDataHolder.getInstance().getReCaptchaVerifyUrl(); - String projectID = CaptchaDataHolder.getInstance().getReCaptchaProjectID(); - String APIKey = CaptchaDataHolder.getInstance().getReCaptchaAPIKey(); - String siteKey = CaptchaDataHolder.getInstance().getReCaptchaSiteKey(); - - String verifyUrl = recaptchaUrl + "/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); + } else { + // For Recaptcha v2 and v3. + httpPost = new HttpPost(CaptchaDataHolder.getInstance().getReCaptchaVerifyUrl()); + List params = Arrays.asList(new BasicNameValuePair("secret", CaptchaDataHolder + .getInstance().getReCaptchaSecretKey()), + new BasicNameValuePair("response", reCaptchaResponse)); + httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8)); HttpResponse response; try { - response = httpclient.execute(httppost); + response = httpclient.execute(httpPost); } catch (IOException e) { - throw new CaptchaServerException("Unable to get the verification response."); + throw new CaptchaServerException("Unable to get the verification response.", e); } - HttpEntity responseEntity = response.getEntity(); - - if (responseEntity == null) { + HttpEntity entity = response.getEntity(); + if (entity == null) { throw new CaptchaServerException("reCaptcha verification response is not received."); } try { - try (InputStream in = responseEntity.getContent()) { - JsonObject verificationResponse = new JsonParser().parse(IOUtils.toString(in)).getAsJsonObject(); + try (InputStream in = entity.getContent()) { + JsonElement jsonElement = JsonParser.parseReader(new InputStreamReader(in, StandardCharsets.UTF_8)); + JsonObject verificationResponse = jsonElement.getAsJsonObject(); if (verificationResponse == null) { throw new CaptchaClientException("Error receiving reCaptcha response from the server"); } - JsonObject tokenProperties = verificationResponse.get("tokenProperties").getAsJsonObject(); - boolean success = tokenProperties.get("valid").getAsBoolean(); - - JsonObject riskAnalysis = verificationResponse.get("riskAnalysis").getAsJsonObject(); - + boolean success = verificationResponse.get(CaptchaConstants.CAPTCHA_SUCCESS) != null + && verificationResponse.get(CaptchaConstants.CAPTCHA_SUCCESS).getAsBoolean(); // Whether this request was a valid reCAPTCHA token. if (!success) { throw new CaptchaClientException("reCaptcha token is invalid. Error:" + verificationResponse.get("error-codes")); } - if (riskAnalysis.get("score") != null) { - double score = riskAnalysis.get("score").getAsDouble(); - // reCAPTCHA enterprise response contains score + if (verificationResponse.get(CaptchaConstants.CAPTCHA_SCORE) != null) { + double score = verificationResponse.get(CaptchaConstants.CAPTCHA_SCORE).getAsDouble(); + // reCAPTCHA v3 response contains score. if (log.isDebugEnabled()) { - log.debug("reCAPTCHA Enterprise response { timestamp:" + - tokenProperties.get("createTime") + ", action: " + - tokenProperties.get("action") + ", score: " + score + " }"); + log.debug("reCAPTCHA v3 response { timestamp:" + + verificationResponse.get("challenge_ts") + ", action: " + + verificationResponse.get("action") + ", score: " + score + " }"); } if (score < scoreThreshold) { throw new CaptchaClientException("reCaptcha score is less than the threshold."); } + } else { + if (log.isDebugEnabled()) { + log.debug("reCAPTCHA v2 response { timestamp:" + + verificationResponse.get("challenge_ts") + " }"); + } } } } catch (IOException e) { @@ -398,7 +400,6 @@ public static boolean isValidCaptcha(String reCaptchaResponse) throws CaptchaExc throw new CaptchaServerException("Unable to cast the response value.", e); } } - return true; } @@ -523,27 +524,15 @@ private static void setReCaptchaConfigs(Properties properties) { boolean recaptchaEnterpriseEnabled = Boolean.parseBoolean(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENTERPRISE_ENABLED)); - if (recaptchaEnterpriseEnabled){ // reCaptcha Enterprise require API key and Project ID - CaptchaDataHolder.getInstance().setReCaptchaEnterpriseEnabled(true); - String reCaptchaAPIKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_API_KEY); - if (StringUtils.isBlank((reCaptchaAPIKey))) { - throw new RuntimeException(getValidationErrorMessage(CaptchaConstants.RE_CAPTCHA_API_KEY)); - } - CaptchaDataHolder.getInstance().setReCaptchaAPIKey(reCaptchaAPIKey); + CaptchaDataHolder.getInstance().setReCaptchaEnterpriseEnabled(recaptchaEnterpriseEnabled); + if (recaptchaEnterpriseEnabled){ + // ReCaptcha Enterprise require Project ID. String reCaptchaProjectID = properties.getProperty(CaptchaConstants.RE_CAPTCHA_PROJECT_ID); if (StringUtils.isBlank(reCaptchaProjectID)) { throw new RuntimeException(getValidationErrorMessage(CaptchaConstants.RE_CAPTCHA_PROJECT_ID)); } CaptchaDataHolder.getInstance().setReCaptchaProjectID(reCaptchaProjectID); - - } else { // reCaptcha V2 and V3 require Secret key - CaptchaDataHolder.getInstance().setReCaptchaEnterpriseEnabled(false); - String reCaptchaSecretKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY); - if (StringUtils.isBlank(reCaptchaSecretKey)) { - throw new RuntimeException(getValidationErrorMessage(CaptchaConstants.RE_CAPTCHA_SECRET_KEY)); - } - CaptchaDataHolder.getInstance().setReCaptchaSecretKey(reCaptchaSecretKey); } String reCaptchaAPIUrl = properties.getProperty(CaptchaConstants.RE_CAPTCHA_API_URL); @@ -564,6 +553,12 @@ private static void setReCaptchaConfigs(Properties properties) { } CaptchaDataHolder.getInstance().setReCaptchaSiteKey(reCaptchaSiteKey); + String reCaptchaSecretKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY); + if (StringUtils.isBlank(reCaptchaSecretKey)) { + throw new RuntimeException(getValidationErrorMessage(CaptchaConstants.RE_CAPTCHA_SECRET_KEY)); + } + CaptchaDataHolder.getInstance().setReCaptchaSecretKey(reCaptchaSecretKey); + String reCaptchaRequestWrapUrls = properties.getProperty(CaptchaConstants.RE_CAPTCHA_REQUEST_WRAP_URLS); if (reCaptchaRequestWrapUrls == null) { throw new RuntimeException(getValidationErrorMessage(CaptchaConstants.RE_CAPTCHA_REQUEST_WRAP_URLS)); diff --git a/features/org.wso2.carbon.identity.captcha.server.feature/resources/conf/captcha-config.properties.j2 b/features/org.wso2.carbon.identity.captcha.server.feature/resources/conf/captcha-config.properties.j2 index 45078ab9ef..88ec472a13 100644 --- a/features/org.wso2.carbon.identity.captcha.server.feature/resources/conf/captcha-config.properties.j2 +++ b/features/org.wso2.carbon.identity.captcha.server.feature/resources/conf/captcha-config.properties.j2 @@ -21,6 +21,9 @@ # Enable Google reCAPTCHA recaptcha.enabled={{recaptcha.enabled}} +# Enable Google reCAPTCHA Enterprise +recaptcha.enterprise.enabled={{recaptcha.enterprise_enabled}} + # Forcefully enable Google reCAPTCHA for all tenants recaptcha.forcefullyEnabledForAllTenants={{recaptcha.forcefully_enabled_for_all_tenants}} @@ -36,6 +39,9 @@ recaptcha.site.key={{recaptcha.site_key}} # reCaptcha secret key recaptcha.secret.key={{recaptcha.secret_key}} +# reCaptcha Enterprise project id +recaptcha.project.id={{recaptcha.project_id}} + # login.do URL paths {% if recaptcha.redirect_urls is defined %} recaptcha.failed.redirect.urls={{recaptcha.redirect_urls}}