Search in sources :

Example 21 with LdapContext

use of javax.naming.ldap.LdapContext in project jackrabbit-oak by apache.

the class InternalLdapServer method addMembers.

public void addMembers(String groupDN, Iterable<String> memberDNs) throws Exception {
    LdapContext ctxt = getWiredContext();
    Attribute attr = new BasicAttribute("member");
    for (String dn : memberDNs) {
        attr.add(dn);
    }
    BasicAttributes attrs = new BasicAttributes();
    attrs.put(attr);
    ctxt.modifyAttributes(groupDN, DirContext.ADD_ATTRIBUTE, attrs);
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) LdapContext(javax.naming.ldap.LdapContext)

Example 22 with LdapContext

use of javax.naming.ldap.LdapContext in project jackrabbit-oak by apache.

the class InternalLdapServer method removeMember.

public void removeMember(String groupDN, String memberDN) throws Exception {
    LdapContext ctxt = getWiredContext();
    BasicAttributes attrs = new BasicAttributes();
    attrs.put("member", memberDN);
    ctxt.modifyAttributes(groupDN, DirContext.REMOVE_ATTRIBUTE, attrs);
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) LdapContext(javax.naming.ldap.LdapContext)

Example 23 with LdapContext

use of javax.naming.ldap.LdapContext in project wildfly by wildfly.

the class LdapUrlTestServlet method runSearch.

/**
     * Try to search in LDAP with search base containing URL. Also try to retrieve RequestControls from LdapContext.
     *
     * @param hostname
     * @return
     * @throws Exception
     */
public static String runSearch(final String hostname, boolean testLdapCtx) throws Exception {
    final StringBuilder result = new StringBuilder();
    final String ldapUrl = "ldap://" + (hostname == null ? "localhost" : hostname) + ":10389";
    final 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");
    env.put(Context.PROVIDER_URL, ldapUrl);
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    final SearchControls ctl = new SearchControls();
    ctl.setReturningAttributes(new String[] { "cn" });
    DirContext dirCtx = null;
    if (testLdapCtx) {
        // LdapContext must also work
        LdapContext ldapCtx = new InitialLdapContext(env, null);
        // next line tests if the LdapContext works
        ldapCtx.getRequestControls();
        dirCtx = ldapCtx;
    } else {
        dirCtx = new InitialDirContext(env);
    }
    final NamingEnumeration<SearchResult> nenum = dirCtx.search(ldapUrl + "/dc=jboss,dc=org", "(uid=jduke)", ctl);
    while (nenum.hasMore()) {
        SearchResult sr = nenum.next();
        Attributes attrs = sr.getAttributes();
        result.append("cn=").append(attrs.get("cn").get());
    }
    dirCtx.close();
    return result.toString();
}
Also used : Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 24 with LdapContext

use of javax.naming.ldap.LdapContext in project cloudstack by apache.

the class LdapManagerImpl method addConfiguration.

@Override
public LdapConfigurationResponse addConfiguration(final String hostname, final int port) throws InvalidParameterValueException {
    LdapConfigurationVO configuration = _ldapConfigurationDao.findByHostname(hostname);
    if (configuration == null) {
        LdapContext context = null;
        try {
            final String providerUrl = "ldap://" + hostname + ":" + port;
            context = _ldapContextFactory.createBindContext(providerUrl);
            configuration = new LdapConfigurationVO(hostname, port);
            _ldapConfigurationDao.persist(configuration);
            s_logger.info("Added new ldap server with hostname: " + hostname);
            return new LdapConfigurationResponse(hostname, port);
        } catch (NamingException | IOException e) {
            s_logger.debug("NamingException while doing an LDAP bind", e);
            throw new InvalidParameterValueException("Unable to bind to the given LDAP server");
        } finally {
            closeContext(context);
        }
    } else {
        throw new InvalidParameterValueException("Duplicate configuration");
    }
}
Also used : LdapConfigurationResponse(org.apache.cloudstack.api.response.LdapConfigurationResponse) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NamingException(javax.naming.NamingException) IOException(java.io.IOException) LdapContext(javax.naming.ldap.LdapContext)

Example 25 with LdapContext

use of javax.naming.ldap.LdapContext in project cloudstack by apache.

the class LdapManagerImpl method getUser.

@Override
public LdapUser getUser(final String username) throws NoLdapUserMatchingQueryException {
    LdapContext context = null;
    try {
        context = _ldapContextFactory.createBindContext();
        final String escapedUsername = LdapUtils.escapeLDAPSearchFilter(username);
        return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUser(escapedUsername, context);
    } catch (NamingException | IOException e) {
        s_logger.debug("ldap Exception: ", e);
        throw new NoLdapUserMatchingQueryException("No Ldap User found for username: " + username);
    } finally {
        closeContext(context);
    }
}
Also used : NamingException(javax.naming.NamingException) IOException(java.io.IOException) LdapContext(javax.naming.ldap.LdapContext)

Aggregations

LdapContext (javax.naming.ldap.LdapContext)43 NamingException (javax.naming.NamingException)14 SearchResult (javax.naming.directory.SearchResult)13 NamingEnumeration (javax.naming.NamingEnumeration)10 SearchControls (javax.naming.directory.SearchControls)9 InitialLdapContext (javax.naming.ldap.InitialLdapContext)9 IOException (java.io.IOException)8 Attributes (javax.naming.directory.Attributes)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 Control (javax.naming.ldap.Control)6 Hashtable (java.util.Hashtable)5 SortControl (javax.naming.ldap.SortControl)4 JndiLdapContextFactory (org.apache.shiro.realm.ldap.JndiLdapContextFactory)4 Attribute (javax.naming.directory.Attribute)3 BasicAttribute (javax.naming.directory.BasicAttribute)3 BasicAttributes (javax.naming.directory.BasicAttributes)3 DirContext (javax.naming.directory.DirContext)3 StartTlsRequest (javax.naming.ldap.StartTlsRequest)3 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)3