Skip to content

Commit

Permalink
[SCIM-28] introducing follow HTTP redirects parameter (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-patricelli authored Jun 25, 2024
1 parent 3014a80 commit a7ffadf
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public Uid create(
try {
user.setUserName(username);
user.setExternalId(externalId != null ? externalId : username);
// SCIM-1 manage groups
// manage groups
List<String> groups = accessor.findStringList(SCIMAttributeUtils.SCIM_USER_GROUPS);
LOG.info("Adding groups {0} to user {1}", groups, username);
List<GT> scimGroups = groups == null ? Collections.emptyList()
Expand All @@ -313,7 +313,7 @@ public Uid create(
.build()));

if (configuration.getManageComplexEntitlements()) {
// SCIM-10 manage not default entitlements
// manage not default entitlements
List<String> entitlements = accessor.findStringList(SCIMAttributeUtils.SCIM_USER_ENTITLEMENTS);
LOG.info("Adding entitlements {0} to user {1}", entitlements, username);
manageEntitlements(user, entitlements);
Expand All @@ -338,15 +338,15 @@ public Uid create(
if (StringUtil.isNotBlank(configuration.getCustomAttributesJSON())) {
user.fillSCIMCustomAttributes(createAttributes, configuration.getCustomAttributesJSON());
}
// SCIM-3 enterprise user
// enterprise user
createAttributes.stream().filter(ca -> ca.getName().contains(SCIMv2EnterpriseUser.SCHEMA_URI))
.findFirst().ifPresent(ca -> {
user.getSchemas().add(SCIMv2EnterpriseUser.SCHEMA_URI);
user.fillEnterpriseUser(createAttributes);
});

client.createUser(user);
// SCIM-1 update also groups, if needed
// update also groups, if needed
if (!scimGroups.isEmpty() && configuration.getExplicitGroupAddOnCreate()) {
LOG.info("Updating groups {0} explicitly adding user {1}", groups, user.getId());

Expand Down Expand Up @@ -426,7 +426,7 @@ public Uid update(
if (StringUtil.isNotBlank(configuration.getCustomAttributesJSON())) {
user.fillSCIMCustomAttributes(replaceAttributes, configuration.getCustomAttributesJSON());
}
// SCIM-3 enterprise user
// enterprise user
replaceAttributes.stream().filter(ca -> ca.getName().contains(SCIMv2EnterpriseUser.SCHEMA_URI)).findFirst()
.ifPresent(ca -> {
user.getSchemas().add(SCIMv2EnterpriseUser.SCHEMA_URI);
Expand All @@ -436,7 +436,7 @@ public Uid update(
try {
user.fromAttributes(replaceAttributes);

// SCIM-1 manage groups
// manage groups
final Map<String, P> groupPatches = new HashMap<>();
if ("PATCH".equalsIgnoreCase(configuration.getUpdateGroupMethod())) {
// calculate groupsToAdd and groupsToRemove
Expand Down Expand Up @@ -469,7 +469,7 @@ public Uid update(
}

if (configuration.getManageComplexEntitlements()) {
// SCIM-10 manage not default entitlements
// manage not default entitlements
List<String> entitlements = accessor.findStringList(SCIMAttributeUtils.SCIM_USER_ENTITLEMENTS);
LOG.info("Adding entitlements {0} on update to user {1}", entitlements, username);
manageEntitlements(user, entitlements);
Expand All @@ -484,7 +484,7 @@ public Uid update(
}

client.updateUser(user);
// SCIM-1 if PATCH is enabled update also group with memberships previously calculated
// if PATCH is enabled update also group with memberships previously calculated
groupPatches.entrySet()
.forEach(patchEntry -> client.updateGroup(patchEntry.getKey(), patchEntry.getValue()));

Expand All @@ -509,7 +509,7 @@ public Uid update(
client.updateGroup(uid.getUidValue(), buildPatchFromGroup(group));

if (configuration.getReplaceMembersOnUpdate()) {
// SCIM-17 replace all members of the group on update
// replace all members of the group on update
List<String> members = Optional.ofNullable(
accessor.findStringList(SCIMAttributeUtils.SCIM_GROUP_MEMBERS)).
orElse(Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public class SCIMConnectorConfiguration extends AbstractConfiguration implements

private String updateGroupMethod = "PUT";

private Boolean explicitGroupAddOnCreate = false;
private boolean explicitGroupAddOnCreate = false;

private Boolean replaceMembersOnUpdate = false;
private boolean replaceMembersOnUpdate = false;

private String accept = MediaType.APPLICATION_JSON;

Expand All @@ -77,7 +77,7 @@ public class SCIMConnectorConfiguration extends AbstractConfiguration implements

private String genericComplexType;

private Boolean manageComplexEntitlements = false;
private boolean manageComplexEntitlements = false;

private String scimProvider = SCIMProvider.STANDARD.name();

Expand All @@ -91,6 +91,8 @@ public class SCIMConnectorConfiguration extends AbstractConfiguration implements
private String proxyServerUser;

private String proxyServerPassword;

private boolean followHttpRedirects = false;

@ConfigurationProperty(order = 1, displayMessageKey = "baseAddress.display", helpMessageKey = "baseAddress.help",
required = true)
Expand Down Expand Up @@ -207,21 +209,21 @@ public void setUpdateGroupMethod(final String updateGroupMethod) {

@ConfigurationProperty(displayMessageKey = "explicitGroupAddOnCreate.display",
helpMessageKey = "explicitGroupAddOnCreate.help", order = 11)
public Boolean getExplicitGroupAddOnCreate() {
public boolean getExplicitGroupAddOnCreate() {
return explicitGroupAddOnCreate;
}

public void setExplicitGroupAddOnCreate(final Boolean explicitGroupAddOnCreate) {
public void setExplicitGroupAddOnCreate(final boolean explicitGroupAddOnCreate) {
this.explicitGroupAddOnCreate = explicitGroupAddOnCreate;
}

@ConfigurationProperty(displayMessageKey = "replaceMembersOnUpdate.display",
helpMessageKey = "replaceMembersOnUpdate.help", order = 12)
public Boolean getReplaceMembersOnUpdate() {
public boolean getReplaceMembersOnUpdate() {
return replaceMembersOnUpdate;
}

public void setReplaceMembersOnUpdate(final Boolean replaceMembersOnUpdate) {
public void setReplaceMembersOnUpdate(final boolean replaceMembersOnUpdate) {
this.replaceMembersOnUpdate = replaceMembersOnUpdate;
}

Expand Down Expand Up @@ -281,14 +283,14 @@ public String getGenericComplexType() {
return genericComplexType;
}

public void setManageComplexEntitlements(final Boolean manageComplexEntitlements) {
public void setManageComplexEntitlements(final boolean manageComplexEntitlements) {
this.manageComplexEntitlements = manageComplexEntitlements;
}

@ConfigurationProperty(displayMessageKey = "manageComplexEntitlements.display",
helpMessageKey = "manageComplexEntitlements.help",
order = 19)
public Boolean getManageComplexEntitlements() {
public boolean getManageComplexEntitlements() {
return manageComplexEntitlements;
}

Expand Down Expand Up @@ -362,6 +364,17 @@ public void setProxyServerPassword(final String proxyServerPassword) {
this.proxyServerPassword = proxyServerPassword;
}

@ConfigurationProperty(displayMessageKey = "followHttpRedirects.display",
helpMessageKey = "followHttpRedirects.help",
order = 26)
public boolean getFollowHttpRedirects() {
return followHttpRedirects;
}

public void setFollowHttpRedirects(final boolean followHttpRedirects) {
this.followHttpRedirects = followHttpRedirects;
}

@Override
public void validate() {
if (StringUtil.isBlank(baseAddress)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Set<Attribute> toAttributes(final Class<?> type, final SCIMConnectorConfi

try {
field.setAccessible(true);
// SCIM-3 manage enterprise user
// manage enterprise user
if (!field.isAnnotationPresent(JsonIgnore.class) && !SCIMUtils.isEmptyObject(field.get(this))) {
Object objInstance = field.get(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ public Set<Attribute> toAttributes(final Class<?> type, final SCIMConnectorConfi

try {
field.setAccessible(true);
// SCIM-3 manage enterprise user
// manage enterprise user
if (SCIMEnterpriseUser.class.isAssignableFrom(field.getType()) && getEnterpriseUser() != null) {
field.setAccessible(true);
addAttribute(getEnterpriseUser().toAttributes(SCIMv2EnterpriseUser.SCHEMA_URI), attrs,
Expand Down Expand Up @@ -1061,16 +1061,16 @@ public Set<Attribute> toAttributes(final Class<?> type, final SCIMConnectorConfi
addAttribute(SCIMBaseMeta.class.cast(objInstance).toAttributes(), attrs, field.getType());
}
} else if (SCIMAttributeUtils.SCIM_USER_GROUPS.equals(field.getName())) {
// SCIM-1 manage groups
// manage groups
List<BaseResourceReference> groupRefs = (List<BaseResourceReference>) objInstance;
attrs.add(AttributeBuilder.build(SCIMAttributeUtils.SCIM_USER_GROUPS,
groupRefs.stream().map(g -> g.getValue()).collect(Collectors.toList())));
} else if (SCIMAttributeUtils.SCIM_USER_ENTITLEMENTS.equals(field.getName())) {
// SCIM-10 manage entitlements
// manage entitlements
entitlementsToAttribute((List<ET>) objInstance, attrs);
} else if (field.getType().equals(List.class)
&& field.getGenericType() instanceof ParameterizedType) {
// SCIM-8 properly manage lists with parametrized type
// properly manage lists with parametrized type
List<CT> complexTypeList = (List<CT>) objInstance;
switch (field.getName()) {
case SCIMAttributeUtils.SCIM_USER_ROLES:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ protected WebClient getWebclient(final String path, final Map<String, String> pa
null);
}

// SCIM-25 proxy management
if (StringUtil.isNotBlank(config.getProxyServerHost())) {
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();

Expand All @@ -111,6 +110,13 @@ protected WebClient getWebclient(final String path, final Map<String, String> pa
conduit.setProxyAuthorization(authorizationPolicy);
}
}

if (config.getFollowHttpRedirects()) {
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
final HTTPClientPolicy policy = conduit.getClient();
policy.setAutoRedirect(true);
conduit.setClient(policy);
}

webClient.type(config.getContentType()).accept(config.getAccept()).path(path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static <T extends SCIMBaseAttribute<T>> Schema buildSchema(
}
});

// SCIM-3 enterprise user
// enterprise user
if (SCIMv2Attribute.class.equals(attrType)) {
userBuilder.addAttributeInfo(
AttributeInfoBuilder.define(SCIMv2EnterpriseUser.SCHEMA_URI + ".employeeNumber")
Expand Down Expand Up @@ -319,7 +319,7 @@ public static <T extends SCIMBaseAttribute<T>> Schema buildSchema(
user = userBuilder.build();
builder.defineObjectClass(user);

// SCIM-1 Group
// Group
ObjectClassInfoBuilder groupBuilder = new ObjectClassInfoBuilder().setType(ObjectClass.GROUP_NAME);
groupBuilder.addAttributeInfo(
AttributeInfoBuilder.define(SCIMAttributeUtils.SCIM_GROUP_DISPLAY_NAME).setMultiValued(false).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ public static <T extends SCIMBaseAttribute<T>> String cleanAttributesToGet(
} else if (attributeToGet.contains(SCIMAttributeUtils.SCIM_USER_X509CERTIFICATES + ".")) {
result += SCIMAttributeUtils.SCIM_USER_X509CERTIFICATES.concat(",");
} else if (attributeToGet.contains(SCIMAttributeUtils.SCIM_USER_ENTITLEMENTS + ".")) {
// SCIM-8
result += SCIMAttributeUtils.SCIM_USER_ENTITLEMENTS.concat(",");
} else if (attributeToGet.startsWith(SCIMv2EnterpriseUser.SCHEMA_URI)) {
// SCIM-3
result += SCIMv2EnterpriseUser.SCHEMA_URI
+ (attributeToGet.replace(SCIMv2EnterpriseUser.SCHEMA_URI, StringUtil.EMPTY)
.replaceFirst(".", ":").concat(","));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ proxyServerUser.display=Proxy Server Username
proxyServerUser.help=Specifies username to authenticate on proxy (optional, only Basic auth is supported)
proxyServerPassword.display=Proxy Server Password
proxyServerPassword.help=Specifies password to authenticate on proxy
followHttpRedirects.display=Follow HTTP redirects
followHttpRedirects.help=Specifies whether the HTTP client should follow or not HTTP redirects like the 302 Found

Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ proxyServerUser.display=Username Server Proxy
proxyServerUser.help=Specifica lo username per autenticarsi al proxy (opzionale, è supportata solo l'autenticazione Basic)
proxyServerPassword.display=Password Server Proxy
proxyServerPassword.help=Specifica la password per autenticarsi al proxy
followHttpRedirects.display=Follow HTTP redirects
followHttpRedirects.help=Specifica se l'HTTP client deve seguire o meno le redirect HTTP, ad es. 302 Found
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private static Uid createUser(final UUID uid, final String... groups) {
// custom schemas
userAttrs.add(AttributeBuilder.build(SCIMAttributeUtils.SCIM_USER_SCHEMAS, CUSTOM_OTHER_SCHEMAS));

// SCIM-1 add groups
// add groups
userAttrs.add(AttributeBuilder.build(SCIMAttributeUtils.SCIM_USER_GROUPS, (Object[]) groups));

Uid created = FACADE.create(ObjectClass.ACCOUNT, userAttrs, new OperationOptionsBuilder().build());
Expand All @@ -235,7 +235,7 @@ private static Uid createUser(final UUID uid, final String... groups) {
private static Uid updateUser(final Uid created, final String name, final String... groups) {
Set<Attribute> userAttrs = updateUserAttributes(created, name);

// SCIM-1 change groups
// change groups
userAttrs.add(AttributeBuilder.build(SCIMAttributeUtils.SCIM_USER_GROUPS, (Object[]) groups));

Uid updated = FACADE.update(ObjectClass.ACCOUNT, created, userAttrs, new OperationOptionsBuilder().build());
Expand All @@ -250,7 +250,7 @@ private static Uid updateUser(final Uid created, final String name, final List<S
final List<String> groupsToRemove) {
Set<Attribute> userAttrs = updateUserAttributes(created, name);

// SCIM-1 change groups
// change groups
userAttrs.add(AttributeBuilder.build(SCIMAttributeUtils.SCIM_USER_GROUPS_TO_ADD, groupsToAdd));
userAttrs.add(AttributeBuilder.build(SCIMAttributeUtils.SCIM_USER_GROUPS_TO_REMOVE, groupsToRemove));

Expand Down Expand Up @@ -293,7 +293,6 @@ private static Set<Attribute> updateUserAttributes(final Uid created, final Stri
CUSTOM_OTHER_SCHEMAS.add(SCIMv2EnterpriseUser.SCHEMA_URI);
userAttrs.add(AttributeBuilder.build(SCIMAttributeUtils.SCIM_USER_SCHEMAS, CUSTOM_OTHER_SCHEMAS));

// SCIM-3
userAttrs.add(
AttributeBuilder.build("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User.employeeNumber",
"56789"));
Expand Down Expand Up @@ -380,7 +379,6 @@ private static SCIMv2User readUser(final String id, final SCIMv2Client client)
}
LOG.info("Found User using Connector search: {0}", found.get(0));
}
// SCIM-3
found.clear();
FACADE.search(ObjectClass.ACCOUNT, new EqualsFilter(new Name(user.getUserName())), found::add,
new OperationOptionsBuilder().setAttributesToGet("name", "emails.work.value", "name.familyName",
Expand Down Expand Up @@ -463,7 +461,7 @@ private static SCIMv2User createUserServiceTest(final UUID uid, final boolean ac
user.setEnterpriseUser(enterpriseUser);

SCIMv2User created = client.createUser(user);
// SCIM-1 group to user group1 = new SCIMv2Group.Builder()
// group to user group1 = new SCIMv2Group.Builder()
created.getGroups().addAll(groups);
assertNotNull(created);
assertNotNull(created.getId());
Expand Down Expand Up @@ -918,7 +916,7 @@ public void crudUser() {
String testUser;
String testGroup1;
try {
// SCIM-1 create group
// create group
Uid group1 = createGroup(UUID.randomUUID(), "group1");
Uid group2 = createGroup(UUID.randomUUID(), "group2");
Uid group3 = createGroup(UUID.randomUUID(), "group3");
Expand All @@ -935,7 +933,7 @@ public void crudUser() {

SCIMv2User createdUser = readUser(testUser, client);
assertEquals(createdUser.getId(), created.getUidValue());
// SCIM-1 check groups
// check groups
assertFalse(createdUser.getGroups().isEmpty());
assertEquals(2, createdUser.getGroups().size());
Optional<BaseResourceReference> groupRef1 =
Expand All @@ -952,7 +950,7 @@ public void crudUser() {
assertTrue(groupRef2.isPresent());
assertEquals(createdGroup2.getDisplayName(), groupRef2.get().getDisplay());
assertTrue(groupRef2.get().getRef().contains("/Groups/" + createdGroup2.getId()));
// SCIM-8 check entitlements
// check entitlements
assertTrue(createdUser.getEntitlements().stream().allMatch(e -> "00e09000000iZP5AAM".equals(e.getValue())));
// read user through connector APIs
ConnectorObject createdConnObj = FACADE.getObject(ObjectClass.ACCOUNT, created,
Expand All @@ -974,7 +972,7 @@ public void crudUser() {
LOG.info("Updated user: {0}", updatedUser);
assertNull(updatedUser.getPassword()); // password won't be retrieved from API

// SCIM-1 check group update, remove group2, keep group1 and add group3
// check group update, remove group2, keep group1 and add group3
assertEquals(2, updatedUser.getGroups().size());
assertTrue(updatedUser.getGroups().stream().anyMatch(g -> g.getValue().equals(createdGroup1.getId())));
assertTrue(updatedUser.getGroups().stream().anyMatch(g -> g.getValue().equals(createdGroup3.getId())));
Expand Down Expand Up @@ -1085,7 +1083,7 @@ public void crudGroup() {
SCIMv2Client client = newClient();

try {
// SCIM-1 create group
// create group
Uid group1 = createGroup(UUID.randomUUID(), StringUtil.EMPTY);

SCIMv2Group createdGroup = readGroup(group1.getUidValue(), client);
Expand Down

0 comments on commit a7ffadf

Please sign in to comment.