Search in sources :

Example 11 with DistinguishedName

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

the class DefaultLdapAuthoritiesPopulatorTests method defaultRoleIsAssignedWhenSet.

@Test
public void defaultRoleIsAssignedWhenSet() {
    populator.setDefaultRole("ROLE_USER");
    assertThat(populator.getContextSource()).isSameAs(getContextSource());
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("cn=notfound"));
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "notfound");
    assertThat(authorities).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_USER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 12 with DistinguishedName

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

the class DefaultLdapAuthoritiesPopulatorTests method groupSearchReturnsExpectedRoles.

@Test
public void groupSearchReturnsExpectedRoles() {
    populator.setRolePrefix("ROLE_");
    populator.setGroupRoleAttribute("ou");
    populator.setSearchSubtree(true);
    populator.setSearchSubtree(false);
    populator.setConvertToUpperCase(true);
    populator.setGroupSearchFilter("(member={0})");
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("uid=ben,ou=people,dc=springframework,dc=org"));
    Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "ben"));
    assertThat(authorities).as("Should have 2 roles").hasSize(2);
    assertThat(authorities.contains("ROLE_DEVELOPER")).isTrue();
    assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 13 with DistinguishedName

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

the class DefaultLdapAuthoritiesPopulatorTests method nullSearchBaseIsAccepted.

@Test
public void nullSearchBaseIsAccepted() throws Exception {
    populator = new DefaultLdapAuthoritiesPopulator(getContextSource(), null);
    populator.setDefaultRole("ROLE_USER");
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(new DirContextAdapter(new DistinguishedName("cn=notused")), "notused");
    assertThat(authorities).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_USER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 14 with DistinguishedName

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

the class DefaultLdapAuthoritiesPopulatorTests method useOfUsernameParameterReturnsExpectedRoles.

@Test
public void useOfUsernameParameterReturnsExpectedRoles() {
    populator.setGroupRoleAttribute("ou");
    populator.setConvertToUpperCase(true);
    populator.setGroupSearchFilter("(ou={1})");
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("uid=ben,ou=people,dc=springframework,dc=org"));
    Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "manager"));
    assertThat(authorities).as("Should have 1 role").hasSize(1);
    assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 15 with DistinguishedName

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

the class DefaultLdapAuthoritiesPopulatorTests method extraRolesAreAdded.

@Test
public void extraRolesAreAdded() throws Exception {
    populator = new DefaultLdapAuthoritiesPopulator(getContextSource(), null) {

        @Override
        protected Set<GrantedAuthority> getAdditionalRoles(DirContextOperations user, String username) {
            return new HashSet<GrantedAuthority>(AuthorityUtils.createAuthorityList("ROLE_EXTRA"));
        }
    };
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(new DirContextAdapter(new DistinguishedName("cn=notused")), "notused");
    assertThat(authorities).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_EXTRA")).isTrue();
}
Also used : DirContextOperations(org.springframework.ldap.core.DirContextOperations) DistinguishedName(org.springframework.ldap.core.DistinguishedName) GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Aggregations

DistinguishedName (org.springframework.ldap.core.DistinguishedName)32 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)23 Test (org.junit.Test)8 GrantedAuthority (org.springframework.security.core.GrantedAuthority)7 DirContext (javax.naming.directory.DirContext)5 BasicAttribute (javax.naming.directory.BasicAttribute)4 SearchResult (javax.naming.directory.SearchResult)4 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)4 BasicAttributes (javax.naming.directory.BasicAttributes)2 ModificationItem (javax.naming.directory.ModificationItem)2 SearchControls (javax.naming.directory.SearchControls)2 DirContextOperations (org.springframework.ldap.core.DirContextOperations)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 MockUserSearch (org.springframework.security.ldap.authentication.MockUserSearch)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 NamingException (javax.naming.NamingException)1