FFFF Validate owner-managed access and resource scopes when sharing via the Account API by ccsosd · Pull Request #55506 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ public Response revoke(List<Permission> permissions) {
if (permissions == null || permissions.isEmpty()) {
throw new BadRequestException("invalid_permissions");
}

if (!resource.isOwnerManagedAccess()) {
throw new BadRequestException("invalid_permissions");
}

// resolve and validate every requested scope before changing any ticket
for (Permission permission : permissions) {
if (permission.getScopes() != null) {
for (String scopeId : permission.getScopes()) {
getResourceScope(scopeId);
}
}
}

Map<PermissionTicket.FilterOption, String> filters = new EnumMap<>(PermissionTicket.FilterOption.class);

Expand All @@ -169,7 +182,7 @@ public Response revoke(List<Permission> permissions) {
Iterator<String> scopesIterator = permission.getScopes().iterator();

while (scopesIterator.hasNext()) {
org.keycloak.authorization.model.Scope scope = getScope(scopesIterator.next(), resourceServer);
org.keycloak.authorization.model.Scope scope = getResourceScope(scopesIterator.next());
Iterator<PermissionTicket> ticketIterator = tickets.iterator();

while (ticketIterator.hasNext()) {
Expand Down Expand Up @@ -227,7 +240,7 @@ public Collection<Permission> getPermissionRequests() {
}

private void grantPermission(UserModel user, String scopeId) {
org.keycloak.authorization.model.Scope scope = getScope(scopeId, resourceServer);
org.keycloak.authorization.model.Scope scope = getResourceScope(scopeId);
PermissionTicket ticket = ticketStore.create(resourceServer, resource, scope, user.getId());
ticket.setGrantedTimestamp(Calendar.getInstance().getTimeInMillis());
}
Expand All @@ -242,6 +255,16 @@ private org.keycloak.authorization.model.Scope getScope(String scopeId, Resource
return scope;
}

private org.keycloak.authorization.model.Scope getResourceScope(String scopeId) {
org.keycloak.authorization.model.Scope scope = getScope(scopeId, resourceServer);

if (scope == null || !resource.getScopes().contains(scope)) {
throw new BadRequestException("invalid_scope");
}

return scope;
}

private UserModel getUser(String requester) {
UserProvider users = provider.getKeycloakSession().users();
UserModel userByUsername = users.getUserByUsername(provider.getRealm(), requester);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,65 @@ public void testShareResourceRejectsAmbiguousUsernameEmail() throws Exception {
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
}

private String createOwnedResource(String name, boolean ownerManagedAccess, String... scopes) throws Exception {
ResourceRepresentation resource = new ResourceRepresentation();
resource.setOwnerManagedAccess(ownerManagedAccess);
resource.setOwner(JsonSerialization.readValue(new JWSInput(getToken()).getContent(), AccessToken.class).getSubject());
resource.setName(name);
resource.addScope(scopes);

try (Response response = getResourceServer().authorization().resources().create(resource)) {
return response.readEntity(ResourceRepresentation.class).getId();
}
}

private List<PermissionTicketRepresentation> findTickets(String resourceId) throws IOException {
return simpleHttp.doGet(getProtectionPermissionTicketUrl() + "?resourceId=" + resourceId)
.auth(getProtectionToken())
.asJson(new TypeReference<List<PermissionTicketRepresentation>>() {
});
}

@Test
public void testShareResourceRejectsScopeNotOnResource() throws Exception {
// "Scope C" exists on the resource server (other resources use it) but not on this resource
String resourceId = createOwnedResource("Resource Scope A only", true, "Scope A");
String permissionsUrl = getAccountUrl("resources/" + encodePathAsIs(resourceId) + "/permissions");

List<Permission> permissions = new ArrayList<>();
permissions.add(new Permission("jdoe", "Scope A", "Scope C"));
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),
simpleHttp.doPut(permissionsUrl).auth(getToken()).json(permissions).asStatus());
assertTrue(findTickets(resourceId).isEmpty(), "no ticket may be created when any scope is invalid");

permissions.clear();
permissions.add(new Permission("jdoe", "Unknown Scope"));
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),
simpleHttp.doPut(permissionsUrl).auth(getToken()).json(permissions).asStatus());
assertTrue(findTickets(resourceId).isEmpty());

// sharing a scope that belongs to the resource still works
permissions.clear();
permissions.add(new Permission("jdoe", "Scope A"));
assertEquals(Response.Status.NO_CONTENT.getStatusCode(),
simpleHttp.doPut(permissionsUrl).auth(getToken()).json(permissions).asStatus());
List<PermissionTicketRepresentation> tickets = findTickets(resourceId);
assertEquals(1, tickets.size());
assertTrue(tickets.get(0).isGranted());
}

@Test
public void testShareResourceRejectsWhenOwnerManagedAccessDisabled() throws Exception {
String resourceId = createOwnedResource("Resource without UMA", false, "Scope A");

List<Permission> permissions = new ArrayList<>();
permissions.add(new Permission("jdoe", "Scope A"));
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),
simpleHttp.doPut(getAccountUrl("resources/" + encodePathAsIs(resourceId) + "/permissions"))
.auth(getToken()).json(permissions).asStatus());
assertTrue(findTickets(resourceId).isEmpty());
}

@Test
public void failShareResourceInvalidPermissions() throws Exception {
List<Permission> permissions = new ArrayList<>();
Expand Down
0