use of javax.naming.directory.Attribute in project uPortal by Jasig.
the class SimpleLdapSecurityContext method getAttributeValue.
/*--------------------- Helper methods ---------------------*/
/**
* Return a single value of an attribute from possibly multiple values, grossly ignoring
* anything else. If there are no values, then return an empty string.
*
* @param attrs LDAP query results
* @param attribute LDAP attribute we are interested in
* @return a single value of the attribute
*/
private String getAttributeValue(Attributes attrs, int attribute) throws NamingException {
NamingEnumeration values = null;
String aValue = "";
if (!isAttribute(attribute))
return aValue;
Attribute attrib = attrs.get(attributes[attribute]);
if (attrib != null) {
for (values = attrib.getAll(); values.hasMoreElements(); ) {
aValue = (String) values.nextElement();
// take only the first attribute value
break;
}
}
return aValue;
}
use of javax.naming.directory.Attribute in project jdk8u_jdk by JetBrains.
the class Rdn method toAttributes.
/**
* Retrieves the {@link javax.naming.directory.Attributes Attributes}
* view of the type/value mappings contained in this Rdn.
*
* @return The non-null attributes containing the type/value
* mappings of this Rdn.
*/
public Attributes toAttributes() {
Attributes attrs = new BasicAttributes(true);
for (int i = 0; i < entries.size(); i++) {
RdnEntry entry = entries.get(i);
Attribute attr = attrs.put(entry.getType(), entry.getValue());
if (attr != null) {
attr.add(entry.getValue());
attrs.put(attr);
}
}
return attrs;
}
use of javax.naming.directory.Attribute in project gerrit by GerritCodeReview.
the class LdapType method guessType.
static LdapType guessType(final DirContext ctx) throws NamingException {
final Attributes rootAtts = ctx.getAttributes("");
Attribute supported = rootAtts.get("supportedCapabilities");
if (supported != null && (supported.contains("1.2.840.113556.1.4.800") || supported.contains("1.2.840.113556.1.4.1851"))) {
return new ActiveDirectory();
}
supported = rootAtts.get("supportedExtension");
if (supported != null && supported.contains("2.16.840.1.113730.3.8.10.1")) {
return new FreeIPA();
}
return RFC_2307;
}
use of javax.naming.directory.Attribute 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.Attribute in project nhin-d by DirectProject.
the class LdapPublicCertUtilImpl method ldapSearch.
/**
* Searches for certificates in public LDAP servers using the subject name.
* @param subjectName The subject's email address or domain name.
* @return Collection of certificates matching the LDAP query for the subject name.
*/
public Collection<X509Certificate> ldapSearch(String subjectName) {
final Collection<X509Certificate> retVal = new ArrayList<X509Certificate>();
String domainName;
// find by host
int index;
if ((index = subjectName.indexOf("@")) > -1)
domainName = subjectName.substring(index + 1);
else
domainName = subjectName;
final String lookupName = LDAP_SRV_PREFIX + domainName;
InitialDirContext ctx = null;
try {
ctx = getDirContext(lookupName);
if (ctx != null) {
// discover the naming contexts
List<String> dNs = getBaseNamingContexts(ctx);
if (!dNs.isEmpty()) {
for (String dn : dNs) {
NamingEnumeration<SearchResult> searchResult = ctx.search(dn, EMAIL_ATTRIBUTE + "=" + subjectName, getDefaultSearchControls());
while (searchResult != null && searchResult.hasMore()) {
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)
Attribute certAttribute = certAttributes.get(CERT_ATTRIBUTE_BINARY);
// binary modifier
if (certAttribute == null)
certAttribute = certAttributes.get(CERT_ATTRIBUTE);
if (certAttribute != null) {
NamingEnumeration<? extends Object> allValues = certAttribute.getAll();
// LDAP may contain a collection of certificates.
while (allValues.hasMoreElements()) {
byte[] rawCert = null;
Object obj = allValues.nextElement();
rawCert = (byte[]) obj;
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
final ByteArrayInputStream inputStream = new ByteArrayInputStream(rawCert);
try {
X509Certificate addCert = (X509Certificate) cf.generateCertificate(inputStream);
retVal.add(addCert);
} finally {
IOUtils.closeQuietly(inputStream);
}
}
}
}
}
}
}
}
}
} catch (Exception e) {
throw new NHINDException("", e);
} finally {
this.closeDirContext(ctx);
}
return retVal;
}
Aggregations