Search in sources :

Example 31 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project adempiere by adempiere.

the class LDAP method validate.

/**
	 * 	Validate User
	 *	@param ldapURL provider url - e.g. ldap://dc.compiere.org
	 *	@param domain domain name = e.g. compiere.org
	 *	@param userName user name - e.g. jjanke
	 *	@param password password 
	 *	@return true if validated with ldap
	 */
public static boolean validate(String ldapURL, String domain, String userName, String password) {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    //	ldap://dc.compiere.org
    env.put(Context.PROVIDER_URL, ldapURL);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    //	jjanke@compiere.org
    // For OpenLDAP uncomment the next line  
    // StringBuffer principal = new StringBuffer("uid=").append(userName).append(",").append(domain);
    StringBuffer principal = new StringBuffer(userName).append("@").append(domain);
    env.put(Context.SECURITY_PRINCIPAL, principal.toString());
    env.put(Context.SECURITY_CREDENTIALS, password);
    //
    try {
        // Create the initial context
        InitialLdapContext ctx = new InitialLdapContext(env, null);
        //	DirContext ctx = new InitialDirContext(env);
        //	Test - Get the attributes
        Attributes answer = ctx.getAttributes("");
        // Print the answer
        if (false)
            dump(answer);
    } catch (AuthenticationException e) {
        log.info("Error: " + principal + " - " + e.getLocalizedMessage());
        return false;
    } catch (Exception e) {
        log.log(Level.SEVERE, ldapURL + " - " + principal, e);
        return false;
    }
    log.info("OK: " + principal);
    return true;
}
Also used : AuthenticationException(javax.naming.AuthenticationException) Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException)

Example 32 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project ranger by apache.

the class LdapDeltaUserGroupBuilder method createLdapContext.

private void createLdapContext() throws Throwable {
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapUrl);
    if (ldapUrl.startsWith("ldaps") && (config.getSSLTrustStorePath() != null && !config.getSSLTrustStorePath().trim().isEmpty())) {
        env.put("java.naming.ldap.factory.socket", "org.apache.ranger.ldapusersync.process.CustomSSLSocketFactory");
    }
    ldapContext = new InitialLdapContext(env, null);
    if (!ldapUrl.startsWith("ldaps")) {
        if (config.isStartTlsEnabled()) {
            tls = (StartTlsResponse) ldapContext.extendedOperation(new StartTlsRequest());
            if (config.getSSLTrustStorePath() != null && !config.getSSLTrustStorePath().trim().isEmpty()) {
                tls.negotiate(CustomSSLSocketFactory.getDefault());
            } else {
                tls.negotiate();
            }
            LOG.info("Starting TLS session...");
        }
    }
    ldapContext.addToEnvironment(Context.SECURITY_PRINCIPAL, ldapBindDn);
    ldapContext.addToEnvironment(Context.SECURITY_CREDENTIALS, ldapBindPassword);
    ldapContext.addToEnvironment(Context.SECURITY_AUTHENTICATION, ldapAuthenticationMechanism);
    ldapContext.addToEnvironment(Context.REFERRAL, ldapReferral);
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) Properties(java.util.Properties) StartTlsRequest(javax.naming.ldap.StartTlsRequest)

Example 33 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project tomcat70 by apache.

the class JNDIRealm method createTlsDirContext.

/**
 * Create a tls enabled LdapContext and set the StartTlsResponse tls
 * instance variable.
 *
 * @param env
 *            Environment to use for context creation
 * @return configured {@link LdapContext}
 * @throws NamingException
 *             when something goes wrong while negotiating the connection
 */
private DirContext createTlsDirContext(Hashtable<String, String> env) throws NamingException {
    Map<String, Object> savedEnv = new HashMap<String, Object>();
    for (String key : Arrays.asList(Context.SECURITY_AUTHENTICATION, Context.SECURITY_CREDENTIALS, Context.SECURITY_PRINCIPAL, Context.SECURITY_PROTOCOL)) {
        Object entry = env.remove(key);
        if (entry != null) {
            savedEnv.put(key, entry);
        }
    }
    LdapContext result = null;
    try {
        result = new InitialLdapContext(env, null);
        tls = (StartTlsResponse) result.extendedOperation(new StartTlsRequest());
        if (getHostnameVerifier() != null) {
            tls.setHostnameVerifier(getHostnameVerifier());
        }
        if (getCipherSuitesArray() != null) {
            tls.setEnabledCipherSuites(getCipherSuitesArray());
        }
        try {
            SSLSession negotiate = tls.negotiate(getSSLSocketFactory());
            containerLog.debug(sm.getString("jndiRealm.negotiatedTls", negotiate.getProtocol()));
        } catch (IOException e) {
            throw new NamingException(e.getMessage());
        }
    } finally {
        if (result != null) {
            for (Map.Entry<String, Object> savedEntry : savedEnv.entrySet()) {
                result.addToEnvironment(savedEntry.getKey(), savedEntry.getValue());
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) InitialLdapContext(javax.naming.ldap.InitialLdapContext) SSLSession(javax.net.ssl.SSLSession) NamingException(javax.naming.NamingException) IOException(java.io.IOException) StartTlsRequest(javax.naming.ldap.StartTlsRequest) HashMap(java.util.HashMap) Map(java.util.Map) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 34 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project OpenOLAT by OpenOLAT.

the class LDAPLoginManagerImpl method bindSystem.

/**
 * Connect to the LDAP server with System DN and Password
 *
 * Configuration: LDAP URL = ldapContext.xml (property=ldapURL) System DN =
 * ldapContext.xml (property=ldapSystemDN) System PW = ldapContext.xml
 * (property=ldapSystemPW)
 *
 * @return The LDAP connection (LdapContext) or NULL if connect fails
 *
 * @throws NamingException
 */
public LdapContext bindSystem() {
    // set LDAP connection attributes
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapLoginModule.getLdapUrl());
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, ldapLoginModule.getLdapSystemDN());
    env.put(Context.SECURITY_CREDENTIALS, ldapLoginModule.getLdapSystemPW());
    if (ldapLoginModule.getLdapConnectionTimeout() != null) {
        env.put(TIMEOUT_KEY, ldapLoginModule.getLdapConnectionTimeout().toString());
    }
    // check ssl
    if (ldapLoginModule.isSslEnabled()) {
        enableSSL(env);
    }
    try {
        InitialLdapContext ctx = new InitialLdapContext(env, new Control[] {});
        ctx.getConnectControls();
        return ctx;
    } catch (NamingException e) {
        log.error("NamingException when trying to bind system with DN::" + ldapLoginModule.getLdapSystemDN() + " and PW::" + ldapLoginModule.getLdapSystemPW() + " on URL::" + ldapLoginModule.getLdapUrl(), e);
        return null;
    } catch (Exception e) {
        log.error("Exception when trying to bind system with DN::" + ldapLoginModule.getLdapSystemDN() + " and PW::" + ldapLoginModule.getLdapSystemPW() + " on URL::" + ldapLoginModule.getLdapUrl(), e);
        return null;
    }
}
Also used : Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException)

Example 35 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project camunda-bpm-platform by camunda.

the class LdapIdentityProviderSession method openContext.

protected InitialLdapContext openContext(String userDn, String password) {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, ldapConfiguration.getInitialContextFactory());
    env.put(Context.SECURITY_AUTHENTICATION, ldapConfiguration.getSecurityAuthentication());
    env.put(Context.PROVIDER_URL, ldapConfiguration.getServerUrl());
    env.put(Context.SECURITY_PRINCIPAL, userDn);
    env.put(Context.SECURITY_CREDENTIALS, password);
    // for anonymous login
    if (ldapConfiguration.isAllowAnonymousLogin() && password.isEmpty()) {
        env.put(Context.SECURITY_AUTHENTICATION, "none");
    }
    if (ldapConfiguration.isUseSsl()) {
        env.put(Context.SECURITY_PROTOCOL, "ssl");
    }
    // add additional properties
    Map<String, String> contextProperties = ldapConfiguration.getContextProperties();
    if (contextProperties != null) {
        env.putAll(contextProperties);
    }
    try {
        return new InitialLdapContext(env, null);
    } catch (AuthenticationException e) {
        throw new LdapAuthenticationException("Could not authenticate with LDAP server", e);
    } catch (NamingException e) {
        throw new IdentityProviderException("Could not connect to LDAP server", e);
    }
}
Also used : AuthenticationException(javax.naming.AuthenticationException) Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) NamingException(javax.naming.NamingException) IdentityProviderException(org.camunda.bpm.engine.impl.identity.IdentityProviderException)

Aggregations

InitialLdapContext (javax.naming.ldap.InitialLdapContext)54 NamingException (javax.naming.NamingException)30 Hashtable (java.util.Hashtable)17 LdapContext (javax.naming.ldap.LdapContext)17 Attributes (javax.naming.directory.Attributes)16 Properties (java.util.Properties)14 SearchResult (javax.naming.directory.SearchResult)14 IOException (java.io.IOException)11 AuthenticationException (javax.naming.AuthenticationException)10 NamingEnumeration (javax.naming.NamingEnumeration)10 StartTlsRequest (javax.naming.ldap.StartTlsRequest)10 BasicAttributes (javax.naming.directory.BasicAttributes)9 Attribute (javax.naming.directory.Attribute)8 SearchControls (javax.naming.directory.SearchControls)8 LdapConfigProperties (org.bedework.calfacade.configs.LdapConfigProperties)7 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)7 StartTlsResponse (javax.naming.ldap.StartTlsResponse)6 CommunicationException (javax.naming.CommunicationException)5 DirContext (javax.naming.directory.DirContext)5 BwGroup (org.bedework.calfacade.BwGroup)5