Search in sources :

Example 6 with PermissionResponse

use of org.keycloak.representations.idm.authorization.PermissionResponse in project keycloak by keycloak.

the class PermissionManagementTest method testCreatePermissionTicketWithResourceWithoutManagedAccess.

@Test
public void testCreatePermissionTicketWithResourceWithoutManagedAccess() throws Exception {
    ResourceRepresentation resource = addResource("Resource A");
    PermissionResponse response = getAuthzClient().protection().permission().create(new PermissionRequest(resource.getName()));
    assertNotNull(response.getTicket());
    assertTrue(getAuthzClient().protection().permission().findByResource(resource.getId()).isEmpty());
}
Also used : PermissionRequest(org.keycloak.representations.idm.authorization.PermissionRequest) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 7 with PermissionResponse

use of org.keycloak.representations.idm.authorization.PermissionResponse in project keycloak by keycloak.

the class PermissionManagementTest method testCreatePermissionTicketWithScopes.

@Test
public void testCreatePermissionTicketWithScopes() throws Exception {
    ResourceRepresentation resource = addResource("Resource A", "kolo", true, "ScopeA", "ScopeB", "ScopeC");
    AuthzClient authzClient = getAuthzClient();
    PermissionResponse response = authzClient.protection("marta", "password").permission().create(new PermissionRequest(resource.getId(), "ScopeA", "ScopeB", "ScopeC"));
    AuthorizationRequest request = new AuthorizationRequest();
    request.setTicket(response.getTicket());
    request.setClaimToken(authzClient.obtainAccessToken("marta", "password").getToken());
    try {
        authzClient.authorization().authorize(request);
    } catch (Exception e) {
    }
    assertPersistence(response, resource, "ScopeA", "ScopeB", "ScopeC");
}
Also used : PermissionRequest(org.keycloak.representations.idm.authorization.PermissionRequest) AuthzClient(org.keycloak.authorization.client.AuthzClient) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 8 with PermissionResponse

use of org.keycloak.representations.idm.authorization.PermissionResponse in project keycloak by keycloak.

the class PermissionManagementTest method testRemoveScopeFromResource.

@Test
public void testRemoveScopeFromResource() throws Exception {
    ResourceRepresentation resource = addResource("Resource A", "kolo", true, "ScopeA", "ScopeB");
    PermissionRequest permissionRequest = new PermissionRequest(resource.getId(), "ScopeA", "ScopeB");
    AuthzClient authzClient = getAuthzClient();
    PermissionResponse response = authzClient.protection("marta", "password").permission().create(permissionRequest);
    assertNotNull(response.getTicket());
    AuthorizationRequest request = new AuthorizationRequest();
    request.setTicket(response.getTicket());
    request.setClaimToken(authzClient.obtainAccessToken("marta", "password").getToken());
    try {
        authzClient.authorization().authorize(request);
    } catch (Exception e) {
    }
    AuthorizationResource authorization = getClient(getRealm()).authorization();
    ResourceScopesResource scopes = authorization.scopes();
    ScopeRepresentation removedScope = scopes.findByName("ScopeA");
    List permissions = authzClient.protection().permission().findByScope(removedScope.getId());
    assertFalse(permissions.isEmpty());
    resource.setScopes(new HashSet<>());
    resource.addScope("ScopeB");
    authorization.resources().resource(resource.getId()).update(resource);
    permissions = authzClient.protection().permission().findByScope(removedScope.getId());
    assertTrue(permissions.isEmpty());
    ScopeRepresentation scopeB = scopes.findByName("ScopeB");
    permissions = authzClient.protection().permission().findByScope(scopeB.getId());
    assertFalse(permissions.isEmpty());
}
Also used : PermissionRequest(org.keycloak.representations.idm.authorization.PermissionRequest) AuthzClient(org.keycloak.authorization.client.AuthzClient) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) ResourceScopesResource(org.keycloak.admin.client.resource.ResourceScopesResource) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ArrayList(java.util.ArrayList) List(java.util.List) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 9 with PermissionResponse

use of org.keycloak.representations.idm.authorization.PermissionResponse in project keycloak by keycloak.

the class PermissionManagementTest method removeUserWithPermissionTicketTest.

@Test
public void removeUserWithPermissionTicketTest() throws Exception {
    String userToRemoveID = createUser(REALM_NAME, "user-to-remove", "password");
    ResourceRepresentation resource = addResource("Resource A", "kolo", true);
    AuthzClient authzClient = getAuthzClient();
    PermissionResponse response = authzClient.protection("user-to-remove", "password").permission().create(new PermissionRequest(resource.getId()));
    AuthorizationRequest request = new AuthorizationRequest();
    request.setTicket(response.getTicket());
    request.setClaimToken(authzClient.obtainAccessToken("user-to-remove", "password").getToken());
    try {
        authzClient.authorization().authorize(request);
    } catch (Exception e) {
    }
    assertPersistence(response, resource);
    // Remove the user and expect the user and also hers permission tickets are successfully removed
    adminClient.realm(REALM_NAME).users().delete(userToRemoveID);
    assertThat(adminClient.realm(REALM_NAME).users().list().stream().map(UserRepresentation::getId).collect(Collectors.toList()), not(hasItem(userToRemoveID)));
    assertThat(getAuthzClient().protection().permission().findByResource(resource.getId()), is(empty()));
}
Also used : PermissionRequest(org.keycloak.representations.idm.authorization.PermissionRequest) AuthzClient(org.keycloak.authorization.client.AuthzClient) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 10 with PermissionResponse

use of org.keycloak.representations.idm.authorization.PermissionResponse in project keycloak by keycloak.

the class PermissionManagementTest method testDeleteResourceAndPermissionTicket.

@Test
public void testDeleteResourceAndPermissionTicket() throws Exception {
    ResourceRepresentation resource = addResource("Resource A", "kolo", true, "ScopeA", "ScopeB", "ScopeC");
    AuthzClient authzClient = getAuthzClient();
    PermissionResponse response = authzClient.protection("marta", "password").permission().create(new PermissionRequest(resource.getId(), "ScopeA", "ScopeB", "ScopeC"));
    AuthorizationRequest request = new AuthorizationRequest();
    request.setTicket(response.getTicket());
    request.setClaimToken(authzClient.obtainAccessToken("marta", "password").getToken());
    try {
        authzClient.authorization().authorize(request);
    } catch (Exception e) {
    }
    assertPersistence(response, resource, "ScopeA", "ScopeB", "ScopeC");
    getAuthzClient().protection().resource().delete(resource.getId());
    assertTrue(getAuthzClient().protection().permission().findByResource(resource.getId()).isEmpty());
}
Also used : PermissionRequest(org.keycloak.representations.idm.authorization.PermissionRequest) AuthzClient(org.keycloak.authorization.client.AuthzClient) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) HttpResponseException(org.keycloak.authorization.client.util.HttpResponseException) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 PermissionRequest (org.keycloak.representations.idm.authorization.PermissionRequest)19 PermissionResponse (org.keycloak.representations.idm.authorization.PermissionResponse)19 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)17 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)16 AuthzClient (org.keycloak.authorization.client.AuthzClient)15 HttpResponseException (org.keycloak.authorization.client.util.HttpResponseException)14 ArrayList (java.util.ArrayList)7 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)5 List (java.util.List)4 AuthorizationResponse (org.keycloak.representations.idm.authorization.AuthorizationResponse)4 PermissionTicketRepresentation (org.keycloak.representations.idm.authorization.PermissionTicketRepresentation)4 ResourceScopesResource (org.keycloak.admin.client.resource.ResourceScopesResource)3 Permission (org.keycloak.representations.idm.authorization.Permission)3 ScopeRepresentation (org.keycloak.representations.idm.authorization.ScopeRepresentation)3 AuthorizationDeniedException (org.keycloak.authorization.client.AuthorizationDeniedException)2 ProtectionResource (org.keycloak.authorization.client.resource.ProtectionResource)2 JWSInput (org.keycloak.jose.jws.JWSInput)2 AccessToken (org.keycloak.representations.AccessToken)2 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)2