Search in sources :

Example 6 with Attributes

use of javax.naming.directory.Attributes in project jetty.project by eclipse.

the class LdapLoginModule method getUserInfo.

/**
     * get the available information about the user
     * <p>
     * for this LoginModule, the credential can be null which will result in a
     * binding ldap authentication scenario
     * <p>
     * roles are also an optional concept if required
     *
     * @param username the user name
     * @return the userinfo for the username
     * @throws Exception if unable to get the user info
     */
public UserInfo getUserInfo(String username) throws Exception {
    Attributes attributes = getUserAttributes(username);
    String pwdCredential = getUserCredentials(attributes);
    if (pwdCredential == null) {
        return null;
    }
    pwdCredential = convertCredentialLdapToJetty(pwdCredential);
    Credential credential = Credential.getCredential(pwdCredential);
    return new LDAPUserInfo(username, credential, attributes);
}
Also used : Credential(org.eclipse.jetty.util.security.Credential) Attributes(javax.naming.directory.Attributes)

Example 7 with Attributes

use of javax.naming.directory.Attributes in project jetty.project by eclipse.

the class LdapLoginModule method getUserRolesByDn.

private List<String> getUserRolesByDn(DirContext dirContext, String userDn) throws LoginException, NamingException {
    List<String> roleList = new ArrayList<String>();
    if (dirContext == null || _roleBaseDn == null || _roleMemberAttribute == null || _roleObjectClass == null) {
        return roleList;
    }
    SearchControls ctls = new SearchControls();
    ctls.setDerefLinkFlag(true);
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setReturningAttributes(new String[] { _roleNameAttribute });
    String filter = "(&(objectClass={0})({1}={2}))";
    Object[] filterArguments = { _roleObjectClass, _roleMemberAttribute, userDn };
    NamingEnumeration<SearchResult> results = dirContext.search(_roleBaseDn, filter, filterArguments, ctls);
    LOG.debug("Found user roles?: " + results.hasMoreElements());
    while (results.hasMoreElements()) {
        SearchResult result = (SearchResult) results.nextElement();
        Attributes attributes = result.getAttributes();
        if (attributes == null) {
            continue;
        }
        Attribute roleAttribute = attributes.get(_roleNameAttribute);
        if (roleAttribute == null) {
            continue;
        }
        NamingEnumeration<?> roles = roleAttribute.getAll();
        while (roles.hasMore()) {
            roleList.add(roles.next().toString());
        }
    }
    return roleList;
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult)

Example 8 with Attributes

use of javax.naming.directory.Attributes in project eureka by Netflix.

the class DnsResolver method getCNamesFromTxtRecord.

/**
     * Looks up the DNS name provided in the JNDI context.
     */
public static Set<String> getCNamesFromTxtRecord(String discoveryDnsName) throws NamingException {
    Attributes attrs = dirContext.getAttributes(discoveryDnsName, new String[] { TXT_RECORD_TYPE });
    Attribute attr = attrs.get(TXT_RECORD_TYPE);
    String txtRecord = null;
    if (attr != null) {
        txtRecord = attr.get().toString();
    }
    Set<String> cnamesSet = new TreeSet<String>();
    if (txtRecord == null || txtRecord.trim().isEmpty()) {
        return cnamesSet;
    }
    String[] cnames = txtRecord.split(" ");
    Collections.addAll(cnamesSet, cnames);
    return cnamesSet;
}
Also used : Attribute(javax.naming.directory.Attribute) TreeSet(java.util.TreeSet) Attributes(javax.naming.directory.Attributes)

Example 9 with Attributes

use of javax.naming.directory.Attributes in project eureka by Netflix.

the class DnsResolver method resolve.

/**
     * Resolve host name to the bottom A-Record or the latest available CNAME
     *
     * @return resolved host name
     */
public static String resolve(String originalHost) {
    String currentHost = originalHost;
    if (isLocalOrIp(currentHost)) {
        return originalHost;
    }
    try {
        String targetHost = null;
        do {
            Attributes attrs = dirContext.getAttributes(currentHost, new String[] { A_RECORD_TYPE, CNAME_RECORD_TYPE });
            Attribute attr = attrs.get(A_RECORD_TYPE);
            if (attr != null) {
                targetHost = attr.get().toString();
            }
            attr = attrs.get(CNAME_RECORD_TYPE);
            if (attr != null) {
                currentHost = attr.get().toString();
            } else {
                targetHost = currentHost;
            }
        } while (targetHost == null);
        return targetHost;
    } catch (NamingException e) {
        logger.warn("Cannot resolve eureka server address " + currentHost + "; returning original value " + originalHost, e);
        return originalHost;
    }
}
Also used : Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException)

Example 10 with Attributes

use of javax.naming.directory.Attributes in project Openfire by igniterealtime.

the class LdapUserProvider method loadUser.

@Override
public User loadUser(String username) throws UserNotFoundException {
    if (username.contains("@")) {
        if (!XMPPServer.getInstance().isLocal(new JID(username))) {
            throw new UserNotFoundException("Cannot load user of remote server: " + username);
        }
        username = username.substring(0, username.lastIndexOf("@"));
    }
    // Un-escape username.
    username = JID.unescapeNode(username);
    DirContext ctx = null;
    try {
        String userDN = manager.findUserDN(username);
        // Load record.
        String[] attributes = new String[] { manager.getUsernameField(), manager.getNameField(), manager.getEmailField(), "createTimestamp", "modifyTimestamp" };
        ctx = manager.getContext(manager.getUsersBaseDN(username));
        Attributes attrs = ctx.getAttributes(userDN, attributes);
        String name = null;
        Attribute nameField = attrs.get(manager.getNameField());
        if (nameField != null) {
            name = (String) nameField.get();
        }
        String email = null;
        Attribute emailField = attrs.get(manager.getEmailField());
        if (emailField != null) {
            email = (String) emailField.get();
        }
        Date creationDate = new Date();
        Attribute creationDateField = attrs.get("createTimestamp");
        if (creationDateField != null && "".equals(((String) creationDateField.get()).trim())) {
            creationDate = parseLDAPDate((String) creationDateField.get());
        }
        Date modificationDate = new Date();
        Attribute modificationDateField = attrs.get("modifyTimestamp");
        if (modificationDateField != null && "".equals(((String) modificationDateField.get()).trim())) {
            modificationDate = parseLDAPDate((String) modificationDateField.get());
        }
        // Escape the username so that it can be used as a JID.
        username = JID.escapeNode(username);
        // As defined by RFC5803.
        Attribute authPassword = attrs.get("authPassword");
        User user = new User(username, name, email, creationDate, modificationDate);
        if (authPassword != null) {
            // The authPassword attribute can be multivalued.
            // Not sure if this is the right API to loop through them.
            NamingEnumeration values = authPassword.getAll();
            while (values.hasMore()) {
                Attribute authPasswordValue = (Attribute) values.next();
                String[] parts = ((String) authPasswordValue.get()).split("$");
                String[] authInfo = parts[1].split(":");
                String[] authValue = parts[2].split(":");
                String scheme = parts[0].trim();
                // We only support SCRAM-SHA-1 at the moment.
                if ("SCRAM-SHA-1".equals(scheme)) {
                    int iterations = Integer.valueOf(authInfo[0].trim());
                    String salt = authInfo[1].trim();
                    String storedKey = authValue[0].trim();
                    String serverKey = authValue[1].trim();
                    user.setSalt(salt);
                    user.setStoredKey(storedKey);
                    user.setServerKey(serverKey);
                    user.setIterations(iterations);
                    break;
                }
            }
        }
        return user;
    } catch (Exception e) {
        throw new UserNotFoundException(e);
    } finally {
        try {
            if (ctx != null) {
                ctx.close();
            }
        } catch (Exception ignored) {
        // Ignore.
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) DirContext(javax.naming.directory.DirContext) Date(java.util.Date) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Aggregations

Attributes (javax.naming.directory.Attributes)81 Attribute (javax.naming.directory.Attribute)57 SearchResult (javax.naming.directory.SearchResult)32 BasicAttributes (javax.naming.directory.BasicAttributes)31 NamingException (javax.naming.NamingException)26 BasicAttribute (javax.naming.directory.BasicAttribute)24 SearchControls (javax.naming.directory.SearchControls)23 NamingEnumeration (javax.naming.NamingEnumeration)19 DirContext (javax.naming.directory.DirContext)19 ArrayList (java.util.ArrayList)17 InitialDirContext (javax.naming.directory.InitialDirContext)12 IOException (java.io.IOException)10 Hashtable (java.util.Hashtable)9 HashSet (java.util.HashSet)8 LdapContext (javax.naming.ldap.LdapContext)8 File (java.io.File)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7 Test (org.junit.Test)6 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5