LdapAuthProvider.java 3.87 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17
package org.jivesoftware.messenger.ldap;

import org.jivesoftware.messenger.auth.AuthProvider;
import org.jivesoftware.messenger.auth.UnauthorizedException;

/**
18 19
 * Implementation of auth provider interface for LDAP
 * authentication service plug-in.
Matt Tucker's avatar
Matt Tucker committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
 *
 * @author Jim Berrettini
 */
public class LdapAuthProvider implements AuthProvider {
    private LdapManager manager;

    public LdapAuthProvider() {
        manager = LdapManager.getInstance();
    }

    /**
     * <p>Determines if the authentication system supports the use of
     * plain-text passwords.</p>
     *
     * @return true - plain text passwords are supported.
     */
    public boolean isPlainSupported() {
        return true;
    }

    /**
     * <p>Determines if the authentication system supports the use
     * of digest authentication.</p>
     *
     * @return false - digest authentication is not currently supported.
     */
    public boolean isDigestSupported() {
        return false;
    }

    /**
     * <p>Validates username and password against data in LDAP store. Returns if the username and password are valid otherwise the method throws an
     * UnauthorizedException.<p>
     *
     * @param username
     * @param password
     * @throws UnauthorizedException
     */
    public void authenticate(String username, String password) throws UnauthorizedException {
        if (username == null || password == null) {
            throw new UnauthorizedException();
        }
        String userDN = null;
        try {
            // The username by itself won't help us much with LDAP since we
            // need a fully qualified dn. We could make the assumption that
            // the baseDN would always be the location of user profiles. For
            // example if the baseDN was set to "ou=People, o=jivesoftare, o=com"
            // then we would be able to directly load users from that node
            // of the LDAP tree. However, it's a poor assumption that only a
            // flat structure will be used. Therefore, we search all subtrees
            // of the baseDN for the username. So, if the baseDN is set to
            // "o=jivesoftware, o=com" then a search will include the "People"
            // node as well all the others under the base.
            userDN = manager.findUserDN(username);

            // See if the user authenticates.
            if (!manager.checkAuthentication(userDN, password)) {
                throw new UnauthorizedException("Username and password don't match");
            }
        }
        catch (Exception e) {
            throw new UnauthorizedException(e);
        }
    }

    /**
     * <p>Validates username, unique session token, and digest generated from the
     * password and token according to the Jabber digest auth protocol against data in LDAP store.
     * Since Digest authentication is not currently supported, throws UnsupportedOperationsException.</p>
     *
     * @param username
     * @param token
     * @param digest
     * @throws UnsupportedOperationException
     */
    public void authenticate(String username, String token, String digest) throws UnsupportedOperationException {
        throw new UnsupportedOperationException("Digest authentication not currently supported.");
    }

    /**
     * <p>Updates username and password. Throws UnsupportedOperationException, as LDAP store is accessed in read-only
     * mode.</p>
     *
     * @param username
     * @param password
     * @throws UnsupportedOperationException
     */
    public void updatePassword(String username, String password) throws UnsupportedOperationException {
        throw new UnsupportedOperationException("Cannot update password in LDAP");
    }
}