001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2007 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.auth.login;
021:
022: import java.io.IOException;
023:
024: import javax.security.auth.callback.Callback;
025: import javax.security.auth.callback.NameCallback;
026: import javax.security.auth.callback.PasswordCallback;
027: import javax.security.auth.callback.UnsupportedCallbackException;
028: import javax.security.auth.login.FailedLoginException;
029: import javax.security.auth.login.LoginException;
030:
031: import org.apache.log4j.Logger;
032:
033: import com.ecyrd.jspwiki.auth.NoSuchPrincipalException;
034: import com.ecyrd.jspwiki.auth.WikiPrincipal;
035: import com.ecyrd.jspwiki.auth.authorize.Role;
036: import com.ecyrd.jspwiki.auth.user.UserDatabase;
037: import com.ecyrd.jspwiki.auth.user.UserProfile;
038:
039: /**
040: * <p>
041: * Logs in a user based on a username, password, and static password file
042: * location. This module must be used with a CallbackHandler (such as
043: * {@link WikiCallbackHandler}) that supports the following Callback types:
044: * </p>
045: * <ol>
046: * <li>{@link javax.security.auth.callback.NameCallback}- supplies the
047: * username</li>
048: * <li>{@link javax.security.auth.callback.PasswordCallback}- supplies the
049: * password</li>
050: * <li>{@link com.ecyrd.jspwiki.auth.login.UserDatabaseCallback}- supplies the
051: * {@link com.ecyrd.jspwiki.auth.user.UserDatabase}</li>
052: * </ol>
053: * <p>
054: * After authentication, a Principals based on the login name will be created
055: * and associated with the Subject.
056: * Also, principals {@link com.ecyrd.jspwiki.auth.authorize.Role#ALL} and
057: * {@link com.ecyrd.jspwiki.auth.authorize.Role#AUTHENTICATED} will be added to
058: * the Subject's principal set.
059: * </p>
060: * @author Andrew Jaquith
061: * @since 2.3
062: */
063: public class UserDatabaseLoginModule extends AbstractLoginModule {
064:
065: private static final Logger log = Logger
066: .getLogger(UserDatabaseLoginModule.class);
067:
068: /**
069: * @see javax.security.auth.spi.LoginModule#login()
070: */
071: public boolean login() throws LoginException {
072: UserDatabaseCallback ucb = new UserDatabaseCallback();
073: NameCallback ncb = new NameCallback("User name");
074: PasswordCallback pcb = new PasswordCallback("Password", false);
075: Callback[] callbacks = new Callback[] { ucb, ncb, pcb };
076: try {
077: m_handler.handle(callbacks);
078: UserDatabase db = ucb.getUserDatabase();
079: String username = ncb.getName();
080: String password = new String(pcb.getPassword());
081:
082: // Look up the user and compare the password hash
083: if (db == null) {
084: throw new FailedLoginException(
085: "No user database: check the callback handler code!");
086: }
087: UserProfile profile = db.findByLoginName(username);
088: String storedPassword = profile.getPassword();
089: if (storedPassword != null
090: && db.validatePassword(username, password)) {
091: if (log.isDebugEnabled()) {
092: log.debug("Logged in loginName=" + username);
093: log
094: .debug("Added Principals Role.AUTHENTICATED,Role.ALL");
095: }
096:
097: // If login succeeds, commit these principals/roles
098: m_principals.add(new PrincipalWrapper(
099: new WikiPrincipal(username,
100: WikiPrincipal.LOGIN_NAME)));
101: m_principals.add(Role.AUTHENTICATED);
102: m_principals.add(Role.ALL);
103:
104: // If login succeeds, overwrite these principals/roles
105: m_principalsToOverwrite.add(WikiPrincipal.GUEST);
106: m_principalsToOverwrite.add(Role.ANONYMOUS);
107: m_principalsToOverwrite.add(Role.ASSERTED);
108:
109: // If login fails, remove these roles
110: m_principalsToRemove.add(Role.AUTHENTICATED);
111:
112: return true;
113: }
114: throw new FailedLoginException(
115: "The username or password is incorrect.");
116: } catch (IOException e) {
117: String message = "IO exception; disallowing login.";
118: log.error(message, e);
119: throw new LoginException(message);
120: } catch (UnsupportedCallbackException e) {
121: String message = "Unable to handle callback; disallowing login.";
122: log.error(message, e);
123: throw new LoginException(message);
124: } catch (NoSuchPrincipalException e) {
125: throw new FailedLoginException(
126: "The username or password is incorrect.");
127: }
128: }
129:
130: }
|