Skip to content

Commit

Permalink
Changes based on comments in recaptcha enterprise
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakshan-Banneheke committed Sep 11, 2023
1 parent bdadd5c commit fd3ae49
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
Expand Down Expand Up @@ -90,34 +89,22 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
HttpEntity entity = response.getEntity();
ReCaptchaVerificationResponseDTO reCaptchaVerificationResponseDTO = new ReCaptchaVerificationResponseDTO();

if (CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE.equals(reCaptchaType)) {
// For ReCaptcha Enterprise.

if (entity == null) {
RecoveryUtil.handleBadRequest("ReCaptcha Enterprise verification response is not received.",
RecoveryUtil.handleBadRequest("ReCaptcha verification response is not received.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
try {
try (InputStream in = entity.getContent()) {
JsonObject verificationResponse = new JsonParser().parse(IOUtils.toString(in)).getAsJsonObject();
try (InputStream in = entity.getContent()) {
JsonObject verificationResponse = new JsonParser().parse(IOUtils.toString(in)).getAsJsonObject();

if (CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE.equals(reCaptchaType)) {
// For Recaptcha Enterprise
JsonObject tokenProperties = verificationResponse.get(CaptchaConstants.CAPTCHA_TOKEN_PROPERTIES)
.getAsJsonObject();
boolean success = tokenProperties.get(CaptchaConstants.CAPTCHA_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 {
// 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();
} else {
// For ReCaptcha v2 and v3.
reCaptchaVerificationResponseDTO.setSuccess(verificationResponse.get(
CaptchaConstants.CAPTCHA_SUCCESS).getAsBoolean());
}
Expand All @@ -126,7 +113,6 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
RecoveryUtil.handleBadRequest("Unable to read the verification response.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
}

return Response.ok(reCaptchaVerificationResponseDTO).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
# 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 @@ -76,7 +76,7 @@ public class CaptchaConstants {
public static final String CAPTCHA_TOKEN_PROPERTIES = "tokenProperties";
public static final String CAPTCHA_RISK_ANALYSIS = "riskAnalysis";
// Captcha Types.
public static final String RE_CAPTCHA_TYPE_ENTERPRISE = "enterprise";
public static final String RE_CAPTCHA_TYPE_ENTERPRISE = "recaptcha-enterprise";

// Default value for threshold for score in reCAPTCHA v3.
public static final double CAPTCHA_V3_DEFAULT_THRESHOLD = 0.5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,46 @@ public static Map<String, String> getClaimValues(User user, int tenantId,
return claimValues;
}

public static boolean isValidCaptcha(String reCaptchaResponse) throws CaptchaException {

CloseableHttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build();
String reCaptchaType = CaptchaDataHolder.getInstance().getReCaptchaType();

HttpPost httpPost;

// If the reCaptcha type is defined and, it is enterprise, the enterprise process will be done. Otherwise,
// the reCaptcha v2/v3 process will be done.
if (CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE.equals(reCaptchaType)) {
// For ReCaptcha Enterprise.
httpPost = createReCaptchaEnterpriseVerificationHttpPost(reCaptchaResponse);
} else {
// For ReCaptcha v2 and v3.
httpPost = createReCaptchaVerificationHttpPost(reCaptchaResponse);
}

HttpResponse response;
try {
response = httpclient.execute(httpPost);
} catch (IOException e) {
throw new CaptchaServerException("Unable to get the verification response.", e);
}

HttpEntity entity = response.getEntity();
if (entity == null) {
throw new CaptchaServerException("reCaptcha verification response is not received.");
}

if (CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE.equals(reCaptchaType)) {
// For ReCaptcha Enterprise.
verifyReCaptchaEnterpriseResponse(entity);
} else {
// For Recaptcha v2 and v3.
verifyReCaptchaResponse(entity);
}

return true;
}

private static HttpPost createReCaptchaEnterpriseVerificationHttpPost(String reCaptchaResponse) {

HttpPost httpPost;
Expand Down Expand Up @@ -394,46 +434,6 @@ private static void verifyReCaptchaResponse(HttpEntity entity)
}
}

public static boolean isValidCaptcha(String reCaptchaResponse) throws CaptchaException {

CloseableHttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build();
String reCaptchaType = CaptchaDataHolder.getInstance().getReCaptchaType();

HttpPost httpPost;

// If the reCaptcha type is defined and, it is enterprise, the enterprise process will be done. Otherwise,
// the reCaptcha v2/v3 process will be done.
if (CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE.equals(reCaptchaType)) {
// For ReCaptcha Enterprise.
httpPost = createReCaptchaEnterpriseVerificationHttpPost(reCaptchaResponse);
} else {
// For ReCaptcha v2 and v3.
httpPost = createReCaptchaVerificationHttpPost(reCaptchaResponse);
}

HttpResponse response;
try {
response = httpclient.execute(httpPost);
} catch (IOException e) {
throw new CaptchaServerException("Unable to get the verification response.", e);
}

HttpEntity entity = response.getEntity();
if (entity == null) {
throw new CaptchaServerException("reCaptcha verification response is not received.");
}

if (CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE.equals(reCaptchaType)) {
// For ReCaptcha Enterprise.
verifyReCaptchaEnterpriseResponse(entity);
} else {
// For Recaptcha v2 and v3.
verifyReCaptchaResponse(entity);
}

return true;
}

public static boolean isMaximumFailedLoginAttemptsReached(String usernameWithDomain, String tenantDomain) throws
CaptchaException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.captcha.internal.CaptchaDataHolder;
Expand All @@ -32,7 +31,8 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static org.testng.Assert.*;

import static org.testng.Assert.assertThrows;

/**
* Unit tests for CaptchaUtil.java
Expand Down

0 comments on commit fd3ae49

Please sign in to comment.