Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Apache httpclient 5 #831

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.slf4j.MDC;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.http.client.request.HttpPostRequest;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.captcha.util.CaptchaConstants;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
Expand Down Expand Up @@ -50,6 +51,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -403,7 +405,11 @@ private static Properties validateCaptchaConfigs(Properties properties) {
* @param reCaptchaResponse ReCaptcha response token
* @param properties ReCaptcha properties
* @return httpResponse
*
* @deprecated Please use {@link #makeCaptchaVerificationHttpRequest(String reCaptchaResponseToken,
* Properties properties)} for httpclient 5.x version
*/
@Deprecated
public static HttpResponse makeCaptchaVerificationHttpRequest(ReCaptchaResponseTokenDTO reCaptchaResponse,
Properties properties) {

Expand All @@ -425,6 +431,24 @@ public static HttpResponse makeCaptchaVerificationHttpRequest(ReCaptchaResponseT
return response;
}

/**
* Make HTTP call for ReCaptcha Verification with the provided ReCaptcha response token.
*
* @param reCaptchaResponseToken ReCaptcha response token.
* @param properties ReCaptcha properties.
* @return HTTP Response
*/
public static org.apache.hc.client5.http.classic.methods.HttpPost makeCaptchaVerificationHttpRequest(
String reCaptchaResponseToken, 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", reCaptchaResponseToken);
return HttpPostRequest.createUrlEncodedRequest(reCaptchaVerifyUrl, params);
}

/**
* Resolves site-key, secret-key and any other property if they are configured using secure vault.
*
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 @@ -78,24 +81,19 @@ public Response verifyCaptcha(ReCaptchaResponseTokenDTO reCaptchaResponse, Strin
}

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

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

HttpResponse response = RecoveryUtil.makeCaptchaVerificationHttpRequest(reCaptchaResponse, properties);
HttpEntity entity = response.getEntity();
HttpPost httpPost = RecoveryUtil.makeCaptchaVerificationHttpRequest(reCaptchaResponse.getToken(), 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,10 +105,17 @@ 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();
Expand Down
17 changes: 10 additions & 7 deletions components/org.wso2.carbon.identity.captcha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
</dependency>
<dependency>
<groupId>org.wso2.orbit.org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.wso2</groupId>
<artifactId>httpcore</artifactId>
<groupId>org.wso2.orbit.org.apache.httpcomponents</groupId>
<artifactId>httpcore5</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
Expand Down Expand Up @@ -81,6 +81,10 @@
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.http.client</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.core</artifactId>
Expand Down Expand Up @@ -161,10 +165,8 @@
org.apache.commons.lang; version="${commons-lang.wso2.osgi.version.range}",
org.apache.commons.collections; version="${commons-collections.wso2.osgi.version.range}",
org.apache.commons.logging.*; version="${commons-logging.osgi.version.range}",
org.apache.http.*,
org.apache.http.client.*;version="${httpcomponents-httpclient.imp.pkg.version.range}",
org.apache.http.impl.client.*;version="${httpcomponents-httpclient.imp.pkg.version.range}",
org.apache.http.message.*;version="${httpcore.osgi.version.range}",
org.apache.hc.client5.http.*;version="${httpclient5.osgi.version.range}",
org.apache.hc.core5.*;version="${httpclient5.osgi.version.range}",

javax.servlet.*; version="${imp.pkg.version.javax.servlet}",

Expand Down Expand Up @@ -199,6 +201,7 @@
org.wso2.carbon.context; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.identity.multi.attribute.login.mgt.*;
version="${carbon.identity.framework.imp.pkg.version.range}",
org.wso2.carbon.http.client.*; version="${carbon.kernel.package.import.version.range}",
</Import-Package>
</instructions>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,21 @@ protected void unsetAccountLockService(AccountLockService accountLockService) {

CaptchaDataHolder.getInstance().setAccountLockService(null);
}

@Reference(
name = "HttpClientService",
service = org.wso2.carbon.http.client.services.HttpClientService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetHttpClientService"
)
protected void setHttpClientService(org.wso2.carbon.http.client.services.HttpClientService httpClientService) {

CaptchaDataHolder.getInstance().setHttpClientService(httpClientService);
}

protected void unsetHttpClientService(org.wso2.carbon.http.client.services.HttpClientService httpClientService) {

CaptchaDataHolder.getInstance().setHttpClientService(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.identity.captcha.internal;

import org.wso2.carbon.http.client.services.HttpClientService;
import org.wso2.carbon.identity.captcha.connector.CaptchaConnector;
import org.wso2.carbon.identity.governance.IdentityGovernanceService;
import org.wso2.carbon.identity.handler.event.account.lock.service.AccountLockService;
Expand Down Expand Up @@ -77,6 +78,8 @@ public class CaptchaDataHolder {

private boolean forcefullyEnabledRecaptchaForAllTenants;

private HttpClientService httpClientService;

private CaptchaDataHolder() {

}
Expand Down Expand Up @@ -268,4 +271,14 @@ public void setForcefullyEnabledRecaptchaForAllTenants(boolean forcefullyEnabled

this.forcefullyEnabledRecaptchaForAllTenants = forcefullyEnabledRecaptchaForAllTenants;
}

public HttpClientService getHttpClientService() {

return httpClientService;
}

public void setHttpClientService(HttpClientService httpClientService) {

this.httpClientService = httpClientService;
}
}
Loading
Loading