Search in sources :

Example 6 with OAuthClient

use of org.keycloak.testsuite.util.OAuthClient in project keycloak by keycloak.

the class AppInitiatedActionResetPasswordTest method uncheckLogoutSessions.

@Test
public void uncheckLogoutSessions() {
    OAuthClient oauth2 = new OAuthClient();
    oauth2.init(driver2);
    UserResource testUser = testRealm().users().get(findUser("test-user@localhost").getId());
    loginPage.open();
    loginPage.login("test-user@localhost", "password");
    events.expectLogin().assertEvent();
    oauth2.doLogin("test-user@localhost", "password");
    events.expectLogin().assertEvent();
    assertEquals(2, testUser.getUserSessions().size());
    doAIA();
    changePasswordPage.assertCurrent();
    changePasswordPage.uncheckLogoutSessions();
    changePasswordPage.changePassword("All Right Then, Keep Your Secrets", "All Right Then, Keep Your Secrets");
    events.expectRequiredAction(EventType.UPDATE_PASSWORD).assertEvent();
    assertKcActionStatus(SUCCESS);
    assertEquals(2, testUser.getUserSessions().size());
}
Also used : OAuthClient(org.keycloak.testsuite.util.OAuthClient) UserResource(org.keycloak.admin.client.resource.UserResource) Test(org.junit.Test)

Example 7 with OAuthClient

use of org.keycloak.testsuite.util.OAuthClient in project keycloak by keycloak.

the class AppInitiatedActionResetPasswordTest method checkLogoutSessions.

@Test
public void checkLogoutSessions() {
    OAuthClient oauth2 = new OAuthClient();
    oauth2.init(driver2);
    loginPage.open();
    loginPage.login("test-user@localhost", "password");
    events.expectLogin().assertEvent();
    UserResource testUser = testRealm().users().get(findUser("test-user@localhost").getId());
    List<UserSessionRepresentation> sessions = testUser.getUserSessions();
    assertEquals(1, sessions.size());
    final String firstSessionId = sessions.get(0).getId();
    oauth2.doLogin("test-user@localhost", "password");
    events.expectLogin().assertEvent();
    assertEquals(2, testUser.getUserSessions().size());
    doAIA();
    changePasswordPage.assertCurrent();
    assertTrue("Logout sessions is checked by default", changePasswordPage.isLogoutSessionsChecked());
    changePasswordPage.changePassword("All Right Then, Keep Your Secrets", "All Right Then, Keep Your Secrets");
    events.expectRequiredAction(EventType.UPDATE_PASSWORD).assertEvent();
    assertKcActionStatus(SUCCESS);
    sessions = testUser.getUserSessions();
    assertEquals(1, sessions.size());
    assertEquals("Old session is still valid", firstSessionId, sessions.get(0).getId());
}
Also used : UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) OAuthClient(org.keycloak.testsuite.util.OAuthClient) UserResource(org.keycloak.admin.client.resource.UserResource) Test(org.junit.Test)

Example 8 with OAuthClient

use of org.keycloak.testsuite.util.OAuthClient in project keycloak by keycloak.

the class EntitlementAPITest method testObtainAllEntitlementsForResourceWithResourcePermission.

@Test
public void testObtainAllEntitlementsForResourceWithResourcePermission() throws Exception {
    ClientResource client = getClient(getRealm(), RESOURCE_SERVER_TEST);
    AuthorizationResource authorization = client.authorization();
    JSPolicyRepresentation policy = new JSPolicyRepresentation();
    policy.setName(KeycloakModelUtils.generateId());
    policy.setCode("$evaluation.grant();");
    authorization.policies().js().create(policy).close();
    ResourceRepresentation resource = new ResourceRepresentation();
    resource.setName(KeycloakModelUtils.generateId());
    resource.addScope("scope:view", "scope:update", "scope:delete");
    try (Response response = authorization.resources().create(resource)) {
        resource = response.readEntity(ResourceRepresentation.class);
    }
    ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
    permission.setName(KeycloakModelUtils.generateId());
    permission.addResource(resource.getId());
    permission.addPolicy(policy.getName());
    authorization.permissions().resource().create(permission).close();
    String accessToken = new OAuthClient().realm("authz-test").clientId(RESOURCE_SERVER_TEST).doGrantAccessTokenRequest("secret", "kolo", "password").getAccessToken();
    AuthzClient authzClient = getAuthzClient(AUTHZ_CLIENT_CONFIG);
    AuthorizationRequest request = new AuthorizationRequest();
    request.addPermission(null, "scope:view", "scope:update", "scope:delete");
    AuthorizationResponse response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    Collection<Permission> permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertEquals(resource.getId(), grantedPermission.getResourceId());
        assertEquals(3, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("scope:view")));
    }
    resource.setScopes(new HashSet<>());
    resource.addScope("scope:view", "scope:update");
    authorization.resources().resource(resource.getId()).update(resource);
    request = new AuthorizationRequest();
    request.addPermission(null, "scope:view", "scope:update", "scope:delete");
    response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertEquals(resource.getId(), grantedPermission.getResourceId());
        assertEquals(2, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("scope:view", "scope:update")));
    }
    request = new AuthorizationRequest();
    request.addPermission(resource.getId(), "scope:view", "scope:update", "scope:delete");
    response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertEquals(resource.getId(), grantedPermission.getResourceId());
        assertEquals(2, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("scope:view", "scope:update")));
    }
}
Also used : AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) OAuthClient(org.keycloak.testsuite.util.OAuthClient) JSPolicyRepresentation(org.keycloak.representations.idm.authorization.JSPolicyRepresentation) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) ResourcePermissionRepresentation(org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) Response(javax.ws.rs.core.Response) TokenIntrospectionResponse(org.keycloak.authorization.client.representation.TokenIntrospectionResponse) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) AuthzClient(org.keycloak.authorization.client.AuthzClient) Permission(org.keycloak.representations.idm.authorization.Permission) ClientResource(org.keycloak.admin.client.resource.ClientResource) Test(org.junit.Test)

Example 9 with OAuthClient

use of org.keycloak.testsuite.util.OAuthClient in project keycloak by keycloak.

the class EntitlementAPITest method testResourceServerAsAudience.

private void testResourceServerAsAudience(String testClientId, String resourceServerClientId, String configFile) throws Exception {
    AuthorizationRequest request = new AuthorizationRequest();
    request.addPermission("Resource 1");
    String accessToken = new OAuthClient().realm("authz-test").clientId(testClientId).doGrantAccessTokenRequest("secret", "marta", "password").getAccessToken();
    AuthorizationResponse response = getAuthzClient(configFile).authorization(accessToken).authorize(request);
    AccessToken rpt = toAccessToken(response.getToken());
    assertEquals(resourceServerClientId, rpt.getAudience()[0]);
}
Also used : AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) OAuthClient(org.keycloak.testsuite.util.OAuthClient) AccessToken(org.keycloak.representations.AccessToken) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse)

Example 10 with OAuthClient

use of org.keycloak.testsuite.util.OAuthClient in project keycloak by keycloak.

the class EntitlementAPITest method testObtainAllEntitlementsInvalidScope.

@Test
public void testObtainAllEntitlementsInvalidScope() throws Exception {
    ClientResource client = getClient(getRealm(), RESOURCE_SERVER_TEST);
    AuthorizationResource authorization = client.authorization();
    JSPolicyRepresentation policy = new JSPolicyRepresentation();
    policy.setName(KeycloakModelUtils.generateId());
    policy.setCode("$evaluation.grant();");
    authorization.policies().js().create(policy).close();
    ResourceRepresentation resource = new ResourceRepresentation();
    resource.setName(KeycloakModelUtils.generateId());
    resource.addScope("sensors:view", "sensors:update", "sensors:delete");
    try (Response response = authorization.resources().create(resource)) {
        resource = response.readEntity(ResourceRepresentation.class);
    }
    ScopePermissionRepresentation permission = new ScopePermissionRepresentation();
    permission.setName(KeycloakModelUtils.generateId());
    permission.addScope("sensors:view");
    permission.addPolicy(policy.getName());
    authorization.permissions().scope().create(permission).close();
    String accessToken = new OAuthClient().realm("authz-test").clientId(RESOURCE_SERVER_TEST).doGrantAccessTokenRequest("secret", "kolo", "password").getAccessToken();
    AuthzClient authzClient = getAuthzClient(AUTHZ_CLIENT_CONFIG);
    AuthorizationRequest request = new AuthorizationRequest();
    request.addPermission(resource.getId(), "sensors:view_invalid");
    try {
        authzClient.authorization(accessToken).authorize(request);
        fail("scope is invalid");
    } catch (RuntimeException expected) {
        assertEquals(400, HttpResponseException.class.cast(expected.getCause()).getStatusCode());
        assertTrue(HttpResponseException.class.cast(expected.getCause()).toString().contains("invalid_scope"));
    }
    request = new AuthorizationRequest();
    request.addPermission(null, "sensors:view_invalid");
    try {
        authzClient.authorization(accessToken).authorize(request);
        fail("scope is invalid");
    } catch (RuntimeException expected) {
        assertEquals(400, HttpResponseException.class.cast(expected.getCause()).getStatusCode());
        assertTrue(HttpResponseException.class.cast(expected.getCause()).toString().contains("invalid_scope"));
    }
}
Also used : AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) Response(javax.ws.rs.core.Response) TokenIntrospectionResponse(org.keycloak.authorization.client.representation.TokenIntrospectionResponse) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) AuthzClient(org.keycloak.authorization.client.AuthzClient) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) OAuthClient(org.keycloak.testsuite.util.OAuthClient) JSPolicyRepresentation(org.keycloak.representations.idm.authorization.JSPolicyRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) ScopePermissionRepresentation(org.keycloak.representations.idm.authorization.ScopePermissionRepresentation) Test(org.junit.Test)

Aggregations

OAuthClient (org.keycloak.testsuite.util.OAuthClient)38 Test (org.junit.Test)30 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)19 AuthorizationResponse (org.keycloak.representations.idm.authorization.AuthorizationResponse)19 AuthzClient (org.keycloak.authorization.client.AuthzClient)18 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)17 ClientResource (org.keycloak.admin.client.resource.ClientResource)17 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)16 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)15 Response (javax.ws.rs.core.Response)12 TokenIntrospectionResponse (org.keycloak.authorization.client.representation.TokenIntrospectionResponse)12 AccessTokenResponse (org.keycloak.representations.AccessTokenResponse)12 PermissionResponse (org.keycloak.representations.idm.authorization.PermissionResponse)12 Permission (org.keycloak.representations.idm.authorization.Permission)11 ScopePermissionRepresentation (org.keycloak.representations.idm.authorization.ScopePermissionRepresentation)11 ResourcePermissionRepresentation (org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation)10 HttpResponseException (org.keycloak.authorization.client.util.HttpResponseException)9 AccessToken (org.keycloak.representations.AccessToken)5 IOException (java.io.IOException)4 EventRepresentation (org.keycloak.representations.idm.EventRepresentation)4