private 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 (final NoSuchElementException ignore) { // ignore exception } catch (final NamingException ignore) { // ignore exception } } } return null; } catch (final InvalidNameException e) { throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); } }
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 | NamingException ignore) { // } } } return null; } catch (InvalidNameException e) { throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); } }
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"); } }
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 | NamingException ignore) { // } } } return null; } catch (InvalidNameException e) { throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); } }
static String[] extractCNs(final String subjectPrincipal) throws SSLException { if (subjectPrincipal == null) { return null; } final List<String> cns = new ArrayList<String>(); 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) { cns.add(value.toString()); } } catch (NoSuchElementException ignore) { } catch (NamingException ignore) { } } } } catch (InvalidNameException e) { throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); } return cns.isEmpty() ? null : cns.toArray(new String[ cns.size() ]); }
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 (final NoSuchElementException ignore) { } catch (final NamingException ignore) { } } } return null; } catch (final InvalidNameException e) { throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); } }
private 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) { } } } } catch (InvalidNameException e) { throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); } return null; }
private static String getGroupNameFromDn(String dnString, String groupNameAttribute) throws NamingException { LdapName dn = new LdapName(dnString); // may throw InvalidNameException Attribute attribute = dn.getRdn(dn.size()-1).toAttributes().get(groupNameAttribute); if (attribute == null) { // We were configured with the wrong group name attribute throw new IdentityStoreConfigurationException("Group name attribute '" + groupNameAttribute + "' not found for DN: " + dnString); } return attribute.get(0).toString(); }
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 (final NoSuchElementException ignore) { } catch (final NamingException ignore) { } } } return null; } catch (final InvalidNameException e) { throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); } }
private static String getGroupNameFromDn(String dnString, String groupNameAttribute) throws NamingException { LdapName dn = new LdapName(dnString); // may throw InvalidNameException Attribute attribute = dn.getRdn(dn.size()-1).toAttributes().get(groupNameAttribute); if (attribute == null) { // We were configured with the wrong group name attribute throw new IdentityStoreConfigurationException("Group name attribute '" + groupNameAttribute + "' not found for DN: " + dnString); } return attribute.get(0).toString(); }
/** * Find the Rdn with the requested key in the supplied Name. * * @param name the Name in which to search for the key. * @param key the attribute key to search for. * @return the rdn corresponding to the <b>first</b> occurrence of the requested key. * @throws NoSuchElementException if no corresponding entry is found. * @since 2.0 */ public static Rdn getRdn(Name name, String key) { Assert.notNull(name, "name must not be null"); Assert.hasText(key, "key must not be blank"); LdapName ldapName = returnOrConstructLdapNameFromName(name); List<Rdn> rdns = ldapName.getRdns(); for (Rdn rdn : rdns) { NamingEnumeration<String> ids = rdn.toAttributes().getIDs(); while (ids.hasMoreElements()) { String id = ids.nextElement(); if(key.equalsIgnoreCase(id)) { return rdn; } } } throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'"); }
public static List<String> getCNs(final X509Certificate cert) { Args.notNull("cert", cert); final List<String> cns = new ArrayList<String>(); try { final String subjectPrincipal = cert.getSubjectX500Principal().getName(X500Principal.RFC2253); for (final Rdn rdn : new LdapName(subjectPrincipal).getRdns()) { final Attribute cnAttr = rdn.toAttributes().get("cn"); if (cnAttr != null) { try { final Object cnValue = cnAttr.get(); if (cnValue != null) { cns.add(cnValue.toString()); } } catch (final Exception ex) { LOG.debug(ex); } } } } catch (final InvalidNameException ex) { LOG.debug(ex); } return cns; }
/** * Find the Rdn with the requested key in the supplied Name. * * @param name the Name in which to search for the key. * @param key the attribute key to search for. * @return the rdn corresponding to the <b>first</b> occurrence of the requested key. * @throws NoSuchElementException if no corresponding entry is found. * @since 2.0 */ public static Rdn getRdn(Name name, String key) { Assert.notNull(name, "name must not be null"); Assert.hasText(key, "key must not be blank"); LdapName ldapName = returnOrConstructLdapNameFromName(name); List<Rdn> rdns = ldapName.getRdns(); for (Rdn rdn : rdns) { NamingEnumeration<String> ids = rdn.toAttributes().getIDs(); while (ids.hasMoreElements()) { String id = ids.nextElement(); if(key.equalsIgnoreCase(id)) { return rdn; } } } throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'"); }
/** * Find the Rdn with the requested key in the supplied Name. * * @param name the Name in which to search for the key. * @param key the attribute key to search for. * @return the rdn corresponding to the <b>first</b> occurrence of the requested key. * @throws NoSuchElementException if no corresponding entry is found. * @since 2.0 */ public static Rdn getRdn(Name name, String key) { Assert.notNull(name, "name must not be null"); Assert.hasText(key, "key must not be blank"); LdapName ldapName = returnOrConstructLdapNameFromName(name); List<Rdn> rdns = ldapName.getRdns(); for (Rdn rdn : rdns) { NamingEnumeration<String> ids = rdn.toAttributes().getIDs(); while (ids.hasMoreElements()) { String id = ids.nextElement(); if(key.equalsIgnoreCase(id)) { return rdn; } } } throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'"); }
private LdapEntry parseRole(String dn, String groupNameAttribute, URI groupReferralAddress) { try { LdapName ldapName = new LdapName(Rdn.unescapeValue(dn).toString()); for (int i = ldapName.size() - 1; i >= 0; i--) { String rdnString = ldapName.get(i); Rdn rdn = new Rdn(rdnString); Attribute attr = rdn.toAttributes().get(groupNameAttribute); if (attr != null) { Object value = attr.get(); if (value != null) { return new LdapEntry( (value instanceof byte[]) ? new String((byte[]) value, StandardCharsets.UTF_8) : value.toString(), dn, groupReferralAddress); } } } } catch (NamingException e) { SECURITY_LOGGER.tracef("Unable to parse role from DN (%s): %s", dn, e.getMessage()); } return null; }
/** * @return the first CN found */ public static String getCN(final X509Certificate cert) { Args.notNull("cert", cert); try { final String subjectPrincipal = cert.getSubjectX500Principal().getName(X500Principal.RFC2253); for (final Rdn rdn : new LdapName(subjectPrincipal).getRdns()) { final Attribute cnAttr = rdn.toAttributes().get("cn"); if (cnAttr != null) { try { final Object cnValue = cnAttr.get(); if (cnValue != null) return cnValue.toString(); } catch (final Exception ex) { LOG.debug(ex); } } } } catch (final InvalidNameException ex) { LOG.debug(ex); } return null; }
private LdapEntry parseRole(String dn, String groupNameAttribute, URI groupReferralAddress) { try { LdapName ldapName = new LdapName(Rdn.unescapeValue(dn).toString()); for (int i = ldapName.size() - 1; i >= 0; i--) { String rdnString = ldapName.get(i); Rdn rdn = new Rdn(rdnString); Attribute attr = rdn.toAttributes().get(groupNameAttribute); if (attr != null) { Object value = attr.get(); if (value != null) { return new LdapEntry( (value instanceof byte[]) ? new String((byte[]) value, StandardCharsets.UTF_8) : value.toString(), dn, groupReferralAddress); } } } } catch (NamingException e) { SECURITY_LOGGER.tracef("Unable to parse role from DN (%s): %s", dn, e.getMessage()); } return null; }
/** * Get the value of the Rdn with the requested key in the supplied Name. * * @param name the Name in which to search for the key. * @param key the attribute key to search for. * @return the value of the rdn corresponding to the <b>first</b> occurrence of the requested key. * @throws NoSuchElementException if no corresponding entry is found. * @since 2.0 */ public static Object getValue(Name name, String key) { NamingEnumeration<? extends Attribute> allAttributes = getRdn(name, key).toAttributes().getAll(); while (allAttributes.hasMoreElements()) { Attribute oneAttribute = allAttributes.nextElement(); if(key.equalsIgnoreCase(oneAttribute.getID())) { try { return oneAttribute.get(); } catch (javax.naming.NamingException e) { throw convertLdapException(e); } } } // This really shouldn't happen throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'"); }
/** * Get the value of the Rdn with the requested key in the supplied Name. * * @param name the Name in which to search for the key. * @param key the attribute key to search for. * @return the value of the rdn corresponding to the <b>first</b> occurrence of the requested key. * @throws NoSuchElementException if no corresponding entry is found. * @since 2.0 */ public static Object getValue(Name name, String key) { NamingEnumeration<? extends Attribute> allAttributes = getRdn(name, key).toAttributes().getAll(); while (allAttributes.hasMoreElements()) { Attribute oneAttribute = allAttributes.nextElement(); if(key.equalsIgnoreCase(oneAttribute.getID())) { try { return oneAttribute.get(); } catch (javax.naming.NamingException e) { throw convertLdapException(e); } } } // This really shouldn't happen throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'"); }
/** * Get the value of the Rdn with the requested key in the supplied Name. * * @param name the Name in which to search for the key. * @param key the attribute key to search for. * @return the value of the rdn corresponding to the <b>first</b> occurrence of the requested key. * @throws NoSuchElementException if no corresponding entry is found. * @since 2.0 */ public static Object getValue(Name name, String key) { NamingEnumeration<? extends Attribute> allAttributes = getRdn(name, key).toAttributes().getAll(); while (allAttributes.hasMoreElements()) { Attribute oneAttribute = allAttributes.nextElement(); if(key.equalsIgnoreCase(oneAttribute.getID())) { try { return oneAttribute.get(); } catch (javax.naming.NamingException e) { throw convertLdapException(e); } } } // This really shouldn't happen throw new NoSuchElementException("No Rdn with the requested key: '" + key + "'"); }