Search in sources :

Example 11 with InitialDirContext

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

the class LDAPUserManager method findUserByQueryCriteria.

@Override
public List<User> findUserByQueryCriteria(final UserQueryImpl query, final Page page) {
    if (query.getId() != null) {
        List<User> result = new ArrayList<User>();
        result.add(findUserById(query.getId()));
        return result;
    } else if (query.getFullNameLike() != null) {
        final String fullNameLike = query.getFullNameLike().replaceAll("%", "");
        LDAPTemplate ldapTemplate = new LDAPTemplate(ldapConfigurator);
        return ldapTemplate.execute(new LDAPCallBack<List<User>>() {

            public List<User> executeInContext(InitialDirContext initialDirContext) {
                List<User> result = new ArrayList<User>();
                try {
                    String searchExpression = ldapConfigurator.getLdapQueryBuilder().buildQueryByFullNameLike(ldapConfigurator, fullNameLike);
                    String baseDn = ldapConfigurator.getUserBaseDn() != null ? ldapConfigurator.getUserBaseDn() : ldapConfigurator.getBaseDn();
                    NamingEnumeration<?> namingEnum = initialDirContext.search(baseDn, searchExpression, createSearchControls());
                    while (namingEnum.hasMore()) {
                        SearchResult searchResult = (SearchResult) namingEnum.next();
                        UserEntity user = new UserEntity();
                        mapSearchResultToUser(searchResult, user);
                        result.add(user);
                    }
                    namingEnum.close();
                } catch (NamingException ne) {
                    logger.debug("Could not execute LDAP query: " + ne.getMessage(), ne);
                    return null;
                }
                return result;
            }
        });
    } else {
        throw new ActivitiIllegalArgumentException("Query is currently not supported by LDAPUserManager.");
    }
}
Also used : User(org.activiti.engine.identity.User) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ArrayList(java.util.ArrayList) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) UserEntity(org.activiti.engine.impl.persistence.entity.UserEntity)

Example 12 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project perun by CESNET.

the class ExtSourceLdap method initContext.

protected void initContext() throws InternalErrorException {
    // Load mapping between LDAP attributes and Perun attributes
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    if (getAttributes().containsKey("referral")) {
        env.put(Context.REFERRAL, (String) getAttributes().get("referral"));
    }
    if (getAttributes().containsKey("url")) {
        env.put(Context.PROVIDER_URL, (String) getAttributes().get("url"));
    } else {
        throw new InternalErrorException("url attributes is required");
    }
    if (getAttributes().containsKey("user")) {
        env.put(Context.SECURITY_PRINCIPAL, (String) getAttributes().get("user"));
    }
    if (getAttributes().containsKey("password")) {
        env.put(Context.SECURITY_CREDENTIALS, (String) getAttributes().get("password"));
    }
    if (getAttributes().containsKey("filteredQuery")) {
        filteredQuery = (String) getAttributes().get("filteredQuery");
    }
    try {
        // ldapMapping contains entries like: firstName={givenName},lastName={sn},email={mail}
        if (getAttributes().get("ldapMapping") == null) {
            throw new InternalErrorException("ldapMapping attributes is required");
        }
        String[] ldapMapping = ((String) getAttributes().get("ldapMapping")).trim().split(",\n");
        mapping = new HashMap<String, String>();
        for (String entry : ldapMapping) {
            String[] values = entry.trim().split("=", 2);
            mapping.put(values[0].trim(), values[1].trim());
        }
        this.dirContext = new InitialDirContext(env);
    } catch (NamingException e) {
        log.error("LDAP exception during creating the context.");
        throw new InternalErrorException(e);
    }
}
Also used : Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 13 with InitialDirContext

use of javax.naming.directory.InitialDirContext 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 14 with InitialDirContext

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

the class LdapClient method connect.

/**
     * Connect to server.
     * 
     * @param host
     *            name of the ldap server
     * @param port
     *            port of the ldap server
     * @param rootdn
     *            base dn to start ldap operations from
     * @param username
     *            user name to use for binding
     * @param password
     *            password to use for binding
     * @throws NamingException
     *             if {@link InitialDirContext} can not be build using the above
     *             parameters
     */
public void connect(String host, String port, String rootdn, String username, String password) throws NamingException {
    Hashtable<String, String> env = new Hashtable<>();
    //$NON-NLS-1$
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    //$NON-NLS-1$ $NON-NLS-2$ $NON-NLS-3$
    env.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port + "/" + rootdn);
    //$NON-NLS-1$
    env.put(Context.REFERRAL, "throw");
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.SECURITY_PRINCIPAL, username);
    dirContext = new InitialDirContext(env);
}
Also used : Hashtable(java.util.Hashtable) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 15 with InitialDirContext

use of javax.naming.directory.InitialDirContext 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)

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