use of javax.naming.ldap.LdapName in project gitblit by gitblit.
the class X509Utils method getMetadata.
public static X509Metadata getMetadata(X509Certificate cert) {
Map<String, String> oids = new HashMap<String, String>();
try {
String dn = cert.getSubjectDN().getName();
LdapName ldapName = new LdapName(dn);
for (int i = 0; i < ldapName.size(); i++) {
String[] val = ldapName.get(i).trim().split("=", 2);
String oid = val[0].toUpperCase().trim();
String data = val[1].trim();
oids.put(oid, data);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
X509Metadata metadata = new X509Metadata(oids.get("CN"), "whocares");
metadata.oids.putAll(oids);
metadata.serialNumber = cert.getSerialNumber().toString();
metadata.notAfter = cert.getNotAfter();
metadata.notBefore = cert.getNotBefore();
metadata.emailAddress = metadata.getOID("E", null);
if (metadata.emailAddress == null) {
metadata.emailAddress = metadata.getOID("EMAILADDRESS", null);
}
return metadata;
}
use of javax.naming.ldap.LdapName in project gerrit by GerritCodeReview.
the class LdapGroupBackend method cnFor.
private static String cnFor(String dn) {
try {
LdapName name = new LdapName(dn);
if (!name.isEmpty()) {
String cn = name.get(name.size() - 1);
int index = cn.indexOf('=');
if (index >= 0) {
cn = cn.substring(index + 1);
}
return cn;
}
} catch (InvalidNameException e) {
logger.atWarning().withCause(e).log("Cannot parse LDAP dn for cn");
}
return dn;
}
use of javax.naming.ldap.LdapName in project tomee by apache.
the class TomEELDAPIdentityStore method getGroupsWithCallerDn.
private Set<String> getGroupsWithCallerDn(final LdapContext ldapContext, final String callerDn) {
if (StringUtils.isEmpty(callerDn)) {
return emptySet();
}
if (StringUtils.isEmpty(definition.groupSearchBase()) && StringUtils.isNotEmpty(definition.groupMemberOfAttribute())) {
Set<String> groups = null;
try {
final Attributes attributes = ldapContext.getAttributes(callerDn, new String[] { definition.groupMemberOfAttribute() });
final Attribute memberOfAttribute = attributes.get(definition.groupMemberOfAttribute());
groups = new HashSet<>();
if (memberOfAttribute != null) {
for (Object group : list(memberOfAttribute.getAll())) {
if (group != null) {
final LdapName dn = new LdapName(group.toString());
final Attribute attribute = dn.getRdn(dn.size() - 1).toAttributes().get(definition.groupNameAttribute());
if (attribute == null) {
throw new RuntimeException(definition.groupNameAttribute() + "does not match any group in DN: " + group.toString());
}
final String groupName = attribute.get(0).toString();
if (groupName != null) {
groups.add(groupName);
}
}
}
}
} catch (final NamingException e) {
// todo better exception handling
throw new RuntimeException(e);
}
return groups;
} else {
String filter = null;
if (StringUtils.isNotEmpty(definition.groupSearchFilter())) {
filter = format(definition.groupSearchFilter(), callerDn);
} else {
filter = format(DEFAULT_GROUP_FILTER, definition.groupMemberAttribute(), callerDn);
}
final List<SearchResult> searchResults = query(ldapContext, definition.groupSearchBase(), filter, getGroupSearchControls());
Set<String> groups = new HashSet<>();
try {
for (SearchResult searchResult : searchResults) {
Attribute attribute = searchResult.getAttributes().get(definition.groupNameAttribute());
if (attribute != null) {
for (Object group : list(attribute.getAll())) {
if (group != null) {
groups.add(group.toString());
}
}
}
}
} catch (final NamingException e) {
// todo better exception handling
throw new RuntimeException(e);
}
return groups;
}
}
use of javax.naming.ldap.LdapName in project midpoint by Evolveum.
the class BasicExpressionFunctions method composeDn.
/**
* Creates a valid LDAP distinguished name from the wide range of components. The method
* can be invoked in many ways, e.g.:
* <p>
* composeDn("cn","foo","o","bar")
* composeDn("cn","foo",new Rdn("o","bar"))
* composeDn(new Rdn("cn","foo"),"ou","baz",new Rdn("o","bar"))
* composeDn(new Rdn("cn","foo"),"ou","baz","o","bar")
* composeDn(new Rdn("cn","foo"),new LdapName("ou=baz,o=bar"))
* composeDn("cn","foo",new LdapName("ou=baz,o=bar"))
* <p>
* Note: the DN is not normalized. The case of the attribute names and white spaces are
* preserved.
*/
public static String composeDn(Object... components) throws InvalidNameException {
if (components == null) {
return null;
}
if (components.length == 0) {
return null;
}
if (components.length == 1 && components[0] == null) {
return null;
}
if (components.length == 1 && (components[0] instanceof String) && StringUtils.isBlank((String) (components[0]))) {
return null;
}
LinkedList<Rdn> rdns = new LinkedList<>();
String attrName = null;
for (Object component : components) {
if (attrName != null && !(component instanceof String || component instanceof PolyString || component instanceof PolyStringType)) {
throw new InvalidNameException("Invalid input to composeDn() function: expected string after '" + attrName + "' argument, but got " + MiscUtil.getClass(component));
}
if (component instanceof Rdn) {
rdns.addFirst((Rdn) component);
} else if (component instanceof PolyString) {
component = component.toString();
} else if (component instanceof PolyStringType) {
component = component.toString();
}
if (component instanceof String) {
if (attrName == null) {
attrName = (String) component;
} else {
rdns.addFirst(new Rdn(attrName, (String) component));
attrName = null;
}
}
if (component instanceof LdapName) {
rdns.addAll(0, ((LdapName) component).getRdns());
}
}
LdapName dn = new LdapName(rdns);
return dn.toString();
}
use of javax.naming.ldap.LdapName in project midpoint by Evolveum.
the class BasicExpressionFunctions method determineLdapSingleAttributeValue.
// We cannot have Collection<String> here. The generic type information will disappear at runtime and the scripts can pass
// anything that they find suitable. E.g. XPath is passing elements
public String determineLdapSingleAttributeValue(String dn, String attributeName, Collection<?> values) throws NamingException {
if (values == null || values.isEmpty()) {
return null;
}
Collection<String> stringValues = null;
// Determine item type, try to convert to strings
Object firstElement = values.iterator().next();
if (firstElement instanceof String) {
stringValues = (Collection) values;
} else if (firstElement instanceof Element) {
stringValues = new ArrayList<>(values.size());
for (Object value : values) {
Element element = (Element) value;
stringValues.add(element.getTextContent());
}
} else {
throw new IllegalArgumentException("Unexpected value type " + firstElement.getClass());
}
if (stringValues.size() == 1) {
return stringValues.iterator().next();
}
if (StringUtils.isBlank(dn)) {
throw new IllegalArgumentException("No dn argument specified, cannot determine which of " + values.size() + " values to use");
}
LdapName parsedDn = new LdapName(dn);
for (int i = 0; i < parsedDn.size(); i++) {
Rdn rdn = parsedDn.getRdn(i);
Attributes rdnAttributes = rdn.toAttributes();
NamingEnumeration<String> rdnIDs = rdnAttributes.getIDs();
while (rdnIDs.hasMore()) {
String rdnID = rdnIDs.next();
Attribute attribute = rdnAttributes.get(rdnID);
if (attributeName.equals(attribute.getID())) {
for (int j = 0; j < attribute.size(); j++) {
Object value = attribute.get(j);
if (stringValues.contains(value)) {
return (String) value;
}
}
}
}
}
// Fallback. No values in DN. Just return the first alphabetically-wise value.
return Collections.min(stringValues);
}
Aggregations