com.floragunn.dlic.auth.ldap.util.LdapHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dlic-search-guard-authbackend-ldap Show documentation
Show all versions of dlic-search-guard-authbackend-ldap Show documentation
Provide access control related features for elasticsearch
/*
* Copyright 2016 by floragunn UG (haftungsbeschränkt) - All rights reserved
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed here is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* This software is free of charge for non-commercial and academic use.
* For commercial use in a production environment you have to obtain a license
* from https://floragunn.com
*
*/
package com.floragunn.dlic.auth.ldap.util;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.ldaptive.Connection;
import org.ldaptive.DerefAliases;
import org.ldaptive.LdapEntry;
import org.ldaptive.LdapException;
import org.ldaptive.Response;
import org.ldaptive.SearchOperation;
import org.ldaptive.SearchRequest;
import org.ldaptive.SearchResult;
import org.ldaptive.SearchScope;
import org.ldaptive.referral.SearchReferralHandler;
public class LdapHelper {
private static final ESLogger log = Loggers.getLogger(LdapHelper.class);
public static List search(final Connection conn, final String baseDn, final String filter, final SearchScope searchScope,
final String... attributes) throws LdapException {
final List entries = new ArrayList<>();
final SearchRequest request = new SearchRequest(baseDn, filter);
request.setReferralHandler(new SearchReferralHandler());
request.setSearchScope(searchScope);
request.setDerefAliases(DerefAliases.ALWAYS);
request.setReturnAttributes(attributes);
final SearchOperation search = new SearchOperation(conn);
// referrals will be followed to build the response
final Response r = search.execute(request);
final org.ldaptive.SearchResult result = r.getResult();
entries.addAll(result.getEntries());
return entries;
}
public static LdapEntry lookup(final Connection conn, final String dn) throws LdapException {
final List entries = search(conn, dn, "(objectClass=*)", SearchScope.OBJECT);
if (entries.size() == 1) {
return entries.get(0);
} else {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy