Search in sources :

Example 36 with DirContextAdapter

use of org.springframework.ldap.core.DirContextAdapter 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.Test)

Example 37 with DirContextAdapter

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

the class LdapUserDetailsMapperTests method testMultipleRoleAttributeValuesAreMappedToAuthorities.

@Test
public void testMultipleRoleAttributeValuesAreMappedToAuthorities() throws Exception {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    mapper.setConvertToUpperCase(false);
    mapper.setRolePrefix("");
    mapper.setRoleAttributes(new String[] { "userRole" });
    DirContextAdapter ctx = new DirContextAdapter();
    ctx.setAttributeValues("userRole", new String[] { "X", "Y", "Z" });
    ctx.setAttributeValue("uid", "ani");
    LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES);
    assertThat(user.getAuthorities()).hasSize(3);
}
Also used : DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.Test)

Example 38 with DirContextAdapter

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

the class LdapUserDetailsMapperTests method testNonRetrievedRoleAttributeIsIgnored.

/**
	 * SEC-303. Non-retrieved role attribute causes NullPointerException
	 */
@Test
public void testNonRetrievedRoleAttributeIsIgnored() throws Exception {
    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.Test)

Example 39 with DirContextAdapter

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

the class LdapUserDetailsMapperTests method testPasswordAttributeIsMappedCorrectly.

@Test
public void testPasswordAttributeIsMappedCorrectly() throws Exception {
    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.Test)

Aggregations

DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)39 DistinguishedName (org.springframework.ldap.core.DistinguishedName)28 Test (org.junit.Test)19 GrantedAuthority (org.springframework.security.core.GrantedAuthority)11 DirContext (javax.naming.directory.DirContext)7 SearchControls (javax.naming.directory.SearchControls)7 SearchResult (javax.naming.directory.SearchResult)7 Name (javax.naming.Name)4 Authentication (org.springframework.security.core.Authentication)4 HashSet (java.util.HashSet)3 BasicAttribute (javax.naming.directory.BasicAttribute)3 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)3 UserDetails (org.springframework.security.core.userdetails.UserDetails)3 List (java.util.List)2 Attribute (javax.naming.directory.Attribute)2 BasicAttributes (javax.naming.directory.BasicAttributes)2 DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 MockUserSearch (org.springframework.security.ldap.authentication.MockUserSearch)2 ArrayList (java.util.ArrayList)1