Search in sources :

Example 1 with DirContextAdapter

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

the class UserDetailsServiceLdapAuthoritiesPopulatorTests method delegationToUserDetailsServiceReturnsCorrectRoles.

@Test
public void delegationToUserDetailsServiceReturnsCorrectRoles() throws Exception {
    UserDetailsService uds = mock(UserDetailsService.class);
    UserDetails user = mock(UserDetails.class);
    when(uds.loadUserByUsername("joe")).thenReturn(user);
    List authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
    when(user.getAuthorities()).thenReturn(authorities);
    UserDetailsServiceLdapAuthoritiesPopulator populator = new UserDetailsServiceLdapAuthoritiesPopulator(uds);
    Collection<? extends GrantedAuthority> auths = populator.getGrantedAuthorities(new DirContextAdapter(), "joe");
    assertThat(auths).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(auths).contains("ROLE_USER")).isTrue();
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UserDetailsServiceLdapAuthoritiesPopulator(org.springframework.security.ldap.authentication.UserDetailsServiceLdapAuthoritiesPopulator) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) List(java.util.List) Test(org.junit.Test)

Example 2 with DirContextAdapter

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

the class ActiveDirectoryLdapAuthenticationProviderTests method bindPrincipalUsed.

// SEC-2897
@Test
public void bindPrincipalUsed() throws Exception {
    // given
    final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))";
    ArgumentCaptor<Object[]> captor = ArgumentCaptor.forClass(Object[].class);
    DirContext ctx = mock(DirContext.class);
    when(ctx.getNameInNamespace()).thenReturn("");
    DirContextAdapter dca = new DirContextAdapter();
    SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
    when(ctx.search(any(Name.class), eq(defaultSearchFilter), captor.capture(), any(SearchControls.class))).thenReturn(new MockNamingEnumeration(sr));
    ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider("mydomain.eu", "ldap://192.168.1.200/");
    customProvider.contextFactory = createContextFactoryReturning(ctx);
    // when
    Authentication result = customProvider.authenticate(joe);
    // then
    assertThat(captor.getValue()).containsOnly("joe@mydomain.eu");
    assertThat(result.isAuthenticated()).isTrue();
}
Also used : Authentication(org.springframework.security.core.Authentication) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) DirContext(javax.naming.directory.DirContext) Name(javax.naming.Name) DistinguishedName(org.springframework.ldap.core.DistinguishedName) Test(org.junit.Test)

Example 3 with DirContextAdapter

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

the class ActiveDirectoryLdapAuthenticationProviderTests method defaultSearchFilter.

@Test
public void defaultSearchFilter() throws Exception {
    // given
    final String defaultSearchFilter = "(&(objectClass=user)(userPrincipalName={0}))";
    DirContext ctx = mock(DirContext.class);
    when(ctx.getNameInNamespace()).thenReturn("");
    DirContextAdapter dca = new DirContextAdapter();
    SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
    when(ctx.search(any(Name.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class))).thenReturn(new MockNamingEnumeration(sr));
    ActiveDirectoryLdapAuthenticationProvider customProvider = new ActiveDirectoryLdapAuthenticationProvider("mydomain.eu", "ldap://192.168.1.200/");
    customProvider.contextFactory = createContextFactoryReturning(ctx);
    // when
    Authentication result = customProvider.authenticate(joe);
    // then
    assertThat(result.isAuthenticated()).isTrue();
    verify(ctx).search(any(DistinguishedName.class), eq(defaultSearchFilter), any(Object[].class), any(SearchControls.class));
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) Authentication(org.springframework.security.core.Authentication) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) DirContext(javax.naming.directory.DirContext) Name(javax.naming.Name) DistinguishedName(org.springframework.ldap.core.DistinguishedName) Test(org.junit.Test)

Example 4 with DirContextAdapter

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

the class ActiveDirectoryLdapAuthenticationProviderTests method duplicateUserSearchCausesError.

@SuppressWarnings("unchecked")
@Test(expected = IncorrectResultSizeDataAccessException.class)
public void duplicateUserSearchCausesError() throws Exception {
    DirContext ctx = mock(DirContext.class);
    when(ctx.getNameInNamespace()).thenReturn("");
    NamingEnumeration<SearchResult> searchResults = mock(NamingEnumeration.class);
    when(searchResults.hasMore()).thenReturn(true, true, false);
    SearchResult searchResult = mock(SearchResult.class);
    when(searchResult.getObject()).thenReturn(new DirContextAdapter("ou=1"), new DirContextAdapter("ou=2"));
    when(searchResults.next()).thenReturn(searchResult);
    when(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))).thenReturn(searchResults);
    provider.contextFactory = createContextFactoryReturning(ctx);
    provider.authenticate(joe);
}
Also used : DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) DirContext(javax.naming.directory.DirContext) Name(javax.naming.Name) DistinguishedName(org.springframework.ldap.core.DistinguishedName) Test(org.junit.Test)

Example 5 with DirContextAdapter

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

the class InetOrgPersonTests method copyMatchesOriginalData.

@Test
public void copyMatchesOriginalData() {
    DirContextAdapter ctx1 = createUserContext();
    DirContextAdapter ctx2 = new DirContextAdapter();
    ctx2.setDn(new DistinguishedName("ignored=ignored"));
    ctx1.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
    InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)).createUserDetails();
    InetOrgPerson p2 = (InetOrgPerson) new InetOrgPerson.Essence(p).createUserDetails();
    p2.populateContext(ctx2);
    assertThat(ctx2).isEqualTo(ctx1);
}
Also used : 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