FFFF Expose sub-attributes of multi-valued complex attributes in SCIM /Schemas by Beehtrix · Pull Request #55798 · 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
7 changes: 7 additions & 0 deletions scim/model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
10BC8
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
import org.keycloak.scim.model.resourcetype.ResourceTypeProviderFactory;
import org.keycloak.scim.protocol.ForbiddenException;
import org.keycloak.scim.resource.Scim;
import org.keycloak.scim.resource.common.MultiValuedAttribute;
import org.keycloak.scim.resource.group.Member;
import org.keycloak.scim.resource.schema.ModelSchema;
import org.keycloak.scim.resource.schema.Schema;
import org.keycloak.scim.resource.schema.Schema.Attribute;
import org.keycloak.scim.resource.spi.ScimResourceTypeProvider;
import org.keycloak.scim.resource.spi.SearchOptions;
import org.keycloak.scim.resource.user.GroupMembership;
import org.keycloak.utils.StringUtil;

import static org.keycloak.scim.resource.Scim.hasDiscoveryEndpointPermission;
Expand Down Expand Up @@ -180,7 +183,7 @@ private void addToExtensionSchema(String schemaName, Attribute attr) {
}).getAttributes().add(attr);
}

private Attribute createTopLevelAttribute(org.keycloak.scim.resource.schema.attribute.Attribute<?, ?> attribute, String name) {
static Attribute createTopLevelAttribute(org.keycloak.scim.resource.schema.attribute.Attribute<?, ?> attribute, String name) {
Attribute attr = new Attribute();

attr.setName(name);
Expand All @@ -192,9 +195,60 @@ private Attribute createTopLevelAttribute(org.keycloak.scim.resource.schema.attr
attr.setCaseExact(attribute.isCaseExact());
attr.setUniqueness(attribute.getUniqueness());

if ("complex".equals(attribute.getType()) && attribute.getComplexType() != null
&& MultiValuedAttribute.class.isAssignableFrom(attribute.getComplexType())) {
attr.setSubAttributes(createMultiValuedSubAttributes(attribute.getComplexType()));
}

return attr;
}

/**
* Sub-attributes of a multi-valued complex attribute backed by {@link MultiValuedAttribute}
* (e.g. {@code User.emails}, {@code User.groups}, {@code Group.members}), as defined by RFC 7643, Section 2.4.
*
* <p>Only {@code value} is consumed on writes. {@code display}, {@code type}, {@code primary} and {@code $ref}
* are derived by the server when a resource is read and any submitted value is ignored, so they are
* advertised as {@code readOnly}. For group memberships ({@code User.groups}, {@code Group.members}) the
* {@code value} is required and {@code immutable}: entries are added or removed as a whole, never edited.
*/
private static List<Attribute> createMultiValuedSubAttributes(Class<?> complexType) {
boolean membership = GroupMembership.class.isAssignableFrom(complexType) || Member.class.isAssignableFrom(complexType);
List<Attribute> subAttributes = new ArrayList<>();

subAttributes.add(createSubAttribute("value", "string", membership ? "immutable" : "readWrite", membership));
subAttributes.add(createSubAttribute("display", "string", "readOnly", false));
subAttributes.add(createSubAttribute("type", "string", "readOnly", false));
subAttributes.add(createSubAttribute("primary", "boolean", "readOnly", false));

Attribute ref = createSubAttribute("$ref", "reference", "readOnly", false);

if (GroupMembership.class.isAssignableFrom(complexType)) {
ref.setReferenceTypes(List.of("Group"));
} else if (Member.class.isAssignableFrom(complexType)) {
// only users can be members of a group
ref.setReferenceTypes(List.of("User"));
}

subAttributes.add(ref);

return subAttributes;
}

private static Attribute createSubAttribute(String name, String type, String mutability, boolean required) {
Attribute subAttr = new Attribute();

subAttr.setName(name);
subAttr.setType(type);
subAttr.setMultiValued(false);
subAttr.setRequired(required);
subAttr.setCaseExact(false);
subAttr.setMutability(mutability);
subAttr.setReturned("default");
subAttr.setUniqueness("none");

return subAttr;
}

@Override
public Schema get(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.keycloak.scim.model.schema;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.keycloak.models.UserModel;
import org.keycloak.scim.resource.group.Member;
import org.keycloak.scim.resource.schema.Schema;
import org.keycloak.scim.resource.schema.attribute.Attribute;
import org.keycloak.scim.resource.user.Email;
import org.keycloak.scim.resource.user.GroupMembership;
import org.keycloak.scim.resource.user.User;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SchemaResourceTypeProviderTest {

@Test
public void testMultiValuedComplexAttributesExposeSubAttributes() {
for (Class<?> type : List.of(Email.class, GroupMembership.class, Member.class)) {
Attribute<UserModel, User> attribute = Attribute.<UserModel, User>complex("attr", type)
.multivalued().build().get(0);

Schema.Attribute rep = SchemaResourceTypeProvider.createTopLevelAttribute(attribute, "attr");

assertEquals("complex", rep.getType());
assertNotNull(rep.getSubAttributes(), type.getSimpleName());
Map<String, Schema.Attribute> subs = rep.getSubAttributes().stream()
.collect(Collectors.toMap(Schema.Attribute::getName, Function.identity()));
assertEquals(5, subs.size());
assertEquals("string", subs.get("value").getType());
assertEquals("string", subs.get("display").getType());
assertEquals("string", subs.get("type").getType());
assertEquals("boolean", subs.get("primary").getType());
assertEquals("reference", subs.get("$ref").getType());
}
}

@Test
public void testSimpleAttributeHasNoSubAttributes() {
Attribute<UserModel, User> attribute = Attribute.<UserModel, User>simple("userName").build().get(0);

assertNull(SchemaResourceTypeProvider.createTopLevelAttribute(attribute, "userName").getSubAttributes());
}

@Test
public void testSubAttributeMutabilityRequirednessAndReferenceTypes() {
Map<String, Schema.Attribute> emails = subAttributes(Email.class);
Map<String, Schema.Attribute> groups = subAttributes(GroupMembership.class);
Map<String, Schema.Attribute> members = subAttributes(Member.class);

assertEquals("readWrite", emails.get("value").getMutability());
assertFalse(emails.get("value").getRequired());
assertNull(emails.get("$ref").getReferenceTypes());

for (Map<String, Schema.Attribute> subs : List.of(emails, groups, members)) {
for (String name : List.of("display", "type", "primary", "$ref")) {
assertEquals("readOnly", subs.get(name).getMutability(), name);
assertFalse(subs.get(name).getRequired(), name);
}
}

for (Map<String, Schema.Attribute> subs : List.of(groups, members)) {
assertEquals("immutable", subs.get("value").getMutability());
assertTrue(subs.get("value").getRequired());
}

assertEquals(List.of("Group"), groups.get("$ref").getReferenceTypes());
assertEquals(List.of("User"), members.get("$ref").getReferenceTypes());
}

private static Map<String, Schema.Attribute> subAttributes(Class<?> type) {
Attribute<UserModel, User> attribute = Attribute.<UserModel, User>complex("attr", type)
.multivalued().build().get(0);

return SchemaResourceTypeProvider.createTopLevelAttribute(attribute, "attr").getSubAttributes().stream()
.collect(Collectors.toMap(Schema.Attribute::getName, Function.identity()));
}
}
0