use of javax.naming.directory.Attributes in project cxf by apache.
the class LdapCertificateRepo method getCertificatesFromLdap.
private List<X509Certificate> getCertificatesFromLdap(String tmpRootDN, String tmpFilter, String tmpAttrName) {
try {
List<X509Certificate> certificates = new ArrayList<>();
NamingEnumeration<SearchResult> answer = ldapSearch.searchSubTree(tmpRootDN, tmpFilter);
while (answer.hasMore()) {
SearchResult sr = answer.next();
Attributes attrs = sr.getAttributes();
Attribute attribute = attrs.get(tmpAttrName);
if (attribute != null) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream((byte[]) attribute.get()));
certificates.add(certificate);
}
}
return certificates;
} catch (CertificateException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (NamingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
use of javax.naming.directory.Attributes in project cxf by apache.
the class LdapCertificateRepo method getCRLsFromLdap.
private List<X509CRL> getCRLsFromLdap(String tmpRootDN, String tmpFilter, String tmpAttrName) {
try {
List<X509CRL> crls = new ArrayList<>();
NamingEnumeration<SearchResult> answer = ldapSearch.searchSubTree(tmpRootDN, tmpFilter);
while (answer.hasMore()) {
SearchResult sr = answer.next();
Attributes attrs = sr.getAttributes();
Attribute attribute = attrs.get(tmpAttrName);
if (attribute != null) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509CRL crl = (X509CRL) cf.generateCRL(new ByteArrayInputStream((byte[]) attribute.get()));
crls.add(crl);
}
}
return crls;
} catch (CertificateException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (NamingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (CRLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
use of javax.naming.directory.Attributes in project Payara by payara.
the class ProxyDirContext method cacheLoad.
/**
* Load entry into cache.
*/
protected void cacheLoad(CacheEntry entry) {
String name = entry.name;
// Retrieve missing info
boolean exists = true;
// Retrieving attributes
if (entry.attributes == null) {
try {
Attributes attributes = dirContext.getAttributes(entry.name);
if (!(attributes instanceof ResourceAttributes)) {
entry.attributes = new ResourceAttributes(attributes);
} else {
entry.attributes = (ResourceAttributes) attributes;
}
} catch (NamingException e) {
exists = false;
}
}
// Retriving object
if ((exists) && (entry.resource == null) && (entry.context == null)) {
try {
Object object = dirContext.lookup(name);
if (object instanceof InputStream) {
entry.resource = new Resource((InputStream) object);
} else if (object instanceof DirContext) {
entry.context = (DirContext) object;
} else if (object instanceof Resource) {
entry.resource = (Resource) object;
} else {
entry.resource = new Resource(new ByteArrayInputStream(object.toString().getBytes(Charset.defaultCharset())));
}
} catch (NamingException e) {
exists = false;
}
}
// Load object content
if ((exists) && (entry.resource != null) && (entry.resource.getContent() == null) && (entry.attributes.getContentLength() >= 0) && (entry.attributes.getContentLength() < (cacheObjectMaxSize * 1024L))) {
int length = (int) entry.attributes.getContentLength();
// The entry size is 1 + the resource size in KB, if it will be
// cached
entry.size += (entry.attributes.getContentLength() / 1024);
InputStream is = null;
try {
is = entry.resource.streamContent();
int pos = 0;
byte[] b = new byte[length];
while (pos < length) {
int n = is.read(b, pos, length - pos);
if (n < 0)
break;
pos = pos + n;
}
entry.resource.setContent(b);
} catch (IOException e) {
// Ignore
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
// Ignore
}
}
}
// Set existence flag
entry.exists = exists;
// Set timestamp
entry.timestamp = System.currentTimeMillis() + cacheTTL;
// Add new entry to cache
synchronized (cache) {
// Check cache size, and remove elements if too big
if ((cache.lookup(name) == null) && cache.allocate(entry.size)) {
cache.load(entry);
}
}
}
use of javax.naming.directory.Attributes in project Payara by payara.
the class ProxyDirContext method getAttributes.
/**
* Retrieves all of the attributes associated with a named object.
*
* @return the set of attributes associated with name
* @param name the name of the object from which to retrieve attributes
* @exception NamingException if a naming exception is encountered
*/
public Attributes getAttributes(String name) throws NamingException {
CacheEntry entry = cacheLookup(name);
if (entry != null) {
if (!entry.exists) {
throw notFoundException;
}
return entry.attributes;
}
Attributes attributes = dirContext.getAttributes(parseName(name));
if (!(attributes instanceof ResourceAttributes)) {
attributes = new ResourceAttributes(attributes);
}
return attributes;
}
use of javax.naming.directory.Attributes in project karaf by apache.
the class LDAPCache method doGetUserRoles.
private String[] doGetUserRoles(String user, String userDn, String userDnNamespace) throws NamingException {
DirContext context = open();
SearchControls controls = new SearchControls();
if (options.getRoleSearchSubtree()) {
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else {
controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
String filter = options.getRoleFilter();
if (filter != null) {
filter = filter.replaceAll(Pattern.quote("%u"), Matcher.quoteReplacement(user));
filter = filter.replaceAll(Pattern.quote("%dn"), Matcher.quoteReplacement(userDn));
filter = filter.replaceAll(Pattern.quote("%fqdn"), Matcher.quoteReplacement(userDnNamespace));
filter = filter.replace("\\", "\\\\");
LOGGER.debug("Looking for the user roles in LDAP with ");
LOGGER.debug(" base DN: " + options.getRoleBaseDn());
LOGGER.debug(" filter: " + filter);
NamingEnumeration<SearchResult> namingEnumeration = context.search(options.getRoleBaseDn(), filter, controls);
try {
List<String> rolesList = new ArrayList<>();
while (namingEnumeration.hasMore()) {
SearchResult result = namingEnumeration.next();
Attributes attributes = result.getAttributes();
Attribute roles1 = attributes.get(options.getRoleNameAttribute());
if (roles1 != null) {
for (int i = 0; i < roles1.size(); i++) {
String role = (String) roles1.get(i);
if (role != null) {
LOGGER.debug("User {} is a member of role {}", user, role);
// handle role mapping
Set<String> roleMappings = tryMappingRole(role);
if (roleMappings.isEmpty()) {
rolesList.add(role);
} else {
for (String roleMapped : roleMappings) {
rolesList.add(roleMapped);
}
}
}
}
}
}
return rolesList.toArray(new String[rolesList.size()]);
} finally {
if (namingEnumeration != null) {
try {
namingEnumeration.close();
} catch (NamingException e) {
// Ignore
}
}
}
} else {
LOGGER.debug("The user role filter is null so no roles are retrieved");
return new String[] {};
}
}
Aggregations