Search in sources :

Example 1 with Rdn

use of javax.naming.ldap.Rdn in project OpenAttestation by OpenAttestation.

the class DN method parseLdapName.

private void parseLdapName(String distinguishedName) {
    try {
        LdapName dn = new LdapName(distinguishedName);
        for (int i = 0; i < dn.size(); i++) {
            Rdn rdn = dn.getRdn(i);
            map.put(rdn.getType(), rdn.getValue().toString());
        }
    } catch (InvalidNameException e) {
        log.error("Cannot extract Common Name from Distinguished Name", e);
    }
}
Also used : InvalidNameException(javax.naming.InvalidNameException) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName)

Example 2 with Rdn

use of javax.naming.ldap.Rdn in project hadoop by apache.

the class LdapGroupsMapping method getRelativeDistinguishedName.

/**
   * A helper method to get the Relative Distinguished Name (RDN) from
   * Distinguished name (DN). According to Active Directory documentation,
   * a group object's RDN is a CN.
   *
   * @param distinguishedName A string representing a distinguished name.
   * @throws NamingException if the DN is malformed.
   * @return a string which represents the RDN
   */
private String getRelativeDistinguishedName(String distinguishedName) throws NamingException {
    LdapName ldn = new LdapName(distinguishedName);
    List<Rdn> rdns = ldn.getRdns();
    if (rdns.isEmpty()) {
        throw new NamingException("DN is empty");
    }
    Rdn rdn = rdns.get(rdns.size() - 1);
    if (rdn.getType().equalsIgnoreCase(groupNameAttr)) {
        String groupName = (String) rdn.getValue();
        return groupName;
    }
    throw new NamingException("Unable to find RDN: The DN " + distinguishedName + " is malformed.");
}
Also used : NamingException(javax.naming.NamingException) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName)

Example 3 with Rdn

use of javax.naming.ldap.Rdn in project midpoint by Evolveum.

the class TestExpressionFunctions method testComposeDnWithSuffix.

@Test
public void testComposeDnWithSuffix() throws Exception {
    final String TEST_NAME = "testComposeDnWithSuffix";
    TestUtil.displayTestTile(TEST_NAME);
    BasicExpressionFunctions basic = createBasicFunctions();
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix(new Rdn("cn", "foo"), "ou=baz,o=bar"));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix(new Rdn("cn", "foo"), new LdapName("ou=baz,o=bar")));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo", "ou=baz,o=bar"));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", PrismTestUtil.createPolyString("foo"), "ou=baz,o=bar"));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", PrismTestUtil.createPolyStringType("foo"), "ou=baz,o=bar"));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo", new LdapName("ou=baz,o=bar")));
    assertEquals("cn=foo,ou=baz\\,baz,o=bar", basic.composeDnWithSuffix("cn", "foo", "ou=baz\\,baz,o=bar"));
    assertEquals("cn=foo,ou=baz\\,baz,o=bar", basic.composeDnWithSuffix("cn", "foo", new LdapName("ou=baz\\,baz,o=bar")));
    assertEquals("cn=foo\\,foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo,foo", "ou=baz,o=bar"));
    assertEquals("cn=foo\\,foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo,foo", new LdapName("ou=baz,o=bar")));
    assertEquals("cn=foo\\=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo=foo", "ou=baz,o=bar"));
    assertEquals("cn=foo\\=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo=foo", new LdapName("ou=baz,o=bar")));
    assertEquals("ou=baz,o=bar", basic.composeDnWithSuffix("ou=baz,o=bar"));
    assertEquals("ou=baz, o=bar", basic.composeDnWithSuffix("ou=baz, o=bar"));
    assertEquals("OU=baz, o=bar", basic.composeDnWithSuffix("OU=baz, o=bar"));
    assertEquals("ou=baz,o=bar", basic.composeDnWithSuffix(new LdapName("ou=baz,o=bar")));
    assertEquals(null, basic.composeDnWithSuffix(null));
    assertEquals(null, basic.composeDnWithSuffix());
    assertEquals(null, basic.composeDnWithSuffix(""));
    assertEquals(null, basic.composeDnWithSuffix("   "));
}
Also used : BasicExpressionFunctions(com.evolveum.midpoint.model.common.expression.functions.BasicExpressionFunctions) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName) Test(org.testng.annotations.Test)

Example 4 with Rdn

use of javax.naming.ldap.Rdn in project nifi by apache.

the class CertificateUtils method compareDNs.

/**
 * Returns true if the two provided DNs are equivalent, regardless of the order of the elements. Returns false if one or both are invalid DNs.
 *
 * Example:
 *
 * CN=test1, O=testOrg, C=US compared to CN=test1, O=testOrg, C=US -> true
 * CN=test1, O=testOrg, C=US compared to O=testOrg, CN=test1, C=US -> true
 * CN=test1, O=testOrg, C=US compared to CN=test2, O=testOrg, C=US -> false
 * CN=test1, O=testOrg, C=US compared to O=testOrg, CN=test2, C=US -> false
 * CN=test1, O=testOrg, C=US compared to                           -> false
 *                           compared to                           -> true
 *
 * @param dn1 the first DN to compare
 * @param dn2 the second DN to compare
 * @return true if the DNs are equivalent, false otherwise
 */
public static boolean compareDNs(String dn1, String dn2) {
    if (dn1 == null) {
        dn1 = "";
    }
    if (dn2 == null) {
        dn2 = "";
    }
    if (StringUtils.isEmpty(dn1) || StringUtils.isEmpty(dn2)) {
        return dn1.equals(dn2);
    }
    try {
        List<Rdn> rdn1 = new LdapName(dn1).getRdns();
        List<Rdn> rdn2 = new LdapName(dn2).getRdns();
        return rdn1.size() == rdn2.size() && rdn1.containsAll(rdn2);
    } catch (InvalidNameException e) {
        logger.warn("Cannot compare DNs: {} and {} because one or both is not a valid DN", dn1, dn2);
        return false;
    }
}
Also used : InvalidNameException(javax.naming.InvalidNameException) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName)

Example 5 with Rdn

use of javax.naming.ldap.Rdn 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)

Aggregations

Rdn (javax.naming.ldap.Rdn)56 LdapName (javax.naming.ldap.LdapName)48 InvalidNameException (javax.naming.InvalidNameException)24 Attribute (javax.naming.directory.Attribute)13 Attributes (javax.naming.directory.Attributes)12 NamingException (javax.naming.NamingException)11 ArrayList (java.util.ArrayList)10 X509Certificate (java.security.cert.X509Certificate)6 List (java.util.List)5 NoSuchElementException (java.util.NoSuchElementException)5 SearchResult (javax.naming.directory.SearchResult)5 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)4 DirContext (javax.naming.directory.DirContext)4 SSLException (javax.net.ssl.SSLException)4 IOException (java.io.IOException)3 BasicAttributes (javax.naming.directory.BasicAttributes)3 LoginException (javax.security.auth.login.LoginException)3 X500Principal (javax.security.auth.x500.X500Principal)3 Test (org.junit.Test)3 BasicExpressionFunctions (com.evolveum.midpoint.model.common.expression.functions.BasicExpressionFunctions)2