Example usage for javax.naming.ldap LdapName LdapName

List of usage examples for javax.naming.ldap LdapName LdapName

Introduction

In this page you can find the example usage for javax.naming.ldap LdapName LdapName.

Prototype

public LdapName(List<Rdn> rdns) 

Source Link

Document

Constructs an LDAP name given its parsed RDN components.

Usage

From source file:org.apache.syncope.client.console.wicket.markup.html.form.preview.BinaryCertPreviewer.java

@Override
public Component preview(final byte[] uploadedBytes) {
    Label commonNameLabel = new Label("certCommonName", new Model<>());
    if (uploadedBytes.length == 0) {
        LOG.info("Enpty certificate");
        return commonNameLabel;
    }//from  w ww  . j a v  a2s .c  om

    try (ByteArrayInputStream certificateStream = new ByteArrayInputStream(uploadedBytes)) {
        X509Certificate certificate = (X509Certificate) CertificateFactory.getInstance("X.509")
                .generateCertificate(certificateStream);

        StringBuilder commonNameBuilder = new StringBuilder("cn=");

        LdapName ldapName = new LdapName(certificate.getIssuerDN().getName());

        for (Rdn rdn : ldapName.getRdns()) {
            if ("CN".equalsIgnoreCase(rdn.getType())) {
                commonNameBuilder
                        .append(rdn.getValue() == null ? StringUtils.EMPTY : rdn.getValue().toString());
            }
        }
        commonNameLabel.setDefaultModelObject(commonNameBuilder.toString());
    } catch (Exception e) {
        LOG.error("Error evaluating certificate file", e);
        commonNameLabel.setDefaultModelObject(getString(Constants.ERROR));
    }

    return this.addOrReplace(commonNameLabel);
}

From source file:org.apache.syncope.console.wicket.markup.html.form.preview.BinaryCertPreviewer.java

@Override
public Component preview() {
    final Label commonNameLabel = new Label("certCommonName", new Model<String>());
    final ByteArrayInputStream certificateStream = new ByteArrayInputStream(uploadedBytes);
    try {//from  www .  j a v  a  2 s  . co m
        final X509Certificate certificate = (X509Certificate) CertificateFactory.getInstance("X.509")
                .generateCertificate(certificateStream);

        final StringBuilder commonNameBuilder = new StringBuilder("cn=");

        final LdapName ldapName = new LdapName(certificate.getIssuerDN().getName());

        for (Rdn rdn : ldapName.getRdns()) {
            if ("CN".equalsIgnoreCase(rdn.getType())) {
                commonNameBuilder
                        .append(rdn.getValue() == null ? StringUtils.EMPTY : rdn.getValue().toString());
            }
        }
        commonNameLabel.setDefaultModelObject(commonNameBuilder.toString());
    } catch (Exception e) {
        LOG.error("Error evaluating certificate file", e);
        throw new IllegalArgumentException("Error evaluating certificate file", e);
    } finally {
        IOUtils.closeQuietly(certificateStream);
    }
    return this.add(commonNameLabel);
}

From source file:org.apache.zeppelin.realm.LdapRealm.java

private void addRoleIfMember(final String userDn, final SearchResult group, final Set<String> roleNames,
        final Set<String> groupNames, final LdapContextFactory ldapContextFactory) throws NamingException {
    NamingEnumeration<? extends Attribute> attributeEnum = null;
    NamingEnumeration<?> ne = null;
    try {//  w  w w.  ja  v  a 2 s .c  o m
        LdapName userLdapDn = new LdapName(userDn);
        Attribute attribute = group.getAttributes().get(getGroupIdAttribute());
        String groupName = attribute.get().toString();

        attributeEnum = group.getAttributes().getAll();
        while (attributeEnum.hasMore()) {
            final Attribute attr = attributeEnum.next();
            if (!memberAttribute.equalsIgnoreCase(attr.getID())) {
                continue;
            }
            ne = attr.getAll();
            while (ne.hasMore()) {
                String attrValue = ne.next().toString();
                if (memberAttribute.equalsIgnoreCase(MEMBER_URL)) {
                    boolean dynamicGroupMember = isUserMemberOfDynamicGroup(userLdapDn, attrValue,
                            ldapContextFactory);
                    if (dynamicGroupMember) {
                        groupNames.add(groupName);
                        String roleName = roleNameFor(groupName);
                        if (roleName != null) {
                            roleNames.add(roleName);
                        } else {
                            roleNames.add(groupName);
                        }
                    }
                } else {
                    // posix groups' members don' include the entire dn
                    if (groupObjectClass.equalsIgnoreCase(POSIX_GROUP)) {
                        attrValue = memberDn(attrValue);
                    }
                    if (userLdapDn.equals(new LdapName(attrValue))) {
                        groupNames.add(groupName);
                        String roleName = roleNameFor(groupName);
                        if (roleName != null) {
                            roleNames.add(roleName);
                        } else {
                            roleNames.add(groupName);
                        }
                        break;
                    }
                }
            }
        }
    } finally {
        try {
            if (attributeEnum != null) {
                attributeEnum.close();
            }
        } finally {
            if (ne != null) {
                ne.close();
            }
        }
    }
}

From source file:org.apache.zeppelin.realm.LdapRealm.java

boolean isUserMemberOfDynamicGroup(LdapName userLdapDn, String memberUrl,
        final LdapContextFactory ldapContextFactory) throws NamingException {
    // ldap://host:port/dn?attributes?scope?filter?extensions
    if (memberUrl == null) {
        return false;
    }/*from w w  w  .  j a v a2s  .  c  om*/
    String[] tokens = memberUrl.split("\\?");
    if (tokens.length < 4) {
        return false;
    }

    String searchBaseString = tokens[0].substring(tokens[0].lastIndexOf("/") + 1);
    String searchScope = tokens[2];
    String searchFilter = tokens[3];

    LdapName searchBaseDn = new LdapName(searchBaseString);

    // do scope test
    if (searchScope.equalsIgnoreCase("base")) {
        log.debug("DynamicGroup SearchScope base");
        return false;
    }
    if (!userLdapDn.toString().endsWith(searchBaseDn.toString())) {
        return false;
    }
    if (searchScope.equalsIgnoreCase("one") && (userLdapDn.size() != searchBaseDn.size() - 1)) {
        log.debug("DynamicGroup SearchScope one");
        return false;
    }
    // search for the filter, substituting base with userDn
    // search for base_dn=userDn, scope=base, filter=filter
    LdapContext systemLdapCtx = null;
    systemLdapCtx = ldapContextFactory.getSystemLdapContext();
    boolean member = false;
    NamingEnumeration<SearchResult> searchResultEnum = null;
    try {
        searchResultEnum = systemLdapCtx.search(userLdapDn, searchFilter,
                searchScope.equalsIgnoreCase("sub") ? SUBTREE_SCOPE : ONELEVEL_SCOPE);
        if (searchResultEnum.hasMore()) {
            return true;
        }
    } finally {
        try {
            if (searchResultEnum != null) {
                searchResultEnum.close();
            }
        } finally {
            LdapUtils.closeContext(systemLdapCtx);
        }
    }
    return member;
}

From source file:org.ballerinalang.auth.ldap.nativeimpl.GetLdapScopesOfUser.java

private String[] getLDAPGroupsListOfUser(String userName, List<String> searchBase,
        CommonLdapConfiguration ldapAuthConfig) throws UserStoreException, NamingException {
    if (userName == null) {
        throw new BallerinaException("userName value is null.");
    }/*from  w ww .jav a  2  s .c om*/

    SearchControls searchCtls = new SearchControls();
    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    // Load normal roles with the user
    String searchFilter = ldapAuthConfig.getGroupNameListFilter();
    String roleNameProperty = ldapAuthConfig.getGroupNameAttribute();
    String membershipProperty = ldapAuthConfig.getMembershipAttribute();
    String nameInSpace = this.getNameInSpaceForUserName(userName, ldapConfiguration);

    if (membershipProperty == null || membershipProperty.length() < 1) {
        throw new BallerinaException("membershipAttribute not set in configuration");
    }

    String membershipValue;
    if (nameInSpace != null) {
        LdapName ldn = new LdapName(nameInSpace);
        if (LdapConstants.MEMBER_UID.equals(ldapAuthConfig.getMembershipAttribute())) {
            // membership value of posixGroup is not DN of the user
            List rdns = ldn.getRdns();
            membershipValue = ((Rdn) rdns.get(rdns.size() - 1)).getValue().toString();
        } else {
            membershipValue = escapeLdapNameForFilter(ldn);
        }
    } else {
        return new String[0];
    }

    searchFilter = "(&" + searchFilter + "(" + membershipProperty + "=" + membershipValue + "))";
    String returnedAtts[] = { roleNameProperty };
    searchCtls.setReturningAttributes(returnedAtts);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Reading roles with the membershipProperty Property: " + membershipProperty);
    }

    List<String> list = this.getListOfNames(searchBase, searchFilter, searchCtls, roleNameProperty, false);
    return list.toArray(new String[list.size()]);
}

From source file:org.ballerinalang.stdlib.ldap.nativeimpl.GetLdapScopesOfUser.java

private static String[] getLDAPGroupsListOfUser(String userName, List<String> searchBase,
        CommonLdapConfiguration ldapAuthConfig, DirContext ldapConnectionContext)
        throws UserStoreException, NamingException {
    if (userName == null) {
        throw new BallerinaException("userName value is null.");
    }//  w w w  .ja va  2  s  . c  om

    SearchControls searchCtls = new SearchControls();
    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    // Load normal roles with the user
    String searchFilter = ldapAuthConfig.getGroupNameListFilter();
    String roleNameProperty = ldapAuthConfig.getGroupNameAttribute();
    String membershipProperty = ldapAuthConfig.getMembershipAttribute();
    String nameInSpace = getNameInSpaceForUserName(userName, ldapAuthConfig, ldapConnectionContext);

    if (membershipProperty == null || membershipProperty.length() < 1) {
        throw new BallerinaException("membershipAttribute not set in configuration");
    }

    String membershipValue;
    if (nameInSpace != null) {
        LdapName ldn = new LdapName(nameInSpace);
        if (LdapConstants.MEMBER_UID.equals(ldapAuthConfig.getMembershipAttribute())) {
            // membership value of posixGroup is not DN of the user
            List rdns = ldn.getRdns();
            membershipValue = ((Rdn) rdns.get(rdns.size() - 1)).getValue().toString();
        } else {
            membershipValue = escapeLdapNameForFilter(ldn);
        }
    } else {
        return new String[0];
    }

    searchFilter = "(&" + searchFilter + "(" + membershipProperty + "=" + membershipValue + "))";
    String returnedAtts[] = { roleNameProperty };
    searchCtls.setReturningAttributes(returnedAtts);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Reading roles with the membershipProperty Property: " + membershipProperty);
    }

    List<String> list = getListOfNames(searchBase, searchFilter, searchCtls, roleNameProperty,
            ldapConnectionContext);
    return list.toArray(new String[list.size()]);
}

From source file:org.codehaus.plexus.redback.common.ldap.connection.LdapConnectionConfiguration.java

public LdapConnectionConfiguration(String hostname, int port, LdapName baseDn, String contextFactory,
        LdapName bindDn, String password, String authenticationMethod, Properties extraProperties)
        throws LdapException {
    this.hostname = hostname;

    this.port = port;

    if (baseDn != null) {
        this.baseDn = new LdapName(baseDn.getRdns());
    }/*from   w ww .  ja v  a2 s.co  m*/

    this.contextFactory = contextFactory;

    if (bindDn != null) {
        this.bindDn = new LdapName(bindDn.getRdns());
    }

    this.password = password;

    this.authenticationMethod = authenticationMethod;

    this.extraProperties = extraProperties;

    check();
}

From source file:org.dcm4che3.conf.dicom.ldap.LdapConfigUtils.java

public static List<Rdn> getNonBaseRdns(String dn, String baseDN) throws InvalidNameException {
    LdapName baseDnName = new LdapName(baseDN);
    LdapName name = new LdapName(dn);

    // ffd to the interesting part
    List<Rdn> rdns = new LinkedList<Rdn>(name.getRdns());
    List<Rdn> baseRdns = baseDnName.getRdns();

    return getNonBaseRdns(rdns, baseRdns);
}

From source file:org.easy.ldap.NamingFactory.java

public static LdapName createName(String dn) {
    LdapName out = null;/*from www.  j a v  a  2 s .  c o m*/
    try {
        out = new LdapName(dn);
    } catch (InvalidNameException e) {
        throw new RuntimeException(dn, e);
    }

    return out;
}

From source file:org.easy.ldap.NamingFactory.java

public static LdapName createName(Rdn... rdns) {
    return new LdapName(Arrays.asList(rdns));
}