Search in sources :

Example 81 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project iaf by ibissource.

the class LdapSender method loopkupDirContext.

/**
 * Retrieves the DirContext from the JNDI environment and sets the <code>providerURL</code> back to <code>ldapProviderURL</code> if specified.
 * @throws ParameterException
 */
protected synchronized DirContext loopkupDirContext(Map paramValueMap) throws NamingException, ParameterException {
    DirContext dirContext;
    if (jndiEnv == null) {
        Hashtable newJndiEnv = getJndiEnv();
        // newJndiEnv.put("com.sun.jndi.ldap.trace.ber", System.err);//ldap response in log for debug purposes
        if (getLdapProviderURL() != null) {
            // Overwriting the (realm)providerURL if specified in configuration
            newJndiEnv.put("java.naming.provider.url", getLdapProviderURL());
        }
        if (principalParameterFound) {
            newJndiEnv.put(Context.SECURITY_PRINCIPAL, paramValueMap.get("principal"));
            newJndiEnv.put(Context.SECURITY_CREDENTIALS, paramValueMap.get("credentials"));
        }
        if (isUsePooling()) {
            // Enable connection pooling
            newJndiEnv.put("com.sun.jndi.ldap.connect.pool", "true");
        // see http://java.sun.com/products/jndi/tutorial/ldap/connect/config.html
        // newJndiEnv.put("com.sun.jndi.ldap.connect.pool.maxsize", "20" );
        // newJndiEnv.put("com.sun.jndi.ldap.connect.pool.prefsize", "10" );
        // newJndiEnv.put("com.sun.jndi.ldap.connect.pool.timeout", "300000" );
        } else {
            // Disable connection pooling
            newJndiEnv.put("com.sun.jndi.ldap.connect.pool", "false");
        }
        if (log.isDebugEnabled())
            log.debug("created environment for LDAP provider URL [" + newJndiEnv.get("java.naming.provider.url") + "]");
        dirContext = new InitialDirContext(newJndiEnv);
        if (!principalParameterFound) {
            jndiEnv = newJndiEnv;
        }
    } else {
        dirContext = new InitialDirContext(jndiEnv);
    }
    return dirContext;
// return (DirContext) dirContextTemplate.lookup(""); 	// return copy to be thread-safe
}
Also used : Hashtable(java.util.Hashtable) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 82 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project stdlib by petergeneric.

the class LDAPUserAuthenticationService method ldapAuthenticate.

private LDAPUserRecord ldapAuthenticate(String inputUsername, final String password) {
    // Allow the user to provide domain-slash-user or user@domain in addition to bare "user" with an implied domain
    final String username;
    final String fullyQualifiedUsername;
    if (inputUsername.indexOf('\\') > 0) {
        final String[] segments = StringUtils.split(inputUsername, "\\", 2);
        final String domain = segments[0];
        // get bare username (discard everything before the slash)
        username = segments[1];
        fullyQualifiedUsername = username + "@" + domain;
    } else if (inputUsername.indexOf('@') > 0) {
        fullyQualifiedUsername = inputUsername;
        // get bare username (discard everything after the @)
        username = StringUtils.split(inputUsername, '@')[0];
    } else {
        // Implied domain
        fullyQualifiedUsername = inputUsername + "@" + domain;
        username = inputUsername;
    }
    try {
        DirContext ldapContext = null;
        try {
            Hashtable<String, String> ldapEnv = new Hashtable<>();
            ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            ldapEnv.put(Context.PROVIDER_URL, ldapEndpoint);
            ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
            ldapEnv.put(Context.SECURITY_PRINCIPAL, fullyQualifiedUsername);
            ldapEnv.put(Context.SECURITY_CREDENTIALS, password);
            // N.B. sometimes takes ~10 seconds
            ldapContext = new InitialDirContext(ldapEnv);
            final NamingEnumeration<SearchResult> answer;
            {
                SearchControls search = new SearchControls();
                search.setSearchScope(SearchControls.SUBTREE_SCOPE);
                search.setReturningAttributes(new String[] { "dn", "name", "samAccountName" });
                final String searchFilter = String.format(this.ldapFilter, username);
                answer = ldapContext.search(ldapSearchBase, searchFilter, search);
            }
            while (answer.hasMoreElements()) {
                SearchResult sr = answer.next();
                Attributes attrs = sr.getAttributes();
                final String dn = sr.getNameInNamespace();
                final String name = attrs.get("name").get().toString();
                final String actualUsername = attrs.get("samAccountName").get().toString();
                // Get the direct & indirect group membership data
                List<LDAPGroup> groups = getGroups(ldapContext, dn);
                return new LDAPUserRecord(actualUsername, name, groups);
            }
            return null;
        } finally {
            if (ldapContext != null)
                ldapContext.close();
        }
    } catch (NamingException e) {
        throw new RuntimeException("Error accessing LDAP server (incorrect username/password or server connection issue, please try again)", e);
    }
}
Also used : Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException)

Example 83 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project activemq-artemis by apache.

the class SaslKrb5LDAPSecurityTest method testRunning.

@Test
public void testRunning() throws Exception {
    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
    env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
    DirContext ctx = new InitialDirContext(env);
    HashSet<String> set = new HashSet<>();
    NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
    while (list.hasMore()) {
        NameClassPair ncp = list.next();
        set.add(ncp.getName());
    }
    Assert.assertTrue(set.contains("uid=admin"));
    Assert.assertTrue(set.contains("ou=users"));
    Assert.assertTrue(set.contains("ou=groups"));
    Assert.assertTrue(set.contains("ou=configuration"));
    Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot"));
    ctx.close();
}
Also used : Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 84 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project activemq-artemis by apache.

the class LDAPSecurityTest method testRunning.

@Test
public void testRunning() throws Exception {
    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
    env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
    DirContext ctx = new InitialDirContext(env);
    HashSet<String> set = new HashSet<>();
    NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
    while (list.hasMore()) {
        NameClassPair ncp = list.next();
        set.add(ncp.getName());
    }
    Assert.assertTrue(set.contains("uid=admin"));
    Assert.assertTrue(set.contains("ou=users"));
    Assert.assertTrue(set.contains("ou=groups"));
    Assert.assertTrue(set.contains("ou=configuration"));
    Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot"));
    ctx.close();
}
Also used : Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 85 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project activemq-artemis by apache.

the class LegacyLDAPSecuritySettingPluginTest method testRunning.

@Test
public void testRunning() throws Exception {
    Hashtable<String, String> env = new Hashtable<>();
    env.put(Context.PROVIDER_URL, "ldap://localhost:1024");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
    env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
    DirContext ctx = new InitialDirContext(env);
    HashSet<String> set = new HashSet<>();
    NamingEnumeration<NameClassPair> list = ctx.list("ou=system");
    while (list.hasMore()) {
        NameClassPair ncp = list.next();
        set.add(ncp.getName());
    }
    Assert.assertTrue(set.contains("uid=admin"));
    Assert.assertTrue(set.contains("ou=users"));
    Assert.assertTrue(set.contains("ou=groups"));
    Assert.assertTrue(set.contains("ou=configuration"));
    Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot"));
}
Also used : Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

InitialDirContext (javax.naming.directory.InitialDirContext)131 DirContext (javax.naming.directory.DirContext)71 NamingException (javax.naming.NamingException)67 Hashtable (java.util.Hashtable)51 Attributes (javax.naming.directory.Attributes)34 Attribute (javax.naming.directory.Attribute)24 SearchResult (javax.naming.directory.SearchResult)22 SearchControls (javax.naming.directory.SearchControls)19 IOException (java.io.IOException)11 CommunicationException (javax.naming.CommunicationException)11 Test (org.junit.Test)11 Properties (java.util.Properties)10 BasicAttributes (javax.naming.directory.BasicAttributes)9 ArrayList (java.util.ArrayList)8 LoginException (javax.security.auth.login.LoginException)8 NameClassPair (javax.naming.NameClassPair)7 HashSet (java.util.HashSet)6 NamingEnumeration (javax.naming.NamingEnumeration)6 BasicAttribute (javax.naming.directory.BasicAttribute)6 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)5