use of org.keycloak.representations.idm.authorization.AuthorizationRequest.Metadata in project keycloak by keycloak.
the class EntitlementAPITest method testPermissionLimit.
public void testPermissionLimit(String configFile) {
AuthorizationRequest request = new AuthorizationRequest();
for (int i = 1; i <= 10; i++) {
request.addPermission("Resource " + i);
}
Metadata metadata = new Metadata();
metadata.setLimit(10);
request.setMetadata(metadata);
AuthorizationResponse response = getAuthzClient(configFile).authorization("marta", "password").authorize(request);
AccessToken rpt = toAccessToken(response.getToken());
List<Permission> permissions = new ArrayList<>(rpt.getAuthorization().getPermissions());
assertEquals(10, permissions.size());
for (int i = 0; i < 10; i++) {
assertEquals("Resource " + (i + 1), permissions.get(i).getResourceName());
}
request = new AuthorizationRequest();
for (int i = 11; i <= 15; i++) {
request.addPermission("Resource " + i);
}
request.setMetadata(metadata);
request.setRpt(response.getToken());
response = getAuthzClient(configFile).authorization("marta", "password").authorize(request);
rpt = toAccessToken(response.getToken());
permissions = new ArrayList<>(rpt.getAuthorization().getPermissions());
assertEquals(10, permissions.size());
for (int i = 0; i < 10; i++) {
if (i < 5) {
assertEquals("Resource " + (i + 11), permissions.get(i).getResourceName());
} else {
assertEquals("Resource " + (i - 4), permissions.get(i).getResourceName());
}
}
request = new AuthorizationRequest();
for (int i = 16; i <= 18; i++) {
request.addPermission("Resource " + i);
}
request.setMetadata(metadata);
request.setRpt(response.getToken());
response = getAuthzClient(configFile).authorization("marta", "password").authorize(request);
rpt = toAccessToken(response.getToken());
permissions = new ArrayList<>(rpt.getAuthorization().getPermissions());
assertEquals(10, permissions.size());
assertEquals("Resource 16", permissions.get(0).getResourceName());
assertEquals("Resource 17", permissions.get(1).getResourceName());
assertEquals("Resource 18", permissions.get(2).getResourceName());
assertEquals("Resource 11", permissions.get(3).getResourceName());
assertEquals("Resource 12", permissions.get(4).getResourceName());
assertEquals("Resource 13", permissions.get(5).getResourceName());
assertEquals("Resource 14", permissions.get(6).getResourceName());
assertEquals("Resource 15", permissions.get(7).getResourceName());
assertEquals("Resource 1", permissions.get(8).getResourceName());
assertEquals("Resource 2", permissions.get(9).getResourceName());
request = new AuthorizationRequest();
metadata.setLimit(5);
request.setMetadata(metadata);
request.setRpt(response.getToken());
response = getAuthzClient(configFile).authorization("marta", "password").authorize(request);
rpt = toAccessToken(response.getToken());
permissions = new ArrayList<>(rpt.getAuthorization().getPermissions());
assertEquals(5, permissions.size());
assertEquals("Resource 16", permissions.get(0).getResourceName());
assertEquals("Resource 17", permissions.get(1).getResourceName());
assertEquals("Resource 18", permissions.get(2).getResourceName());
assertEquals("Resource 11", permissions.get(3).getResourceName());
assertEquals("Resource 12", permissions.get(4).getResourceName());
}
use of org.keycloak.representations.idm.authorization.AuthorizationRequest.Metadata in project keycloak by keycloak.
the class EntitlementAPITest method testRptRequestWithResourceName.
private void testRptRequestWithResourceName(String configFile) {
Metadata metadata = new Metadata();
metadata.setIncludeResourceName(true);
assertResponse(metadata, () -> getAuthzClient(configFile).authorization("marta", "password").authorize());
AuthorizationRequest request = new AuthorizationRequest();
request.setMetadata(metadata);
request.addPermission("Resource 13");
assertResponse(metadata, () -> getAuthzClient(configFile).authorization("marta", "password").authorize(request));
request.setMetadata(null);
assertResponse(metadata, () -> getAuthzClient(configFile).authorization("marta", "password").authorize(request));
}
use of org.keycloak.representations.idm.authorization.AuthorizationRequest.Metadata in project keycloak by keycloak.
the class EntitlementAPITest method testRptRequestWithoutResourceName.
public void testRptRequestWithoutResourceName(String configFile) {
Metadata metadata = new Metadata();
metadata.setIncludeResourceName(false);
assertResponse(metadata, () -> {
AuthorizationRequest request = new AuthorizationRequest();
request.setMetadata(metadata);
request.addPermission("Resource 1");
return getAuthzClient(configFile).authorization("marta", "password").authorize(request);
});
}
use of org.keycloak.representations.idm.authorization.AuthorizationRequest.Metadata in project keycloak by keycloak.
the class AuthorizationTokenService method createPermissions.
private Collection<ResourcePermission> createPermissions(PermissionTicketToken ticket, KeycloakAuthorizationRequest request, ResourceServer resourceServer, AuthorizationProvider authorization, EvaluationContext context) {
KeycloakIdentity identity = (KeycloakIdentity) context.getIdentity();
StoreFactory storeFactory = authorization.getStoreFactory();
Map<String, ResourcePermission> permissionsToEvaluate = new LinkedHashMap<>();
ResourceStore resourceStore = storeFactory.getResourceStore();
ScopeStore scopeStore = storeFactory.getScopeStore();
Metadata metadata = request.getMetadata();
final AtomicInteger limit = metadata != null && metadata.getLimit() != null ? new AtomicInteger(metadata.getLimit()) : null;
for (Permission permission : ticket.getPermissions()) {
if (limit != null && limit.get() <= 0) {
break;
}
Set<Scope> requestedScopesModel = resolveRequestedScopes(request, resourceServer, scopeStore, permission);
String resourceId = permission.getResourceId();
if (resourceId != null) {
resolveResourcePermission(request, resourceServer, identity, authorization, storeFactory, permissionsToEvaluate, resourceStore, limit, permission, requestedScopesModel, resourceId);
} else {
resolveScopePermissions(request, resourceServer, authorization, permissionsToEvaluate, resourceStore, limit, requestedScopesModel);
}
}
resolvePreviousGrantedPermissions(ticket, request, resourceServer, permissionsToEvaluate, resourceStore, scopeStore, limit);
return permissionsToEvaluate.values();
}
use of org.keycloak.representations.idm.authorization.AuthorizationRequest.Metadata in project keycloak by keycloak.
the class EntitlementAPITest method testInvalidRequestWithClaimsFromConfidentialClient.
@Test
public void testInvalidRequestWithClaimsFromConfidentialClient() throws IOException {
AuthorizationRequest request = new AuthorizationRequest();
request.addPermission("Resource 13");
HashMap<Object, Object> obj = new HashMap<>();
obj.put("claim-a", "claim-a");
request.setClaimToken(Base64Url.encode(JsonSerialization.writeValueAsBytes(obj)));
assertResponse(new Metadata(), () -> getAuthzClient(AUTHZ_CLIENT_CONFIG).authorization("marta", "password").authorize(request));
}
Aggregations