Search in sources :

Example 41 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project uPortal by Jasig.

the class LDAPGroupStore method getConnection.

protected DirContext getConnection() {
    // JNDI boilerplate to connect to an initial context
    DirContext context = (DirContext) contexts.get("context");
    if (context == null) {
        Hashtable jndienv = new Hashtable();
        jndienv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        jndienv.put(Context.SECURITY_AUTHENTICATION, "simple");
        if (url.startsWith("ldaps")) {
            // Handle SSL connections
            String newurl = url.substring(0, 4) + url.substring(5);
            jndienv.put(Context.SECURITY_PROTOCOL, "ssl");
            jndienv.put(Context.PROVIDER_URL, newurl);
        } else {
            jndienv.put(Context.PROVIDER_URL, url);
        }
        if (logonid != null)
            jndienv.put(Context.SECURITY_PRINCIPAL, logonid);
        if (logonpassword != null)
            jndienv.put(Context.SECURITY_CREDENTIALS, logonpassword);
        try {
            context = new InitialDirContext(jndienv);
        } catch (NamingException nex) {
            log.error("LDAPGroupStore: unable to get context", nex);
        }
        contexts.put("context", context);
    }
    return context;
}
Also used : Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 42 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project platformlayer by platformlayer.

the class ITOpenLdapService method testLdap.

private void testLdap(String ldapUrl, Secret adminPassword) throws NamingException {
    Hashtable<String, String> env = new Hashtable<String, String>();
    String sp = "com.sun.jndi.ldap.LdapCtxFactory";
    env.put(Context.INITIAL_CONTEXT_FACTORY, sp);
    env.put(Context.PROVIDER_URL, ldapUrl);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=test,dc=platformlayer,dc=org");
    env.put(Context.SECURITY_CREDENTIALS, adminPassword.plaintext());
    DirContext ctx = new InitialDirContext(env);
    NamingEnumeration results = ctx.list("dc=test,dc=platformlayer,dc=org");
    while (results.hasMore()) {
        NameClassPair sr = (NameClassPair) results.next();
        System.out.println(sr.getNameInNamespace());
    }
    ctx.close();
}
Also used : Hashtable(java.util.Hashtable) NameClassPair(javax.naming.NameClassPair) NamingEnumeration(javax.naming.NamingEnumeration) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 43 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project new-cloud by xie-summer.

the class LoginUtil method passportCheck.

public static boolean passportCheck(String username, String password) {
    // LDAP登陆地址
    String ldapUrl = ConstUtils.LDAP_URL;
    if (StringUtils.isBlank(ldapUrl)) {
        logger.warn("ldap url is empty!!");
        return true;
    }
    if (ConstUtils.IS_DEBUG) {
        logger.warn("isDebug=true return");
        return true;
    }
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("java.naming.provider.url", ldapUrl);
    env.put("java.naming.security.authentication", "simple");
    env.put("java.naming.security.principal", username + ConstUtils.EMAIL_SUFFIX);
    env.put("java.naming.security.credentials", password);
    DirContext ctx = null;
    try {
        ctx = new InitialDirContext(env);
        if (ctx != null) {
            return true;
        }
    } catch (Exception e) {
        logger.error("username {} passportCheck: " + e.getMessage(), username, e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return false;
}
Also used : Hashtable(java.util.Hashtable) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 44 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project OpenClinica by OpenClinica.

the class SystemController method getLdapModule.

public HashMap<String, Object> getLdapModule(StudyBean studyBean) {
    String enabled = CoreResources.getField("ldap.enabled");
    String ldapHost = CoreResources.getField("ldap.host");
    String username = CoreResources.getField("ldap.userDn");
    String password = CoreResources.getField("ldap.password");
    String result = "";
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapHost);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    // replace with user DN
    env.put(Context.SECURITY_PRINCIPAL, username);
    env.put(Context.SECURITY_CREDENTIALS, password);
    DirContext ctx = null;
    try {
        ctx = new InitialDirContext(env);
        result = "ACTIVE";
    } catch (Exception e) {
        result = "INACTIVE";
    }
    HashMap<String, String> mapMetadata = new HashMap<>();
    mapMetadata.put("ldap.host", ldapHost);
    HashMap<String, Object> mapWebService = new HashMap<>();
    mapWebService.put("enabled", enabled.equalsIgnoreCase("true") ? "True" : "False");
    mapWebService.put("status", result);
    mapWebService.put("metadata", mapMetadata);
    HashMap<String, Object> mapModule = new HashMap<>();
    mapModule.put("Ldap", mapWebService);
    return mapModule;
}
Also used : HashMap(java.util.HashMap) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties) MessagingException(javax.mail.MessagingException) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) MailException(org.springframework.mail.MailException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 45 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.

the class AdUserConnectorType method getCertificateFromAD.

/**
 * Try to find authority for this certificate in AD (we want to return certificate with the biggest validity).
 */
private X509Certificate getCertificateFromAD(X509Certificate serverCrt, String port, String host, String user, String password) {
    X509Certificate resultCertificate = null;
    DirContext ldapContext = null;
    try {
        // Init LDAP context.
        Hashtable<String, String> ldapEnv = getAdEnvironment(host, "389", user, password, false);
        ldapContext = new InitialDirContext(ldapEnv);
        boolean continueSearching = true;
        resultCertificate = serverCrt;
        while (continueSearching) {
            X509Certificate authorityCrtOnAD = findAuthorityCrtOnAD(resultCertificate, ldapContext);
            if (authorityCrtOnAD != null) {
                // Validate certificate by found authority.
                try {
                    CertificateUtils.verifyCertificate(resultCertificate, authorityCrtOnAD);
                } catch (CertificateException ex) {
                    throw new ResultCodeException(AccResultCode.WIZARD_AD_CONNECTOR_CRT_NOT_TRUSTED, ImmutableMap.of("serialNumber", authorityCrtOnAD.getSerialNumber().toString(16).toUpperCase()), ex);
                }
            }
            if (authorityCrtOnAD == null) {
                // No authority certificate was found, previous certificate is result.
                continueSearching = false;
            } else if (authorityCrtOnAD.getIssuerDN() != null && resultCertificate.getIssuerDN() != null && resultCertificate.getIssuerDN().getName().equals(authorityCrtOnAD.getIssuerDN().getName())) {
                // Issuer name in previous and returned authority certificate is same -> returned certificate is result.
                resultCertificate = authorityCrtOnAD;
                continueSearching = false;
            } else if (authorityCrtOnAD.getIssuerDN() == null || Strings.isBlank(authorityCrtOnAD.getIssuerDN().getName())) {
                // Found authority certificate doesn't have issuer -> it is result.
                resultCertificate = authorityCrtOnAD;
                continueSearching = false;
            } else {
                // Next round.
                resultCertificate = authorityCrtOnAD;
            }
        }
    } catch (CommunicationException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
    } catch (NamingException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", serverCrt != null ? serverCrt.getSubjectDN().getName() : host), ex);
    } catch (CertificateException ex) {
        throw new CoreException(ex.getLocalizedMessage(), ex);
    } finally {
        if (ldapContext != null) {
            try {
                ldapContext.close();
            } catch (NamingException e) {
                // Only log it.
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }
    return resultCertificate;
}
Also used : CommunicationException(javax.naming.CommunicationException) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) CertificateException(java.security.cert.CertificateException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InitialDirContext(javax.naming.directory.InitialDirContext) X509Certificate(java.security.cert.X509Certificate)

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