use of javax.naming.directory.SearchControls in project cloudstack by apache.
the class OpenLdapUserManagerImpl method searchUsers.
@Override
public List<LdapUser> searchUsers(final String username, final LdapContext context) throws NamingException, IOException {
final SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(_ldapConfiguration.getScope());
searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
String basedn = _ldapConfiguration.getBaseDn();
if (StringUtils.isBlank(basedn)) {
throw new IllegalArgumentException("ldap basedn is not configured");
}
byte[] cookie = null;
int pageSize = _ldapConfiguration.getLdapPageSize();
context.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
final List<LdapUser> users = new ArrayList<LdapUser>();
NamingEnumeration<SearchResult> results;
do {
results = context.search(basedn, generateSearchFilter(username), searchControls);
while (results.hasMoreElements()) {
final SearchResult result = results.nextElement();
if (!isUserDisabled(result)) {
users.add(createUser(result));
}
}
Control[] contextControls = context.getResponseControls();
if (contextControls != null) {
for (Control control : contextControls) {
if (control instanceof PagedResultsResponseControl) {
PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
cookie = prrc.getCookie();
}
}
} else {
s_logger.info("No controls were sent from the ldap server");
}
context.setRequestControls(new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
} while (cookie != null);
return users;
}
use of javax.naming.directory.SearchControls in project karaf by apache.
the class LDAPCache method open.
public synchronized DirContext open() throws NamingException {
if (isContextAlive()) {
return context;
}
clearCache();
context = new InitialDirContext(options.getEnv());
EventDirContext eventContext = ((EventDirContext) context.lookup(""));
final SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
if (!options.getDisableCache()) {
String filter = options.getUserFilter();
filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement("*"));
filter = filter.replace("\\", "\\\\");
eventContext.addNamingListener(options.getUserBaseDn(), filter, constraints, this);
filter = options.getRoleFilter();
if (filter != null) {
filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement("*"));
filter = filter.replaceAll(Pattern.quote("%dn"), Matcher.quoteReplacement("*"));
filter = filter.replaceAll(Pattern.quote("%fqdn"), Matcher.quoteReplacement("*"));
filter = filter.replace("\\", "\\\\");
eventContext.addNamingListener(options.getRoleBaseDn(), filter, constraints, this);
}
}
return context;
}
use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.
the class LdapCertUtilImpl method getDefaultSearchControls.
// /CLOVER:OFF
protected SearchControls getDefaultSearchControls() {
SearchControls ctls = new SearchControls();
ctls.setReturningObjFlag(true);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
ctls.setReturningAttributes(new String[] { ldapEnvironment.getReturningCertAttribute() });
return ctls;
}
use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.
the class LdapCertUtilImpl method ldapSearch.
public Collection<X509Certificate> ldapSearch(String subjectName) {
DirContext ctx = null;
try {
ctx = getInitialDirContext(ldapEnvironment.getEnv());
final SearchControls ctls = getDefaultSearchControls();
NamingEnumeration<SearchResult> searchResult = ctx.search(ldapEnvironment.getLdapSearchBase(), ldapEnvironment.getLdapSearchAttribute() + "=" + subjectName, ctls);
ArrayList<X509Certificate> certificates = new ArrayList<X509Certificate>();
while (searchResult != null && searchResult.hasMoreElements()) {
final SearchResult certEntry = searchResult.nextElement();
if (certEntry != null) {
final Attributes certAttributes = certEntry.getAttributes();
if (certAttributes != null) {
// get only the returning cert attribute (for now, ignore all other attributes)
final Attribute certAttribute = certAttributes.get(ldapEnvironment.getReturningCertAttribute());
if (certAttribute != null) {
NamingEnumeration<? extends Object> allValues = certAttribute.getAll();
// LDAP may contain a collection of certificates.
while (allValues.hasMoreElements()) {
String ksBytes = (String) allValues.nextElement();
Base64 base64 = new Base64();
byte[] decode = base64.decode(ksBytes.getBytes());
ByteArrayInputStream inputStream = new ByteArrayInputStream(decode);
if (certificateFormat.equalsIgnoreCase("pkcs12")) {
try {
processPKCS12FileFormatAndAddToCertificates(inputStream, certificates);
} catch (Exception e) {
closeDirContext(ctx);
throw new NHINDException("", e);
}
} else {
if (certificateFormat.equalsIgnoreCase("X.509") || certificateFormat.equalsIgnoreCase("X509")) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate addCert = (X509Certificate) cf.generateCertificate(inputStream);
certificates.add(addCert);
} else {
closeDirContext(ctx);
throw new NHINDException("Invalid certificate format requested");
}
}
}
}
}
}
}
return certificates;
} catch (NamingException e) {
closeDirContext(ctx);
throw new NHINDException("", e);
} catch (CertificateException e) {
closeDirContext(ctx);
throw new NHINDException("", e);
}
}
use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.
the class LdapPublicCertUtilImpl method getDefaultSearchControls.
/**
* Gets the search controls for searching the LDAP server. The default controls use SUBTREE_SCOPE and
* return the userSMIMECertificate attribute.
* @return A search control object.
*/
protected SearchControls getDefaultSearchControls() {
SearchControls ctls = new SearchControls();
ctls.setReturningObjFlag(true);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
ctls.setReturningAttributes(new String[] { CERT_ATTRIBUTE, CERT_ATTRIBUTE_BINARY });
return ctls;
}
Aggregations