|
| 1 | +package org.wso2.identity.integration.test.rest.api.server.application.management.v1; |
| 2 | + |
| 3 | +import com.nimbusds.oauth2.sdk.AccessTokenResponse; |
| 4 | +import com.nimbusds.oauth2.sdk.AuthorizationGrant; |
| 5 | +import com.nimbusds.oauth2.sdk.ClientCredentialsGrant; |
| 6 | +import com.nimbusds.oauth2.sdk.ResourceOwnerPasswordCredentialsGrant; |
| 7 | +import com.nimbusds.oauth2.sdk.Scope; |
| 8 | +import com.nimbusds.oauth2.sdk.TokenRequest; |
| 9 | +import com.nimbusds.oauth2.sdk.TokenResponse; |
| 10 | +import com.nimbusds.oauth2.sdk.auth.ClientAuthentication; |
| 11 | +import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic; |
| 12 | +import com.nimbusds.oauth2.sdk.auth.Secret; |
| 13 | +import com.nimbusds.oauth2.sdk.http.HTTPResponse; |
| 14 | +import com.nimbusds.oauth2.sdk.id.ClientID; |
| 15 | +import org.json.JSONObject; |
| 16 | +import org.testng.Assert; |
| 17 | +import org.testng.annotations.AfterClass; |
| 18 | +import org.testng.annotations.BeforeClass; |
| 19 | +import org.testng.annotations.DataProvider; |
| 20 | +import org.testng.annotations.Factory; |
| 21 | +import org.testng.annotations.Test; |
| 22 | +import org.wso2.carbon.automation.engine.context.TestUserMode; |
| 23 | +import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.ApplicationResponseModel; |
| 24 | +import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.AssociatedRolesConfig; |
| 25 | +import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.InboundProtocolListItem; |
| 26 | +import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.InboundProtocols; |
| 27 | +import org.wso2.identity.integration.test.rest.api.server.application.management.v1.model.OpenIDConnectConfiguration; |
| 28 | +import org.wso2.identity.integration.test.rest.api.server.organization.management.v1.OrganizationManagementBaseTest; |
| 29 | +import org.wso2.identity.integration.test.rest.api.server.roles.v2.model.Audience; |
| 30 | +import org.wso2.identity.integration.test.rest.api.server.roles.v2.model.Permission; |
| 31 | +import org.wso2.identity.integration.test.rest.api.server.roles.v2.model.RoleV2; |
| 32 | +import org.wso2.identity.integration.test.restclients.OAuth2RestClient; |
| 33 | +import org.wso2.identity.integration.test.restclients.OrgMgtRestClient; |
| 34 | +import org.wso2.identity.integration.test.utils.OAuth2Constant; |
| 35 | + |
| 36 | +import java.net.URI; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +public class OrganizationOAuth2ApplicationManagementSuccessTest extends OrganizationManagementBaseTest { |
| 43 | + |
| 44 | + private static final String AUTHORIZED_APIS_JSON = "org-based-authorized-apis.json"; |
| 45 | + private static final String SUB_ORG_NAME = "subOrg"; |
| 46 | + |
| 47 | + private OrgMgtRestClient orgMgtRestClient; |
| 48 | + private OAuth2RestClient oAuth2RestClient; |
| 49 | + private String subOrgId; |
| 50 | + private String switchedM2MToken; |
| 51 | + private String subOrgAppToken; |
| 52 | + |
| 53 | + @Factory(dataProvider = "restAPIUserConfigProvider") |
| 54 | + public OrganizationOAuth2ApplicationManagementSuccessTest(TestUserMode userMode) throws Exception { |
| 55 | + |
| 56 | + super.init(userMode); |
| 57 | + this.context = isServer; |
| 58 | + this.authenticatingUserName = context.getContextTenant().getTenantAdmin().getUserName(); |
| 59 | + this.authenticatingCredential = context.getContextTenant().getTenantAdmin().getPassword(); |
| 60 | + this.tenant = context.getContextTenant().getDomain(); |
| 61 | + } |
| 62 | + |
| 63 | + @BeforeClass(alwaysRun = true) |
| 64 | + public void initClass() throws Exception { |
| 65 | + |
| 66 | + super.testInit("v1", swaggerDefinition, tenant); |
| 67 | + oAuth2RestClient = new OAuth2RestClient(serverURL, tenantInfo); |
| 68 | + |
| 69 | + orgMgtRestClient = new OrgMgtRestClient(isServer, tenantInfo, serverURL, |
| 70 | + new JSONObject(readResource(AUTHORIZED_APIS_JSON, this.getClass()))); |
| 71 | + subOrgId = orgMgtRestClient.addOrganization(SUB_ORG_NAME); |
| 72 | + switchedM2MToken = orgMgtRestClient.switchM2MToken(subOrgId); |
| 73 | + orgMgtRestClient.addOrganizationUser("sub-org-user", "SubOrgUser@123"); |
| 74 | + } |
| 75 | + |
| 76 | + @AfterClass(alwaysRun = true) |
| 77 | + public void atEnd() throws Exception { |
| 78 | + |
| 79 | + orgMgtRestClient.deleteOrganization(subOrgId); |
| 80 | + orgMgtRestClient.closeHttpClient(); |
| 81 | + oAuth2RestClient.closeHttpClient(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testCreateOAuth2ApplicationInOrganization() throws Exception { |
| 86 | + |
| 87 | + String body = readResource("create-basic-oauth2-application.json", this.getClass()); |
| 88 | + |
| 89 | + oAuth2RestClient.createApplicationInSubOrganization(body, switchedM2MToken); |
| 90 | + System.out.println("Sub Organization Application ID : " + oAuth2RestClient.getAppIdUsingAppNameInOrganization("My SAMPLE APP", switchedM2MToken)); |
| 91 | + String subOrganizationAppId = oAuth2RestClient.getAppIdUsingAppNameInOrganization("My SAMPLE APP", |
| 92 | + switchedM2MToken); |
| 93 | + |
| 94 | + // Authorizing the APIs to the sub organization application |
| 95 | + authorizeSystemAPIsToSubOrganizationApp(oAuth2RestClient, subOrganizationAppId, |
| 96 | + new ArrayList<>(Arrays.asList("/o/scim2/Roles", "/o/oauth2/introspect")), switchedM2MToken); |
| 97 | + |
| 98 | + // Creating an application role for the sub organization application |
| 99 | + RoleV2 role; |
| 100 | + String displayName; |
| 101 | + List<String> schemas = Collections.emptyList(); |
| 102 | + List<Permission> permissions = new ArrayList<>(); |
| 103 | + permissions.add(new Permission("internal_org_role_mgt_create")); |
| 104 | + permissions.add(new Permission("internal_org_role_mgt_view")); |
| 105 | + displayName = "Application Role"; |
| 106 | + Audience roleAudience = new Audience("APPLICATION", subOrganizationAppId); |
| 107 | + role = new RoleV2(roleAudience, displayName, permissions, schemas); |
| 108 | + oAuth2RestClient.createV2RolesInSubOrganization(role, switchedM2MToken); |
| 109 | + ApplicationResponseModel subOrgAppModel = oAuth2RestClient.getSubOrgApplication(subOrganizationAppId, |
| 110 | + switchedM2MToken); |
| 111 | + |
| 112 | + // Validate application details |
| 113 | + Assert.assertEquals(subOrgAppModel.getName(), "My SAMPLE APP"); |
| 114 | + |
| 115 | + // Validate application role audience and roles |
| 116 | + AssociatedRolesConfig associatedRolesConfig = subOrgAppModel.getAssociatedRoles(); |
| 117 | + Assert.assertEquals(associatedRolesConfig.getAllowedAudience().toString(), "APPLICATION"); |
| 118 | + Assert.assertEquals(associatedRolesConfig.getRoles().get(0).getName(), "Application Role"); |
| 119 | + |
| 120 | + // Validate application inbound protocols |
| 121 | + List<InboundProtocolListItem> inboundProtocols = subOrgAppModel.getInboundProtocols(); |
| 122 | + Assert.assertEquals(inboundProtocols.size(), 1); |
| 123 | + } |
| 124 | + |
| 125 | + @Test(dependsOnMethods = "testCreateOAuth2ApplicationInOrganization") |
| 126 | + public void testIssueAccessTokenFromSubOrgApplicationFromCCGrant() throws Exception { |
| 127 | + |
| 128 | + String subOrganizationAppId = oAuth2RestClient.getAppIdUsingAppNameInOrganization("My SAMPLE APP", |
| 129 | + switchedM2MToken); |
| 130 | + OpenIDConnectConfiguration oidcConfig = oAuth2RestClient.getOIDCInboundDetailsForSubOrgApplications( |
| 131 | + subOrganizationAppId, switchedM2MToken); |
| 132 | + String subOrgAppClientId = oidcConfig.getClientId(); |
| 133 | + String clientSecret = oidcConfig.getClientSecret(); |
| 134 | + |
| 135 | + // Issue access token from sub organization application |
| 136 | + AccessTokenResponse accessTokenResponse = getSubOrgApplicationToken("client_credentials", subOrgAppClientId, clientSecret, subOrgId); |
| 137 | + subOrgAppToken = accessTokenResponse.getTokens().getAccessToken().getValue(); |
| 138 | + Assert.assertNotNull(subOrgAppToken); |
| 139 | + String scopes = accessTokenResponse.getTokens().getAccessToken().getScope().toString(); |
| 140 | + String[] scopeArray = scopes.split(" "); |
| 141 | + Assert.assertEquals(scopeArray.length, 6); |
| 142 | + Assert.assertTrue(Arrays.asList(scopeArray).contains("internal_org_role_mgt_create")); |
| 143 | + Assert.assertTrue(Arrays.asList(scopeArray).contains("internal_org_role_mgt_view")); |
| 144 | + Assert.assertTrue(Arrays.asList(scopeArray).contains("internal_org_role_mgt_update")); |
| 145 | + Assert.assertTrue(Arrays.asList(scopeArray).contains("internal_org_role_mgt_delete")); |
| 146 | + } |
| 147 | + |
| 148 | + public void testAccessResourcesFromTokensIssuedFromSubOrgApplication() throws Exception { |
| 149 | + |
| 150 | + // Access resources from tokens issued from sub organization application |
| 151 | + oAuth2RestClient.getRoles(subOrgAppToken); |
| 152 | + } |
| 153 | + |
| 154 | + private AccessTokenResponse getSubOrgApplicationToken(String grantType, String clientId, String clientSecretStr, String orgId) throws Exception { |
| 155 | + |
| 156 | + URI tokenEndpoint = new URI("https://localhost:9853/t/carbon.super/o/" + orgId + "/oauth2/token"); |
| 157 | + |
| 158 | + ClientID clientID = new ClientID(clientId); |
| 159 | + Secret clientSecret = new Secret(clientSecretStr); |
| 160 | + ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSecret); |
| 161 | + |
| 162 | + AuthorizationGrant authorizationGrant; |
| 163 | + switch (grantType) { |
| 164 | + case OAuth2Constant.OAUTH2_GRANT_TYPE_CLIENT_CREDENTIALS: |
| 165 | + authorizationGrant = new ClientCredentialsGrant(); |
| 166 | + break; |
| 167 | + case OAuth2Constant.OAUTH2_GRANT_TYPE_RESOURCE_OWNER: |
| 168 | + authorizationGrant = new ResourceOwnerPasswordCredentialsGrant(null, null); |
| 169 | + break; |
| 170 | + default: |
| 171 | + throw new Exception("Unsupported grant type"); |
| 172 | + } |
| 173 | + Scope scope = new Scope("SYSTEM"); |
| 174 | + |
| 175 | + TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, authorizationGrant, scope); |
| 176 | + HTTPResponse tokenHTTPResp = request.toHTTPRequest().send(); |
| 177 | + TokenResponse tokenResponse = TokenResponse.parse(tokenHTTPResp); |
| 178 | + return tokenResponse.toSuccessResponse(); |
| 179 | + } |
| 180 | + |
| 181 | + @DataProvider(name = "restAPIUserConfigProvider") |
| 182 | + public static Object[][] restAPIUserConfigProvider() { |
| 183 | + |
| 184 | + return new Object[][]{ |
| 185 | + {TestUserMode.SUPER_TENANT_ADMIN} |
| 186 | +// {TestUserMode.TENANT_ADMIN} |
| 187 | + }; |
| 188 | + } |
| 189 | +} |
0 commit comments