Search in sources :

Example 6 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 7 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project jforum2 by rafaelsteil.

the class LDAPAuthenticator method validateLogin.

/**
	 * @see net.jforum.sso.LoginAuthenticator#validateLogin(java.lang.String, java.lang.String, java.util.Map)
	 */
public User validateLogin(String username, String password, Map extraParams) {
    Hashtable environment = this.prepareEnvironment();
    StringBuffer principal = new StringBuffer(256).append(SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_PREFIX)).append(username).append(',').append(SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_SUFFIX));
    environment.put(Context.SECURITY_PRINCIPAL, principal.toString());
    environment.put(Context.SECURITY_CREDENTIALS, password);
    DirContext dir = null;
    try {
        dir = new InitialDirContext(environment);
        String lookupPrefix = SystemGlobals.getValue(ConfigKeys.LDAP_LOOKUP_PREFIX);
        String lookupSuffix = SystemGlobals.getValue(ConfigKeys.LDAP_LOOKUP_SUFFIX);
        if (lookupPrefix == null || lookupPrefix.length() == 0) {
            lookupPrefix = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_PREFIX);
        }
        if (lookupSuffix == null || lookupSuffix.length() == 0) {
            lookupSuffix = SystemGlobals.getValue(ConfigKeys.LDAP_LOGIN_SUFFIX);
        }
        String lookupPrincipal = lookupPrefix + username + "," + lookupSuffix;
        Attribute att = dir.getAttributes(lookupPrincipal).get(SystemGlobals.getValue(ConfigKeys.LDAP_FIELD_EMAIL));
        SSOUtils utils = new SSOUtils();
        if (!utils.userExists(username)) {
            String email = att != null ? (String) att.get() : "noemail";
            utils.register("ldap", email);
        }
        return utils.getUser();
    } catch (AuthenticationException e) {
        return null;
    } catch (NamingException e) {
        return null;
    } finally {
        if (dir != null) {
            try {
                dir.close();
            } catch (NamingException e) {
            //close jndi context
            }
        }
    }
}
Also used : Attribute(javax.naming.directory.Attribute) AuthenticationException(javax.naming.AuthenticationException) Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 8 with InitialDirContext

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

the class LdapPublicCertUtilImpl method getDirContext.

/**
	 * Creates the LDAP directory context from an SRV lookup name.
	 * @param lookupName The SRV record name used to discover the LDAP services.
	 * @return And InitialDirContext object that is connected to a discoverd LDAP service.
	 * @throws Exception
	 */
protected InitialDirContext getDirContext(String lookupName) throws Exception {
    // try the configured servers first
    InitialDirContext ctx = null;
    Lookup lu = LookupFactory.getFactory().getInstance(new Name(lookupName), Type.SRV);
    // default retries is 3, limit to 2
    lu.setResolver(createExResolver(servers.toArray(new String[servers.size()]), 2, 3));
    final Record[] retRecords = lu.run();
    if (retRecords != null && retRecords.length > 0) {
        String ldapURL = createLDAPUrl(retRecords);
        final Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, LDAP_FACTORY);
        env.put(Context.PROVIDER_URL, ldapURL);
        env.put(Context.SECURITY_AUTHENTICATION, "none");
        env.put(LDAP_TIMEOUT, DEFAULT_LDAP_TIMEOUT);
        env.put(LDAP_CONNECT_TIMEOUT, DEFAULT_LDAP_CONNECT_TIMEOUT);
        env.put("java.naming.ldap.attributes.binary", "userCertificate, usercertificate");
        ctx = new InitialDirContext(env);
    }
    return ctx;
}
Also used : Hashtable(java.util.Hashtable) Lookup(org.nhindirect.stagent.cert.impl.util.Lookup) Record(org.xbill.DNS.Record) SRVRecord(org.xbill.DNS.SRVRecord) InitialDirContext(javax.naming.directory.InitialDirContext) Name(org.xbill.DNS.Name)

Example 9 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 10 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project OpenAM by OpenRock.

the class AMSetupServlet method getLdapHostAndPort.

// Method to get hostname and port number with the
// provided Domain Name for Active Directory user data store.
private String[] getLdapHostAndPort(String domainName) throws NamingException, IOException {
    if (!domainName.endsWith(".")) {
        domainName += '.';
    }
    DirContext ictx;
    // The resource record type A is defined in RFC 1035.
    try {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        ictx = new InitialDirContext(env);
        Attributes attributes = ictx.getAttributes(domainName, new String[] { "A" });
        Attribute attrib = attributes.get("A");
        if (attrib == null) {
            throw new NamingException();
        }
    } catch (NamingException e) {
        // throw exception.
        throw e;
    }
    // then look for the LDAP server
    String serverHostName;
    String serverPortStr;
    String ldapServer = "_ldap._tcp." + domainName;
    try {
        // Attempting to resolve ldapServer to SRV record.
        // This is a mechanism defined in MSDN, querying
        // SRV records for _ldap._tcp.DOMAINNAME.
        // and get host and port from domain.
        Attributes attributes = ictx.getAttributes(ldapServer, new String[] { "SRV" });
        Attribute attr = attributes.get("SRV");
        if (attr == null) {
            throw new NamingException();
        }
        String[] srv = attr.get().toString().split(" ");
        String hostNam = srv[3];
        serverHostName = hostNam.substring(0, hostNam.length() - 1);
        serverPortStr = srv[2];
    } catch (NamingException e) {
        // throw exception.
        throw e;
    }
    // try to connect to LDAP port to make sure this machine
    // has LDAP service
    int serverPort = Integer.parseInt(serverPortStr);
    try {
        new Socket(serverHostName, serverPort).close();
    } catch (IOException e) {
        throw e;
    }
    String[] hostAndPort = new String[2];
    hostAndPort[0] = serverHostName;
    hostAndPort[1] = serverPortStr;
    return hostAndPort;
}
Also used : Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) IOException(java.io.IOException) Socket(java.net.Socket)

Aggregations

InitialDirContext (javax.naming.directory.InitialDirContext)37 NamingException (javax.naming.NamingException)18 DirContext (javax.naming.directory.DirContext)18 Hashtable (java.util.Hashtable)17 Attributes (javax.naming.directory.Attributes)9 IOException (java.io.IOException)8 Attribute (javax.naming.directory.Attribute)7 Properties (java.util.Properties)5 Socket (java.net.Socket)4 SearchResult (javax.naming.directory.SearchResult)4 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 NamingEnumeration (javax.naming.NamingEnumeration)2 SearchControls (javax.naming.directory.SearchControls)2 InitialLdapContext (javax.naming.ldap.InitialLdapContext)2 LdapContext (javax.naming.ldap.LdapContext)2 JndiUtils.getInitialDirContext (com.facebook.presto.server.security.util.jndi.JndiUtils.getInitialDirContext)1 AuthenticationFailedException (com.google.gerrit.server.account.AuthenticationFailedException)1 LdapCtx (com.sun.jndi.ldap.LdapCtx)1