Search in sources :

Example 1 with LdapContext

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

the class LdapProducer method process.

public void process(Exchange exchange) throws Exception {
    String filter = exchange.getIn().getBody(String.class);
    DirContext dirContext = getDirContext();
    try {
        // could throw NamingException
        List<SearchResult> data;
        if (pageSize == null) {
            data = simpleSearch(dirContext, filter);
        } else {
            if (!(dirContext instanceof LdapContext)) {
                throw new IllegalArgumentException("When using attribute 'pageSize' for a ldap endpoint, you must provide a LdapContext (subclass of DirContext)");
            }
            data = pagedSearch((LdapContext) dirContext, filter);
        }
        exchange.getOut().setBody(data);
        exchange.getOut().setHeaders(exchange.getIn().getHeaders());
        exchange.getOut().setAttachments(exchange.getIn().getAttachments());
    } finally {
        if (dirContext != null) {
            dirContext.close();
        }
    }
}
Also used : SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext) LdapContext(javax.naming.ldap.LdapContext)

Example 2 with LdapContext

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

the class LdapRouteTest method setup.

@Before
public void setup() throws Exception {
    // you can assign port number in the @CreateTransport annotation
    port = super.getLdapServer().getPort();
    LdapContext ctx = getWiredContext(ldapServer);
    SimpleRegistry reg = new SimpleRegistry();
    reg.put("localhost:" + port, ctx);
    camel = new DefaultCamelContext(reg);
    template = camel.createProducerTemplate();
}
Also used : SimpleRegistry(org.apache.camel.impl.SimpleRegistry) LdapContext(javax.naming.ldap.LdapContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Before(org.junit.Before)

Example 3 with LdapContext

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

the class JNDIRealm method createTlsDirContext.

/**
     * Create a tls enabled LdapContext and set the StartTlsResponse tls
     * instance variable.
     *
     * @param env
     *            Environment to use for context creation
     * @return configured {@link LdapContext}
     * @throws NamingException
     *             when something goes wrong while negotiating the connection
     */
private DirContext createTlsDirContext(Hashtable<String, String> env) throws NamingException {
    Map<String, Object> savedEnv = new HashMap<>();
    for (String key : Arrays.asList(Context.SECURITY_AUTHENTICATION, Context.SECURITY_CREDENTIALS, Context.SECURITY_PRINCIPAL, Context.SECURITY_PROTOCOL)) {
        Object entry = env.remove(key);
        if (entry != null) {
            savedEnv.put(key, entry);
        }
    }
    LdapContext result = null;
    try {
        result = new InitialLdapContext(env, null);
        tls = (StartTlsResponse) result.extendedOperation(new StartTlsRequest());
        if (getHostnameVerifier() != null) {
            tls.setHostnameVerifier(getHostnameVerifier());
        }
        if (getCipherSuitesArray() != null) {
            tls.setEnabledCipherSuites(getCipherSuitesArray());
        }
        try {
            SSLSession negotiate = tls.negotiate(getSSLSocketFactory());
            containerLog.debug(sm.getString("jndiRealm.negotiatedTls", negotiate.getProtocol()));
        } catch (IOException e) {
            throw new NamingException(e.getMessage());
        }
    } finally {
        if (result != null) {
            for (Map.Entry<String, Object> savedEntry : savedEnv.entrySet()) {
                result.addToEnvironment(savedEntry.getKey(), savedEntry.getValue());
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) InitialLdapContext(javax.naming.ldap.InitialLdapContext) SSLSession(javax.net.ssl.SSLSession) NamingException(javax.naming.NamingException) IOException(java.io.IOException) StartTlsRequest(javax.naming.ldap.StartTlsRequest) HashMap(java.util.HashMap) Map(java.util.Map) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 4 with LdapContext

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

the class LdapRealm method isUserMemberOfDynamicGroup.

boolean isUserMemberOfDynamicGroup(LdapName userLdapDn, String memberUrl, final LdapContextFactory ldapContextFactory) throws NamingException {
    if (memberUrl == null) {
        return false;
    }
    String[] tokens = memberUrl.split("\\?");
    if (tokens.length < 4) {
        return false;
    }
    String searchBaseString = tokens[0].substring(tokens[0].lastIndexOf("/") + 1);
    String searchScope = tokens[2];
    String searchFilter = tokens[3];
    LdapName searchBaseDn = new LdapName(searchBaseString);
    // do scope test
    if (searchScope.equalsIgnoreCase("base")) {
        log.debug("DynamicGroup SearchScope base");
        return false;
    }
    if (!userLdapDn.toString().endsWith(searchBaseDn.toString())) {
        return false;
    }
    if (searchScope.equalsIgnoreCase("one") && (userLdapDn.size() != searchBaseDn.size() - 1)) {
        log.debug("DynamicGroup SearchScope one");
        return false;
    }
    // search for the filter, substituting base with userDn
    // search for base_dn=userDn, scope=base, filter=filter
    LdapContext systemLdapCtx = null;
    systemLdapCtx = ldapContextFactory.getSystemLdapContext();
    boolean member = false;
    NamingEnumeration<SearchResult> searchResultEnum = null;
    try {
        searchResultEnum = systemLdapCtx.search(userLdapDn, searchFilter, searchScope.equalsIgnoreCase("sub") ? SUBTREE_SCOPE : ONELEVEL_SCOPE);
        if (searchResultEnum.hasMore()) {
            return true;
        }
    } finally {
        try {
            if (searchResultEnum != null) {
                searchResultEnum.close();
            }
        } finally {
            LdapUtils.closeContext(systemLdapCtx);
        }
    }
    return member;
}
Also used : SearchResult(javax.naming.directory.SearchResult) LdapContext(javax.naming.ldap.LdapContext) LdapName(javax.naming.ldap.LdapName)

Example 5 with LdapContext

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

the class LdapRealm method getRoles.

private Set<String> getRoles(PrincipalCollection principals, final LdapContextFactory ldapContextFactory) throws NamingException {
    final String username = (String) getAvailablePrincipal(principals);
    LdapContext systemLdapCtx = null;
    try {
        systemLdapCtx = ldapContextFactory.getSystemLdapContext();
        return rolesFor(principals, username, systemLdapCtx, ldapContextFactory);
    } catch (AuthenticationException ae) {
        ae.printStackTrace();
        return Collections.emptySet();
    } finally {
        LdapUtils.closeContext(systemLdapCtx);
    }
}
Also used : AuthenticationException(javax.naming.AuthenticationException) 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