Search in sources :

Example 6 with MappingsRepresentation

use of org.keycloak.representations.idm.MappingsRepresentation in project keycloak by keycloak.

the class AbstractIdentityProviderMapperTest method findUser.

protected UserRepresentation findUser(String realm, String userName, String email) {
    UsersResource consumerUsers = adminClient.realm(realm).users();
    List<UserRepresentation> users = consumerUsers.list();
    assertThat("There must be exactly one user", users, hasSize(1));
    UserRepresentation user = users.get(0);
    assertThat("Username has to match", user.getUsername(), equalTo(userName));
    assertThat("Email has to match", user.getEmail(), equalTo(email));
    MappingsRepresentation roles = consumerUsers.get(user.getId()).roles().getAll();
    List<String> realmRoles = roles.getRealmMappings().stream().map(RoleRepresentation::getName).collect(Collectors.toList());
    user.setRealmRoles(realmRoles);
    Map<String, List<String>> clientRoles = new HashMap<>();
    if (roles.getClientMappings() != null) {
        roles.getClientMappings().forEach((key, value) -> clientRoles.put(key, value.getMappings().stream().map(RoleRepresentation::getName).collect(Collectors.toList())));
    }
    user.setClientRoles(clientRoles);
    return user;
}
Also used : MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) HashMap(java.util.HashMap) UsersResource(org.keycloak.admin.client.resource.UsersResource) List(java.util.List) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 7 with MappingsRepresentation

use of org.keycloak.representations.idm.MappingsRepresentation in project keycloak by keycloak.

the class RoleMapperResource method getRoleMappings.

/**
 * Get role mappings
 *
 * @return
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public MappingsRepresentation getRoleMappings() {
    viewPermission.require();
    List<RoleRepresentation> realmRolesRepresentation = new ArrayList<>();
    Map<String, ClientMappingsRepresentation> appMappings = new HashMap<>();
    final AtomicReference<ClientMappingsRepresentation> mappings = new AtomicReference<>();
    roleMapper.getRoleMappingsStream().forEach(roleMapping -> {
        RoleContainerModel container = roleMapping.getContainer();
        if (container instanceof RealmModel) {
            realmRolesRepresentation.add(ModelToRepresentation.toBriefRepresentation(roleMapping));
        } else if (container instanceof ClientModel) {
            ClientModel clientModel = (ClientModel) container;
            mappings.set(appMappings.get(clientModel.getClientId()));
            if (mappings.get() == null) {
                mappings.set(new ClientMappingsRepresentation());
                mappings.get().setId(clientModel.getId());
                mappings.get().setClient(clientModel.getClientId());
                mappings.get().setMappings(new ArrayList<>());
                appMappings.put(clientModel.getClientId(), mappings.get());
            }
            mappings.get().getMappings().add(ModelToRepresentation.toBriefRepresentation(roleMapping));
        }
    });
    MappingsRepresentation all = new MappingsRepresentation();
    if (!realmRolesRepresentation.isEmpty())
        all.setRealmMappings(realmRolesRepresentation);
    if (!appMappings.isEmpty())
        all.setClientMappings(appMappings);
    return all;
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) ClientMappingsRepresentation(org.keycloak.representations.idm.ClientMappingsRepresentation) RoleContainerModel(org.keycloak.models.RoleContainerModel) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 8 with MappingsRepresentation

use of org.keycloak.representations.idm.MappingsRepresentation in project keycloak by keycloak.

the class ClientScopeTest method testScopes.

@Test
public void testScopes() {
    RoleRepresentation realmCompositeRole = createRealmRole("realm-composite");
    RoleRepresentation realmChildRole = createRealmRole("realm-child");
    testRealmResource().roles().get("realm-composite").addComposites(Collections.singletonList(realmChildRole));
    assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.roleResourceCompositesPath("realm-composite"), Collections.singletonList(realmChildRole), ResourceType.REALM_ROLE);
    // create client scope
    ClientScopeRepresentation scopeRep = new ClientScopeRepresentation();
    scopeRep.setName("bar-scope");
    String scopeId = createClientScope(scopeRep);
    // update with some scopes
    String accountMgmtId = testRealmResource().clients().findByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).get(0).getId();
    RoleRepresentation viewAccountRoleRep = testRealmResource().clients().get(accountMgmtId).roles().get(AccountRoles.VIEW_PROFILE).toRepresentation();
    RoleMappingResource scopesResource = clientScopes().get(scopeId).getScopeMappings();
    scopesResource.realmLevel().add(Collections.singletonList(realmCompositeRole));
    assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.clientScopeRoleMappingsRealmLevelPath(scopeId), Collections.singletonList(realmCompositeRole), ResourceType.REALM_SCOPE_MAPPING);
    scopesResource.clientLevel(accountMgmtId).add(Collections.singletonList(viewAccountRoleRep));
    assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.clientScopeRoleMappingsClientLevelPath(scopeId, accountMgmtId), Collections.singletonList(viewAccountRoleRep), ResourceType.CLIENT_SCOPE_MAPPING);
    // test that scopes are available (also through composite role)
    List<RoleRepresentation> allRealm = scopesResource.realmLevel().listAll();
    List<RoleRepresentation> availableRealm = scopesResource.realmLevel().listAvailable();
    List<RoleRepresentation> effectiveRealm = scopesResource.realmLevel().listEffective();
    List<RoleRepresentation> accountRoles = scopesResource.clientLevel(accountMgmtId).listAll();
    assertNames(allRealm, "realm-composite");
    assertNames(availableRealm, "realm-child", Constants.OFFLINE_ACCESS_ROLE, Constants.AUTHZ_UMA_AUTHORIZATION, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-test");
    assertNames(effectiveRealm, "realm-composite", "realm-child");
    assertNames(accountRoles, AccountRoles.VIEW_PROFILE);
    MappingsRepresentation mappingsRep = clientScopes().get(scopeId).getScopeMappings().getAll();
    assertNames(mappingsRep.getRealmMappings(), "realm-composite");
    assertNames(mappingsRep.getClientMappings().get(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID).getMappings(), AccountRoles.VIEW_PROFILE);
    // remove scopes
    scopesResource.realmLevel().remove(Collections.singletonList(realmCompositeRole));
    assertAdminEvents.assertEvent(getRealmId(), OperationType.DELETE, AdminEventPaths.clientScopeRoleMappingsRealmLevelPath(scopeId), Collections.singletonList(realmCompositeRole), ResourceType.REALM_SCOPE_MAPPING);
    scopesResource.clientLevel(accountMgmtId).remove(Collections.singletonList(viewAccountRoleRep));
    assertAdminEvents.assertEvent(getRealmId(), OperationType.DELETE, AdminEventPaths.clientScopeRoleMappingsClientLevelPath(scopeId, accountMgmtId), Collections.singletonList(viewAccountRoleRep), ResourceType.CLIENT_SCOPE_MAPPING);
    // assert scopes are removed
    allRealm = scopesResource.realmLevel().listAll();
    availableRealm = scopesResource.realmLevel().listAvailable();
    effectiveRealm = scopesResource.realmLevel().listEffective();
    accountRoles = scopesResource.clientLevel(accountMgmtId).listAll();
    assertNames(allRealm);
    assertNames(availableRealm, "realm-composite", "realm-child", Constants.OFFLINE_ACCESS_ROLE, Constants.AUTHZ_UMA_AUTHORIZATION, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-test");
    assertNames(effectiveRealm);
    assertNames(accountRoles);
    // remove scope
    removeClientScope(scopeId);
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) RoleMappingResource(org.keycloak.admin.client.resource.RoleMappingResource) Test(org.junit.Test)

Example 9 with MappingsRepresentation

use of org.keycloak.representations.idm.MappingsRepresentation in project keycloak by keycloak.

the class AbstractMigrationTest method testCliConsoleScopeSize.

private void testCliConsoleScopeSize(RealmResource realm) {
    ClientRepresentation cli = realm.clients().findByClientId(Constants.ADMIN_CLI_CLIENT_ID).get(0);
    ClientRepresentation console = realm.clients().findByClientId(Constants.ADMIN_CONSOLE_CLIENT_ID).get(0);
    MappingsRepresentation scopeMappings = realm.clients().get(console.getId()).getScopeMappings().getAll();
    Assert.assertNull(scopeMappings.getClientMappings());
    Assert.assertNull(scopeMappings.getRealmMappings());
    scopeMappings = realm.clients().get(cli.getId()).getScopeMappings().getAll();
    Assert.assertNull(scopeMappings.getClientMappings());
    Assert.assertNull(scopeMappings.getRealmMappings());
}
Also used : MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 10 with MappingsRepresentation

use of org.keycloak.representations.idm.MappingsRepresentation in project keycloak by keycloak.

the class GroupTest method rolesCanBeAssignedEvenWhenTheyAreAlreadyIndirectlyAssigned.

/**
 * Test for KEYCLOAK-10603.
 */
@Test
public void rolesCanBeAssignedEvenWhenTheyAreAlreadyIndirectlyAssigned() {
    RealmResource realm = adminClient.realms().realm("test");
    createRealmRole(realm, RoleBuilder.create().name("realm-composite").build());
    createRealmRole(realm, RoleBuilder.create().name("realm-child").build());
    realm.roles().get("realm-composite").addComposites(Collections.singletonList(realm.roles().get("realm-child").toRepresentation()));
    try (Response response = realm.clients().create(ClientBuilder.create().clientId("myclient").build())) {
        String clientId = ApiUtil.getCreatedId(response);
        getCleanup().addClientUuid(clientId);
        realm.clients().get(clientId).roles().create(RoleBuilder.create().name("client-composite").build());
        realm.clients().get(clientId).roles().create(RoleBuilder.create().name("client-child").build());
        realm.clients().get(clientId).roles().get("client-composite").addComposites(Collections.singletonList(realm.clients().get(clientId).roles().get("client-child").toRepresentation()));
        GroupRepresentation group = new GroupRepresentation();
        group.setName("group");
        // Roles+clients tested elsewhere
        assertAdminEvents.clear();
        String groupId = createGroup(realm, group).getId();
        RoleMappingResource roles = realm.groups().group(groupId).roles();
        // Make indirect assignments: assign composite roles
        roles.realmLevel().add(Collections.singletonList(realm.roles().get("realm-composite").toRepresentation()));
        RoleRepresentation clientComposite = realm.clients().get(clientId).roles().get("client-composite").toRepresentation();
        roles.clientLevel(clientId).add(Collections.singletonList(clientComposite));
        // Check state before making the direct assignments
        assertNames(roles.realmLevel().listAll(), "realm-composite");
        assertNames(roles.realmLevel().listAvailable(), "realm-child", "admin", "offline_access", Constants.AUTHZ_UMA_AUTHORIZATION, "user", "customer-user-premium", "realm-composite-role", "sample-realm-role", "attribute-role", Constants.DEFAULT_ROLES_ROLE_PREFIX + "-test");
        assertNames(roles.realmLevel().listEffective(), "realm-composite", "realm-child");
        assertNames(roles.clientLevel(clientId).listAll(), "client-composite");
        assertNames(roles.clientLevel(clientId).listAvailable(), "client-child");
        assertNames(roles.clientLevel(clientId).listEffective(), "client-composite", "client-child");
        // Make direct assignments for roles which are already indirectly assigned
        roles.realmLevel().add(Collections.singletonList(realm.roles().get("realm-child").toRepresentation()));
        RoleRepresentation clientChild = realm.clients().get(clientId).roles().get("client-child").toRepresentation();
        roles.clientLevel(clientId).add(Collections.singletonList(clientChild));
        // List realm roles
        assertNames(roles.realmLevel().listAll(), "realm-composite", "realm-child");
        assertNames(roles.realmLevel().listAvailable(), "admin", "offline_access", Constants.AUTHZ_UMA_AUTHORIZATION, "user", "customer-user-premium", "realm-composite-role", "sample-realm-role", "attribute-role", Constants.DEFAULT_ROLES_ROLE_PREFIX + "-test");
        assertNames(roles.realmLevel().listEffective(), "realm-composite", "realm-child");
        // List client roles
        assertNames(roles.clientLevel(clientId).listAll(), "client-composite", "client-child");
        assertNames(roles.clientLevel(clientId).listAvailable());
        assertNames(roles.clientLevel(clientId).listEffective(), "client-composite", "client-child");
        // Get mapping representation
        MappingsRepresentation all = roles.getAll();
        assertNames(all.getRealmMappings(), "realm-composite", "realm-child");
        assertEquals(1, all.getClientMappings().size());
        assertNames(all.getClientMappings().get("myclient").getMappings(), "client-composite", "client-child");
    }
}
Also used : Response(javax.ws.rs.core.Response) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) MappingsRepresentation(org.keycloak.representations.idm.MappingsRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) RoleMappingResource(org.keycloak.admin.client.resource.RoleMappingResource) Test(org.junit.Test)

Aggregations

MappingsRepresentation (org.keycloak.representations.idm.MappingsRepresentation)10 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)7 Test (org.junit.Test)5 RoleMappingResource (org.keycloak.admin.client.resource.RoleMappingResource)5 Response (javax.ws.rs.core.Response)4 RealmResource (org.keycloak.admin.client.resource.RealmResource)4 GroupRepresentation (org.keycloak.representations.idm.GroupRepresentation)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 NoCache (org.jboss.resteasy.annotations.cache.NoCache)2 ClientModel (org.keycloak.models.ClientModel)2 ClientMappingsRepresentation (org.keycloak.representations.idm.ClientMappingsRepresentation)2 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 NotFoundException (javax.ws.rs.NotFoundException)1 ClientResource (org.keycloak.admin.client.resource.ClientResource)1