Search in sources :

Example 41 with DistinguishedName

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

the class ActiveDirectoryLdapAuthenticationProviderTests method nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal.

@Test
public void nullDomainIsSupportedIfAuthenticatingWithFullUserPrincipal() throws Exception {
    this.provider = new ActiveDirectoryLdapAuthenticationProvider(null, "ldap://192.168.1.200/");
    DirContext ctx = mock(DirContext.class);
    given(ctx.getNameInNamespace()).willReturn("");
    DirContextAdapter dca = new DirContextAdapter();
    SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
    given(ctx.search(eq(new DistinguishedName("DC=mydomain,DC=eu")), any(String.class), any(Object[].class), any(SearchControls.class))).willReturn(new MockNamingEnumeration(sr));
    this.provider.contextFactory = createContextFactoryReturning(ctx);
    assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
    this.provider.authenticate(new UsernamePasswordAuthenticationToken("joe@mydomain.eu", "password"));
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) SearchResult(javax.naming.directory.SearchResult) SearchControls(javax.naming.directory.SearchControls) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) DirContext(javax.naming.directory.DirContext) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) Test(org.junit.jupiter.api.Test)

Example 42 with DistinguishedName

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

the class ActiveDirectoryLdapAuthenticationProviderTests method checkAuthentication.

private void checkAuthentication(String rootDn, ActiveDirectoryLdapAuthenticationProvider provider) throws NamingException {
    DirContext ctx = mock(DirContext.class);
    given(ctx.getNameInNamespace()).willReturn("");
    DirContextAdapter dca = new DirContextAdapter();
    SearchResult sr = new SearchResult("CN=Joe Jannsen,CN=Users", dca, dca.getAttributes());
    @SuppressWarnings("deprecation") DistinguishedName searchBaseDn = new DistinguishedName(rootDn);
    given(ctx.search(eq(searchBaseDn), any(String.class), any(Object[].class), any(SearchControls.class))).willReturn(new MockNamingEnumeration(sr)).willReturn(new MockNamingEnumeration(sr));
    provider.contextFactory = createContextFactoryReturning(ctx);
    Authentication result = provider.authenticate(this.joe);
    assertThat(result.getAuthorities()).isEmpty();
    dca.addAttributeValue("memberOf", "CN=Admin,CN=Users,DC=mydomain,DC=eu");
    result = provider.authenticate(this.joe);
    assertThat(result.getAuthorities()).hasSize(1);
}
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) DirContext(javax.naming.directory.DirContext)

Aggregations

DistinguishedName (org.springframework.ldap.core.DistinguishedName)42 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)28 Test (org.junit.jupiter.api.Test)18 GrantedAuthority (org.springframework.security.core.GrantedAuthority)9 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)5 BasicAttribute (javax.naming.directory.BasicAttribute)4 DirContext (javax.naming.directory.DirContext)4 SearchResult (javax.naming.directory.SearchResult)3 Authentication (org.springframework.security.core.Authentication)3 NetworkGroupDTO (com.tomasio.projects.trainning.dto.NetworkGroupDTO)2 DAOException (com.tomasio.projects.trainning.exception.DAOException)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Attribute (javax.naming.directory.Attribute)2 BasicAttributes (javax.naming.directory.BasicAttributes)2 ModificationItem (javax.naming.directory.ModificationItem)2 SearchControls (javax.naming.directory.SearchControls)2 LdapTemplate (org.springframework.ldap.core.LdapTemplate)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2