Search in sources :

Example 46 with Attributes

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);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) CertificateException(java.security.cert.CertificateException) NamingException(javax.naming.NamingException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 47 with Attributes

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);
    }
}
Also used : X509CRL(java.security.cert.X509CRL) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) NamingException(javax.naming.NamingException) CRLException(java.security.cert.CRLException)

Example 48 with Attributes

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);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attributes(javax.naming.directory.Attributes) DirContext(javax.naming.directory.DirContext) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) NamingException(javax.naming.NamingException)

Example 49 with Attributes

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;
}
Also used : Attributes(javax.naming.directory.Attributes)

Example 50 with 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[] {};
    }
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) EventDirContext(javax.naming.event.EventDirContext) DirContext(javax.naming.directory.DirContext)

Aggregations

Attributes (javax.naming.directory.Attributes)252 Attribute (javax.naming.directory.Attribute)135 SearchResult (javax.naming.directory.SearchResult)87 NamingException (javax.naming.NamingException)84 BasicAttributes (javax.naming.directory.BasicAttributes)72 ArrayList (java.util.ArrayList)61 BasicAttribute (javax.naming.directory.BasicAttribute)56 SearchControls (javax.naming.directory.SearchControls)55 DirContext (javax.naming.directory.DirContext)49 NamingEnumeration (javax.naming.NamingEnumeration)44 Test (org.junit.Test)34 InitialDirContext (javax.naming.directory.InitialDirContext)32 LdapContext (javax.naming.ldap.LdapContext)29 HashMap (java.util.HashMap)25 InitialLdapContext (javax.naming.ldap.InitialLdapContext)24 Hashtable (java.util.Hashtable)20 HashSet (java.util.HashSet)18 Map (java.util.Map)17 IOException (java.io.IOException)16 Identity (org.olat.core.id.Identity)16