Search in sources :

Example 1 with ClientScopesResource

use of org.keycloak.admin.client.resource.ClientScopesResource in project keycloak by keycloak.

the class ClientScopePolicyTest method testRemovePolicyWhenRemovingScope.

@Test
public void testRemovePolicyWhenRemovingScope() {
    createClientScopePolicy("Client Scope To Remove Policy", "to-remove-a", "to-remove-b");
    ClientScopesResource clientScopes = getRealm().clientScopes();
    ClientScopeRepresentation scopeRep = clientScopes.findAll().stream().filter(r -> r.getName().equals("to-remove-a")).findAny().get();
    getClient().removeDefaultClientScope(scopeRep.getId());
    getRealm().clientScopes().get(scopeRep.getId()).remove();
    ClientScopePolicyRepresentation policyRep = getClient().authorization().policies().clientScope().findByName("Client Scope To Remove Policy");
    final String id = scopeRep.getId();
    assertFalse(policyRep.getClientScopes().stream().anyMatch(def -> def.getId().equals(id)));
    scopeRep = clientScopes.findAll().stream().filter(r -> r.getName().equals("to-remove-b")).findAny().get();
    getClient().removeDefaultClientScope(scopeRep.getId());
    getRealm().clientScopes().get(scopeRep.getId()).remove();
    assertNull(getClient().authorization().policies().clientScope().findByName("Client Scope To Remove Policy"));
}
Also used : ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) AuthzClient(org.keycloak.authorization.client.AuthzClient) RealmBuilder(org.keycloak.testsuite.util.RealmBuilder) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) AuthorizationDeniedException(org.keycloak.authorization.client.AuthorizationDeniedException) UserBuilder(org.keycloak.testsuite.util.UserBuilder) ResourcePermissionRepresentation(org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation) Assert.fail(org.junit.Assert.fail) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) ClientResource(org.keycloak.admin.client.resource.ClientResource) Before(org.junit.Before) ClientScopesResource(org.keycloak.admin.client.resource.ClientScopesResource) Assert.assertNotNull(org.junit.Assert.assertNotNull) RealmResource(org.keycloak.admin.client.resource.RealmResource) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) Test(org.junit.Test) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) PermissionRequest(org.keycloak.representations.idm.authorization.PermissionRequest) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) ClientBuilder(org.keycloak.testsuite.util.ClientBuilder) ClientScopeBuilder(org.keycloak.testsuite.util.ClientScopeBuilder) ClientScopePolicyRepresentation(org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation) ClientScopePolicyRepresentation(org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientScopesResource(org.keycloak.admin.client.resource.ClientScopesResource) Test(org.junit.Test)

Example 2 with ClientScopesResource

use of org.keycloak.admin.client.resource.ClientScopesResource in project keycloak by keycloak.

the class AbstractBasePhotozExampleAdapterTest method setManageAlbumScopeRequired.

protected void setManageAlbumScopeRequired() {
    ClientScopeRepresentation clientScope = new ClientScopeRepresentation();
    clientScope.setName("manage-albums");
    clientScope.setProtocol("openid-connect");
    ProtocolMapperRepresentation mapper = new ProtocolMapperRepresentation();
    mapper.setName("manage-albums");
    mapper.setProtocol("openid-connect");
    mapper.setProtocolMapper(UserClientRoleMappingMapper.PROVIDER_ID);
    Map<String, String> config = new HashMap<>();
    config.put("access.token.claim", "true");
    config.put("id.token.claim", "true");
    config.put("userinfo.token.claim", "true");
    config.put(ProtocolMapperUtils.USER_MODEL_CLIENT_ROLE_MAPPING_CLIENT_ID, "photoz-restful-api");
    mapper.setConfig(config);
    clientScope.setProtocolMappers(Arrays.asList(mapper));
    RealmResource realmResource = realmsResouce().realm(REALM_NAME);
    ClientScopesResource clientScopes = realmResource.clientScopes();
    Response resp = clientScopes.create(clientScope);
    Assert.assertEquals(201, resp.getStatus());
    resp.close();
    String clientScopeId = ApiUtil.getCreatedId(resp);
    ClientResource resourceServer = getClientResource(RESOURCE_SERVER_ID);
    clientScopes.get(clientScopeId).getScopeMappings().clientLevel(resourceServer.toRepresentation().getId()).add(Arrays.asList(resourceServer.roles().get("manage-albums").toRepresentation()));
    ClientResource html5ClientApp = getClientResource("photoz-html5-client");
    html5ClientApp.addOptionalClientScope(clientScopeId);
    html5ClientApp.getScopeMappings().realmLevel().add(Arrays.asList(realmResource.roles().get("user").toRepresentation(), realmResource.roles().get("admin").toRepresentation()));
    ClientRepresentation clientRep = html5ClientApp.toRepresentation();
    clientRep.setFullScopeAllowed(false);
    html5ClientApp.update(clientRep);
}
Also used : Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) RealmResource(org.keycloak.admin.client.resource.RealmResource) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) ClientScopesResource(org.keycloak.admin.client.resource.ClientScopesResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 3 with ClientScopesResource

use of org.keycloak.admin.client.resource.ClientScopesResource in project keycloak by keycloak.

the class TokenIntrospectionTest method afterAbstractKeycloakTestRealmImport.

@Override
protected void afterAbstractKeycloakTestRealmImport() {
    ClientScopesResource clientScopesResource = testRealm().clientScopes();
    List<ClientScopeRepresentation> clientScopeRepresentations = clientScopesResource.findAll();
    for (ClientScopeRepresentation scope : clientScopeRepresentations) {
        List<ProtocolMapperRepresentation> mappers = scope.getProtocolMappers();
        if (mappers != null) {
            for (ProtocolMapperRepresentation mapper : mappers) {
                if ("username".equals(mapper.getName())) {
                    Map<String, String> config = mapper.getConfig();
                    config.put("user.attribute", "username");
                    config.put("claim.name", "preferred_username12");
                    clientScopesResource.get(scope.getId()).getProtocolMappers().update(mapper.getId(), mapper);
                }
            }
        }
    }
}
Also used : ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientScopesResource(org.keycloak.admin.client.resource.ClientScopesResource)

Example 4 with ClientScopesResource

use of org.keycloak.admin.client.resource.ClientScopesResource in project keycloak by keycloak.

the class UserInfoTest method switchIncludeRolesInUserInfoEndpoint.

private void switchIncludeRolesInUserInfoEndpoint(boolean includeRoles) {
    ClientScopesResource clientScopesResource = adminClient.realm("test").clientScopes();
    ClientScopeRepresentation rolesClientScope = clientScopesResource.findAll().stream().filter(clientScope -> "roles".equals(clientScope.getName())).findAny().get();
    ProtocolMappersResource protocolMappersResource = clientScopesResource.get(rolesClientScope.getId()).getProtocolMappers();
    ProtocolMapperRepresentation realmRolesMapper = protocolMappersResource.getMappers().stream().filter(mapper -> "realm roles".equals(mapper.getName())).findAny().get();
    realmRolesMapper.getConfig().put(INCLUDE_IN_USERINFO, String.valueOf(includeRoles));
    ProtocolMapperRepresentation clientRolesMapper = protocolMappersResource.getMappers().stream().filter(mapper -> "client roles".equals(mapper.getName())).findAny().get();
    clientRolesMapper.getConfig().put(INCLUDE_IN_USERINFO, String.valueOf(includeRoles));
    protocolMappersResource.update(realmRolesMapper.getId(), realmRolesMapper);
    protocolMappersResource.update(clientRolesMapper.getId(), clientRolesMapper);
}
Also used : ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientScopesResource(org.keycloak.admin.client.resource.ClientScopesResource) ProtocolMappersResource(org.keycloak.admin.client.resource.ProtocolMappersResource)

Aggregations

ClientScopesResource (org.keycloak.admin.client.resource.ClientScopesResource)4 ClientScopeRepresentation (org.keycloak.representations.idm.ClientScopeRepresentation)4 ProtocolMapperRepresentation (org.keycloak.representations.idm.ProtocolMapperRepresentation)3 ClientResource (org.keycloak.admin.client.resource.ClientResource)2 RealmResource (org.keycloak.admin.client.resource.RealmResource)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Response (javax.ws.rs.core.Response)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertNull (org.junit.Assert.assertNull)1 Assert.fail (org.junit.Assert.fail)1 Before (org.junit.Before)1 Test (org.junit.Test)1 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)1 ClientsResource (org.keycloak.admin.client.resource.ClientsResource)1 ProtocolMappersResource (org.keycloak.admin.client.resource.ProtocolMappersResource)1 AuthorizationDeniedException (org.keycloak.authorization.client.AuthorizationDeniedException)1 AuthzClient (org.keycloak.authorization.client.AuthzClient)1 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)1