Search in sources :

Example 11 with DirContext

use of javax.naming.directory.DirContext in project spring-security by spring-projects.

the class ActiveDirectoryLdapAuthenticationProviderTests method failedUserSearchCausesBadCredentials.

@Test(expected = BadCredentialsException.class)
public void failedUserSearchCausesBadCredentials() throws Exception {
    DirContext ctx = mock(DirContext.class);
    when(ctx.getNameInNamespace()).thenReturn("");
    when(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))).thenThrow(new NameNotFoundException());
    provider.contextFactory = createContextFactoryReturning(ctx);
    provider.authenticate(joe);
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) 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 12 with DirContext

use of javax.naming.directory.DirContext 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 13 with DirContext

use of javax.naming.directory.DirContext 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 14 with DirContext

use of javax.naming.directory.DirContext 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 15 with DirContext

use of javax.naming.directory.DirContext in project spring-security by spring-projects.

the class JndiDnsResolver method resolveServiceIpAddress.

/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.springframework.security.remoting.dns.DnsResolver#resolveServiceIpAddress(java
	 * .lang.String, java.lang.String)
	 */
public String resolveServiceIpAddress(String serviceType, String domain) {
    DirContext ctx = this.ctxFactory.getCtx();
    String hostname = resolveServiceEntry(serviceType, domain, ctx);
    return resolveIpAddress(hostname, ctx);
}
Also used : DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

DirContext (javax.naming.directory.DirContext)76 NamingException (javax.naming.NamingException)32 InitialDirContext (javax.naming.directory.InitialDirContext)32 SearchResult (javax.naming.directory.SearchResult)26 SearchControls (javax.naming.directory.SearchControls)22 Attributes (javax.naming.directory.Attributes)18 Attribute (javax.naming.directory.Attribute)16 NamingEnumeration (javax.naming.NamingEnumeration)14 Test (org.junit.Test)14 Hashtable (java.util.Hashtable)12 DistinguishedName (org.springframework.ldap.core.DistinguishedName)11 Name (javax.naming.Name)7 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 BasicAttribute (javax.naming.directory.BasicAttribute)6 BasicAttributes (javax.naming.directory.BasicAttributes)6 Authentication (org.springframework.security.core.Authentication)5 Principal (java.security.Principal)3 LdapContext (javax.naming.ldap.LdapContext)3