DefaultAuthProvider.java 8.52 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: 1116 $
 * $Date: 2005-03-10 20:18:08 -0300 (Thu, 10 Mar 2005) $
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19 20
 */

21
package org.jivesoftware.openfire.auth;
22

23 24 25 26 27 28
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

29
import org.jivesoftware.database.DbConnectionManager;
30
import org.jivesoftware.openfire.XMPPServer;
31 32 33 34
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.JiveGlobals;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
35 36

/**
37
 * Default AuthProvider implementation. It authenticates against the <tt>ofUser</tt>
38 39 40 41 42 43 44 45 46
 * database table and supports plain text and digest authentication.
 *
 * Because each call to authenticate() makes a database connection, the
 * results of authentication should be cached whenever possible.
 *
 * @author Matt Tucker
 */
public class DefaultAuthProvider implements AuthProvider {

47 48
	private static final Logger Log = LoggerFactory.getLogger(DefaultAuthProvider.class);

49
    private static final String LOAD_PASSWORD =
50
            "SELECT plainPassword,encryptedPassword FROM ofUser WHERE username=?";
51
    private static final String UPDATE_PASSWORD =
52
            "UPDATE ofUser SET plainPassword=?, encryptedPassword=? WHERE username=?";
53 54 55 56 57

    /**
     * Constructs a new DefaultAuthProvider.
     */
    public DefaultAuthProvider() {
58

59
    }
60 61 62 63 64 65

    public void authenticate(String username, String password) throws UnauthorizedException {
        if (username == null || password == null) {
            throw new UnauthorizedException();
        }
        username = username.trim().toLowerCase();
66 67 68 69
        if (username.contains("@")) {
            // Check that the specified domain matches the server's domain
            int index = username.indexOf("@");
            String domain = username.substring(index + 1);
70
            if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
71 72 73 74 75 76
                username = username.substring(0, index);
            } else {
                // Unknown domain. Return authentication failed.
                throw new UnauthorizedException();
            }
        }
77
        try {
78
            if (!password.equals(getPassword(username))) {
79 80 81
                throw new UnauthorizedException();
            }
        }
82
        catch (UserNotFoundException unfe) {
83 84 85 86 87 88 89 90 91 92
            throw new UnauthorizedException();
        }
        // Got this far, so the user must be authorized.
    }

    public void authenticate(String username, String token, String digest) throws UnauthorizedException {
        if (username == null || token == null || digest == null) {
            throw new UnauthorizedException();
        }
        username = username.trim().toLowerCase();
93 94 95 96
        if (username.contains("@")) {
            // Check that the specified domain matches the server's domain
            int index = username.indexOf("@");
            String domain = username.substring(index + 1);
97
            if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
98 99 100 101 102 103
                username = username.substring(0, index);
            } else {
                // Unknown domain. Return authentication failed.
                throw new UnauthorizedException();
            }
        }
104
        try {
105
            String password = getPassword(username);
106
            String anticipatedDigest = AuthFactory.createDigest(token, password);
107 108 109 110
            if (!digest.equalsIgnoreCase(anticipatedDigest)) {
                throw new UnauthorizedException();
            }
        }
111
        catch (UserNotFoundException unfe) {
112 113 114 115 116 117 118 119 120 121 122 123
            throw new UnauthorizedException();
        }
        // Got this far, so the user must be authorized.
    }

    public boolean isPlainSupported() {
        return true;
    }

    public boolean isDigestSupported() {
        return true;
    }
124 125 126 127 128 129 130 131

    public String getPassword(String username) throws UserNotFoundException {
        if (!supportsPasswordRetrieval()) {
            // Reject the operation since the provider is read-only
            throw new UnsupportedOperationException();
        }
        Connection con = null;
        PreparedStatement pstmt = null;
132
        ResultSet rs = null;
133 134 135 136
        if (username.contains("@")) {
            // Check that the specified domain matches the server's domain
            int index = username.indexOf("@");
            String domain = username.substring(index + 1);
137
            if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
138 139 140 141 142 143
                username = username.substring(0, index);
            } else {
                // Unknown domain.
                throw new UserNotFoundException();
            }
        }
144 145 146 147
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_PASSWORD);
            pstmt.setString(1, username);
148
            rs = pstmt.executeQuery();
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            if (!rs.next()) {
                throw new UserNotFoundException(username);
            }
            String plainText = rs.getString(1);
            String encrypted = rs.getString(2);
            if (encrypted != null) {
                try {
                    return AuthFactory.decryptPassword(encrypted);
                }
                catch (UnsupportedOperationException uoe) {
                    // Ignore and return plain password instead.
                }
            }
            return plainText;
        }
        catch (SQLException sqle) {
            throw new UserNotFoundException(sqle);
        }
        finally {
168
            DbConnectionManager.closeConnection(rs, pstmt, con);
169 170 171 172 173 174 175
        }
    }

    public void setPassword(String username, String password) throws UserNotFoundException {
        // Determine if the password should be stored as plain text or encrypted.
        boolean usePlainPassword = JiveGlobals.getBooleanProperty("user.usePlainPassword");
        String encryptedPassword = null;
176 177 178 179
        if (username.contains("@")) {
            // Check that the specified domain matches the server's domain
            int index = username.indexOf("@");
            String domain = username.substring(index + 1);
180
            if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
181 182 183 184 185 186
                username = username.substring(0, index);
            } else {
                // Unknown domain.
                throw new UserNotFoundException();
            }
        }
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        if (!usePlainPassword) {
            try {
                encryptedPassword = AuthFactory.encryptPassword(password);
                // Set password to null so that it's inserted that way.
                password = null;
            }
            catch (UnsupportedOperationException uoe) {
                // Encryption may fail. In that case, ignore the error and
                // the plain password will be stored.
            }
        }

        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(UPDATE_PASSWORD);
            if (password == null) {
                pstmt.setNull(1, Types.VARCHAR);
            }
            else {
                pstmt.setString(1, password);
            }
            if (encryptedPassword == null) {
                pstmt.setNull(2, Types.VARCHAR);
            }
            else {
                pstmt.setString(2, encryptedPassword);
            }
            pstmt.setString(3, username);
            pstmt.executeUpdate();
        }
        catch (SQLException sqle) {
            throw new UserNotFoundException(sqle);
        }
        finally {
223
            DbConnectionManager.closeConnection(pstmt, con);
224 225 226 227 228 229
        }
    }

    public boolean supportsPasswordRetrieval() {
        return true;
    }
230
}