diff --git a/modules/integration/tests-integration/tests-backend/pom.xml b/modules/integration/tests-integration/tests-backend/pom.xml index 51350a63419..ebf130c74ac 100644 --- a/modules/integration/tests-integration/tests-backend/pom.xml +++ b/modules/integration/tests-integration/tests-backend/pom.xml @@ -458,19 +458,6 @@ run - - packaging-war-artifacts-oidc - process-test-resources - - - - - - - - run - - packaging-war-artifacts-passivests process-test-resources diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/auth/PasswordlessSMSOTPAuthTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/auth/PasswordlessSMSOTPAuthTestCase.java index 527dad0a3fb..85de57987ae 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/auth/PasswordlessSMSOTPAuthTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/auth/PasswordlessSMSOTPAuthTestCase.java @@ -39,7 +39,7 @@ import org.testng.annotations.Factory; import org.testng.annotations.Test; import org.wso2.carbon.automation.engine.context.TestUserMode; -import org.wso2.identity.integration.test.base.MockClientCallback; +import org.wso2.identity.integration.test.base.MockApplicationServer; import org.wso2.identity.integration.test.base.MockSMSProvider; import org.wso2.identity.integration.test.oidc.OIDCAbstractIntegrationTest; import org.wso2.identity.integration.test.oidc.OIDCUtilTest; @@ -90,7 +90,7 @@ public class PasswordlessSMSOTPAuthTestCase extends OIDCAbstractIntegrationTest private String authorizationCode; private MockSMSProvider mockSMSProvider; - private MockClientCallback mockClientCallback; + private MockApplicationServer mockApplicationServer; private TestUserMode userMode; @@ -116,8 +116,8 @@ public void testInit() throws Exception { mockSMSProvider = new MockSMSProvider(); mockSMSProvider.start(); - mockClientCallback = new MockClientCallback(); - mockClientCallback.start(); + mockApplicationServer = new MockApplicationServer(); + mockApplicationServer.start(); super.init(); @@ -170,7 +170,7 @@ public void atEnd() throws Exception { scim2RestClient.closeHttpClient(); mockSMSProvider.stop(); - mockClientCallback.stop(); + mockApplicationServer.stop(); } @Test(groups = "wso2.is", description = "Test passwordless authentication with SMS OTP") @@ -189,7 +189,7 @@ private void sendAuthorizeRequest() throws Exception { List urlParameters = new ArrayList<>(); urlParameters.add(new BasicNameValuePair("response_type", OAuth2Constant.OAUTH2_GRANT_TYPE_CODE)); urlParameters.add(new BasicNameValuePair("client_id", oidcApplication.getClientId())); - urlParameters.add(new BasicNameValuePair("redirect_uri", MockClientCallback.CALLBACK_URL)); + urlParameters.add(new BasicNameValuePair("redirect_uri", oidcApplication.getCallBackURL())); urlParameters.add(new BasicNameValuePair("scope", "openid")); @@ -212,7 +212,7 @@ private void performUserLogin() throws Exception { HttpResponse response = sendLoginPostForOtp(client, sessionDataKey, mockSMSProvider.getOTP()); EntityUtils.consume(response.getEntity()); - authorizationCode = mockClientCallback.getAuthorizationCode(); + authorizationCode = mockApplicationServer.getAuthorizationCodeForApp(oidcApplication.getApplicationName()); assertNotNull(authorizationCode); } @@ -241,7 +241,7 @@ private HttpResponse sendTokenRequestForCodeGrant() throws Exception { List urlParameters = new ArrayList<>(); urlParameters.add(new BasicNameValuePair("code", authorizationCode)); urlParameters.add(new BasicNameValuePair("grant_type", OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE)); - urlParameters.add(new BasicNameValuePair("redirect_uri", MockClientCallback.CALLBACK_URL)); + urlParameters.add(new BasicNameValuePair("redirect_uri", oidcApplication.getCallBackURL())); urlParameters.add(new BasicNameValuePair("client_id", oidcApplication.getClientSecret())); urlParameters.add(new BasicNameValuePair("scope", "openid")); @@ -259,9 +259,8 @@ private HttpResponse sendTokenRequestForCodeGrant() throws Exception { private OIDCApplication initOIDCApplication() { - OIDCApplication playgroundApp = new OIDCApplication(OIDCUtilTest.playgroundAppOneAppName, - OIDCUtilTest.playgroundAppOneAppContext, - MockClientCallback.CALLBACK_URL); + OIDCApplication playgroundApp = new OIDCApplication(MockApplicationServer.Constants.APP1.NAME, + MockApplicationServer.Constants.APP1.CALLBACK_URL); return playgroundApp; } diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/auth/SecondaryStoreUserLoginTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/auth/SecondaryStoreUserLoginTestCase.java index 598c449f16a..e23be6dec76 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/auth/SecondaryStoreUserLoginTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/auth/SecondaryStoreUserLoginTestCase.java @@ -208,7 +208,7 @@ private void checkAuthorizationCode(String sessionDataKeyConsent) throws Excepti private void createAndRegisterPlaygroundApplication() throws Exception { - playgroundApp = new OIDCApplication(PLAYGROUND_APP_NAME, PLAYGROUND_APP_CONTEXT, PLAYGROUND_APP_CALLBACK_URI); + playgroundApp = new OIDCApplication(PLAYGROUND_APP_NAME, PLAYGROUND_APP_CALLBACK_URI); playgroundApp.addRequiredClaim(OIDCUtilTest.emailClaimUri); playgroundApp.addRequiredClaim(OIDCUtilTest.firstNameClaimUri); playgroundApp.addRequiredClaim(OIDCUtilTest.lastNameClaimUri); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/MockApplicationServer.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/MockApplicationServer.java new file mode 100644 index 00000000000..ab5455d8fd8 --- /dev/null +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/MockApplicationServer.java @@ -0,0 +1,231 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.identity.integration.test.base; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; +import com.github.tomakehurst.wiremock.extension.ResponseTransformerV2; +import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; +import com.github.tomakehurst.wiremock.http.Response; +import com.github.tomakehurst.wiremock.stubbing.ServeEvent; +import org.wso2.identity.integration.common.utils.ISIntegrationTest; +import org.wso2.identity.integration.test.util.Utils; + +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.matching; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; + +/** + * Mock application server to test OIDC related flows. + */ +public class MockApplicationServer { + + public static class MockClient { + private final AtomicReference authorizationCode = new AtomicReference<>(); + private final AtomicReference errorCode = new AtomicReference<>(); + + public AtomicReference getAuthorizationCode() { + return authorizationCode; + } + + public AtomicReference getErrorCode() { + return errorCode; + } + } + + public static class Constants { + public static class APP1 { + public static final String CALLBACK_URL = "https://localhost:8091/dummyApp/oauth2client"; + public static final String NAME = "playground.appone"; + public static final String CALLBACK_URL_PATH = "/dummyApp/oauth2client"; + } + + public static class APP2 { + public static final String CALLBACK_URL = "https://localhost:8091/dummyApp2/oauth2client"; + public static final String NAME = "playground.apptwo"; + public static final String CALLBACK_URL_PATH = "/dummyApp2/oauth2client"; + } + } + + private final Map apps = new HashMap<>(); + + private WireMockServer wireMockServer; + + public MockApplicationServer() { + + MockClient app1 = new MockClient(); + MockClient app2 = new MockClient(); + apps.put(Constants.APP1.NAME, app1); + apps.put(Constants.APP2.NAME, app2); + } + + public void start() { + + wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig() + .httpsPort(8091) + .httpDisabled(true) + .keystorePath(Paths.get(Utils.getResidentCarbonHome(), "repository", "resources", "security", + ISIntegrationTest.KEYSTORE_NAME).toAbsolutePath().toString()) + .keystorePassword("wso2carbon") + .keyManagerPassword("wso2carbon") + .extensions(new ResponseTemplateTransformer(null, true, null, null), + new ResponseTransformerV2() { + + @Override + public Response transform(Response response, ServeEvent serveEvent) { + + AtomicReference authorizationCode + = (AtomicReference) serveEvent.getTransformerParameters().get("code"); + authorizationCode.set(serveEvent.getRequest().getQueryParams().get("code") + .firstValue()); + return response; + } + + @Override + public boolean applyGlobally() { + return false; + } + + @Override + public String getName() { + return "authz-code-transformer"; + } + }, + new ResponseTransformerV2() { + + @Override + public Response transform(Response response, ServeEvent serveEvent) { + + AtomicReference errorCode + = (AtomicReference) serveEvent.getTransformerParameters().get("error"); + errorCode.set(serveEvent.getRequest().getQueryParams().get("error").firstValue()); + return response; + } + + @Override + public boolean applyGlobally() { + return false; + } + + @Override + public String getName() { + return "error-code-transformer"; + } + })); + + wireMockServer.start(); + + // Configure the mock client endpoints for App 1 + configureMockEndpointsForApp(Constants.APP1.CALLBACK_URL_PATH, apps.get(Constants.APP1.NAME)); + // Configure the mock client endpoints for App 2 + configureMockEndpointsForApp(Constants.APP2.CALLBACK_URL_PATH, apps.get(Constants.APP2.NAME)); + } + + public void stop() { + + if (wireMockServer != null) { + wireMockServer.stop(); + } + } + + private void configureMockEndpointsForApp(String urlPath, MockClient app) { + + try { + wireMockServer.stubFor(get(urlPathEqualTo(urlPath)) + .withQueryParam("code", matching(".*")) + .willReturn(aResponse() + .withTransformers("response-template", "authz-code-transformer") + .withTransformerParameter("code", app.getAuthorizationCode()) + .withTransformerParameter("error", app.getErrorCode()) + .withStatus(200))); + wireMockServer.stubFor(post(urlPathEqualTo(urlPath)) + .withQueryParam("code", matching(".*")) + .willReturn(aResponse() + .withTransformers("response-template", "authz-code-transformer") + .withTransformerParameter("code", app.getAuthorizationCode()) + .withTransformerParameter("error", app.getErrorCode()) + .withStatus(200))); + wireMockServer.stubFor(get(urlPathEqualTo(urlPath)) + .withQueryParam("code", matching(".*")) + .withQueryParam("session_state", matching(".*")) + .willReturn(aResponse() + .withTransformers("response-template", "authz-code-transformer") + .withTransformerParameter("code", app.getAuthorizationCode()) + .withTransformerParameter("error", app.getErrorCode()) + .withStatus(200))); + wireMockServer.stubFor(post(urlPathEqualTo(urlPath)) + .withQueryParam("code", matching(".*")) + .withQueryParam("session_state", matching(".*")) + .willReturn(aResponse() + .withTransformers("response-template", "authz-code-transformer") + .withTransformerParameter("code", app.getAuthorizationCode()) + .withTransformerParameter("error", app.getErrorCode()) + .withStatus(200))); + wireMockServer.stubFor(get(urlPathEqualTo(urlPath)) + .withQueryParam("error_description", matching(".*")) + .withQueryParam("error", matching(".*")) + .willReturn(aResponse() + .withTransformers("response-template", "error-code-transformer") + .withTransformerParameter("code", app.getAuthorizationCode()) + .withTransformerParameter("error", app.getErrorCode()) + .withStatus(200))); + wireMockServer.stubFor(get(urlEqualTo(urlPath)) + .willReturn(aResponse() + .withTransformers("response-template") + .withStatus(200))); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public void verifyLogoutRedirectionForApp(String appName) { + + wireMockServer.verify(getRequestedFor(urlEqualTo(getCallbackUrlPath(appName)))); + } + + public String getAuthorizationCodeForApp(String appName) { + + return apps.get(appName).getAuthorizationCode().get(); + } + + public String getErrorCode(String appName) { + + return apps.get(appName).getErrorCode().get(); + } + + private String getCallbackUrlPath(String appName) { + switch (appName) { + case Constants.APP1.NAME: + return Constants.APP1.CALLBACK_URL_PATH; + case Constants.APP2.NAME: + return Constants.APP2.CALLBACK_URL_PATH; + default: + throw new IllegalArgumentException("Unknown app name: " + appName); + } + } +} diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/MockClientCallback.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/MockClientCallback.java deleted file mode 100644 index 955f56ba48d..00000000000 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/MockClientCallback.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.identity.integration.test.base; - -import com.github.tomakehurst.wiremock.WireMockServer; -import com.github.tomakehurst.wiremock.core.WireMockConfiguration; -import com.github.tomakehurst.wiremock.extension.ResponseTransformerV2; -import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; -import com.github.tomakehurst.wiremock.http.Response; -import com.github.tomakehurst.wiremock.stubbing.ServeEvent; -import org.wso2.identity.integration.common.utils.ISIntegrationTest; -import org.wso2.identity.integration.test.util.Utils; - -import java.nio.file.Paths; -import java.util.concurrent.atomic.AtomicReference; - -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.get; -import static com.github.tomakehurst.wiremock.client.WireMock.matching; -import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; - -/** - * Mock client callback endpoint to test OIDC related flows. - */ -public class MockClientCallback { - - public static final String CALLBACK_URL = "https://localhost:8091/dummyApp/oauth2client"; - - private final AtomicReference authorizationCode = new AtomicReference<>(); - - private WireMockServer wireMockServer; - - public void start() { - - wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig() - .httpsPort(8091) - .httpDisabled(true) - .keystorePath(Paths.get(Utils.getResidentCarbonHome(), "repository", "resources", "security", - ISIntegrationTest.KEYSTORE_NAME).toAbsolutePath().toString()) - .keystorePassword("wso2carbon") - .keyManagerPassword("wso2carbon") - .extensions(new ResponseTemplateTransformer(null, true, null, null), - new ResponseTransformerV2() { - - @Override - public Response transform(Response response, ServeEvent serveEvent) { - - authorizationCode.set(serveEvent.getRequest().getQueryParams().get("code").firstValue()); - return response; - } - - @Override - public boolean applyGlobally() { - return false; - } - - @Override - public String getName() { - return "authz-code-transformer"; - } - })); - - wireMockServer.start(); - - // Configure the mock client endpoints. - configureMockEndpoints(); - } - - public void stop() { - - if (wireMockServer != null) { - wireMockServer.stop(); - } - } - - private void configureMockEndpoints() { - - try { - wireMockServer.stubFor(get(urlPathEqualTo("/dummyApp/oauth2client")) - .withQueryParam("code", matching(".*")) - .willReturn(aResponse() - .withTransformers("response-template", "authz-code-transformer") - .withStatus(200))); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public String getAuthorizationCode() { - - return authorizationCode.get(); - } -} diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/TomcatInitializerTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/TomcatInitializerTestCase.java index 743c9b3b19b..2fdc751b53c 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/TomcatInitializerTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/base/TomcatInitializerTestCase.java @@ -43,8 +43,6 @@ public class TomcatInitializerTestCase extends ISIntegrationTest { "travelocity.com-registrymount", "avis.com", "PassiveSTSSampleApp", - "playground.appone", - "playground.apptwo", "playground2" }; private static final Log LOG = LogFactory.getLog(TomcatInitializerTestCase.class); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oauth2/OAuth2TokenExchangeGrantTypeTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oauth2/OAuth2TokenExchangeGrantTypeTestCase.java index 13921edf7ae..813b6bbb3be 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oauth2/OAuth2TokenExchangeGrantTypeTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oauth2/OAuth2TokenExchangeGrantTypeTestCase.java @@ -566,8 +566,7 @@ private OAuthConsumerAppDTO getOAuthConsumerAppDTO(OIDCApplication application) private void updateServiceProviderWithOIDCConfigs(int portOffset, String applicationName, ServiceProvider serviceProvider) throws Exception { - OIDCApplication application = new OIDCApplication(applicationName, "/" + applicationName, - OAuth2Constant.CALLBACK_URL); + OIDCApplication application = new OIDCApplication(applicationName, OAuth2Constant.CALLBACK_URL); OAuthConsumerAppDTO appDTO = getOAuthConsumerAppDTO(application); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCAbstractIntegrationTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCAbstractIntegrationTest.java index 6742e9b8823..1f5dc59d813 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCAbstractIntegrationTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCAbstractIntegrationTest.java @@ -20,13 +20,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; +import org.apache.http.message.BasicNameValuePair; import org.testng.Assert; import org.wso2.carbon.automation.engine.context.TestUserMode; import org.wso2.identity.integration.test.oauth2.OAuth2ServiceAbstractIntegrationTest; @@ -53,6 +51,8 @@ import java.util.List; import java.util.Map; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.AUTHORIZE_ENDPOINT_URL; + /** * This class defines basic functionality needed to initiate an OIDC test. */ @@ -188,28 +188,19 @@ public void testSendAuthenticationRequest(OIDCApplication application, boolean i HttpClient client, CookieStore cookieStore) throws Exception { - List urlParameters = OIDCUtilTest.getNameValuePairs(application, - getTenantQualifiedURL(OAuth2Constant.APPROVAL_URL, tenantInfo.getDomain())); - - HttpResponse response = sendPostRequestWithParameters(client, urlParameters, String.format - (OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + OAuth2Constant.PlaygroundAppPaths - .appUserAuthorizePath)); + List urlParameters = new ArrayList<>(); + urlParameters.add(new BasicNameValuePair("response_type", OAuth2Constant.OAUTH2_GRANT_TYPE_CODE)); + urlParameters.add(new BasicNameValuePair("client_id", application.getClientId())); + urlParameters.add(new BasicNameValuePair("redirect_uri", application.getCallBackURL())); - Header locationHeader = response.getFirstHeader(OAuth2Constant.HTTP_RESPONSE_HEADER_LOCATION); - EntityUtils.consume(response.getEntity()); + urlParameters.add(new BasicNameValuePair("scope", "openid")); - if (isFirstAuthenticationRequest) { - response = sendGetRequest(client, locationHeader.getValue()); - } else { - HttpClient httpClientWithoutAutoRedirections = HttpClientBuilder.create().disableRedirectHandling() - .setDefaultCookieStore(cookieStore).build(); - response = sendGetRequest(httpClientWithoutAutoRedirections, locationHeader.getValue()); - } + HttpResponse response = sendPostRequestWithParameters(client, urlParameters, + getTenantQualifiedURL(AUTHORIZE_ENDPOINT_URL, tenantInfo.getDomain())); Map keyPositionMap = new HashMap<>(1); if (isFirstAuthenticationRequest) { OIDCUtilTest.setSessionDataKey(response, keyPositionMap); - } else { Assert.assertFalse(Utils.requestMissingClaims(response)); } diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCAuthCodeGrantSSOTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCAuthCodeGrantSSOTestCase.java index 730c5aed824..221ebf6e788 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCAuthCodeGrantSSOTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCAuthCodeGrantSSOTestCase.java @@ -33,6 +33,7 @@ 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.BasicHeader; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.simple.JSONValue; @@ -40,6 +41,7 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import org.wso2.identity.integration.test.base.MockApplicationServer; import org.wso2.identity.integration.test.oidc.bean.OIDCApplication; import org.wso2.identity.integration.test.rest.api.user.common.model.Email; import org.wso2.identity.integration.test.rest.api.user.common.model.Name; @@ -49,7 +51,6 @@ import org.wso2.identity.integration.test.utils.OAuth2Constant; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.util.ArrayList; @@ -57,6 +58,11 @@ import java.util.List; import java.util.Map; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.ACCESS_TOKEN_ENDPOINT; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.AUTHORIZATION_HEADER; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.AUTHORIZE_ENDPOINT_URL; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE; + /** * This test class tests OIDC SSO functionality for two replying party applications. */ @@ -76,6 +82,7 @@ public class OIDCAuthCodeGrantSSOTestCase extends OIDCAbstractIntegrationTest { protected RequestConfig requestConfig; protected HttpClient client; protected List consentParameters = new ArrayList<>(); + private MockApplicationServer mockApplicationServer; @BeforeClass(alwaysRun = true) public void testInit() throws Exception { @@ -100,6 +107,9 @@ public void testInit() throws Exception { .setDefaultCookieStore(cookieStore) .build(); + mockApplicationServer = new MockApplicationServer(); + mockApplicationServer.start(); + } @AfterClass(alwaysRun = true) @@ -108,6 +118,7 @@ public void testClear() throws Exception { deleteUser(user); deleteApplications(); clear(); + mockApplicationServer.stop(); } @Test(groups = "wso2.is", description = "Test authz endpoint before creating a valid session") @@ -122,12 +133,13 @@ public void testAuthzRequestWithoutValidSessionForIDENTITY5581() throws Exceptio .addParameter("prompt", "none") .addParameter("redirect_uri", application.getCallBackURL()).build(); HttpResponse httpResponse = sendGetRequest(client, uri.toString()); - String contentData = DataExtractUtil.getContentData(httpResponse); - Assert.assertTrue(contentData.contains("login_required")); + EntityUtils.consume(httpResponse.getEntity()); + Assert.assertTrue(mockApplicationServer.getErrorCode(application.getApplicationName()).contains("login_required")); } - @Test(groups = "wso2.is", description = "Initiate authentication request from playground.appone", dependsOnMethods = "testAuthzRequestWithoutValidSessionForIDENTITY5581") + @Test(groups = "wso2.is", description = "Initiate authentication request from playground.appone", + dependsOnMethods = "testAuthzRequestWithoutValidSessionForIDENTITY5581") public void testSendAuthenticationRequestFromRP1() throws Exception { testSendAuthenticationRequest(applications.get(OIDCUtilTest.playgroundAppOneAppName), true, client, cookieStore); @@ -164,7 +176,8 @@ public void testUserClaimsFromRP1() throws Exception { @Test(groups = "wso2.is", description = "Initiate authentication request from playground.apptwo") public void testSendAuthenticationRequestFromRP2() throws Exception { - testSendAuthenticationRequest(applications.get(OIDCUtilTest.playgroundAppTwoAppName), false, client, cookieStore); + testSendAuthenticationRequest(applications.get(OIDCUtilTest.playgroundAppTwoAppName), false, client, + cookieStore); } @Test(groups = "wso2.is", description = "Approve consent for playground.apptwo", dependsOnMethods = @@ -189,23 +202,22 @@ public void testUserClaimsFromRP2() throws Exception { } public void testSendAuthenticationRequest(OIDCApplication application, boolean isFirstAuthenticationRequest, - HttpClient client, CookieStore cookieStore) - throws Exception { - - List urlParameters = OIDCUtilTest.getNameValuePairs(application); - HttpResponse response = sendPostRequestWithParameters(client, urlParameters, String.format - (OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + OAuth2Constant.PlaygroundAppPaths - .appUserAuthorizePath)); - Assert.assertNotNull(response, "Authorization request failed for " + application.getApplicationName() + ". " - + "Authorized response is null"); + HttpClient client, CookieStore cookieStore) throws Exception { - Header locationHeader = response.getFirstHeader(OAuth2Constant.HTTP_RESPONSE_HEADER_LOCATION); + List urlParameters = new ArrayList<>(); + urlParameters.add(new BasicNameValuePair("response_type", OAuth2Constant.OAUTH2_GRANT_TYPE_CODE)); + urlParameters.add(new BasicNameValuePair("client_id", application.getClientId())); + urlParameters.add(new BasicNameValuePair("redirect_uri", application.getCallBackURL())); - Assert.assertNotNull(locationHeader, "Authorization request failed for " + application.getApplicationName() + - ". Authorized response header is null"); - EntityUtils.consume(response.getEntity()); + urlParameters.add(new BasicNameValuePair("scope", "openid email profile")); + HttpResponse response; if (isFirstAuthenticationRequest) { + response = sendPostRequestWithParameters(client, urlParameters, + getTenantQualifiedURL(AUTHORIZE_ENDPOINT_URL, tenantInfo.getDomain())); + Header locationHeader = response.getFirstHeader(OAuth2Constant.HTTP_RESPONSE_HEADER_LOCATION); + EntityUtils.consume(response.getEntity()); + response = sendGetRequest(client, locationHeader.getValue()); } else { HttpClient httpClientWithoutAutoRedirections = HttpClientBuilder.create() @@ -213,11 +225,12 @@ public void testSendAuthenticationRequest(OIDCApplication application, boolean i .setDefaultRequestConfig(requestConfig) .disableRedirectHandling() .setDefaultCookieStore(cookieStore).build(); - response = sendGetRequest(httpClientWithoutAutoRedirections, locationHeader.getValue()); + response = sendPostRequestWithParameters(httpClientWithoutAutoRedirections, urlParameters, + getTenantQualifiedURL(AUTHORIZE_ENDPOINT_URL, tenantInfo.getDomain())); } - Assert.assertNotNull(response, "Authorization request failed for " + application.getApplicationName() + ". " - + "Authorized user response is null."); + Assert.assertNotNull(response, "Authorization request failed for " + application.getApplicationName() + + ". Authorized user response is null."); Map keyPositionMap = new HashMap<>(1); if (isFirstAuthenticationRequest) { @@ -238,12 +251,11 @@ public void testSendAuthenticationRequest(OIDCApplication application, boolean i String pastrCookie = Utils.getPastreCookie(response); Assert.assertNotNull(pastrCookie, "pastr cookie not found in response."); EntityUtils.consume(response.getEntity()); - Header oauthConsentLocationHeader = consentLocationHeader; - Assert.assertNotNull(oauthConsentLocationHeader, "OAuth consent url is null for " + - oauthConsentLocationHeader.getValue()); + Assert.assertNotNull(consentLocationHeader, "OAuth consent url is null for " + + consentLocationHeader.getValue()); consentParameters.addAll(Utils.getConsentRequiredClaimsFromResponse(response)); - response = sendGetRequest(client, oauthConsentLocationHeader.getValue()); + response = sendGetRequest(client, consentLocationHeader.getValue()); keyPositionMap.put("name=\"sessionDataKeyConsent\"", 1); List keyValues = DataExtractUtil.extractSessionConsentDataFromResponse @@ -272,6 +284,7 @@ private void testAuthentication(OIDCApplication application) throws Exception { EntityUtils.consume(response.getEntity()); response = sendGetRequest(client, locationHeader.getValue()); + Map keyPositionMap = new HashMap<>(1); keyPositionMap.put("name=\"sessionDataKeyConsent\"", 1); List keyValues = DataExtractUtil.extractSessionConsentDataFromResponse(response, @@ -297,16 +310,10 @@ private void testConsentApproval(OIDCApplication application) throws Exception { EntityUtils.consume(response.getEntity()); response = sendPostRequest(client, locationHeader.getValue()); - Assert.assertNotNull(response, "Authorization code response is invalid for " + application.getApplicationName - ()); - - Map keyPositionMap = new HashMap<>(1); - keyPositionMap.put("Authorization Code", 1); - List keyValues = DataExtractUtil.extractTableRowDataFromResponse(response, - keyPositionMap); - Assert.assertNotNull(keyValues, "Authorization code not received for " + application.getApplicationName()); + Assert.assertNotNull(response, "Authorization code response is invalid for " + + application.getApplicationName()); - authorizationCode = keyValues.get(0).getValue(); + authorizationCode = mockApplicationServer.getAuthorizationCodeForApp(application.getApplicationName()); Assert.assertNotNull(authorizationCode, "Authorization code not received for " + application .getApplicationName()); EntityUtils.consume(response.getEntity()); @@ -314,35 +321,33 @@ private void testConsentApproval(OIDCApplication application) throws Exception { private void testGetAccessToken(OIDCApplication application) throws Exception { - HttpResponse response = sendGetAccessTokenPost(client, application); - Assert.assertNotNull(response, "Access token response is invalid for " + application.getApplicationName()); - EntityUtils.consume(response.getEntity()); - - response = sendPostRequest(client, String.format(OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + - OAuth2Constant.PlaygroundAppPaths.appAuthorizePath)); - - Map keyPositionMap = new HashMap<>(1); - keyPositionMap.put("name=\"accessToken\"", 1); - List keyValues = DataExtractUtil.extractInputValueFromResponse(response, - keyPositionMap); - Assert.assertNotNull(keyValues, "Access token not received for " + application.getApplicationName()); - - accessToken = keyValues.get(0).getValue(); - Assert.assertNotNull(accessToken, "Access token not received for " + application.getApplicationName()); - EntityUtils.consume(response.getEntity()); - - response = sendPostRequest(client, String.format(OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + - OAuth2Constant.PlaygroundAppPaths.appAuthorizePath)); - - keyPositionMap = new HashMap<>(1); - keyPositionMap.put("id=\"loggedUser\"", 1); - keyValues = DataExtractUtil.extractLabelValueFromResponse(response, keyPositionMap); - Assert.assertNotNull(keyValues, "No user logged in for " + application.getApplicationName()); + List urlParameters = new ArrayList<>(); + urlParameters.add(new BasicNameValuePair("code", authorizationCode)); + urlParameters.add(new BasicNameValuePair("grant_type", OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE)); + urlParameters.add(new BasicNameValuePair("redirect_uri", application.getCallBackURL())); + urlParameters.add(new BasicNameValuePair("client_id", application.getClientSecret())); + + urlParameters.add(new BasicNameValuePair("scope", "openid")); + + List
headers = new ArrayList<>(); + headers.add(new BasicHeader(AUTHORIZATION_HEADER, + OAuth2Constant.BASIC_HEADER + " " + getBase64EncodedString(application.getClientId(), + application.getClientSecret()))); + headers.add(new BasicHeader("Content-Type", "application/x-www-form-urlencoded")); + headers.add(new BasicHeader("User-Agent", OAuth2Constant.USER_AGENT)); + + HttpResponse response = sendPostRequest(client, headers, urlParameters, + getTenantQualifiedURL(ACCESS_TOKEN_ENDPOINT, tenantInfo.getDomain())); + String responseString = EntityUtils.toString(response.getEntity()); + Map responseMap = (Map) JSONValue.parse(responseString); + accessToken = (String) responseMap.get("access_token"); + + String idToken = (String) responseMap.get("id_token"); + String[] tokenParts = idToken.split("\\."); + String payload = new String(java.util.Base64.getUrlDecoder().decode(tokenParts[1])); + Map parsedIdToken = (Map) JSONValue.parse(payload); + Assert.assertNotNull(parsedIdToken.get("sub"), "No user logged in for " + application.getApplicationName()); - String loggedUser = keyValues.get(0).getValue(); - Assert.assertNotNull(loggedUser, "Logged user is null for " + application.getApplicationName()); - Assert.assertNotEquals(loggedUser, "null", "Logged user is null for " + application.getApplicationName()); - Assert.assertNotEquals(loggedUser, "", "Logged user is null for " + application.getApplicationName()); EntityUtils.consume(response.getEntity()); } @@ -376,14 +381,13 @@ protected void initUser() throws Exception { protected void initApplications() throws Exception { OIDCApplication playgroundApp = new OIDCApplication(OIDCUtilTest.playgroundAppOneAppName, - OIDCUtilTest.playgroundAppOneAppContext, OIDCUtilTest.playgroundAppOneAppCallBackUri); playgroundApp.addRequiredClaim(OIDCUtilTest.emailClaimUri); playgroundApp.addRequiredClaim(OIDCUtilTest.firstNameClaimUri); playgroundApp.addRequiredClaim(OIDCUtilTest.lastNameClaimUri); applications.put(OIDCUtilTest.playgroundAppOneAppName, playgroundApp); - playgroundApp = new OIDCApplication(OIDCUtilTest.playgroundAppTwoAppName, OIDCUtilTest.playgroundAppTwoAppContext, + playgroundApp = new OIDCApplication(OIDCUtilTest.playgroundAppTwoAppName, OIDCUtilTest.playgroundAppTwoAppCallBackUri); playgroundApp.addRequiredClaim(OIDCUtilTest.emailClaimUri); playgroundApp.addRequiredClaim(OIDCUtilTest.firstNameClaimUri); @@ -404,17 +408,4 @@ protected void deleteApplications() throws Exception { deleteApplication(entry.getValue()); } } - - protected HttpResponse sendGetAccessTokenPost(HttpClient client, OIDCApplication application) throws IOException { - - List urlParameters = new ArrayList<>(); - urlParameters.add(new BasicNameValuePair("callbackurl", application.getCallBackURL())); - urlParameters.add(new BasicNameValuePair("accessEndpoint", OAuth2Constant.ACCESS_TOKEN_ENDPOINT)); - urlParameters.add(new BasicNameValuePair("consumerSecret", application.getClientSecret())); - HttpResponse response = sendPostRequestWithParameters(client, urlParameters, String.format - (OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + OAuth2Constant.PlaygroundAppPaths - .accessTokenRequestPath)); - - return response; - } } diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCRPInitiatedLogoutTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCRPInitiatedLogoutTestCase.java index f9729d7711c..db6a2b48545 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCRPInitiatedLogoutTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCRPInitiatedLogoutTestCase.java @@ -32,6 +32,7 @@ import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import org.wso2.identity.integration.test.base.MockApplicationServer; import org.wso2.identity.integration.test.oidc.bean.OIDCApplication; import org.wso2.identity.integration.test.rest.api.user.common.model.Email; import org.wso2.identity.integration.test.rest.api.user.common.model.Name; @@ -45,6 +46,8 @@ import java.util.List; import java.util.Map; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.AUTHORIZE_ENDPOINT_URL; + /** * This test class tests the OIDC RP-Initiated logout flows */ @@ -62,6 +65,7 @@ public class OIDCRPInitiatedLogoutTestCase extends OIDCAbstractIntegrationTest { protected List consentParameters = new ArrayList<>(); OIDCApplication playgroundAppOne; OIDCApplication playgroundAppTwo; + private MockApplicationServer mockApplicationServer; @BeforeClass(alwaysRun = true) public void testInit() throws Exception { @@ -88,6 +92,9 @@ public void testInit() throws Exception { .setDefaultCookieSpecRegistry(cookieSpecRegistry) .setDefaultRequestConfig(requestConfig) .build(); + + mockApplicationServer = new MockApplicationServer(); + mockApplicationServer.start(); } @AfterClass(alwaysRun = true) @@ -97,6 +104,7 @@ public void testClear() throws Exception { deleteApplication(playgroundAppOne); deleteApplication(playgroundAppTwo); clear(); + mockApplicationServer.stop(); } @AfterMethod @@ -145,12 +153,14 @@ public void testOIDCLogoutPrecedence() throws Exception { private void testInitiateOIDCRequest(OIDCApplication application, HttpClient client) throws Exception { - List urlParameters = OIDCUtilTest.getNameValuePairs(application); - HttpResponse response = sendPostRequestWithParameters(client, urlParameters, String.format - (OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + - OAuth2Constant.PlaygroundAppPaths.appUserAuthorizePath)); - Assert.assertNotNull(response, "Authorization request failed for " + application.getApplicationName() + - ". Authorized response is null."); + List urlParameters = new ArrayList<>(); + urlParameters.add(new BasicNameValuePair("response_type", OAuth2Constant.OAUTH2_GRANT_TYPE_CODE)); + urlParameters.add(new BasicNameValuePair("client_id", application.getClientId())); + urlParameters.add(new BasicNameValuePair("redirect_uri", application.getCallBackURL())); + urlParameters.add(new BasicNameValuePair("scope", "openid email profile")); + + HttpResponse response = sendPostRequestWithParameters(client, urlParameters, + getTenantQualifiedURL(AUTHORIZE_ENDPOINT_URL, tenantInfo.getDomain())); Header locationHeader = response.getFirstHeader(OAuth2Constant.HTTP_RESPONSE_HEADER_LOCATION); @@ -196,13 +206,8 @@ private void testOIDCLogin(OIDCApplication application, boolean checkConsent) th sessionDataKeyConsent = keyValues.get(0).getValue(); Assert.assertNotNull(sessionDataKeyConsent, "sessionDataKeyConsent is null."); } else { - keyPositionMap.put("Authorization Code", 1); - List keyValues = DataExtractUtil.extractTableRowDataFromResponse(response, - keyPositionMap); - Assert.assertNotNull(keyValues, "Authorization code not received for " + - application.getApplicationName()); - - authorizationCode = new AuthorizationCode(keyValues.get(0).getValue()); + authorizationCode = new AuthorizationCode( + mockApplicationServer.getAuthorizationCodeForApp(application.getApplicationName())); Assert.assertNotNull(authorizationCode, "Authorization code not received for " + application .getApplicationName()); } @@ -221,17 +226,8 @@ private void testOIDCConsentApproval(OIDCApplication application) throws Excepti EntityUtils.consume(response.getEntity()); response = sendPostRequest(client, locationHeader.getValue()); - Assert.assertNotNull(response, "Authorization code response is invalid for " + - application.getApplicationName()); - - Map keyPositionMap = new HashMap<>(1); - keyPositionMap.put("Authorization Code", 1); - List keyValues = DataExtractUtil.extractTableRowDataFromResponse(response, - keyPositionMap); - Assert.assertNotNull(keyValues, "Authorization code not received for " + - application.getApplicationName()); - - authorizationCode = new AuthorizationCode(keyValues.get(0).getValue()); + authorizationCode = new AuthorizationCode( + mockApplicationServer.getAuthorizationCodeForApp(application.getApplicationName())); Assert.assertNotNull(authorizationCode, "Authorization code not received for " + application .getApplicationName()); EntityUtils.consume(response.getEntity()); @@ -296,10 +292,8 @@ private void testOIDCLogout(boolean checkSuccess, OIDCApplication application, B Assert.assertTrue(redirectUrl.contains(application.getCallBackURL()), "Not redirected to the" + "post logout redirect url"); response = sendGetRequest(client, redirectUrl); - Assert.assertNotNull(response, "OIDC Logout failed."); - String result = DataExtractUtil.getContentData(response); - Assert.assertTrue(result.contains("WSO2 OAuth2 Playground"), "OIDC logout failed."); EntityUtils.consume(response.getEntity()); + mockApplicationServer.verifyLogoutRedirectionForApp(application.getApplicationName()); } else { Assert.assertTrue(redirectUrl.contains("oauth2_error.do")); } @@ -320,7 +314,6 @@ protected void initUser() { protected OIDCApplication initApplicationOne() { playgroundAppOne = new OIDCApplication(OIDCUtilTest.playgroundAppOneAppName, - OIDCUtilTest.playgroundAppOneAppContext, OIDCUtilTest.playgroundAppOneAppCallBackUri); playgroundAppOne.addRequiredClaim(OIDCUtilTest.emailClaimUri); playgroundAppOne.addRequiredClaim(OIDCUtilTest.firstNameClaimUri); @@ -330,7 +323,6 @@ protected OIDCApplication initApplicationOne() { protected OIDCApplication initApplicationTwo() { playgroundAppTwo = new OIDCApplication(OIDCUtilTest.playgroundAppTwoAppName, - OIDCUtilTest.playgroundAppTwoAppContext, OIDCUtilTest.playgroundAppTwoAppCallBackUri); playgroundAppOne.addRequiredClaim(OIDCUtilTest.emailClaimUri); playgroundAppOne.addRequiredClaim(OIDCUtilTest.firstNameClaimUri); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCSPWiseSkipLoginConsentTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCSPWiseSkipLoginConsentTestCase.java index 20061ed6240..ac33d2b6ef7 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCSPWiseSkipLoginConsentTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCSPWiseSkipLoginConsentTestCase.java @@ -30,6 +30,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.automation.engine.context.TestUserMode; +import org.wso2.identity.integration.test.base.MockApplicationServer; import org.wso2.identity.integration.test.oidc.bean.OIDCApplication; import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.AdvancedApplicationConfiguration; import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.ApplicationPatchModel; @@ -46,6 +47,7 @@ public class OIDCSPWiseSkipLoginConsentTestCase extends OIDCAbstractIntegrationT private CookieStore cookieStore = new BasicCookieStore(); protected String sessionDataKey; protected String sessionDataKeyConsent; + private MockApplicationServer mockApplicationServer; @BeforeClass(alwaysRun = true) public void testInit() throws Exception { @@ -57,6 +59,9 @@ public void testInit() throws Exception { createApplications(); configureSPToSkipConsent(); client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build(); + + mockApplicationServer = new MockApplicationServer(); + mockApplicationServer.start(); } @AfterClass(alwaysRun = true) @@ -64,6 +69,7 @@ public void clearObjects() throws Exception { deleteObjects(); clear(); + mockApplicationServer.stop(); } private void deleteObjects() throws Exception { @@ -83,16 +89,16 @@ private void configureSPToSkipConsent() throws Exception { @Test(groups = "wso2.is", description = "Test authz endpoint before creating a valid session") public void testCreateUserSession() throws Exception { - testSendAuthenticationRequest(OIDCUtilTest.applications.get(OIDCUtilTest.playgroundAppOneAppName), true, client, - cookieStore); + testSendAuthenticationRequest(OIDCUtilTest.applications.get(OIDCUtilTest.playgroundAppOneAppName), true, + client, cookieStore); testAuthentication(); } @Test(groups = "wso2.is", description = "Initiate authentication request from playground.apptwo") - public void testIntiateLoginRequestForAlreadyLoggedUser() throws Exception { + public void testInitiateLoginRequestForAlreadyLoggedUser() throws Exception { - testSendAuthenticationRequest(OIDCUtilTest.applications.get(OIDCUtilTest.playgroundAppTwoAppName), false, client - , cookieStore); + testSendAuthenticationRequest(OIDCUtilTest.applications.get(OIDCUtilTest.playgroundAppTwoAppName), false, + client, cookieStore); } private void testAuthentication() throws Exception { diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCSSOConsentTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCSSOConsentTestCase.java index e71147375b9..138cd18c3c6 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCSSOConsentTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCSSOConsentTestCase.java @@ -31,9 +31,11 @@ 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.BasicHeader; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONObject; +import org.json.simple.JSONValue; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -41,6 +43,7 @@ import org.wso2.carbon.automation.engine.context.beans.Tenant; import org.wso2.carbon.automation.engine.context.beans.User; import org.apache.commons.lang.StringUtils; +import org.wso2.identity.integration.test.base.MockApplicationServer; import org.wso2.identity.integration.test.oidc.bean.OIDCApplication; import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.ApplicationPatchModel; import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.Claim; @@ -55,8 +58,11 @@ import org.wso2.identity.integration.test.utils.OAuth2Constant; import static org.apache.commons.lang.StringUtils.isBlank; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.ACCESS_TOKEN_ENDPOINT; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.AUTHORIZATION_HEADER; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.AUTHORIZE_ENDPOINT_URL; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -81,6 +87,7 @@ public class OIDCSSOConsentTestCase extends OIDCAbstractIntegrationTest { protected List consentParameters = new ArrayList<>(); OIDCApplication playgroundApp; private String claimsToGetConsent; + private MockApplicationServer mockApplicationServer; @BeforeClass(alwaysRun = true) public void testInit() throws Exception { @@ -105,6 +112,9 @@ public void testInit() throws Exception { .setDefaultCookieSpecRegistry(cookieSpecRegistry) .setDefaultRequestConfig(requestConfig) .build(); + + mockApplicationServer = new MockApplicationServer(); + mockApplicationServer.start(); } @AfterClass(alwaysRun = true) @@ -113,6 +123,7 @@ public void testClear() throws Exception { deleteUser(user); deleteApplication(playgroundApp); clear(); + mockApplicationServer.stop(); } @Test(groups = "wso2.is", description = "Test consent management after updating " + @@ -140,17 +151,16 @@ public void testConsentWithAppClaimConfigUpdate() throws Exception { public void testSendAuthenticationRequest(OIDCApplication application, HttpClient client) throws Exception { - List urlParameters = OIDCUtilTest.getNameValuePairs(application); - HttpResponse response = sendPostRequestWithParameters(client, urlParameters, String.format - (OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + - OAuth2Constant.PlaygroundAppPaths.appUserAuthorizePath)); - Assert.assertNotNull(response, "Authorization request failed for " + application.getApplicationName() + - ". Authorized response is null."); + List urlParameters = new ArrayList<>(); + urlParameters.add(new BasicNameValuePair("response_type", OAuth2Constant.OAUTH2_GRANT_TYPE_CODE)); + urlParameters.add(new BasicNameValuePair("client_id", application.getClientId())); + urlParameters.add(new BasicNameValuePair("redirect_uri", application.getCallBackURL())); + + urlParameters.add(new BasicNameValuePair("scope", "openid email profile")); + HttpResponse response = sendPostRequestWithParameters(client, urlParameters, + getTenantQualifiedURL(AUTHORIZE_ENDPOINT_URL, tenantInfo.getDomain())); Header locationHeader = response.getFirstHeader(OAuth2Constant.HTTP_RESPONSE_HEADER_LOCATION); - - Assert.assertNotNull(locationHeader, "Authorization request failed for " + - application.getApplicationName() + ". Authorized response header is null."); EntityUtils.consume(response.getEntity()); response = sendGetRequest(client, locationHeader.getValue()); @@ -222,53 +232,41 @@ private void testConsentApproval(OIDCApplication application) throws Exception { EntityUtils.consume(response.getEntity()); response = sendPostRequest(client, locationHeader.getValue()); - Assert.assertNotNull(response, "Authorization code response is invalid for " + - application.getApplicationName()); - - Map keyPositionMap = new HashMap<>(1); - keyPositionMap.put("Authorization Code", 1); - List keyValues = DataExtractUtil.extractTableRowDataFromResponse(response, - keyPositionMap); - Assert.assertNotNull(keyValues, "Authorization code not received for " + - application.getApplicationName()); + EntityUtils.consume(response.getEntity()); - authorizationCode = keyValues.get(0).getValue(); + authorizationCode = mockApplicationServer.getAuthorizationCodeForApp(application.getApplicationName()); Assert.assertNotNull(authorizationCode, "Authorization code not received for " + application .getApplicationName()); - EntityUtils.consume(response.getEntity()); } private void testGetAccessToken(OIDCApplication application) throws Exception { - HttpResponse response = sendGetAccessTokenPost(client, application); - Assert.assertNotNull(response, "Access token response is invalid for " + - application.getApplicationName()); - EntityUtils.consume(response.getEntity()); - - response = sendPostRequest(client, String.format(OIDCUtilTest.targetApplicationUrl, - application.getApplicationContext() + OAuth2Constant.PlaygroundAppPaths.appAuthorizePath)); - - Map keyPositionMap = new HashMap<>(1); - keyPositionMap.put("name=\"accessToken\"", 1); - List keyValues = DataExtractUtil.extractInputValueFromResponse(response, - keyPositionMap); - Assert.assertNotNull(keyValues, "Access token not received for " + application.getApplicationName()); - - accessToken = keyValues.get(0).getValue(); - Assert.assertNotNull(accessToken, "Access token not received for " + application.getApplicationName()); - EntityUtils.consume(response.getEntity()); - - response = sendPostRequest(client, String.format(OIDCUtilTest.targetApplicationUrl, - application.getApplicationContext() + OAuth2Constant.PlaygroundAppPaths.appAuthorizePath)); - - keyPositionMap = new HashMap<>(1); - keyPositionMap.put("id=\"loggedUser\"", 1); - keyValues = DataExtractUtil.extractLabelValueFromResponse(response, keyPositionMap); - Assert.assertNotNull(keyValues, "No user logged in for " + application.getApplicationName()); - - String loggedUser = keyValues.get(0).getValue(); - Assert.assertNotNull(loggedUser, "Logged user is null for " + application.getApplicationName()); - EntityUtils.consume(response.getEntity()); + List urlParameters = new ArrayList<>(); + urlParameters.add(new BasicNameValuePair("code", authorizationCode)); + urlParameters.add(new BasicNameValuePair("grant_type", OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE)); + urlParameters.add(new BasicNameValuePair("redirect_uri", application.getCallBackURL())); + urlParameters.add(new BasicNameValuePair("client_id", application.getClientSecret())); + + urlParameters.add(new BasicNameValuePair("scope", "openid")); + + List
headers = new ArrayList<>(); + headers.add(new BasicHeader(AUTHORIZATION_HEADER, + OAuth2Constant.BASIC_HEADER + " " + getBase64EncodedString(application.getClientId(), + application.getClientSecret()))); + headers.add(new BasicHeader("Content-Type", "application/x-www-form-urlencoded")); + headers.add(new BasicHeader("User-Agent", OAuth2Constant.USER_AGENT)); + + HttpResponse response = sendPostRequest(client, headers, urlParameters, + getTenantQualifiedURL(ACCESS_TOKEN_ENDPOINT, tenantInfo.getDomain())); + String responseString = EntityUtils.toString(response.getEntity()); + Map responseMap = (Map) JSONValue.parse(responseString); + accessToken = (String) responseMap.get("access_token"); + + String idToken = (String) responseMap.get("id_token"); + String[] tokenParts = idToken.split("\\."); + String payload = new String(java.util.Base64.getUrlDecoder().decode(tokenParts[1])); + Map parsedIdToken = (Map) JSONValue.parse(payload); + Assert.assertNotNull(parsedIdToken.get("sub"), "No user logged in for " + application.getApplicationName()); } protected void initUser() throws Exception { @@ -283,7 +281,6 @@ protected void initUser() throws Exception { protected OIDCApplication initApplication() { playgroundApp = new OIDCApplication(OIDCUtilTest.playgroundAppOneAppName, - OIDCUtilTest.playgroundAppOneAppContext, OIDCUtilTest.playgroundAppOneAppCallBackUri); playgroundApp.addRequiredClaim(OIDCUtilTest.emailClaimUri); playgroundApp.addRequiredClaim(OIDCUtilTest.firstNameClaimUri); @@ -306,19 +303,6 @@ private void updateApplication(OIDCApplication playgroundApp) throws Exception { updateApplication(playgroundApp.getApplicationId(), new ApplicationPatchModel().claimConfiguration(claimConfig)); } - protected HttpResponse sendGetAccessTokenPost(HttpClient client, OIDCApplication application) throws IOException { - - List urlParameters = new ArrayList<>(); - urlParameters.add(new BasicNameValuePair("callbackurl", application.getCallBackURL())); - urlParameters.add(new BasicNameValuePair("accessEndpoint", OAuth2Constant.ACCESS_TOKEN_ENDPOINT)); - urlParameters.add(new BasicNameValuePair("consumerSecret", application.getClientSecret())); - HttpResponse response = sendPostRequestWithParameters(client, urlParameters, String.format - (OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + - OAuth2Constant.PlaygroundAppPaths.accessTokenRequestPath)); - - return response; - } - private void performOIDCLogout() { try { diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCUtilTest.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCUtilTest.java index 9fd38e90710..8a5f866d25b 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCUtilTest.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/OIDCUtilTest.java @@ -22,6 +22,7 @@ import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.message.BasicNameValuePair; +import org.wso2.identity.integration.test.base.MockApplicationServer; import org.wso2.identity.integration.test.oidc.bean.OIDCApplication; import org.wso2.identity.integration.test.rest.api.user.common.model.Email; import org.wso2.identity.integration.test.rest.api.user.common.model.Name; @@ -54,17 +55,11 @@ public class OIDCUtilTest { public static final String profile = "default"; protected static String sessionDataKey; - public static final String playgroundAppOneAppName = "playground.appone"; - public static final String playgroundAppOneAppCallBackUri = "http://localhost:" + TOMCAT_PORT + "/playground" + "" + - ".appone/oauth2client"; - public static final String playgroundAppOneAppContext = "/playground.appone"; + public static final String playgroundAppOneAppName = MockApplicationServer.Constants.APP1.NAME; + public static final String playgroundAppOneAppCallBackUri = MockApplicationServer.Constants.APP1.CALLBACK_URL; - public static final String playgroundAppTwoAppName = "playground.apptwo"; - public static final String playgroundAppTwoAppCallBackUri = "http://localhost:" + TOMCAT_PORT + "/playground" + "" + - ".apptwo/oauth2client"; - public static final String playgroundAppTwoAppContext = "/playground.apptwo"; - - public static final String targetApplicationUrl = "http://localhost:" + TOMCAT_PORT + "%s"; + public static final String playgroundAppTwoAppName = MockApplicationServer.Constants.APP2.NAME; + public static final String playgroundAppTwoAppCallBackUri = MockApplicationServer.Constants.APP2.CALLBACK_URL; public static final String emailClaimUri = "http://wso2.org/claims/emailaddress"; public static final String firstNameClaimUri = "http://wso2.org/claims/givenname"; @@ -87,14 +82,14 @@ public static void initUser() { */ public static void initApplications() { - OIDCApplication playgroundApp = new OIDCApplication(playgroundAppOneAppName, playgroundAppOneAppContext, + OIDCApplication playgroundApp = new OIDCApplication(playgroundAppOneAppName, playgroundAppOneAppCallBackUri); playgroundApp.addRequiredClaim(emailClaimUri); playgroundApp.addRequiredClaim(firstNameClaimUri); playgroundApp.addRequiredClaim(lastNameClaimUri); applications.put(playgroundAppOneAppName, playgroundApp); - playgroundApp = new OIDCApplication(playgroundAppTwoAppName, playgroundAppTwoAppContext, + playgroundApp = new OIDCApplication(playgroundAppTwoAppName, playgroundAppTwoAppCallBackUri); playgroundApp.addRequiredClaim(emailClaimUri); playgroundApp.addRequiredClaim(firstNameClaimUri); diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/bean/OIDCApplication.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/bean/OIDCApplication.java index c42ecbc0e9e..a71263f1109 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/bean/OIDCApplication.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/oidc/bean/OIDCApplication.java @@ -27,7 +27,6 @@ public class OIDCApplication { private String applicationName; private String clientId; private String clientSecret; - private String applicationContext; private String callBackURL; private String subjectClaimURI; private List requiredClaims = null; @@ -36,9 +35,8 @@ public OIDCApplication() { } - public OIDCApplication(String applicationName, String applicationContext, String callBackURL) { + public OIDCApplication(String applicationName, String callBackURL) { this.applicationName = applicationName; - this.applicationContext = applicationContext; this.callBackURL = callBackURL; } @@ -74,14 +72,6 @@ public void setClientSecret(String clientSecret) { this.clientSecret = clientSecret; } - public String getApplicationContext() { - return applicationContext; - } - - public void setApplicationContext(String applicationContext) { - this.applicationContext = applicationContext; - } - public String getCallBackURL() { return callBackURL; } diff --git a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/recovery/PasswordRecoveryTestCase.java b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/recovery/PasswordRecoveryTestCase.java index cc6f9981325..c699c6006bd 100644 --- a/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/recovery/PasswordRecoveryTestCase.java +++ b/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/identity/integration/test/recovery/PasswordRecoveryTestCase.java @@ -43,6 +43,7 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import org.wso2.identity.integration.test.base.MockApplicationServer; import org.wso2.identity.integration.test.oidc.OIDCAbstractIntegrationTest; import org.wso2.identity.integration.test.oidc.OIDCUtilTest; import org.wso2.identity.integration.test.oidc.bean.OIDCApplication; @@ -60,6 +61,8 @@ import java.util.ArrayList; import java.util.List; +import static org.wso2.identity.integration.test.utils.OAuth2Constant.AUTHORIZE_ENDPOINT_URL; + /** * Test password recovery functionality. */ @@ -72,6 +75,7 @@ public class PasswordRecoveryTestCase extends OIDCAbstractIntegrationTest { private CloseableHttpClient client; private OIDCApplication oidcApplication; private UserObject userObject; + private MockApplicationServer mockApplicationServer; public static final String USERNAME = "recoverytestuser"; public static final String PASSWORD = "Oidcsessiontestuser@123"; @@ -103,6 +107,9 @@ public void testInit() throws Exception { userObject = initUser(); createUser(userObject); + + mockApplicationServer = new MockApplicationServer(); + mockApplicationServer.start(); } @AfterClass(alwaysRun = true) @@ -114,6 +121,7 @@ public void testClear() throws Exception { identityGovernanceRestClient.closeHttpClient(); client.close(); Utils.getMailServer().purgeEmailFromAllMailboxes(); + mockApplicationServer.stop(); } @Test @@ -129,12 +137,13 @@ public void testPasswordRecovery() throws Exception { private String retrievePasswordResetURL(OIDCApplication application, HttpClient client) throws Exception { - List urlParameters = OIDCUtilTest.getNameValuePairs(application, - getTenantQualifiedURL(OAuth2Constant.APPROVAL_URL, tenantInfo.getDomain())); - - HttpResponse response = sendPostRequestWithParameters(client, urlParameters, String.format - (OIDCUtilTest.targetApplicationUrl, application.getApplicationContext() + OAuth2Constant.PlaygroundAppPaths - .appUserAuthorizePath)); + List urlParameters = new ArrayList<>(); + urlParameters.add(new BasicNameValuePair("response_type", OAuth2Constant.OAUTH2_GRANT_TYPE_CODE)); + urlParameters.add(new BasicNameValuePair("client_id", application.getClientId())); + urlParameters.add(new BasicNameValuePair("redirect_uri", application.getCallBackURL())); + urlParameters.add(new BasicNameValuePair("scope", "openid email profile")); + HttpResponse response = sendPostRequestWithParameters(client, urlParameters, + getTenantQualifiedURL(AUTHORIZE_ENDPOINT_URL, tenantInfo.getDomain())); Header authorizeRequestURL = response.getFirstHeader(OAuth2Constant.HTTP_RESPONSE_HEADER_LOCATION); EntityUtils.consume(response.getEntity()); @@ -192,7 +201,6 @@ private void updatePasswordRecoveryFeatureStatus(boolean enable) throws IOExcept private OIDCApplication initApplication() { OIDCApplication playgroundApp = new OIDCApplication(OIDCUtilTest.playgroundAppOneAppName, - OIDCUtilTest.playgroundAppOneAppContext, OIDCUtilTest.playgroundAppOneAppCallBackUri); playgroundApp.addRequiredClaim(OIDCUtilTest.emailClaimUri); playgroundApp.addRequiredClaim(OIDCUtilTest.firstNameClaimUri);