Search in sources :

Example 21 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 22 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project jetcd by coreos.

the class DnsSrvNameResolver method getServers.

@Override
protected List<ResolvedServerInfo> getServers() {
    try {
        DirContext ctx = new InitialDirContext(ENV);
        NamingEnumeration<?> resolved = ctx.getAttributes(name, ATTRIBUTE_IDS).get("srv").getAll();
        List<ResolvedServerInfo> servers = new LinkedList<>();
        while (resolved.hasMore()) {
            servers.add(srvRecordToServerInfo((String) resolved.next()));
        }
        return servers;
    } catch (Exception e) {
        LOGGER.warn("", e);
    }
    return Collections.emptyList();
}
Also used : DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) ResolvedServerInfo(io.grpc.ResolvedServerInfo) LinkedList(java.util.LinkedList)

Example 23 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project jetty.project by eclipse.

the class LdapLoginModule method bindingLogin.

/**
     * binding authentication check
     * This method of authentication works only if the user branch of the DIT (ldap tree)
     * has an ACI (access control instruction) that allow the access to any user or at least
     * for the user that logs in.
     *
     * @param username the user name
     * @param password the password
     * @return true always
     * @throws LoginException if unable to bind the login
     * @throws NamingException if failure to bind login
     */
public boolean bindingLogin(String username, Object password) throws LoginException, NamingException {
    SearchResult searchResult = findUser(username);
    String userDn = searchResult.getNameInNamespace();
    LOG.info("Attempting authentication: " + userDn);
    Hashtable<Object, Object> environment = getEnvironment();
    if (userDn == null || "".equals(userDn)) {
        throw new NamingException("username may not be empty");
    }
    environment.put(Context.SECURITY_PRINCIPAL, userDn);
    // RFC 4513 section 6.3.1, protect against ldap server implementations that allow successful binding on empty passwords
    if (password == null || "".equals(password)) {
        throw new NamingException("password may not be empty");
    }
    environment.put(Context.SECURITY_CREDENTIALS, password);
    DirContext dirContext = new InitialDirContext(environment);
    List<String> roles = getUserRolesByDn(dirContext, userDn);
    UserInfo userInfo = new UserInfo(username, null, roles);
    setCurrentUser(new JAASUserInfo(userInfo));
    setAuthenticated(true);
    return true;
}
Also used : SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 24 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project orientdb by orientechnologies.

the class OStorageRemote method parseServerURLs.

/**
   * Parse the URLs. Multiple URLs must be separated by semicolon (;)
   */
protected void parseServerURLs() {
    String lastHost = null;
    int dbPos = url.indexOf('/');
    if (dbPos == -1) {
        // SHORT FORM
        addHost(url);
        lastHost = url;
        name = url;
    } else {
        name = url.substring(url.lastIndexOf("/") + 1);
        for (String host : url.substring(0, dbPos).split(ADDRESS_SEPARATOR)) {
            lastHost = host;
            addHost(host);
        }
    }
    synchronized (serverURLs) {
        if (serverURLs.size() == 1 && OGlobalConfiguration.NETWORK_BINARY_DNS_LOADBALANCING_ENABLED.getValueAsBoolean()) {
            // LOOK FOR LOAD BALANCING DNS TXT RECORD
            final String primaryServer = lastHost;
            OLogManager.instance().debug(this, "Retrieving URLs from DNS '%s' (timeout=%d)...", primaryServer, OGlobalConfiguration.NETWORK_BINARY_DNS_LOADBALANCING_TIMEOUT.getValueAsInteger());
            try {
                final Hashtable<String, String> env = new Hashtable<String, String>();
                env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
                env.put("com.sun.jndi.ldap.connect.timeout", OGlobalConfiguration.NETWORK_BINARY_DNS_LOADBALANCING_TIMEOUT.getValueAsString());
                final DirContext ictx = new InitialDirContext(env);
                final String hostName = !primaryServer.contains(":") ? primaryServer : primaryServer.substring(0, primaryServer.indexOf(":"));
                final Attributes attrs = ictx.getAttributes(hostName, new String[] { "TXT" });
                final Attribute attr = attrs.get("TXT");
                if (attr != null) {
                    for (int i = 0; i < attr.size(); ++i) {
                        String configuration = (String) attr.get(i);
                        if (configuration.startsWith("\""))
                            configuration = configuration.substring(1, configuration.length() - 1);
                        if (configuration != null) {
                            serverURLs.clear();
                            final String[] parts = configuration.split(" ");
                            for (String part : parts) {
                                if (part.startsWith("s=")) {
                                    addHost(part.substring("s=".length()));
                                }
                            }
                        }
                    }
                }
            } catch (NamingException ignore) {
            }
        }
    }
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 25 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project presto by prestodb.

the class LdapFilter method authenticate.

private Principal authenticate(String user, String password) throws AuthenticationException {
    Map<String, String> environment = createEnvironment(user, password);
    InitialDirContext context = null;
    try {
        context = createDirContext(environment);
        checkForGroupMembership(user, context);
        log.debug("Authentication successful for user %s", user);
        return new LdapPrincipal(user);
    } catch (javax.naming.AuthenticationException e) {
        String formattedAsciiMessage = format("Invalid credentials: %s", JAVA_ISO_CONTROL.removeFrom(e.getMessage()));
        log.debug("Authentication failed for user [%s]. %s", user, e.getMessage());
        throw new AuthenticationException(UNAUTHORIZED, formattedAsciiMessage, e);
    } catch (NamingException e) {
        log.debug("Authentication failed", e.getMessage());
        throw new AuthenticationException(INTERNAL_SERVER_ERROR, "Authentication failed", e);
    } finally {
        closeContext(context);
    }
}
Also used : NamingException(javax.naming.NamingException) JndiUtils.getInitialDirContext(com.facebook.presto.server.security.util.jndi.JndiUtils.getInitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

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