use of org.keycloak.representations.idm.authorization.ScopeRepresentation in project keycloak by keycloak.
the class AlbumService method createProtectedResource.
private void createProtectedResource(Album album) {
log.debug("Creating ProtectedResource for " + album);
try {
HashSet<ScopeRepresentation> scopes = new HashSet<>();
scopes.add(new ScopeRepresentation(SCOPE_ALBUM_VIEW));
scopes.add(new ScopeRepresentation(SCOPE_ALBUM_DELETE));
ResourceRepresentation albumResource = new ResourceRepresentation(album.getName(), scopes, "/album/" + album.getName(), "http://photoz.com/album");
albumResource.setOwner(album.getUserId());
if (album.isUserManaged()) {
albumResource.setOwnerManagedAccess(true);
}
getAuthzClient().protection().resource().create(albumResource);
} catch (Exception e) {
throw new RuntimeException("Could not register protected resource.", e);
}
}
use of org.keycloak.representations.idm.authorization.ScopeRepresentation in project keycloak by keycloak.
the class PermissionManagementTest method assertPersistence.
private void assertPersistence(PermissionResponse response, ResourceRepresentation resource, String... scopeNames) throws Exception {
String ticket = response.getTicket();
assertNotNull(ticket);
int expectedPermissions = scopeNames.length > 0 ? scopeNames.length : 1;
List<PermissionTicketRepresentation> tickets = getAuthzClient().protection().permission().findByResource(resource.getId());
assertEquals(expectedPermissions, tickets.size());
PermissionTicketToken token = new JWSInput(ticket).readJsonContent(PermissionTicketToken.class);
List<Permission> tokenPermissions = token.getPermissions();
assertNotNull(tokenPermissions);
assertEquals(expectedPermissions, scopeNames.length > 0 ? scopeNames.length : tokenPermissions.size());
Iterator<Permission> permissionIterator = tokenPermissions.iterator();
while (permissionIterator.hasNext()) {
Permission resourcePermission = permissionIterator.next();
long count = tickets.stream().filter(representation -> representation.getResource().equals(resourcePermission.getResourceId())).count();
if (count == (scopeNames.length > 0 ? scopeNames.length : 1)) {
permissionIterator.remove();
}
}
assertTrue(tokenPermissions.isEmpty());
ArrayList<PermissionTicketRepresentation> expectedTickets = new ArrayList<>(tickets);
Iterator<PermissionTicketRepresentation> ticketIterator = expectedTickets.iterator();
while (ticketIterator.hasNext()) {
PermissionTicketRepresentation ticketRep = ticketIterator.next();
assertFalse(ticketRep.isGranted());
if (ticketRep.getScope() != null) {
ScopeRepresentation scope = getClient(getRealm()).authorization().scopes().scope(ticketRep.getScope()).toRepresentation();
if (Arrays.asList(scopeNames).contains(scope.getName())) {
ticketIterator.remove();
}
} else if (ticketRep.getResource().equals(resource.getId())) {
ticketIterator.remove();
}
}
assertTrue(expectedTickets.isEmpty());
}
use of org.keycloak.representations.idm.authorization.ScopeRepresentation 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.ScopeRepresentation in project keycloak by keycloak.
the class ExportImportUtil method assertAuthorizationSettingsTestAppAuthz.
private static void assertAuthorizationSettingsTestAppAuthz(RealmResource realmRsc) {
AuthorizationResource authzResource = ApiUtil.findAuthorizationSettings(realmRsc, "test-app-authz");
Assert.assertNotNull(authzResource);
List<ResourceRepresentation> resources = authzResource.resources().resources();
Assert.assertEquals(4, resources.size());
ResourceServerRepresentation authzSettings = authzResource.getSettings();
List<Predicate<ResourceRepresentation>> resourcePredicates = new ArrayList<>();
resourcePredicates.add(resourceRep -> {
if ("Admin Resource".equals(resourceRep.getName())) {
Assert.assertEquals(authzSettings.getClientId(), resourceRep.getOwner().getId());
Assert.assertEquals("/protected/admin/*", resourceRep.getUri());
Assert.assertEquals("http://test-app-authz/protected/admin", resourceRep.getType());
Assert.assertEquals("http://icons.com/icon-admin", resourceRep.getIconUri());
Assert.assertEquals(1, resourceRep.getScopes().size());
return true;
}
return false;
});
resourcePredicates.add(resourceRep -> {
if ("Protected Resource".equals(resourceRep.getName())) {
Assert.assertEquals(authzSettings.getClientId(), resourceRep.getOwner().getId());
Assert.assertEquals("/*", resourceRep.getUri());
Assert.assertEquals("http://test-app-authz/protected/resource", resourceRep.getType());
Assert.assertEquals("http://icons.com/icon-resource", resourceRep.getIconUri());
Assert.assertEquals(1, resourceRep.getScopes().size());
return true;
}
return false;
});
resourcePredicates.add(resourceRep -> {
if ("Premium Resource".equals(resourceRep.getName())) {
Assert.assertEquals(authzSettings.getClientId(), resourceRep.getOwner().getId());
Assert.assertEquals("/protected/premium/*", resourceRep.getUri());
Assert.assertEquals("urn:test-app-authz:protected:resource", resourceRep.getType());
Assert.assertEquals("http://icons.com/icon-premium", resourceRep.getIconUri());
Assert.assertEquals(1, resourceRep.getScopes().size());
return true;
}
return false;
});
resourcePredicates.add(resourceRep -> {
if ("Main Page".equals(resourceRep.getName())) {
Assert.assertEquals(authzSettings.getClientId(), resourceRep.getOwner().getId());
Assert.assertNull(resourceRep.getUri());
Assert.assertEquals("urn:test-app-authz:protected:resource", resourceRep.getType());
Assert.assertEquals("http://icons.com/icon-main-page", resourceRep.getIconUri());
Assert.assertEquals(3, resourceRep.getScopes().size());
return true;
}
return false;
});
assertPredicate(resources, resourcePredicates);
List<ScopeRepresentation> scopes = authzResource.scopes().scopes();
Assert.assertEquals(6, scopes.size());
List<Predicate<ScopeRepresentation>> scopePredicates = new ArrayList<>();
scopePredicates.add(scopeRepresentation -> "admin-access".equals(scopeRepresentation.getName()));
scopePredicates.add(scopeRepresentation -> "resource-access".equals(scopeRepresentation.getName()));
scopePredicates.add(scopeRepresentation -> "premium-access".equals(scopeRepresentation.getName()));
scopePredicates.add(scopeRepresentation -> "urn:test-app-authz:page:main:actionForAdmin".equals(scopeRepresentation.getName()));
scopePredicates.add(scopeRepresentation -> "urn:test-app-authz:page:main:actionForUser".equals(scopeRepresentation.getName()));
scopePredicates.add(scopeRepresentation -> "urn:test-app-authz:page:main:actionForPremiumUser".equals(scopeRepresentation.getName()));
assertPredicate(scopes, scopePredicates);
List<PolicyRepresentation> policies = authzResource.policies().policies();
Assert.assertEquals(14, policies.size());
List<Predicate<PolicyRepresentation>> policyPredicates = new ArrayList<>();
policyPredicates.add(policyRepresentation -> "Any Admin Policy".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "Any User Policy".equals(policyRepresentation.getName()));
policyPredicates.add(representation -> "Client and Realm Role Policy".equals(representation.getName()));
policyPredicates.add(representation -> "Client Test Policy".equals(representation.getName()));
policyPredicates.add(representation -> "Group Policy Test".equals(representation.getName()));
policyPredicates.add(policyRepresentation -> "Only Premium User Policy".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "wburke policy".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "All Users Policy".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "Premium Resource Permission".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "Administrative Resource Permission".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "Protected Resource Permission".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "Action 1 on Main Page Resource Permission".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "Action 2 on Main Page Resource Permission".equals(policyRepresentation.getName()));
policyPredicates.add(policyRepresentation -> "Action 3 on Main Page Resource Permission".equals(policyRepresentation.getName()));
assertPredicate(policies, policyPredicates);
}
use of org.keycloak.representations.idm.authorization.ScopeRepresentation in project keycloak by keycloak.
the class ResourcesRestServiceTest method testApprovePermissionRequest.
@Test
public void testApprovePermissionRequest() throws IOException {
Resource resource = getMyResources().get(0);
List<Permission> requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
});
assertTrue(requests.isEmpty());
for (String userName : userNames) {
List<String> scopes = new ArrayList<>();
if ("bob".equals(userName)) {
scopes.add("Scope D");
} else if ("alice".equals(userName)) {
scopes.add("Scope C");
} else if ("jdoe".equals(userName)) {
scopes.add("Scope C");
scopes.add("Scope D");
}
for (String scope : scopes) {
PermissionTicketRepresentation ticket = new PermissionTicketRepresentation();
ticket.setGranted(false);
ticket.setOwner("test-user@localhost");
ticket.setRequesterName(userName);
ticket.setResource(resource.getId());
ticket.setScopeName(scope);
authzClient.protection("test-user@localhost", "password").permission().create(ticket);
}
}
requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
});
assertEquals(3, requests.size());
Iterator<Permission> iterator = requests.iterator();
while (iterator.hasNext()) {
Permission permission = iterator.next();
String username = permission.getUsername();
List<String> scopes = permission.getScopes();
if ("bob".equals(username)) {
scopes.clear();
} else if ("jdoe".equals(username)) {
scopes.remove("Scope C");
}
}
SimpleHttp.doPut(getAccountUrl("resources/" + resource.getId() + "/permissions"), httpClient).auth(tokenUtil.getToken()).json(requests).asResponse();
requests = doGet("/" + resource.getId() + "/permissions/requests", new TypeReference<List<Permission>>() {
});
assertTrue(requests.isEmpty());
for (String user : Arrays.asList("alice", "jdoe")) {
AbstractResourceService.ResourcePermission sharedResource = getSharedWithMe(user).stream().filter(resource1 -> resource1.getId().equals(resource.getId())).findAny().orElse(null);
assertNotNull(sharedResource);
Set<ScopeRepresentation> scopes = sharedResource.getScopes();
if ("alice".equals(user)) {
assertEquals(1, scopes.size());
assertTrue(scopes.stream().anyMatch(scope -> "Scope C".equals(scope.getName())));
} else if ("jdoe".equals(user)) {
assertEquals(1, scopes.size());
assertTrue(scopes.stream().anyMatch(scope -> "Scope D".equals(scope.getName())));
}
}
}
Aggregations