Skip to content

Commit

Permalink
Change code logic using reCaptcha type
Browse files Browse the repository at this point in the history
  • Loading branch information
AwesomeNipun committed Jun 19, 2023
1 parent 0416a18 commit 5cd52be
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@

@ApiModel(description = "")
public class ReCaptchaPropertiesDTO {



private Boolean reCaptchaEnabled = null;


private Boolean reCaptchaEnterpriseEnabled = null;
private String reCaptchaType = null;


private String reCaptchaKey = null;


private String reCaptchaAPI = null;


/**
**/
@ApiModelProperty(value = "")
Expand All @@ -40,12 +40,12 @@ public void setReCaptchaEnabled(Boolean reCaptchaEnabled) {
/**
**/
@ApiModelProperty(value = "")
@JsonProperty("reCaptchaEnterpriseEnabled")
public Boolean getReCaptchaEnterpriseEnabled() {
return reCaptchaEnterpriseEnabled;
@JsonProperty("reCaptchaType")
public String getReCaptchaType() {
return reCaptchaType;
}
public void setReCaptchaEnterpriseEnabled(Boolean reCaptchaEnterpriseEnabled) {
this.reCaptchaEnterpriseEnabled = reCaptchaEnterpriseEnabled;
public void setReCaptchaType(String reCaptchaType) {
this.reCaptchaType = reCaptchaType;
}


Expand Down Expand Up @@ -80,7 +80,7 @@ public String toString() {
sb.append("class ReCaptchaPropertiesDTO {\n");

sb.append(" reCaptchaEnabled: ").append(reCaptchaEnabled).append("\n");
sb.append(" reCaptchaEnterpriseEnabled: ").append(reCaptchaEnterpriseEnabled).append("\n");
sb.append(" reCaptchaEnterpriseEnabled: ").append(reCaptchaType).append("\n");
sb.append(" reCaptchaKey: ").append(reCaptchaKey).append("\n");
sb.append(" reCaptchaAPI: ").append(reCaptchaAPI).append("\n");
sb.append("}\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
Expand Down Expand Up @@ -453,32 +452,32 @@ public static Properties getValidatedCaptchaConfigs() {
private static Properties validateCaptchaConfigs(Properties properties) {

boolean reCaptchaEnabled = Boolean.valueOf(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENABLED));
String reCaptchaType = properties.getProperty(CaptchaConstants.RE_CAPTCHA_TYPE);

boolean reCaptchaEnterpriseEnabled = Boolean.valueOf(properties.getProperty(CaptchaConstants
.RE_CAPTCHA_ENTERPRISE_ENABLED));

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);
}
if (StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_API_URL))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_API_URL),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_SECRET_KEY),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_VERIFY_URL))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_VERIFY_URL),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (reCaptchaEnterpriseEnabled &&
StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_PROJECT_ID))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants
.RE_CAPTCHA_PROJECT_ID), Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (reCaptchaEnabled && 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);
}
if (StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_API_URL))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_API_URL),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (reCaptchaEnabled && StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_SECRET_KEY),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_VERIFY_URL))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_VERIFY_URL),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (reCaptchaEnabled && StringUtils.isBlank(reCaptchaType)) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants.RE_CAPTCHA_TYPE),
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
if (reCaptchaType.equals(CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE) &&
StringUtils.isBlank(properties.getProperty(CaptchaConstants.RE_CAPTCHA_PROJECT_ID))) {
RecoveryUtil.handleBadRequest(String.format("%s is not found ", CaptchaConstants
.RE_CAPTCHA_PROJECT_ID), Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
return properties;
}
Expand All @@ -494,45 +493,19 @@ public static HttpResponse makeCaptchaVerificationHttpRequest(ReCaptchaResponseT
Properties properties) {

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));
String reCaptchaVerifyUrl = properties.getProperty(CaptchaConstants.RE_CAPTCHA_VERIFY_URL);
CloseableHttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build();
HttpPost httpPost;

if (reCaptchaEnterpriseEnabled) {
// For ReCaptcha Enterprise.
String projectID = properties.getProperty(CaptchaConstants.RE_CAPTCHA_PROJECT_ID);
String siteKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SITE_KEY);
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,
siteKey);
StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8);
httpPost.setEntity(entity);

try {
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.
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);
} catch (IOException e) {
RecoveryUtil.handleBadRequest(String.format("Unable to get the verification response : %s",
e.getMessage()), Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
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));

try {
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);
}
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,20 @@ public Response getCaptcha(String captchaType, String recoveryType, String tenan

Properties properties = RecoveryUtil.getValidatedCaptchaConfigs();
boolean reCaptchaEnabled = Boolean.valueOf(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENABLED));
boolean reCaptchaEnterpriseEnabled =
Boolean.valueOf(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENTERPRISE_ENABLED));
String reCaptchaType = properties.getProperty(CaptchaConstants.RE_CAPTCHA_TYPE);
boolean forcefullyEnabledRecaptchaForAllTenants =
Boolean.valueOf(properties.getProperty(CaptchaConstants.FORCEFULLY_ENABLED_RECAPTCHA_FOR_ALL_TENANTS));
ReCaptchaPropertiesDTO reCaptchaPropertiesDTO = new ReCaptchaPropertiesDTO();

if (reCaptchaEnabled && (forcefullyEnabledRecaptchaForAllTenants ||
RecoveryUtil.checkCaptchaEnabledResidentIdpConfiguration(tenantDomain, recoveryType))) {
reCaptchaPropertiesDTO.setReCaptchaEnabled(reCaptchaEnabled);
reCaptchaPropertiesDTO.setReCaptchaEnabled(true);
reCaptchaPropertiesDTO.setReCaptchaKey(properties.getProperty(CaptchaConstants.RE_CAPTCHA_SITE_KEY));
reCaptchaPropertiesDTO.setReCaptchaAPI(properties.getProperty(CaptchaConstants.RE_CAPTCHA_API_URL));
reCaptchaPropertiesDTO.setReCaptchaEnterpriseEnabled(reCaptchaEnterpriseEnabled);
reCaptchaPropertiesDTO.setReCaptchaType(reCaptchaType);
return Response.ok(reCaptchaPropertiesDTO).build();
} else {
reCaptchaPropertiesDTO.setReCaptchaEnabled(false);
reCaptchaPropertiesDTO.setReCaptchaEnterpriseEnabled(false);
return Response.ok(reCaptchaPropertiesDTO).build();
}
}
Expand All @@ -85,8 +83,7 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin

Properties properties = RecoveryUtil.getValidatedCaptchaConfigs();
boolean reCaptchaEnabled = Boolean.valueOf(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENABLED));
boolean reCaptchaEnterpriseEnabled =
Boolean.valueOf(properties.getProperty(CaptchaConstants.RE_CAPTCHA_ENTERPRISE_ENABLED));
String reCaptchaType = properties.getProperty(CaptchaConstants.RE_CAPTCHA_TYPE);

if (!reCaptchaEnabled) {
RecoveryUtil.handleBadRequest("ReCaptcha is disabled", Constants.INVALID);
Expand All @@ -96,7 +93,7 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
HttpEntity entity = response.getEntity();
ReCaptchaVerificationResponseDTO reCaptchaVerificationResponseDTO = new ReCaptchaVerificationResponseDTO();

if (reCaptchaEnterpriseEnabled) {
if (reCaptchaType.equals(CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE)) {
// For ReCaptcha Enterprise.
if (entity == null) {
RecoveryUtil.handleBadRequest("ReCaptcha Enterprise verification response is not received.",
Expand All @@ -114,7 +111,7 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
RecoveryUtil.handleBadRequest("Unable to read the verification response.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
} else {
} else if (reCaptchaType.equals(CaptchaConstants.RE_CAPTCHA_TYPE_DEFAULT)){
// For ReCaptcha v2 and v3.
try {
if (entity == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,8 @@ definitions:
properties:
reCaptchaEnabled:
type: boolean
reCaptchaEnterpriseEnabled:
type: boolean
reCaptchaType:
type: string
reCaptchaKey:
type: string
reCaptchaAPI:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# Enable Google reCAPTCHA
recaptcha.enabled=true

# Google reCAPTCHA type
recaptcha.type=default

# reCaptcha API URL
recaptcha.api.url=https://www.google.com/recaptcha/api.js

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class CaptchaDataHolder {

private boolean reCaptchaEnabled;

private boolean reCaptchaEnterpriseEnabled;
private String reCaptchaType;

private String reCaptchaAPIUrl;

Expand Down Expand Up @@ -88,14 +88,14 @@ public void setReCaptchaEnabled(boolean reCaptchaEnabled) {
this.reCaptchaEnabled = reCaptchaEnabled;
}

public boolean isReCaptchaEnterpriseEnabled() {
public String getReCaptchaType() {

return reCaptchaEnterpriseEnabled;
return reCaptchaType;
}

public void setReCaptchaEnterpriseEnabled(boolean reCaptchaEnterpriseEnabled) {
public void setReCaptchaType(String reCaptchaType) {

this.reCaptchaEnterpriseEnabled = reCaptchaEnterpriseEnabled;
this.reCaptchaType = reCaptchaType;
}

public String getReCaptchaProjectID() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CaptchaConstants {

public static final String RE_CAPTCHA_ENABLED = "recaptcha.enabled";

public static final String RE_CAPTCHA_ENTERPRISE_ENABLED = "recaptcha.enterprise.enabled";
public static final String RE_CAPTCHA_TYPE = "recaptcha.type";

public static final String FORCEFULLY_ENABLED_RECAPTCHA_FOR_ALL_TENANTS = "recaptcha" +
".forcefullyEnabledForAllTenants";
Expand Down Expand Up @@ -70,8 +70,10 @@ public class CaptchaConstants {
public static final String AUTH_FAILURE_MSG = "authFailureMsg";
public static final String RECAPTCHA_FAIL_MSG_KEY = "recaptcha.fail.message";
public static final String TRUE = "true";

public static final String CAPTCHA_VALID = "valid";
// Captcha Types.
public static final String RE_CAPTCHA_TYPE_DEFAULT = "default";
public static final String RE_CAPTCHA_TYPE_ENTERPRISE = "enterprise";

// 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 5cd52be

Please sign in to comment.