Java Code Examples for javax.naming.ldap.Rdn#getValue()

The following examples show how to use javax.naming.ldap.Rdn#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CertKeyToUserNameMapper.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Returns Subject DN from X509Certificate
 *
 * @param cert
 * @return Subject DN as a user name
 */
@Override
public String getUserName(Certificate cert) {
    X509Certificate certificate = (X509Certificate) cert;
    String dn = certificate.getSubjectDN().getName();
    LdapName ldapDn = getLdapName(dn);

    if (key == null) {
        throw new IllegalArgumentException("Must set a key");
    }

    for (Rdn rdn : ldapDn.getRdns()) {
        if (key.equalsIgnoreCase(rdn.getType())) {
            return (String)rdn.getValue();
        }
    }

    throw new IllegalArgumentException("No " + key + " key found in certificate DN: " + dn);
}
 
Example 2
Source File: HostnameVerifierImpl.java    From mariadb-connector-j with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static String extractCommonName(String principal) throws SSLException {
  if (principal == null) {
    return null;
  }
  try {
    LdapName ldapName = new LdapName(principal);

    for (Rdn rdn : ldapName.getRdns()) {
      if (rdn.getType().equalsIgnoreCase("CN")) {
        Object obj = rdn.getValue();
        if (obj != null) {
          return obj.toString();
        }
      }
    }
    return null;
  } catch (InvalidNameException e) {
    throw new SSLException("DN value \"" + principal + "\" is invalid");
  }
}
 
Example 3
Source File: DirectoryGroupDN.java    From hesperides with GNU General Public License v3.0 6 votes vote down vote up
public static String extractCnFromDn(String dn) {
    String cn = null;
    try {
        LdapName ldapName = new LdapName(dn);
        for (Rdn rdn : ldapName.getRdns()) {
            if (rdn.getType().equalsIgnoreCase("CN")) {
                cn = (String) rdn.getValue();
            }
        }
    } catch (InvalidNameException e) {
        throw new IllegalArgumentException("Invalid DN: " + dn, e);
    }
    if (cn == null) {
        throw new IllegalArgumentException("Can't find CN in DN: " + dn);
    }
    return cn;
}
 
Example 4
Source File: CustomX509TrustManager.java    From logback-gelf with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static String getCommonName(final X509Certificate cert) throws InvalidNameException {
    final LdapName ldapName = new LdapName(cert.getSubjectDN().getName());
    for (final Rdn rdn : ldapName.getRdns()) {
        if ("CN".equalsIgnoreCase(rdn.getType())) {
            return (String) rdn.getValue();
        }
    }

    throw new InvalidNameException("No common name found");
}
 
Example 5
Source File: LdapUtils.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * Get the value of the Rdn at the requested index in the supplied Name.
 *
 * @param name the Name to work on.
 * @param index The 0-based index of the rdn value to retrieve. Must be in the range [0,size()).
 * @return the value of the rdn at the requested index.
 * @throws IndexOutOfBoundsException if index is outside the specified range.
 * @since 2.0
 */
public static Object getValue(Name name, int index) {
    Assert.notNull(name, "name must not be null");

    LdapName ldapName = returnOrConstructLdapNameFromName(name);
    Rdn rdn = ldapName.getRdn(index);
    if(rdn.size() > 1) {
        LOGGER.warn("Rdn at position " + index + " of dn '" + name +
                "' is multi-value - returned value is not to be trusted. " +
                "Consider using name-based getValue method instead");
    }
    return rdn.getValue();
}
 
Example 6
Source File: ServiceLocator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 7
Source File: ServiceLocator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 8
Source File: ServiceLocator.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 9
Source File: ServiceLocator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 10
Source File: ServiceLocator.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 11
Source File: ServiceLocator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 12
Source File: ServiceLocator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 13
Source File: ServiceLocator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 14
Source File: ServiceLocator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throws InvalidNameException If the distinguished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuilder domain = new StringBuilder();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 15
Source File: ServiceLocator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 16
Source File: ServiceLocator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 17
Source File: PkiServiceProvider.java    From NCANode with MIT License 5 votes vote down vote up
private static void parseRdn(Rdn rdn, JSONObject subject) {
    if (rdn.getType().equalsIgnoreCase("CN")) {
        subject.put("commonName", rdn.getValue());
    } else if (rdn.getType().equalsIgnoreCase("SURNAME")) {
        subject.put("surname", rdn.getValue());
    } else if (rdn.getType().equalsIgnoreCase("SERIALNUMBER")) {

        String sn = ((String)rdn.getValue());

        if (sn.startsWith("BIN")) {
            subject.put("bin", ((String)rdn.getValue()).replaceAll("^BIN", ""));
        } else {
            subject.put("iin", ((String)rdn.getValue()).replaceAll("^IIN", ""));
        }
    } else if (rdn.getType().equalsIgnoreCase("C")) {
        subject.put("country", rdn.getValue());
    } else if (rdn.getType().equalsIgnoreCase("L")) {
        subject.put("locality", rdn.getValue());
    } else if (rdn.getType().equalsIgnoreCase("S")) {
        subject.put("state", rdn.getValue());
    } else if (rdn.getType().equalsIgnoreCase("E")) {
        subject.put("email", rdn.getValue());
    } else if (rdn.getType().equalsIgnoreCase("O")) {
        subject.put("organization", rdn.getValue());
    } else if (rdn.getType().equalsIgnoreCase("OU")) {
        subject.put("bin", ((String)rdn.getValue()).replaceAll("^BIN", ""));
    } else if (rdn.getType().equalsIgnoreCase("G")) {
        subject.put("lastName", rdn.getValue());
    }
}
 
Example 18
Source File: ServiceLocator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 19
Source File: ServiceLocator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps a distinguished name (RFC 2253) to a fully qualified domain name.
 * Processes a sequence of RDNs having a DC attribute.
 * The special RDN "DC=." denotes the root of the domain tree.
 * Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
 * RDN "DC=." all reset the domain name and processing continues.
 *
 * @param dn A string distinguished name (RFC 2253).
 * @return A domain name or null if none can be derived.
 * @throw InvalidNameException If the distinugished name is invalid.
 */
static String mapDnToDomainName(String dn) throws InvalidNameException {
    if (dn == null) {
        return null;
    }
    StringBuffer domain = new StringBuffer();
    LdapName ldapName = new LdapName(dn);

    // process RDNs left-to-right
    //List<Rdn> rdnList = ldapName.getRdns();

    List<Rdn> rdnList = ldapName.getRdns();
    for (int i = rdnList.size() - 1; i >= 0; i--) {
        //Rdn rdn = rdnList.get(i);
        Rdn rdn = rdnList.get(i);

        // single-valued RDN with a DC attribute
        if ((rdn.size() == 1) &&
            ("dc".equalsIgnoreCase(rdn.getType()) )) {
            Object attrval = rdn.getValue();
            if (attrval instanceof String) {
                if (attrval.equals(".") ||
                    (domain.length() == 1 && domain.charAt(0) == '.')) {
                    domain.setLength(0); // reset (when current or previous
                                         //        RDN value is "DC=.")
                }
                if (domain.length() > 0) {
                    domain.append('.');
                }
                domain.append(attrval);
            } else {
                domain.setLength(0); // reset (when binary-valued attribute)
            }
        } else {
            domain.setLength(0); // reset (when multi-valued RDN or non-DC)
        }
    }
    return (domain.length() != 0) ? domain.toString() : null;
}
 
Example 20
Source File: DefaultSubjectProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Create the SubjectBean using the specified principal.
 */
protected SubjectBean createSubjectBean(
    Principal principal, SubjectProviderParameters subjectProviderParameters
) {
    TokenProviderParameters providerParameters = subjectProviderParameters.getProviderParameters();
    TokenRequirements tokenRequirements = providerParameters.getTokenRequirements();
    KeyRequirements keyRequirements = providerParameters.getKeyRequirements();

    String tokenType = tokenRequirements.getTokenType();
    String keyType = keyRequirements.getKeyType();
    String confirmationMethod = getSubjectConfirmationMethod(tokenType, keyType);

    String subjectName = principal.getName();
    String localSubjectNameIDFormat = subjectNameIDFormat;
    if (SAML2Constants.NAMEID_FORMAT_UNSPECIFIED.equals(localSubjectNameIDFormat)
        && principal instanceof X500Principal) {
        // Just use the "cn" instead of the entire DN
        try {
            LdapName ln = new LdapName(principal.getName());

            for (Rdn rdn : ln.getRdns()) {
                if ("CN".equalsIgnoreCase(rdn.getType()) && (rdn.getValue() instanceof String)) {
                    subjectName = (String)rdn.getValue();
                    break;
                }
            }
        } catch (Throwable ex) {
            subjectName = principal.getName();
            //Ignore, not X500 compliant thus use the whole string as the value
        }
    } else if (!SAML2Constants.NAMEID_FORMAT_UNSPECIFIED.equals(localSubjectNameIDFormat)) {
        /* Set subjectNameIDFormat correctly based on type of principal
            unless already set to some value other than unspecified */
        if (principal instanceof UsernameTokenPrincipal) {
            localSubjectNameIDFormat = SAML2Constants.NAMEID_FORMAT_PERSISTENT;
        } else if (principal instanceof X500Principal) {
            localSubjectNameIDFormat = SAML2Constants.NAMEID_FORMAT_X509_SUBJECT_NAME;
        } else if (principal instanceof KerberosPrincipal) {
            localSubjectNameIDFormat = SAML2Constants.NAMEID_FORMAT_KERBEROS;
        } else if (localSubjectNameIDFormat == null) {
            localSubjectNameIDFormat = SAML2Constants.NAMEID_FORMAT_UNSPECIFIED;
        }
    }

    SubjectBean subjectBean =
        new SubjectBean(subjectName, subjectNameQualifier, confirmationMethod);
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Creating new subject with principal name: " + principal.getName());
    }
    subjectBean.setSubjectNameIDFormat(localSubjectNameIDFormat);

    return subjectBean;
}