use of org.keycloak.representations.idm.authorization.PermissionRequest in project keycloak by keycloak.
the class PermissionManagementTest method testDeleteScopeAndPermissionTicket.
@Test
public void testDeleteScopeAndPermissionTicket() throws Exception {
ResourceRepresentation resource = addResource("Resource A", "kolo", true, "ScopeA", "ScopeB", "ScopeC");
PermissionRequest permissionRequest = new PermissionRequest(resource.getId());
permissionRequest.setScopes(new HashSet<>(Arrays.asList("ScopeA", "ScopeB", "ScopeC")));
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) {
}
assertEquals(3, authzClient.protection().permission().findByResource(resource.getId()).size());
AuthorizationResource authorization = getClient(getRealm()).authorization();
ResourceScopesResource scopes = authorization.scopes();
ScopeRepresentation scope = scopes.findByName("ScopeA");
List permissions = authzClient.protection().permission().findByScope(scope.getId());
assertFalse(permissions.isEmpty());
assertEquals(1, permissions.size());
resource.setScopes(Collections.emptySet());
authorization.resources().resource(resource.getId()).update(resource);
scopes.scope(scope.getId()).remove();
assertTrue(authzClient.protection().permission().findByScope(scope.getId()).isEmpty());
assertEquals(0, authzClient.protection().permission().findByResource(resource.getId()).size());
}
use of org.keycloak.representations.idm.authorization.PermissionRequest in project keycloak by keycloak.
the class PermissionManagementTest method failInvalidScope.
@Test
public void failInvalidScope() throws Exception {
addResource("Resource A", "ScopeA", "ScopeB");
try {
PermissionRequest permissionRequest = new PermissionRequest("Resource A");
permissionRequest.setScopes(new HashSet<>(Arrays.asList("ScopeA", "ScopeC")));
getAuthzClient().protection().permission().create(permissionRequest);
fail("Should fail, resource does not exist");
} catch (RuntimeException cause) {
assertTrue(HttpResponseException.class.isInstance(cause.getCause()));
assertEquals(400, HttpResponseException.class.cast(cause.getCause()).getStatusCode());
assertTrue(new String((HttpResponseException.class.cast(cause.getCause()).getBytes())).contains("invalid_scope"));
}
}
use of org.keycloak.representations.idm.authorization.PermissionRequest in project keycloak by keycloak.
the class PermissionManagementTest method testPermissionForTypedScope.
@Test
public void testPermissionForTypedScope() throws Exception {
ResourceRepresentation typedResource = addResource("Typed Resource", "ScopeC");
typedResource.setType("typed-resource");
getClient(getRealm()).authorization().resources().resource(typedResource.getId()).update(typedResource);
ResourceRepresentation resourceA = addResource("Resource A", "marta", true, "ScopeA", "ScopeB");
resourceA.setType(typedResource.getType());
getClient(getRealm()).authorization().resources().resource(resourceA.getId()).update(resourceA);
PermissionRequest permissionRequest = new PermissionRequest(resourceA.getId());
permissionRequest.setScopes(new HashSet<>(Arrays.asList("ScopeA", "ScopeC")));
AuthzClient authzClient = getAuthzClient();
PermissionResponse response = authzClient.protection("kolo", "password").permission().create(permissionRequest);
AuthorizationRequest request = new AuthorizationRequest();
request.setTicket(response.getTicket());
request.setClaimToken(authzClient.obtainAccessToken("kolo", "password").getToken());
try {
authzClient.authorization().authorize(request);
} catch (Exception e) {
}
assertPersistence(response, resourceA, "ScopeA", "ScopeC");
}
use of org.keycloak.representations.idm.authorization.PermissionRequest in project keycloak by keycloak.
the class GroupPathPolicyTest method testOnlyChildrenPolicy.
@Test
public void testOnlyChildrenPolicy() throws Exception {
RealmResource realm = getRealm();
AuthzClient authzClient = getAuthzClient();
PermissionRequest request = new PermissionRequest("Resource B");
String ticket = authzClient.protection().permission().create(request).getTicket();
try {
authzClient.authorization("kolo", "password").authorize(new AuthorizationRequest(ticket));
fail("Should fail because user is not granted with expected role");
} catch (AuthorizationDeniedException ignore) {
}
GroupRepresentation group = getGroup("/Group A/Group B/Group C");
UserRepresentation user = realm.users().search("kolo").get(0);
realm.users().get(user.getId()).joinGroup(group.getId());
AuthorizationResponse response = authzClient.authorization("kolo", "password").authorize(new AuthorizationRequest(ticket));
assertNotNull(response.getToken());
try {
authzClient.authorization("marta", "password").authorize(new AuthorizationRequest(ticket));
fail("Should fail because user is not granted with expected role");
} catch (AuthorizationDeniedException ignore) {
}
}
use of org.keycloak.representations.idm.authorization.PermissionRequest in project keycloak by keycloak.
the class PermissionManagementTest method failInvalidResource.
@Test
public void failInvalidResource() {
try {
getAuthzClient().protection().permission().create(new PermissionRequest("Invalid Resource"));
fail("Should fail, resource does not exist");
} catch (RuntimeException cause) {
assertTrue(HttpResponseException.class.isInstance(cause.getCause()));
assertEquals(400, HttpResponseException.class.cast(cause.getCause()).getStatusCode());
assertTrue(new String(HttpResponseException.class.cast(cause.getCause()).getBytes()).contains("invalid_resource_id"));
}
try {
getAuthzClient().protection().permission().create(new PermissionRequest());
fail("Should fail, resource is empty");
} catch (RuntimeException cause) {
cause.printStackTrace();
assertTrue(HttpResponseException.class.isInstance(cause.getCause()));
assertEquals(400, HttpResponseException.class.cast(cause.getCause()).getStatusCode());
assertTrue(new String((HttpResponseException.class.cast(cause.getCause()).getBytes())).contains("invalid_resource_id"));
}
}
Aggregations