Search in sources :

Example 26 with DirContextAdapter

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

the class DefaultLdapAuthoritiesPopulatorTests method subGroupRolesAreNotFoundByDefault.

@Test
public void subGroupRolesAreNotFoundByDefault() {
    populator.setGroupRoleAttribute("ou");
    populator.setConvertToUpperCase(true);
    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 2 roles").hasSize(2);
    assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
    assertThat(authorities.contains("ROLE_DEVELOPER")).isTrue();
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter)

Example 27 with DirContextAdapter

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

the class DefaultLdapAuthoritiesPopulatorTests method userDnWithEscapedCharacterParameterReturnsExpectedRoles.

@Test
public void userDnWithEscapedCharacterParameterReturnsExpectedRoles() {
    populator.setGroupRoleAttribute("ou");
    populator.setConvertToUpperCase(true);
    populator.setGroupSearchFilter("(member={0})");
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("cn=mouse\\, jerry,ou=people,dc=springframework,dc=org"));
    Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "notused"));
    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 28 with DirContextAdapter

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

the class LdapUserDetailsManagerTests method setUp.

@Before
public void setUp() throws Exception {
    mgr = new LdapUserDetailsManager(getContextSource());
    template = new SpringSecurityLdapTemplate(getContextSource());
    DirContextAdapter ctx = new DirContextAdapter();
    ctx.setAttributeValue("objectclass", "organizationalUnit");
    ctx.setAttributeValue("ou", "test people");
    template.bind("ou=test people", ctx, null);
    ctx.setAttributeValue("ou", "testgroups");
    template.bind("ou=testgroups", ctx, null);
    DirContextAdapter group = new DirContextAdapter();
    group.setAttributeValue("objectclass", "groupOfNames");
    group.setAttributeValue("cn", "clowns");
    group.setAttributeValue("member", "cn=nobody,ou=test people,dc=springframework,dc=org");
    template.bind("cn=clowns,ou=testgroups", group, null);
    group.setAttributeValue("cn", "acrobats");
    template.bind("cn=acrobats,ou=testgroups", group, null);
    mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=test people", "uid"));
    mgr.setGroupSearchBase("ou=testgroups");
    mgr.setGroupRoleAttributeName("cn");
    mgr.setGroupMemberAttributeName("member");
    mgr.setUserDetailsMapper(new PersonContextMapper());
}
Also used : SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) DefaultLdapUsernameToDnMapper(org.springframework.security.ldap.DefaultLdapUsernameToDnMapper) Before(org.junit.Before)

Example 29 with DirContextAdapter

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

the class NestedLdapAuthoritiesPopulatorTests method testClosureDudeJDevelopersWithMembershipAsAttributeValues.

@Test
public void testClosureDudeJDevelopersWithMembershipAsAttributeValues() {
    populator.setAttributeNames(new HashSet(Arrays.asList("member")));
    DirContextAdapter ctx = new DirContextAdapter("uid=closuredude,ou=people,dc=springframework,dc=org");
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "closuredude");
    assertThat(authorities).hasSize(5);
    assertThat(authorities).isEqualTo(Arrays.asList(closureDevelopers, javaDevelopers, circularJavaDevelopers, jDevelopers, groovyDevelopers));
    LdapAuthority[] ldapAuthorities = authorities.toArray(new LdapAuthority[0]);
    assertThat(ldapAuthorities.length).isEqualTo(5);
    // closure group
    assertThat(ldapAuthorities[0].getAttributes().containsKey("member")).isTrue();
    assertThat(ldapAuthorities[0].getAttributes().get("member")).isNotNull();
    assertThat(ldapAuthorities[0].getAttributes().get("member")).hasSize(1);
    assertThat(ldapAuthorities[0].getFirstAttributeValue("member")).isEqualTo("uid=closuredude,ou=people,dc=springframework,dc=org");
    // java group
    assertThat(ldapAuthorities[1].getAttributes().containsKey("member")).isTrue();
    assertThat(ldapAuthorities[1].getAttributes().get("member")).isNotNull();
    assertThat(ldapAuthorities[1].getAttributes().get("member")).hasSize(3);
    assertThat(groovyDevelopers.getDn()).isEqualTo(ldapAuthorities[1].getFirstAttributeValue("member"));
    assertThat(scalaDevelopers.getDn()).isEqualTo(ldapAuthorities[2].getAttributes().get("member"));
    // test non existent attribute
    assertThat(ldapAuthorities[2].getFirstAttributeValue("test")).isNull();
    assertThat(ldapAuthorities[2].getAttributeValues("test")).isNotNull();
    assertThat(ldapAuthorities[2].getAttributeValues("test")).isEmpty();
    // test role name
    assertThat(ldapAuthorities[3].getAuthority()).isEqualTo(jDevelopers.getAuthority());
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with DirContextAdapter

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

the class NestedLdapAuthoritiesPopulatorTests method testScalaDudeJDevelopersAuthorities.

@Test
public void testScalaDudeJDevelopersAuthorities() {
    DirContextAdapter ctx = new DirContextAdapter("uid=scaladude,ou=people,dc=springframework,dc=org");
    Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "scaladude");
    assertThat(authorities).hasSize(5);
    assertThat(Arrays.asList(javaDevelopers, scalaDevelopers, circularJavaDevelopers, jDevelopers, groovyDevelopers)).isEqualTo(authorities);
}
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