Search in sources :

Example 51 with ClientScopeRepresentation

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

the class AudienceTest method beforeTest.

@Before
public void beforeTest() {
    // Check if already exists
    ClientScopeResource clientScopeRes = ApiUtil.findClientScopeByName(testRealm(), "audience-scope");
    if (clientScopeRes != null) {
        return;
    }
    // Create client scope 'audience-scope' and add as optional scope to the 'test-app' client
    ClientScopeRepresentation clientScope = new ClientScopeRepresentation();
    clientScope.setName("audience-scope");
    clientScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    Response resp = testRealm().clientScopes().create(clientScope);
    String clientScopeId = ApiUtil.getCreatedId(resp);
    resp.close();
    ClientResource client = ApiUtil.findClientByClientId(testRealm(), "test-app");
    client.addOptionalClientScope(clientScopeId);
}
Also used : Response(javax.ws.rs.core.Response) ClientScopeResource(org.keycloak.admin.client.resource.ClientScopeResource) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) Before(org.junit.Before)

Example 52 with ClientScopeRepresentation

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

the class OfflineTokenTest method offlineTokenDisabledForClient.

@Test
public void offlineTokenDisabledForClient() throws Exception {
    // Remove offline-access scope from client
    ClientScopeRepresentation offlineScope = adminClient.realm("test").clientScopes().findAll().stream().filter((ClientScopeRepresentation clientScope) -> {
        return OAuth2Constants.OFFLINE_ACCESS.equals(clientScope.getName());
    }).findFirst().get();
    ClientManager.realm(adminClient.realm("test")).clientId("offline-client").fullScopeAllowed(false).removeClientScope(offlineScope.getId(), false);
    oauth.scope(OAuth2Constants.OFFLINE_ACCESS);
    oauth.clientId("offline-client");
    oauth.redirectUri(offlineClientAppUri);
    oauth.openLoginForm();
    assertTrue(driver.getCurrentUrl().contains("error_description=Invalid+scopes"));
    // Revert changes
    ClientManager.realm(adminClient.realm("test")).clientId("offline-client").fullScopeAllowed(true).addClientScope(offlineScope.getId(), false);
}
Also used : ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Example 53 with ClientScopeRepresentation

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

the class OAuthGrantTest method oauthGrantDynamicScopeParamRequired.

@Test
@EnableFeature(value = Profile.Feature.DYNAMIC_SCOPES, skipRestart = true)
public void oauthGrantDynamicScopeParamRequired() {
    RealmResource appRealm = adminClient.realm(REALM_NAME);
    ClientResource thirdParty = findClientByClientId(appRealm, THIRD_PARTY_APP);
    // Create clientScope
    ClientScopeRepresentation scope = new ClientScopeRepresentation();
    scope.setName("foo-dynamic-scope");
    scope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    scope.setAttributes(new HashMap<String, String>() {

        {
            put(ClientScopeModel.IS_DYNAMIC_SCOPE, "true");
            put(ClientScopeModel.DYNAMIC_SCOPE_REGEXP, "foo-dynamic-scope:*");
        }
    });
    Response response = appRealm.clientScopes().create(scope);
    String dynamicFooScopeId = ApiUtil.getCreatedId(response);
    response.close();
    getCleanup().addClientScopeId(dynamicFooScopeId);
    // Add clientScope as optional to client
    thirdParty.addOptionalClientScope(dynamicFooScopeId);
    // Assert clientScope not on grant screen when not requested
    oauth.clientId(THIRD_PARTY_APP);
    oauth.scope("foo-dynamic-scope:withparam");
    oauth.doLogin("test-user@localhost", "password");
    grantPage.assertCurrent();
    List<String> grants = grantPage.getDisplayedGrants();
    Assert.assertTrue(grants.contains("foo-dynamic-scope: withparam"));
    grantPage.accept();
    EventRepresentation loginEvent = events.expectLogin().client(THIRD_PARTY_APP).detail(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED).assertEvent();
    String code = new OAuthClient.AuthorizationEndpointResponse(oauth).getCode();
    OAuthClient.AccessTokenResponse res = oauth.doAccessTokenRequest(code, "password");
    events.expectCodeToToken(loginEvent.getDetails().get(Details.CODE_ID), loginEvent.getSessionId()).client(THIRD_PARTY_APP).assertEvent();
    oauth.openLogout();
    events.expectLogout(loginEvent.getSessionId()).assertEvent();
    // login again to check whether the Dynamic scope and only the dynamic scope is requested again
    oauth.scope("foo-dynamic-scope:withparam");
    oauth.doLogin("test-user@localhost", "password");
    grantPage.assertCurrent();
    grants = grantPage.getDisplayedGrants();
    Assert.assertEquals(1, grants.size());
    Assert.assertTrue(grants.contains("foo-dynamic-scope: withparam"));
    grantPage.accept();
    events.expectLogin().client(THIRD_PARTY_APP).detail(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED).assertEvent();
    // Revoke
    accountAppsPage.open();
    accountAppsPage.revokeGrant(THIRD_PARTY_APP);
    events.expect(EventType.REVOKE_GRANT).client("account").detail(Details.REVOKED_CLIENT, THIRD_PARTY_APP).assertEvent();
    // cleanup
    oauth.scope(null);
    thirdParty.removeOptionalClientScope(dynamicFooScopeId);
}
Also used : Response(javax.ws.rs.core.Response) OAuthClient(org.keycloak.testsuite.util.OAuthClient) RealmResource(org.keycloak.admin.client.resource.RealmResource) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) EventRepresentation(org.keycloak.representations.idm.EventRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) EnableFeature(org.keycloak.testsuite.arquillian.annotation.EnableFeature)

Example 54 with ClientScopeRepresentation

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

the class OAuthGrantTest method oauthGrantScopeParamRequired.

@Test
public void oauthGrantScopeParamRequired() throws Exception {
    RealmResource appRealm = adminClient.realm(REALM_NAME);
    ClientResource thirdParty = findClientByClientId(appRealm, THIRD_PARTY_APP);
    // Create clientScope
    ClientScopeRepresentation scope1 = new ClientScopeRepresentation();
    scope1.setName("foo-scope");
    scope1.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    Response response = appRealm.clientScopes().create(scope1);
    String fooScopeId = ApiUtil.getCreatedId(response);
    response.close();
    getCleanup().addClientScopeId(fooScopeId);
    // Add clientScope as optional to client
    thirdParty.addOptionalClientScope(fooScopeId);
    // Assert clientScope not on grant screen when not requested
    oauth.clientId(THIRD_PARTY_APP);
    oauth.doLoginGrant("test-user@localhost", "password");
    grantPage.assertCurrent();
    List<String> grants = grantPage.getDisplayedGrants();
    Assert.assertFalse(grants.contains("foo-scope"));
    grantPage.cancel();
    events.expectLogin().client(THIRD_PARTY_APP).error("rejected_by_user").removeDetail(Details.CONSENT).session(Matchers.nullValue(String.class)).assertEvent();
    oauth.scope("foo-scope");
    oauth.doLoginGrant("test-user@localhost", "password");
    grantPage.assertCurrent();
    grants = grantPage.getDisplayedGrants();
    Assert.assertTrue(grants.contains("foo-scope"));
    grantPage.accept();
    events.expectLogin().client(THIRD_PARTY_APP).detail(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED).assertEvent();
    // Revoke
    accountAppsPage.open();
    accountAppsPage.revokeGrant(THIRD_PARTY_APP);
    events.expect(EventType.REVOKE_GRANT).client("account").detail(Details.REVOKED_CLIENT, THIRD_PARTY_APP).assertEvent();
    // cleanup
    oauth.scope(null);
    thirdParty.removeOptionalClientScope(fooScopeId);
}
Also used : Response(javax.ws.rs.core.Response) RealmResource(org.keycloak.admin.client.resource.RealmResource) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 55 with ClientScopeRepresentation

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

the class OAuthGrantTest method oauthGrantClientScopeMappers.

// KEYCLOAK-4326
@Test
public void oauthGrantClientScopeMappers() throws Exception {
    // Add client scope with some protocol mapper
    RealmResource appRealm = adminClient.realm(REALM_NAME);
    ClientScopeRepresentation scope1 = new ClientScopeRepresentation();
    scope1.setName("foo-addr");
    scope1.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
    Response response = appRealm.clientScopes().create(scope1);
    String fooScopeId = ApiUtil.getCreatedId(response);
    response.close();
    ProtocolMapperRepresentation protocolMapper = ProtocolMapperUtil.createAddressMapper(true, true, true);
    response = appRealm.clientScopes().get(fooScopeId).getProtocolMappers().createMapper(protocolMapper);
    response.close();
    // Add clientScope to client
    ClientResource thirdParty = findClientByClientId(appRealm, THIRD_PARTY_APP);
    thirdParty.addDefaultClientScope(fooScopeId);
    getCleanup().addClientScopeId(fooScopeId);
    // Login
    oauth.clientId(THIRD_PARTY_APP);
    oauth.doLoginGrant("test-user@localhost", "password");
    grantPage.assertCurrent();
    grantPage.assertGrants(OAuthGrantPage.EMAIL_CONSENT_TEXT, OAuthGrantPage.PROFILE_CONSENT_TEXT, OAuthGrantPage.ROLES_CONSENT_TEXT, "foo-addr");
    grantPage.accept();
    events.expectLogin().client(THIRD_PARTY_APP).detail(Details.CONSENT, Details.CONSENT_VALUE_CONSENT_GRANTED).assertEvent();
    // Go to user's application screen
    accountAppsPage.open();
    Assert.assertTrue(accountAppsPage.isCurrent());
    Map<String, AccountApplicationsPage.AppEntry> apps = accountAppsPage.getApplications();
    Assert.assertTrue(apps.containsKey("third-party"));
    Assert.assertTrue(apps.get("third-party").getClientScopesGranted().contains("foo-addr"));
    // Login as admin and see the consent screen of particular user
    UserResource user = ApiUtil.findUserByUsernameId(appRealm, "test-user@localhost");
    List<Map<String, Object>> consents = user.getConsents();
    Assert.assertEquals(1, consents.size());
    // Assert automatically logged another time
    oauth.openLoginForm();
    appPage.assertCurrent();
    events.expectLogin().detail(Details.AUTH_METHOD, OIDCLoginProtocol.LOGIN_PROTOCOL).detail(Details.CONSENT, Details.CONSENT_VALUE_PERSISTED_CONSENT).removeDetail(Details.USERNAME).client(THIRD_PARTY_APP).assertEvent();
    // Revoke
    accountAppsPage.open();
    accountAppsPage.revokeGrant(THIRD_PARTY_APP);
    events.expect(EventType.REVOKE_GRANT).client("account").detail(Details.REVOKED_CLIENT, THIRD_PARTY_APP).assertEvent();
    // Cleanup
    thirdParty.removeDefaultClientScope(fooScopeId);
}
Also used : Response(javax.ws.rs.core.Response) RealmResource(org.keycloak.admin.client.resource.RealmResource) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) UserResource(org.keycloak.admin.client.resource.UserResource) ClientResource(org.keycloak.admin.client.resource.ClientResource) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

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