Search in sources :

Example 21 with DirContext

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

the class BindAuthenticator method bindWithDn.

private DirContextOperations bindWithDn(String userDnStr, String username, String password, Attributes attrs) {
    BaseLdapPathContextSource ctxSource = (BaseLdapPathContextSource) getContextSource();
    DistinguishedName userDn = new DistinguishedName(userDnStr);
    DistinguishedName fullDn = new DistinguishedName(userDn);
    fullDn.prepend(ctxSource.getBaseLdapPath());
    logger.debug("Attempting to bind as " + fullDn);
    DirContext ctx = null;
    try {
        ctx = getContextSource().getContext(fullDn.toString(), password);
        // Check for password policy control
        PasswordPolicyControl ppolicy = PasswordPolicyControlExtractor.extractControl(ctx);
        logger.debug("Retrieving attributes...");
        if (attrs == null || attrs.size() == 0) {
            attrs = ctx.getAttributes(userDn, getUserAttributes());
        }
        DirContextAdapter result = new DirContextAdapter(attrs, userDn, ctxSource.getBaseLdapPath());
        if (ppolicy != null) {
            result.setAttributeValue(ppolicy.getID(), ppolicy);
        }
        return result;
    } catch (NamingException e) {
        // unless a subclass wishes to implement more specialized behaviour.
        if ((e instanceof org.springframework.ldap.AuthenticationException) || (e instanceof org.springframework.ldap.OperationNotSupportedException)) {
            handleBindException(userDnStr, username, e);
        } else {
            throw e;
        }
    } catch (javax.naming.NamingException e) {
        throw LdapUtils.convertLdapException(e);
    } finally {
        LdapUtils.closeContext(ctx);
    }
    return null;
}
Also used : DistinguishedName(org.springframework.ldap.core.DistinguishedName) BaseLdapPathContextSource(org.springframework.ldap.core.support.BaseLdapPathContextSource) PasswordPolicyControl(org.springframework.security.ldap.ppolicy.PasswordPolicyControl) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) NamingException(org.springframework.ldap.NamingException) DirContext(javax.naming.directory.DirContext)

Example 22 with DirContext

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

the class DefaultSpringSecurityContextSourceTests method cantBindWithWrongPasswordImmediatelyAfterSuccessfulBind.

// SEC-1145. Confirms that there is no issue here with pooling.
@Test(expected = AuthenticationException.class)
public void cantBindWithWrongPasswordImmediatelyAfterSuccessfulBind() throws Exception {
    DirContext ctx = null;
    try {
        ctx = getContextSource().getContext("uid=Bob,ou=people,dc=springframework,dc=org", "bobspassword");
    } catch (Exception e) {
    }
    assertThat(ctx).isNotNull();
    // com.sun.jndi.ldap.LdapPoolManager.showStats(System.out);
    ctx.close();
    // com.sun.jndi.ldap.LdapPoolManager.showStats(System.out);
    // Now get it gain, with wrong password. Should fail.
    ctx = getContextSource().getContext("uid=Bob,ou=people,dc=springframework,dc=org", "wrongpassword");
    ctx.close();
}
Also used : DirContext(javax.naming.directory.DirContext) AuthenticationException(org.springframework.ldap.AuthenticationException) Test(org.junit.Test)

Example 23 with DirContext

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

the class SpringSecurityLdapTemplateITests method nonSpringLdapSearchCodeTestMethod.

@Test
public void nonSpringLdapSearchCodeTestMethod() throws Exception {
    java.util.Hashtable<String, String> env = new java.util.Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:" + ApacheDSServerIntegrationTests.getServerPort());
    env.put(Context.SECURITY_PRINCIPAL, "");
    env.put(Context.SECURITY_CREDENTIALS, "");
    DirContext ctx = new javax.naming.directory.InitialDirContext(env);
    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    controls.setReturningObjFlag(true);
    controls.setReturningAttributes(null);
    String param = "cn=mouse\\, jerry,ou=people,dc=springframework,dc=org";
    javax.naming.NamingEnumeration<SearchResult> results = ctx.search("ou=groups,dc=springframework,dc=org", "(member={0})", new String[] { param }, controls);
    assertThat(results.hasMore()).as("Expected a result").isTrue();
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext)

Example 24 with DirContext

use of javax.naming.directory.DirContext in project nhin-d by DirectProject.

the class LDAPResearchTest method testLdapSearch.

@SuppressWarnings("unchecked")
public void testLdapSearch() throws Exception {
    CertCacheFactory.getInstance().flushAll();
    int port = configuration.getLdapPort();
    String url = "ldap://localhost:" + port + "/" + "cn=lookupTest";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    InitialContext initialContext = new InitialContext(env);
    assertNotNull(initialContext);
    DirContext dirContext = (DirContext) initialContext.lookup("");
    Attributes attributes = dirContext.getAttributes("");
    assertNotNull(attributes);
    NamingEnumeration<Attribute> namingEnum = (NamingEnumeration<Attribute>) attributes.getAll();
    while (namingEnum.hasMoreElements()) {
        Attribute attr = namingEnum.nextElement();
        System.out.println("Name: " + attr.getID() + "\r\nValue: " + attr.get() + "\r\n\r\n");
    }
    //Set<SearchResult> results = searchDNs( "(email=gm2552@cerner.com)", "", "ou=privKeys, ou=cerner, ou=com", 
    //        SearchControls.SUBTREE_SCOPE , dirContext);
    LdapStoreConfiguration ldapStoreConfiguration = new LdapStoreConfiguration(new String[] { url }, "", "email", "privKeyStore", "X509");
    LdapCertificateStoreProvider provider = new LdapCertificateStoreProvider(ldapStoreConfiguration, null, null);
    LDAPCertificateStore certificateResolver = (LDAPCertificateStore) provider.get();
    Collection<X509Certificate> certs = certificateResolver.getCertificates("gm2552@cerner.com");
    /*LdapEnvironment ldapEnvironment = new LdapEnvironment(env, "privKeyStore", "", "email");
		LdapCertUtilImpl ldapcertUtilImpl = new LdapCertUtilImpl(ldapEnvironment, "", "X.509");
		LDAPCertificateStore ldapCertStore = new LDAPCertificateStore(ldapcertUtilImpl, new KeyStoreCertificateStore(), null);
		
		Collection<X509Certificate> certs = ldapCertStore.getCertificates("gm2552@cerner.com");
		*/
    assertEquals(1, certs.size());
    X509Certificate cert = certs.iterator().next();
    assertFalse(cert instanceof X509CertificateEx);
    assertTrue(cert.getSubjectX500Principal().toString().contains("bob@nhind.hsgincubator.com"));
}
Also used : LdapStoreConfiguration(org.nhindirect.stagent.cert.impl.LdapStoreConfiguration) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) DirContext(javax.naming.directory.DirContext) InitialContext(javax.naming.InitialContext) X509Certificate(java.security.cert.X509Certificate) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) LDAPCertificateStore(org.nhindirect.stagent.cert.impl.LDAPCertificateStore) LdapCertificateStoreProvider(org.nhindirect.stagent.cert.impl.provider.LdapCertificateStoreProvider)

Example 25 with DirContext

use of javax.naming.directory.DirContext in project nhin-d by DirectProject.

the class LDAPResearchTest method createContext.

private DirContext createContext(String partition) throws Exception {
    int port = configuration.getLdapPort();
    String url = "ldap://localhost:" + port + "/" + partition;
    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    InitialContext initialContext = new InitialContext(env);
    assertNotNull(initialContext);
    return (DirContext) initialContext.lookup("");
}
Also used : Hashtable(java.util.Hashtable) DirContext(javax.naming.directory.DirContext) InitialContext(javax.naming.InitialContext)

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