Skip to content

Commit

Permalink
Merge pull request #16233 from shanggeeth/httpclient-cookie-store
Browse files Browse the repository at this point in the history
Change default cookie specs provider to RFC6265CookieSpecProvider
  • Loading branch information
shanggeeth authored Jul 14, 2023
2 parents b0e52f8 + c951707 commit 2801d29
Show file tree
Hide file tree
Showing 43 changed files with 913 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Lookup;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
Expand Down Expand Up @@ -65,6 +71,8 @@
*/
public class CrossProtocolLogoutTestCase extends ISIntegrationTest {

private Lookup<CookieSpecProvider> cookieSpecRegistry;
private RequestConfig requestConfig;
private HttpClient client;
private final String OIDC_APP_NAME = "playground2";
private final String SAML_ISSUER = "travelocity.com";
Expand Down Expand Up @@ -93,7 +101,16 @@ public void testInit() throws Exception {
createServiceProvider();
createSAMLApplication();

client = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).build();
cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
client = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore())
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();
SAML_SSO_URL = identityContextUrls.getWebAppURLHttps() + "/samlsso";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Lookup;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
Expand Down Expand Up @@ -102,6 +108,8 @@ public class EmailOTPTestCase extends ISIntegrationTest {
private static final String profileName = "default";
private static final String TENANT_DOMAIN_PARAM = "tenantDomain";

private Lookup<CookieSpecProvider> cookieSpecRegistry;
private RequestConfig requestConfig;
private HttpClient httpClient;
private ApplicationManagementServiceClient applicationManagementServiceClient;
private SAMLSSOConfigServiceClient ssoConfigServiceClient;
Expand Down Expand Up @@ -134,8 +142,17 @@ public void testInit() throws Exception {
identityProviderMgtServiceClient = new IdentityProviderMgtServiceClient(sessionCookie, backendURL,
configContext);
remoteUSMServiceClient = new RemoteUserStoreManagerServiceClient(backendURL, sessionCookie);
httpClient = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).build();

cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
httpClient = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore())
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();
createUser();
createApplication();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Lookup;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.message.BasicNameValuePair;
import org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationConfig;
import org.wso2.carbon.identity.application.common.model.xsd.InboundAuthenticationRequestConfig;
Expand Down Expand Up @@ -72,6 +75,9 @@ public class AbstractApplicationAuthzTestCase extends ISIntegrationTest {
protected HttpClient httpClientAzUser;
protected HttpClient httpClientNonAzUser;

protected Lookup<CookieSpecProvider> cookieSpecRegistry;
protected RequestConfig requestConfig;

protected HttpResponse sendSAMLMessage(String url, String samlMsgKey, String samlMsgValue) throws IOException {

List<NameValuePair> urlParameters = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
Expand Down Expand Up @@ -102,8 +107,20 @@ public void testInit() throws Exception {
remoteUSMServiceClient = new RemoteUserStoreManagerServiceClient(backendURL, sessionCookie);
entitlementPolicyClient = new EntitlementPolicyServiceClient(backendURL, sessionCookie);

httpClientAzUser = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).build();
httpClientNonAzUser = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).build();
cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
httpClientAzUser = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore())
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();
httpClientNonAzUser = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore())
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();

createRole(AZ_TEST_TENANT_ROLE);
createUser(AZ_TEST_TENANT_USER, AZ_TEST_TENANT_USER_PW, new String[]{AZ_TEST_TENANT_ROLE});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
Expand Down Expand Up @@ -99,8 +104,20 @@ public void testInit() throws Exception {
remoteUSMServiceClient = new RemoteUserStoreManagerServiceClient(backendURL, sessionCookie);
entitlementPolicyClient = new EntitlementPolicyServiceClient(backendURL, sessionCookie);

httpClientAzUser = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).build();
httpClientNonAzUser = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).build();
cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
httpClientAzUser = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore())
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();
httpClientNonAzUser = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore())
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();

createRole(AZ_TEST_ROLE);
createUser(AZ_TEST_USER, AZ_TEST_USER_PW, new String[]{AZ_TEST_ROLE});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Lookup;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.wso2.carbon.automation.engine.context.AutomationContext;
import org.wso2.carbon.automation.engine.frameworkutils.FrameworkPathUtil;
import org.wso2.carbon.automation.extensions.servers.carbonserver.MultipleServersManager;
import org.wso2.carbon.identity.application.common.model.idp.xsd.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.xsd.ServiceProvider;
Expand Down Expand Up @@ -286,7 +290,17 @@ public void closeHttpConnection(HttpResponse response) throws IOException {

public HttpClient getNewHttpClientWithCookieStore() {

return HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).build();
Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
RequestConfig requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
return HttpClientBuilder.create()
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(new BasicCookieStore())
.build();
}

private String getSecureServiceUrl(int portOffset, String baseUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Lookup;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
Expand Down Expand Up @@ -84,6 +90,8 @@ public class AdaptiveScriptTemporaryClaimPersistenceTestCase extends AbstractAda
private ApplicationManagementServiceClient applicationManagementServiceClient;
private CookieStore cookieStore = new BasicCookieStore();
private CloseableHttpClient client;
private Lookup<CookieSpecProvider> cookieSpecRegistry;
private RequestConfig requestConfig;
private HttpResponse response;
private String idToken;
private AuthorizationCode authorizationCode;
Expand All @@ -110,7 +118,15 @@ public void testInit() throws Exception {
applicationManagementServiceClient = new ApplicationManagementServiceClient(sessionCookie, backendURL,
configContext);

cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
client = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.disableRedirectHandling()
.setDefaultCookieStore(cookieStore)
.build();
Expand Down Expand Up @@ -336,7 +352,10 @@ private HttpResponse sendExistingSessionConsentGetRequest(CookieStore cookieStor
List<NameValuePair> consentRequiredClaimsFromResponse)
throws Exception {

HttpClient httpClientWithoutAutoRedirections = HttpClientBuilder.create().disableRedirectHandling()
HttpClient httpClientWithoutAutoRedirections = HttpClientBuilder.create()
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.setDefaultRequestConfig(requestConfig)
.disableRedirectHandling()
.setDefaultCookieStore(cookieStore).build();
consentRequiredClaimsFromResponse.addAll(Utils.getConsentRequiredClaimsFromResponse(response));
Header locationHeader = response.getFirstHeader(OAuth2Constant.HTTP_RESPONSE_HEADER_LOCATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Lookup;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
Expand Down Expand Up @@ -81,6 +87,8 @@ public class RiskBasedLoginTestCase extends AbstractAdaptiveAuthenticationTestCa
private ApplicationManagementServiceClient applicationManagementServiceClient;
private WebAppAdminClient webAppAdminClient;
private CookieStore cookieStore = new BasicCookieStore();
private Lookup<CookieSpecProvider> cookieSpecRegistry;
private RequestConfig requestConfig;
private HttpClient client;
private HttpResponse response;
private List<NameValuePair> consentParameters = new ArrayList<>();
Expand Down Expand Up @@ -150,9 +158,17 @@ public void testInit() throws Exception {
configContext);
webAppAdminClient = new WebAppAdminClient(backendURL, sessionCookie);

cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
client = HttpClientBuilder.create()
.disableRedirectHandling()
.setDefaultCookieStore(cookieStore)
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();

String script = getConditionalAuthScript("RiskBasedLoginScript.js");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Lookup;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.RFC6265CookieSpecProvider;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
Expand Down Expand Up @@ -101,6 +107,8 @@ public class ExtendSessionEndpointAuthCodeGrantTestCase extends OAuth2ServiceAbs
private static final String SESSION_EXTENDER_ENDPOINT_GET_URL = SESSION_EXTENDER_ENDPOINT_URL + "?%s=%s";
private static final String SESSIONS_ENDPOINT_URI = "https://localhost:9853/api/users/v1/me/sessions";

private Lookup<CookieSpecProvider> cookieSpecRegistry;
private RequestConfig requestConfig;
private CloseableHttpClient firstPartyClient;
private CloseableHttpClient thirdPartyClient;
private String sessionDataKey;
Expand All @@ -120,8 +128,23 @@ public void testInit() throws Exception {
context = isServer;
this.authenticatingUserName = context.getContextTenant().getContextUser().getUserName();
this.authenticatingCredential = context.getContextTenant().getContextUser().getPassword();
firstPartyClient = HttpClientBuilder.create().disableRedirectHandling().build();
thirdPartyClient = HttpClientBuilder.create().disableRedirectHandling().build();

cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
.register(CookieSpecs.DEFAULT, new RFC6265CookieSpecProvider())
.build();
requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build();
firstPartyClient = HttpClientBuilder.create()
.disableRedirectHandling()
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();
thirdPartyClient = HttpClientBuilder.create()
.disableRedirectHandling()
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieSpecRegistry(cookieSpecRegistry)
.build();
}

@AfterClass(alwaysRun = true)
Expand Down
Loading

0 comments on commit 2801d29

Please sign in to comment.