Search in sources :

Example 36 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class LdapUtils method getFullDn.

/**
 * Gets the full dn of a name by prepending the name of the context it is relative to.
 * If the name already contains the base name, it is returned unaltered.
 */
public static DistinguishedName getFullDn(DistinguishedName dn, Context baseCtx) throws NamingException {
    DistinguishedName baseDn = new DistinguishedName(baseCtx.getNameInNamespace());
    if (dn.contains(baseDn)) {
        return dn;
    }
    baseDn.append(dn);
    return baseDn;
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName)

Example 37 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class LdapUserDetailsMapperTests method testNonRetrievedRoleAttributeIsIgnored.

/**
 * SEC-303. Non-retrieved role attribute causes NullPointerException
 */
@Test
public void testNonRetrievedRoleAttributeIsIgnored() {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    mapper.setRoleAttributes(new String[] { "userRole", "nonRetrievedAttribute" });
    BasicAttributes attrs = new BasicAttributes();
    attrs.put(new BasicAttribute("userRole", "x"));
    DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
    ctx.setAttributeValue("uid", "ani");
    LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES);
    assertThat(user.getAuthorities()).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_X");
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.jupiter.api.Test)

Example 38 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class LdapUserDetailsMapperTests method testPasswordAttributeIsMappedCorrectly.

@Test
public void testPasswordAttributeIsMappedCorrectly() {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    mapper.setPasswordAttributeName("myappsPassword");
    BasicAttributes attrs = new BasicAttributes();
    attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes()));
    DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
    ctx.setAttributeValue("uid", "ani");
    LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES);
    assertThat(user.getPassword()).isEqualTo("mypassword");
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.jupiter.api.Test)

Example 39 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class ActiveDirectoryLdapAuthenticationProvider method loadUserAuthorities.

/**
 * Creates the user authority list from the values of the {@code memberOf} attribute
 * obtained from the user's Active Directory entry.
 */
@Override
protected Collection<? extends GrantedAuthority> loadUserAuthorities(DirContextOperations userData, String username, String password) {
    String[] groups = userData.getStringAttributes("memberOf");
    if (groups == null) {
        this.logger.debug("No values for 'memberOf' attribute.");
        return AuthorityUtils.NO_AUTHORITIES;
    }
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("'memberOf' attribute values: " + Arrays.asList(groups));
    }
    List<GrantedAuthority> authorities = new ArrayList<>(groups.length);
    for (String group : groups) {
        authorities.add(new SimpleGrantedAuthority(new DistinguishedName(group).removeLast().getValue()));
    }
    return authorities;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) DistinguishedName(org.springframework.ldap.core.DistinguishedName) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList)

Example 40 with DistinguishedName

use of org.springframework.ldap.core.DistinguishedName in project spring-security by spring-projects.

the class InetOrgPersonTests method mappingBackToContextMatchesOriginalData.

@Test
public void mappingBackToContextMatchesOriginalData() {
    DirContextAdapter ctx1 = createUserContext();
    DirContextAdapter ctx2 = new DirContextAdapter();
    ctx1.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
    ctx2.setDn(new DistinguishedName("ignored=ignored"));
    InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)).createUserDetails();
    p.populateContext(ctx2);
    assertThat(ctx2).isEqualTo(ctx1);
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

DistinguishedName (org.springframework.ldap.core.DistinguishedName)42 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)28 Test (org.junit.jupiter.api.Test)18 GrantedAuthority (org.springframework.security.core.GrantedAuthority)9 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)5 BasicAttribute (javax.naming.directory.BasicAttribute)4 DirContext (javax.naming.directory.DirContext)4 SearchResult (javax.naming.directory.SearchResult)3 Authentication (org.springframework.security.core.Authentication)3 NetworkGroupDTO (com.tomasio.projects.trainning.dto.NetworkGroupDTO)2 DAOException (com.tomasio.projects.trainning.exception.DAOException)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Attribute (javax.naming.directory.Attribute)2 BasicAttributes (javax.naming.directory.BasicAttributes)2 ModificationItem (javax.naming.directory.ModificationItem)2 SearchControls (javax.naming.directory.SearchControls)2 LdapTemplate (org.springframework.ldap.core.LdapTemplate)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2