Search in sources :

Example 31 with DirContext

use of javax.naming.directory.DirContext in project geode by apache.

the class LdapUserAuthenticator method authenticate.

@Override
public Principal authenticate(final Properties credentials, final DistributedMember member) {
    final String userName = credentials.getProperty(UserPasswordAuthInit.USER_NAME);
    if (userName == null) {
        throw new AuthenticationFailedException("LdapUserAuthenticator: user name property [" + UserPasswordAuthInit.USER_NAME + "] not provided");
    }
    String password = credentials.getProperty(UserPasswordAuthInit.PASSWORD);
    if (password == null) {
        password = "";
    }
    final Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.ldap.LdapCtxFactory.class.getName());
    env.put(Context.PROVIDER_URL, this.ldapUrlScheme + this.ldapServer + '/' + this.baseDomainName);
    env.put(Context.SECURITY_PRINCIPAL, "uid=" + userName + "," + this.baseDomainName);
    env.put(Context.SECURITY_CREDENTIALS, password);
    try {
        final DirContext ctx = new InitialDirContext(env);
        ctx.close();
    } catch (Exception e) {
        throw new AuthenticationFailedException("LdapUserAuthenticator: Failure with provided username, password combination for user name: " + userName, e);
    }
    return new UsernamePrincipal(userName);
}
Also used : AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) Properties(java.util.Properties) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException)

Example 32 with DirContext

use of javax.naming.directory.DirContext in project jmeter by apache.

the class LdapExtClient method connect.

/**
     * connect to server
     *
     * @param host
     *            name of the server to connect
     * @param port
     *            port of the server to connect
     * @param rootdn
     *            base of the tree to operate on
     * @param username
     *            name of the user to use for binding
     * @param password
     *            password to use for binding
     * @param connTimeOut
     *            connection timeout for connecting the server see
     *            "com.sun.jndi.ldap.connect.timeout"
     * @param secure
     *            flag whether ssl should be used
     * @return newly created {@link DirContext}
     * @exception NamingException
     *                when creating the {@link DirContext} fails
     */
public static DirContext connect(String host, String port, String rootdn, String username, String password, String connTimeOut, boolean secure) throws NamingException {
    DirContext dirContext;
    Hashtable<String, String> env = new Hashtable<>();
    // $NON-NLS-1$
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    StringBuilder sb = new StringBuilder(80);
    if (secure) {
        // $NON-NLS-1$
        sb.append("ldaps://");
    } else {
        // $NON-NLS-1$
        sb.append("ldap://");
    }
    sb.append(host);
    if (port.length() > 0) {
        // $NON-NLS-1$
        sb.append(":");
        sb.append(port);
    }
    // $NON-NLS-1$
    sb.append("/");
    sb.append(rootdn);
    env.put(Context.PROVIDER_URL, sb.toString());
    // $NON-NLS-1$
    log.info("prov_url= " + env.get(Context.PROVIDER_URL));
    if (connTimeOut.length() > 0) {
        // $NON-NLS-1$
        env.put("com.sun.jndi.ldap.connect.timeout", connTimeOut);
    }
    // $NON-NLS-1$
    env.put(Context.REFERRAL, "throw");
    // $NON-NLS-1$  // $NON-NLS-2$
    env.put("java.naming.batchsize", "0");
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.SECURITY_PRINCIPAL, username);
    dirContext = new InitialDirContext(env);
    return dirContext;
}
Also used : Hashtable(java.util.Hashtable) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 33 with DirContext

use of javax.naming.directory.DirContext in project jmeter by apache.

the class LDAPExtSampler method addTest.

/***************************************************************************
     * This will do the add test for the User defined TestCase
     *
     **************************************************************************/
private void addTest(DirContext dirContext, SampleResult res) throws NamingException {
    try {
        res.sampleStart();
        DirContext ctx = LdapExtClient.createTest(dirContext, getUserAttributes(), getBaseEntryDN());
        // the createTest() method creates an extra context which needs to be closed
        ctx.close();
    } finally {
        res.sampleEnd();
    }
}
Also used : DirContext(javax.naming.directory.DirContext)

Example 34 with DirContext

use of javax.naming.directory.DirContext in project karaf by apache.

the class LDAPBackingEngine method listUsers.

@Override
public List<UserPrincipal> listUsers() {
    DirContext context = null;
    ArrayList<UserPrincipal> users = new ArrayList<>();
    try {
        context = cache.open();
        SearchControls controls = new SearchControls();
        if (options.getUserSearchSubtree()) {
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        } else {
            controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        }
        String filter = options.getUserFilter();
        filter = filter.replaceAll(Pattern.quote("%u"), "*");
        filter = filter.replace("\\", "\\\\");
        LOGGER.debug("Looking for the users in LDAP with ");
        LOGGER.debug("  base DN: " + options.getUserBaseDn());
        LOGGER.debug("  filter: " + filter);
        NamingEnumeration namingEnumeration = context.search(options.getUserBaseDn(), filter, controls);
        try {
            while (namingEnumeration.hasMore()) {
                SearchResult result = (SearchResult) namingEnumeration.next();
                // We need to do the following because slashes are handled badly. For example, when searching
                // for a user with lots of special characters like cn=admin,=+<>#;\
                // SearchResult contains 2 different results:
                //
                // SearchResult.getName = cn=admin\,\=\+\<\>\#\;\\\\
                // SearchResult.getNameInNamespace = cn=admin\,\=\+\<\>#\;\\,ou=people,dc=example,dc=com
                //
                // the second escapes the slashes correctly.
                String userDNNamespace = result.getNameInNamespace();
                // handle case where cn, ou, dc case doesn't match
                int indexOfUserBaseDN = userDNNamespace.toLowerCase().indexOf("," + options.getUserBaseDn().toLowerCase());
                String userDN = (indexOfUserBaseDN > 0) ? userDNNamespace.substring(0, indexOfUserBaseDN) : result.getName();
                // we need to pull out the cn=, uid=, ect.. from the user name to get the actual user name
                String userName = userDN;
                if (userDN.contains("="))
                    userName = userDN.split("=")[1];
                users.add(new UserPrincipal(userName));
            }
        } finally {
            if (namingEnumeration != null) {
                try {
                    namingEnumeration.close();
                } catch (NamingException e) {
                // Ignore
                }
            }
        }
        return users;
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal)

Example 35 with DirContext

use of javax.naming.directory.DirContext in project karaf by apache.

the class LDAPCache method doGetUserDnAndNamespace.

protected String[] doGetUserDnAndNamespace(String user) throws NamingException {
    DirContext context = open();
    SearchControls controls = new SearchControls();
    if (options.getUserSearchSubtree()) {
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    } else {
        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    }
    String filter = options.getUserFilter();
    filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement(user));
    filter = filter.replace("\\", "\\\\");
    LOGGER.debug("Looking for the user in LDAP with ");
    LOGGER.debug("  base DN: " + options.getUserBaseDn());
    LOGGER.debug("  filter: " + filter);
    NamingEnumeration namingEnumeration = context.search(options.getUserBaseDn(), filter, controls);
    try {
        if (!namingEnumeration.hasMore()) {
            LOGGER.warn("User " + user + " not found in LDAP.");
            return null;
        }
        LOGGER.debug("Found the user DN.");
        SearchResult result = (SearchResult) namingEnumeration.next();
        // We need to do the following because slashes are handled badly. For example, when searching
        // for a user with lots of special characters like cn=admin,=+<>#;\
        // SearchResult contains 2 different results:
        //
        // SearchResult.getName = cn=admin\,\=\+\<\>\#\;\\\\
        // SearchResult.getNameInNamespace = cn=admin\,\=\+\<\>#\;\\,ou=people,dc=example,dc=com
        //
        // the second escapes the slashes correctly.
        String userDNNamespace = result.getNameInNamespace();
        // handle case where cn, ou, dc case doesn't match
        int indexOfUserBaseDN = userDNNamespace.toLowerCase().indexOf("," + options.getUserBaseDn().toLowerCase());
        String userDN = (indexOfUserBaseDN > 0) ? userDNNamespace.substring(0, indexOfUserBaseDN) : result.getName();
        return new String[] { userDN, userDNNamespace };
    } finally {
        if (namingEnumeration != null) {
            try {
                namingEnumeration.close();
            } catch (NamingException e) {
            // Ignore
            }
        }
    }
}
Also used : SearchControls(javax.naming.directory.SearchControls) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) EventDirContext(javax.naming.event.EventDirContext) DirContext(javax.naming.directory.DirContext)

Aggregations

DirContext (javax.naming.directory.DirContext)111 NamingException (javax.naming.NamingException)51 InitialDirContext (javax.naming.directory.InitialDirContext)43 SearchResult (javax.naming.directory.SearchResult)27 SearchControls (javax.naming.directory.SearchControls)24 Attributes (javax.naming.directory.Attributes)21 Attribute (javax.naming.directory.Attribute)17 IOException (java.io.IOException)16 NamingEnumeration (javax.naming.NamingEnumeration)16 Hashtable (java.util.Hashtable)14 Test (org.junit.Test)14 DistinguishedName (org.springframework.ldap.core.DistinguishedName)11 ProxyDirContext (org.apache.naming.resources.ProxyDirContext)10 WebDirContext (org.apache.naming.resources.WebDirContext)9 ArrayList (java.util.ArrayList)8 BaseDirContext (org.apache.naming.resources.BaseDirContext)8 FileDirContext (org.apache.naming.resources.FileDirContext)8 WARDirContext (org.apache.naming.resources.WARDirContext)8 Name (javax.naming.Name)7 BasicAttribute (javax.naming.directory.BasicAttribute)7