Search in sources :

Example 16 with DirContextAdapter

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

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

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

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

Example 20 with DirContextAdapter

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

the class NestedLdapAuthoritiesPopulatorTests method testScalaDudeJDevelopersAuthoritiesWithSearchLimit.

@Test
public void testScalaDudeJDevelopersAuthoritiesWithSearchLimit() {
    populator.setMaxSearchDepth(1);
    DirContextAdapter ctx = new DirContextAdapter("uid=scaladude,ou=people,dc=springframework,dc=org");
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "scaladude");
    assertThat(authorities).hasSize(1);
    assertThat(authorities).isEqualTo(Arrays.asList(scalaDevelopers));
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) 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