forked from honza-toegel/ebics-java-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
map active directory memberOf to spring role
- Loading branch information
Showing
2 changed files
with
49 additions
and
20 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
.../src/main/kotlin/org/ebics/client/ebicsrestapi/ldap/ActiveDirectoryRoleMapperPopulator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.ebics.client.ebicsrestapi.ldap | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.ldap.core.DirContextOperations | ||
import org.springframework.ldap.core.DistinguishedName | ||
import org.springframework.security.core.GrantedAuthority | ||
import org.springframework.security.core.authority.AuthorityUtils | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority | ||
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator | ||
|
||
|
||
/** | ||
* Translates ad memberOf attribute to role based on ldap search property [LdapSearchProperties.mapping] | ||
* inspired by [DefaultActiveDirectoryAuthoritiesPopulator] | ||
*/ | ||
class ActiveDirectoryRoleMapperPopulator(val mapping: Map<String,String>?) : LdapAuthoritiesPopulator { | ||
private val logger = LoggerFactory.getLogger(ActiveDirectoryRoleMapperPopulator::class.java) | ||
override fun getGrantedAuthorities( | ||
userData: DirContextOperations?, | ||
username: String? | ||
): Collection<GrantedAuthority?>? { | ||
val groups = userData?.getStringAttributes("memberOf") | ||
if (groups == null) { | ||
logger.debug("No values for 'memberOf' attribute."); | ||
return AuthorityUtils.NO_AUTHORITIES; | ||
} | ||
if (logger.isDebugEnabled) logger.debug("'memberOf' attribute values: " + groups.asList()); | ||
|
||
return buildList { | ||
for (group in groups) { | ||
val mappedRole = mapping?.get(DistinguishedName(group).removeLast().value) | ||
if (mappedRole != null) add(SimpleGrantedAuthority("ROLE_${mappedRole.uppercase()}")) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters