Search in sources :

Example 1 with ProtocolMappersUpdater

use of org.keycloak.testsuite.updaters.ProtocolMappersUpdater in project keycloak by keycloak.

the class OIDCProtocolMappersTest method testUserGroupRoleToAttributeMappersScopedWithDifferentClient.

@Test
public void testUserGroupRoleToAttributeMappersScopedWithDifferentClient() throws Exception {
    final String clientId = "test-app-scope";
    final String diffClient = "test-app";
    final String realmName = "test";
    final ProtocolMapperRepresentation realmMapper = ProtocolMapperUtil.createUserRealmRoleMappingMapper("pref.", "Realm roles mapper", "roles-custom.realm", true, true);
    final ProtocolMapperRepresentation clientMapper = ProtocolMapperUtil.createUserClientRoleMappingMapper(diffClient, null, "Client roles mapper", "roles-custom.test-app", true, true);
    try (ClientAttributeUpdater cau = ClientAttributeUpdater.forClient(adminClient, realmName, clientId).setDirectAccessGrantsEnabled(true);
        ProtocolMappersUpdater protocolMappers = new ProtocolMappersUpdater(cau.getResource().getProtocolMappers())) {
        protocolMappers.add(realmMapper, clientMapper).update();
        // Login user
        oauth.clientId(clientId);
        OAuthClient.AccessTokenResponse response = browserLogin("password", "rich.roles@redhat.com", "password");
        IDToken idToken = oauth.verifyIDToken(response.getIdToken());
        // Verify attribute is filled
        Map<String, Object> roleMappings = (Map<String, Object>) idToken.getOtherClaims().get("roles-custom");
        assertNotNull(roleMappings);
        assertThat(roleMappings.keySet(), containsInAnyOrder("realm", diffClient));
        String realmRoleMappings = (String) roleMappings.get("realm");
        String testAppScopeMappings = (String) roleMappings.get(diffClient);
        assertRolesString(realmRoleMappings, "pref.admin", "pref.user", "pref.customer-user-premium");
        assertRolesString(testAppScopeMappings, "customer-admin-composite-role", "customer-admin");
    }
}
Also used : ClientAttributeUpdater(org.keycloak.testsuite.updaters.ClientAttributeUpdater) OAuthClient(org.keycloak.testsuite.util.OAuthClient) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ProtocolMappersUpdater(org.keycloak.testsuite.updaters.ProtocolMappersUpdater) IDToken(org.keycloak.representations.IDToken) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Map(java.util.Map) HashMap(java.util.HashMap) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)1 Test (org.junit.Test)1 IDToken (org.keycloak.representations.IDToken)1 ProtocolMapperRepresentation (org.keycloak.representations.idm.ProtocolMapperRepresentation)1 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)1 ClientAttributeUpdater (org.keycloak.testsuite.updaters.ClientAttributeUpdater)1 ProtocolMappersUpdater (org.keycloak.testsuite.updaters.ProtocolMappersUpdater)1 OAuthClient (org.keycloak.testsuite.util.OAuthClient)1