Search in sources :

Example 36 with Attributes

use of javax.naming.directory.Attributes in project rabbitmq-java-client by rabbitmq.

the class DnsSrvRecordAddressResolver method lookupSrvRecords.

protected List<SrvRecord> lookupSrvRecords(String service, String dnsUrls) throws IOException {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    env.put("java.naming.provider.url", dnsUrls);
    List<SrvRecord> records = new ArrayList<SrvRecord>();
    try {
        DirContext ctx = new InitialDirContext(env);
        Attributes attributes = ctx.getAttributes(service, new String[] { "SRV" });
        NamingEnumeration<?> servers = attributes.get("srv").getAll();
        while (servers.hasMore()) {
            records.add(mapSrvRecord((String) servers.next()));
        }
    } catch (NamingException e) {
        throw new IOException("Error during DNS SRV query", e);
    }
    return records;
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) IOException(java.io.IOException)

Example 37 with Attributes

use of javax.naming.directory.Attributes in project Lucee by lucee.

the class AbsDefaultHostnameVerifier method extractCN.

static String extractCN(final String subjectPrincipal) throws SSLException {
    if (subjectPrincipal == null) {
        return null;
    }
    try {
        final LdapName subjectDN = new LdapName(subjectPrincipal);
        final List<Rdn> rdns = subjectDN.getRdns();
        for (int i = rdns.size() - 1; i >= 0; i--) {
            final Rdn rds = rdns.get(i);
            final Attributes attributes = rds.toAttributes();
            final Attribute cn = attributes.get("cn");
            if (cn != null) {
                try {
                    final Object value = cn.get();
                    if (value != null) {
                        return value.toString();
                    }
                } catch (NoSuchElementException ignore) {
                } catch (NamingException ignore) {
                }
            }
        }
        return null;
    } catch (InvalidNameException e) {
        throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
    }
}
Also used : InvalidNameException(javax.naming.InvalidNameException) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) Rdn(javax.naming.ldap.Rdn) SSLException(javax.net.ssl.SSLException) NoSuchElementException(java.util.NoSuchElementException) LdapName(javax.naming.ldap.LdapName)

Example 38 with Attributes

use of javax.naming.directory.Attributes in project Lucee by lucee.

the class LDAPClient method query.

/**
 * @param dn
 * @param strAttributes
 * @param scope
 * @param startrow
 * @param maxrows
 * @param timeout
 * @param sort
 * @param sortType
 * @param sortDirection
 * @param start
 * @param separator
 * @param filter
 * @return
 * @throws NamingException
 * @throws PageException
 * @throws IOException
 */
public Query query(String strAttributes, int scope, int startrow, int maxrows, int timeout, String[] sort, int sortType, int sortDirection, String start, String separator, String filter) throws NamingException, PageException, IOException {
    // strAttributes=strAttributes.trim();
    boolean attEQAsterix = strAttributes.trim().equals("*");
    String[] attributes = attEQAsterix ? new String[] { "name", "value" } : toStringAttributes(strAttributes, ",");
    // Control
    SearchControls controls = new SearchControls();
    controls.setReturningObjFlag(true);
    controls.setSearchScope(scope);
    if (!attEQAsterix)
        controls.setReturningAttributes(toStringAttributes(strAttributes, ","));
    if (maxrows > 0)
        controls.setCountLimit(startrow + maxrows + 1);
    if (timeout > 0)
        controls.setTimeLimit(timeout);
    InitialLdapContext context = new InitialLdapContext(env, null);
    // Search
    Query qry = new QueryImpl(attributes, 0, "query");
    try {
        NamingEnumeration results = context.search(start, filter, controls);
        // Fill result
        int row = 1;
        if (!attEQAsterix) {
            while (results.hasMoreElements()) {
                SearchResult resultRow = (SearchResult) results.next();
                if (row++ < startrow)
                    continue;
                int len = qry.addRow();
                NamingEnumeration rowEnum = resultRow.getAttributes().getAll();
                String dn = resultRow.getNameInNamespace();
                qry.setAtEL("dn", len, dn);
                while (rowEnum.hasMore()) {
                    Attribute attr = (Attribute) rowEnum.next();
                    Collection.Key key = KeyImpl.init(attr.getID());
                    Enumeration values = attr.getAll();
                    Object value;
                    String existing, strValue;
                    while (values.hasMoreElements()) {
                        value = values.nextElement();
                        strValue = Caster.toString(value, null);
                        existing = Caster.toString(qry.getAt(key, len, null), null);
                        if (!StringUtil.isEmpty(existing) && !StringUtil.isEmpty(strValue)) {
                            value = existing + separator + strValue;
                        } else if (!StringUtil.isEmpty(existing))
                            value = existing;
                        qry.setAtEL(key, len, value);
                    }
                }
                if (maxrows > 0 && len >= maxrows)
                    break;
            }
        } else {
            outer: while (results.hasMoreElements()) {
                SearchResult resultRow = (SearchResult) results.next();
                if (row++ < startrow)
                    continue;
                Attributes attributesRow = resultRow.getAttributes();
                NamingEnumeration rowEnum = attributesRow.getIDs();
                while (rowEnum.hasMoreElements()) {
                    int len = qry.addRow();
                    String name = Caster.toString(rowEnum.next());
                    Object value = null;
                    try {
                        value = attributesRow.get(name).get();
                    } catch (Exception e) {
                    }
                    qry.setAtEL("name", len, name);
                    qry.setAtEL("value", len, value);
                    if (maxrows > 0 && len >= maxrows)
                        break outer;
                }
                qry.setAtEL("name", qry.size(), "dn");
            }
        }
    } finally {
        context.close();
    }
    // Sort
    if (sort != null && sort.length > 0) {
        int order = sortDirection == SORT_DIRECTION_ASC ? Query.ORDER_ASC : Query.ORDER_DESC;
        for (int i = sort.length - 1; i >= 0; i--) {
            String item = sort[i];
            if (item.indexOf(' ') != -1)
                item = ListUtil.first(item, " ", true);
            qry.sort(KeyImpl.getInstance(item), order);
        // keys[i] = new SortKey(item);
        }
    }
    return qry;
}
Also used : Enumeration(java.util.Enumeration) NamingEnumeration(javax.naming.NamingEnumeration) Query(lucee.runtime.type.Query) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) ClassException(lucee.commons.lang.ClassException) NamingException(javax.naming.NamingException) PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) QueryImpl(lucee.runtime.type.QueryImpl) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Collection(lucee.runtime.type.Collection) SearchControls(javax.naming.directory.SearchControls)

Example 39 with Attributes

use of javax.naming.directory.Attributes in project cxf by apache.

the class LdapClaimsHandler method retrieveClaimValues.

public ProcessedClaimCollection retrieveClaimValues(ClaimCollection claims, ClaimsParameters parameters) {
    String user = null;
    boolean useLdapLookup = false;
    Principal principal = parameters.getPrincipal();
    if (principal instanceof KerberosPrincipal) {
        KerberosPrincipal kp = (KerberosPrincipal) principal;
        StringTokenizer st = new StringTokenizer(kp.getName(), "@");
        user = st.nextToken();
    } else if (principal instanceof X500Principal) {
        X500Principal x500p = (X500Principal) principal;
        LOG.warning("Unsupported principal type X500: " + x500p.getName());
        return new ProcessedClaimCollection();
    } else if (principal != null) {
        user = principal.getName();
        if (user == null) {
            LOG.warning("User must not be null");
            return new ProcessedClaimCollection();
        }
        useLdapLookup = LdapUtils.isDN(user);
    } else {
        LOG.warning("Principal is null");
        return new ProcessedClaimCollection();
    }
    if (LOG.isLoggable(Level.FINEST)) {
        LOG.finest("Retrieve claims for user " + user);
    }
    Map<String, Attribute> ldapAttributes = null;
    if (useLdapLookup) {
        AttributesMapper<Map<String, Attribute>> mapper = new AttributesMapper<Map<String, Attribute>>() {

            public Map<String, Attribute> mapFromAttributes(Attributes attrs) throws NamingException {
                Map<String, Attribute> map = new HashMap<>();
                NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
                while (attrEnum.hasMore()) {
                    Attribute att = attrEnum.next();
                    map.put(att.getID(), att);
                }
                return map;
            }
        };
        ldapAttributes = ldap.lookup(user, mapper);
    } else {
        List<String> searchAttributeList = new ArrayList<>();
        for (Claim claim : claims) {
            String claimType = claim.getClaimType().toString();
            if (getClaimsLdapAttributeMapping().keySet().contains(claimType)) {
                searchAttributeList.add(getClaimsLdapAttributeMapping().get(claimType));
            } else {
                if (LOG.isLoggable(Level.FINER)) {
                    LOG.finer("Unsupported claim: " + claimType);
                }
            }
        }
        String[] searchAttributes = searchAttributeList.toArray(new String[searchAttributeList.size()]);
        if (this.userBaseDn != null) {
            ldapAttributes = LdapUtils.getAttributesOfEntry(ldap, this.userBaseDn, this.getObjectClass(), this.getUserNameAttribute(), user, searchAttributes);
        }
        if (this.userBaseDNs != null && (ldapAttributes == null || ldapAttributes.isEmpty())) {
            for (String userBase : userBaseDNs) {
                ldapAttributes = LdapUtils.getAttributesOfEntry(ldap, userBase, this.getObjectClass(), this.getUserNameAttribute(), user, searchAttributes);
                if (ldapAttributes != null && !ldapAttributes.isEmpty()) {
                    // User found
                    break;
                }
            }
        }
    }
    if (ldapAttributes == null || ldapAttributes.isEmpty()) {
        // No result
        if (LOG.isLoggable(Level.INFO)) {
            LOG.info("User '" + user + "' not found");
        }
        return new ProcessedClaimCollection();
    }
    ProcessedClaimCollection claimsColl = new ProcessedClaimCollection();
    for (Claim claim : claims) {
        ProcessedClaim c = processClaim(claim, ldapAttributes, principal);
        if (c != null) {
            // c.setIssuer(issuer);
            // c.setOriginalIssuer(originalIssuer);
            // c.setNamespace(namespace);
            claimsColl.add(c);
        }
    }
    return claimsColl;
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Attribute(javax.naming.directory.Attribute) AttributesMapper(org.springframework.ldap.core.AttributesMapper) HashMap(java.util.HashMap) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) StringTokenizer(java.util.StringTokenizer) X500Principal(javax.security.auth.x500.X500Principal) HashMap(java.util.HashMap) Map(java.util.Map) X500Principal(javax.security.auth.x500.X500Principal) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Principal(java.security.Principal) Claim(org.apache.cxf.rt.security.claims.Claim)

Example 40 with Attributes

use of javax.naming.directory.Attributes in project cxf by apache.

the class DefaultHostnameVerifier method extractCN.

static String extractCN(final String subjectPrincipal) throws SSLException {
    if (subjectPrincipal == null) {
        return null;
    }
    try {
        final LdapName subjectDN = new LdapName(subjectPrincipal);
        final List<Rdn> rdns = subjectDN.getRdns();
        for (int i = rdns.size() - 1; i >= 0; i--) {
            final Rdn rds = rdns.get(i);
            final Attributes attributes = rds.toAttributes();
            final Attribute cn = attributes.get("cn");
            if (cn != null) {
                try {
                    final Object value = cn.get();
                    if (value != null) {
                        return value.toString();
                    }
                } catch (NoSuchElementException ignore) {
                // 
                } catch (NamingException ignore) {
                // 
                }
            }
        }
        return null;
    } catch (InvalidNameException e) {
        throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
    }
}
Also used : InvalidNameException(javax.naming.InvalidNameException) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) Rdn(javax.naming.ldap.Rdn) SSLException(javax.net.ssl.SSLException) NoSuchElementException(java.util.NoSuchElementException) LdapName(javax.naming.ldap.LdapName)

Aggregations

Attributes (javax.naming.directory.Attributes)99 Attribute (javax.naming.directory.Attribute)66 NamingException (javax.naming.NamingException)37 BasicAttributes (javax.naming.directory.BasicAttributes)36 SearchResult (javax.naming.directory.SearchResult)36 BasicAttribute (javax.naming.directory.BasicAttribute)29 SearchControls (javax.naming.directory.SearchControls)24 ArrayList (java.util.ArrayList)22 DirContext (javax.naming.directory.DirContext)22 NamingEnumeration (javax.naming.NamingEnumeration)20 IOException (java.io.IOException)13 InitialDirContext (javax.naming.directory.InitialDirContext)13 Hashtable (java.util.Hashtable)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 LdapContext (javax.naming.ldap.LdapContext)8 File (java.io.File)7 InputStream (java.io.InputStream)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7