Search in sources :

Example 36 with ClientScopeRepresentation

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

the class ClientScopeTest method updateAssignedDefaultClientScopeToDynamicScope.

@Test
@EnableFeature(value = Profile.Feature.DYNAMIC_SCOPES, skipRestart = true)
public void updateAssignedDefaultClientScopeToDynamicScope() {
    ClientRepresentation clientRep = new ClientRepresentation();
    clientRep.setClientId("dyn-scope-client");
    clientRep.setProtocol("openid-connect");
    String clientUuid = createClient(clientRep);
    getCleanup().addClientUuid(clientUuid);
    ClientScopeRepresentation scopeRep = new ClientScopeRepresentation();
    scopeRep.setName("dynamic-scope-def");
    scopeRep.setProtocol("openid-connect");
    String scopeDefId = createClientScope(scopeRep);
    getCleanup().addClientScopeId(scopeDefId);
    testRealmResource().clients().get(clientUuid).addDefaultClientScope(scopeDefId);
    scopeRep.setAttributes(new HashMap<String, String>() {

        {
            put(ClientScopeModel.IS_DYNAMIC_SCOPE, "true");
            put(ClientScopeModel.DYNAMIC_SCOPE_REGEXP, "dynamic-scope-def:*:*");
        }
    });
    try {
        clientScopes().get(scopeDefId).update(scopeRep);
        Assert.fail("This update should fail");
    } catch (ClientErrorException ex) {
        MatcherAssert.assertThat(ex.getResponse(), Matchers.statusCodeIs(Status.BAD_REQUEST));
    }
}
Also used : ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientErrorException(javax.ws.rs.ClientErrorException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test) EnableFeature(org.keycloak.testsuite.arquillian.annotation.EnableFeature)

Example 37 with ClientScopeRepresentation

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

the class ClientScopeTest method scopesRemainAfterClientUpdate.

// KEYCLOAK-18332
@Test
public void scopesRemainAfterClientUpdate() {
    // Create a bunch of scopes
    ClientScopeRepresentation scopeRep = new ClientScopeRepresentation();
    scopeRep.setName("scope-def");
    scopeRep.setProtocol("openid-connect");
    String scopeDefId = createClientScope(scopeRep);
    getCleanup().addClientScopeId(scopeDefId);
    scopeRep = new ClientScopeRepresentation();
    scopeRep.setName("scope-opt");
    scopeRep.setProtocol("openid-connect");
    String scopeOptId = createClientScope(scopeRep);
    getCleanup().addClientScopeId(scopeOptId);
    // Add scope-def as default and scope-opt as optional client scope
    testRealmResource().addDefaultDefaultClientScope(scopeDefId);
    assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.defaultDefaultClientScopePath(scopeDefId), ResourceType.CLIENT_SCOPE);
    testRealmResource().addDefaultOptionalClientScope(scopeOptId);
    assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.defaultOptionalClientScopePath(scopeOptId), ResourceType.CLIENT_SCOPE);
    // Create a client
    ClientRepresentation clientRep = new ClientRepresentation();
    clientRep.setClientId("bar-client");
    clientRep.setProtocol("openid-connect");
    String clientUuid = createClient(clientRep);
    ClientResource client = testRealmResource().clients().get(clientUuid);
    getCleanup().addClientUuid(clientUuid);
    assertTrue(getClientScopeNames(client.getDefaultClientScopes()).contains("scope-def"));
    assertTrue(getClientScopeNames(client.getOptionalClientScopes()).contains("scope-opt"));
    // Remove the scopes from client
    client.removeDefaultClientScope(scopeDefId);
    client.removeOptionalClientScope(scopeOptId);
    List<String> expectedDefScopes = getClientScopeNames(client.getDefaultClientScopes());
    List<String> expectedOptScopes = getClientScopeNames(client.getOptionalClientScopes());
    assertFalse(expectedDefScopes.contains("scope-def"));
    assertFalse(expectedOptScopes.contains("scope-opt"));
    // Update the client
    clientRep = client.toRepresentation();
    // Make a small change
    clientRep.setDescription("desc");
    client.update(clientRep);
    // Assert scopes are intact
    assertEquals(expectedDefScopes, getClientScopeNames(client.getDefaultClientScopes()));
    assertEquals(expectedOptScopes, getClientScopeNames(client.getOptionalClientScopes()));
}
Also used : ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 38 with ClientScopeRepresentation

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

the class ClientScopeTest method testRemoveClientScope.

@Test
public void testRemoveClientScope() {
    // Create scope1
    ClientScopeRepresentation scopeRep = new ClientScopeRepresentation();
    scopeRep.setName("scope1");
    String scope1Id = createClientScope(scopeRep);
    List<ClientScopeRepresentation> clientScopes = clientScopes().findAll();
    assertTrue(getClientScopeNames(clientScopes).contains("scope1"));
    // Create scope2
    scopeRep = new ClientScopeRepresentation();
    scopeRep.setName("scope2");
    String scope2Id = createClientScope(scopeRep);
    clientScopes = clientScopes().findAll();
    assertTrue(getClientScopeNames(clientScopes).contains("scope2"));
    // Remove scope1
    removeClientScope(scope1Id);
    clientScopes = clientScopes().findAll();
    Assert.assertFalse(getClientScopeNames(clientScopes).contains("scope1"));
    assertTrue(getClientScopeNames(clientScopes).contains("scope2"));
    // Remove scope2
    removeClientScope(scope2Id);
    clientScopes = clientScopes().findAll();
    Assert.assertFalse(getClientScopeNames(clientScopes).contains("scope1"));
    Assert.assertFalse(getClientScopeNames(clientScopes).contains("scope2"));
}
Also used : ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) Test(org.junit.Test)

Example 39 with ClientScopeRepresentation

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

the class ClientScopeTest method testCreateInvalidRegexpDynamicScope.

@Test
@EnableFeature(value = Profile.Feature.DYNAMIC_SCOPES, skipRestart = true)
public void testCreateInvalidRegexpDynamicScope() {
    ClientScopeRepresentation scopeRep = new ClientScopeRepresentation();
    scopeRep.setName("dynamic-scope-def4");
    scopeRep.setProtocol("openid-connect");
    scopeRep.setAttributes(new HashMap<String, String>() {

        {
            put(ClientScopeModel.IS_DYNAMIC_SCOPE, "true");
            put(ClientScopeModel.DYNAMIC_SCOPE_REGEXP, "dynamic-scope-def:*:*");
        }
    });
    handleExpectedCreateFailure(scopeRep, 400, "Invalid format for the Dynamic Scope regexp dynamic-scope-def:*:*");
}
Also used : ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) Test(org.junit.Test) EnableFeature(org.keycloak.testsuite.arquillian.annotation.EnableFeature)

Example 40 with ClientScopeRepresentation

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

the class ClientScopeTest method defaultOptionalClientScopeCanBeAssignedToClientAsDefaultScope.

// KEYCLOAK-9999
@Test
public void defaultOptionalClientScopeCanBeAssignedToClientAsDefaultScope() {
    // Create optional client scope
    ClientScopeRepresentation optionalClientScope = new ClientScopeRepresentation();
    optionalClientScope.setName("optional-client-scope");
    optionalClientScope.setProtocol("openid-connect");
    String optionalClientScopeId = createClientScope(optionalClientScope);
    getCleanup().addClientScopeId(optionalClientScopeId);
    testRealmResource().addDefaultOptionalClientScope(optionalClientScopeId);
    assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.defaultOptionalClientScopePath(optionalClientScopeId), ResourceType.CLIENT_SCOPE);
    // Ensure that scope is optional
    List<String> realmOptionalScopes = getClientScopeNames(testRealmResource().getDefaultOptionalClientScopes());
    assertTrue(realmOptionalScopes.contains("optional-client-scope"));
    // Create client
    ClientRepresentation client = new ClientRepresentation();
    client.setClientId("test-client");
    client.setDefaultClientScopes(Collections.singletonList("optional-client-scope"));
    String clientUuid = createClient(client);
    getCleanup().addClientUuid(clientUuid);
    // Ensure that default optional client scope is a default scope of the client
    List<String> clientDefaultScopes = getClientScopeNames(testRealmResource().clients().get(clientUuid).getDefaultClientScopes());
    assertTrue(clientDefaultScopes.contains("optional-client-scope"));
    // Ensure that no optional scopes are assigned to the client, even if there are default optional scopes!
    List<String> clientOptionalScopes = getClientScopeNames(testRealmResource().clients().get(clientUuid).getOptionalClientScopes());
    assertTrue(clientOptionalScopes.isEmpty());
    // Unassign optional client scope from realm for cleanup
    testRealmResource().removeDefaultOptionalClientScope(optionalClientScopeId);
    assertAdminEvents.assertEvent(getRealmId(), OperationType.DELETE, AdminEventPaths.defaultOptionalClientScopePath(optionalClientScopeId), ResourceType.CLIENT_SCOPE);
}
Also used : ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Aggregations

ClientScopeRepresentation (org.keycloak.representations.idm.ClientScopeRepresentation)75 Test (org.junit.Test)62 Response (javax.ws.rs.core.Response)27 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)27 ClientResource (org.keycloak.admin.client.resource.ClientResource)25 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)16 RealmResource (org.keycloak.admin.client.resource.RealmResource)15 EnableFeature (org.keycloak.testsuite.arquillian.annotation.EnableFeature)13 ConsentRepresentation (org.keycloak.representations.account.ConsentRepresentation)11 ConsentScopeRepresentation (org.keycloak.representations.account.ConsentScopeRepresentation)11 ProtocolMapperRepresentation (org.keycloak.representations.idm.ProtocolMapperRepresentation)11 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)11 TokenUtil (org.keycloak.testsuite.util.TokenUtil)11 HashMap (java.util.HashMap)10 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)10 OAuthClient (org.keycloak.testsuite.util.OAuthClient)10 List (java.util.List)8 ClientScopeResource (org.keycloak.admin.client.resource.ClientScopeResource)6 SimpleHttp (org.keycloak.broker.provider.util.SimpleHttp)6 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)6