Skip to content

LDAP: allow multiple roles to be fetched from group role attribute #17021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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 @@ -17,6 +17,7 @@
package org.springframework.security.ldap.userdetails;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -188,7 +189,7 @@ public void customAuthoritiesMappingFunction() {
this.populator.setAuthorityMapper((record) -> {
String dn = record.get(SpringSecurityLdapTemplate.DN_KEY).get(0);
String role = record.get(this.populator.getGroupRoleAttribute()).get(0);
return new LdapAuthority(role, dn);
return Collections.singletonList(new LdapAuthority(role, dn));
});

DirContextAdapter ctx = new DirContextAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,8 @@

package org.springframework.security.ldap.userdetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import javax.naming.directory.SearchControls;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.core.log.LogMessage;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextOperations;
Expand All @@ -38,7 +26,10 @@
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.ldap.SpringSecurityLdapTemplate;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.naming.directory.SearchControls;
import java.util.*;
import java.util.function.Function;

/**
* The default strategy for obtaining user role information from the directory.
Expand Down Expand Up @@ -150,7 +141,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
/**
* The mapping function to be used to populate authorities.
*/
private Function<Map<String, List<String>>, GrantedAuthority> authorityMapper;
private Function<Map<String, List<String>>, List<? extends GrantedAuthority>> authorityMapper;

/**
* Constructor for group search scenarios. <tt>userRoleAttributes</tt> may still be
Expand All @@ -171,18 +162,15 @@ else if (groupSearchBase.isEmpty()) {
logger.info("Will perform group search from the context source base since groupSearchBase is empty.");
}
this.authorityMapper = (record) -> {
List<String> roles = record.get(this.groupRoleAttribute);
if (CollectionUtils.isEmpty(roles)) {
return null;
}
String role = roles.get(0);
if (role == null) {
return null;
}
if (this.convertToUpperCase) {
role = role.toUpperCase(Locale.ROOT);
}
return new SimpleGrantedAuthority(this.rolePrefix + role);
List<String> roles = record.getOrDefault(this.groupRoleAttribute, Collections.emptyList());
return roles.stream().filter(Objects::nonNull).map(role -> {
if (this.convertToUpperCase) {
return role.toUpperCase(Locale.ROOT);
}
else {
return role;
}
}).map(role -> new SimpleGrantedAuthority(this.rolePrefix + role)).toList();
};
}

Expand Down Expand Up @@ -234,10 +222,7 @@ public Set<GrantedAuthority> getGroupMembershipRoles(String userDn, String usern
new String[] { this.groupRoleAttribute });
logger.debug(LogMessage.of(() -> "Found roles from search " + userRoles));
for (Map<String, List<String>> role : userRoles) {
GrantedAuthority authority = this.authorityMapper.apply(role);
if (authority != null) {
authorities.add(authority);
}
authorities.addAll(this.authorityMapper.apply(role));
}
return authorities;
}
Expand Down Expand Up @@ -311,7 +296,8 @@ public void setIgnorePartialResultException(boolean ignore) {
* {@link GrantedAuthority} given the context record.
* @param authorityMapper the mapping function
*/
public void setAuthorityMapper(Function<Map<String, List<String>>, GrantedAuthority> authorityMapper) {
public void setAuthorityMapper(
Function<Map<String, List<String>>, List<? extends GrantedAuthority>> authorityMapper) {
Assert.notNull(authorityMapper, "authorityMapper must not be null");
this.authorityMapper = authorityMapper;
}
Expand Down