Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hwupathum committed Jul 3, 2024
1 parent 1659602 commit cf6f252
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ private static Properties validateCaptchaConfigs(Properties properties) {
* @param reCaptchaResponse ReCaptcha response token
* @param properties ReCaptcha properties
* @return httpResponse
*
* @deprecated Please create a new method with apache httpclient 5.x version
*/
@Deprecated
public static HttpResponse makeCaptchaVerificationHttpRequest(ReCaptchaResponseTokenDTO reCaptchaResponse,
Properties properties) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
package org.wso2.carbon.identity.recovery.endpoint.impl;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.wso2.carbon.http.client.HttpClientConstants;
import org.wso2.carbon.http.client.exception.HttpClientException;
import org.wso2.carbon.http.client.handler.JsonResponseHandler;
import org.wso2.carbon.http.client.request.HttpPostRequest;
import org.wso2.carbon.identity.captcha.internal.CaptchaDataHolder;
import org.wso2.carbon.identity.captcha.util.CaptchaConstants;
import org.wso2.carbon.identity.recovery.endpoint.CaptchaApiService;
import org.wso2.carbon.identity.recovery.endpoint.Constants;
Expand All @@ -33,7 +35,8 @@
import org.wso2.carbon.identity.recovery.endpoint.dto.ReCaptchaVerificationResponseDTO;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.ws.rs.core.Response;

Expand Down Expand Up @@ -85,17 +88,12 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
RecoveryUtil.handleBadRequest("ReCaptcha is disabled", Constants.INVALID);
}

HttpResponse response = RecoveryUtil.makeCaptchaVerificationHttpRequest(reCaptchaResponse, properties);
HttpEntity entity = response.getEntity();
HttpPost httpPost = makeCaptchaVerificationHttpRequest(reCaptchaResponse, properties);
ReCaptchaVerificationResponseDTO reCaptchaVerificationResponseDTO = new ReCaptchaVerificationResponseDTO();

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();

try {
JsonObject verificationResponse = CaptchaDataHolder.getInstance().getHttpClientService()
.getClosableHttpClient(CaptchaApiServiceImpl.class.getName()).execute(httpPost, new JsonResponseHandler());
if (CaptchaConstants.RE_CAPTCHA_TYPE_ENTERPRISE.equals(reCaptchaType)) {
// For Recaptcha Enterprise.
JsonObject tokenProperties = verificationResponse.get(CaptchaConstants.CAPTCHA_TOKEN_PROPERTIES)
Expand All @@ -107,12 +105,30 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
reCaptchaVerificationResponseDTO.setSuccess(verificationResponse.get(
CaptchaConstants.CAPTCHA_SUCCESS).getAsBoolean());
}
} catch (IOException e) {
} catch (HttpClientException e) {
if (HttpClientConstants.Error.RESPONSE_ENTITY_EMPTY.getCode().equals(e.getErrorCode())) {
RecoveryUtil.handleBadRequest("ReCaptcha verification response is not received.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
}
log.error("Unable to read the verification response.", e);
RecoveryUtil.handleBadRequest("Unable to read the verification response.",
Constants.STATUS_INTERNAL_SERVER_ERROR_MESSAGE_DEFAULT);
} 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.ok(reCaptchaVerificationResponseDTO).build();
}

private HttpPost makeCaptchaVerificationHttpRequest(ReCaptchaResponseTokenDTO reCaptchaResponse,
Properties properties) {

String reCaptchaSecretKey = properties.getProperty(CaptchaConstants.RE_CAPTCHA_SECRET_KEY);
String reCaptchaVerifyUrl = properties.getProperty(CaptchaConstants.RE_CAPTCHA_VERIFY_URL);
Map<String, String> params = new HashMap<>();
params.put("secret", reCaptchaSecretKey);
params.put("response", reCaptchaResponse.getToken());
return HttpPostRequest.createUrlEncodedRequest(reCaptchaVerifyUrl, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public static boolean isValidCaptcha(String reCaptchaResponse) throws CaptchaExc
if (HttpClientConstants.Error.RESPONSE_ENTITY_EMPTY.getCode().equals(e.getErrorCode())) {
throw new CaptchaServerException("reCaptcha verification response is not received.");
}
throw new CaptchaServerException("Unable to read the verification response.", e.getCause());
throw new CaptchaServerException("Unable to read the verification response.", e);
} catch (IOException e) {
throw new CaptchaServerException("Unable to get the verification response.", e);
}
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,8 @@
<identity.data.publisher.authentication.version>5.3.27</identity.data.publisher.authentication.version>

<!--Carbon Kernel Version-->
<carbon.kernel.version>4.10.12-SNAPSHOT</carbon.kernel.version>
<carbon.kernel.feature.version>4.10.12-SNAPSHOT</carbon.kernel.feature.version>
<carbon.kernel.version>4.10.17-SNAPSHOT</carbon.kernel.version>
<carbon.kernel.feature.version>4.10.17-SNAPSHOT</carbon.kernel.feature.version>
<carbon.kernel.package.import.version.range>[4.5.0, 5.0.0)</carbon.kernel.package.import.version.range>
<carbon.user.api.imp.pkg.version.range>[1.0.1, 2.0.0)</carbon.user.api.imp.pkg.version.range>
<carbon.kernel.registry.imp.pkg.version.range>[1.0.1, 2.0.0)</carbon.kernel.registry.imp.pkg.version.range>
Expand Down

0 comments on commit cf6f252

Please sign in to comment.