001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * XMLLoginHandler.java
020: *
021: * This class is responsible for authenticating a login request against the
022: * XML user information.
023: */
024:
025: // the package path
026: package com.rift.coad.lib.security.user.xml;
027:
028: // java imports
029: import java.util.Map;
030:
031: // logging import
032: import org.apache.log4j.Logger;
033:
034: // coadunation imports
035: import com.rift.coad.lib.security.login.AuthValues;
036: import com.rift.coad.lib.security.login.LoginHandler;
037: import com.rift.coad.lib.security.login.LoginException;
038: import com.rift.coad.lib.security.login.LoginInfoHandler;
039: import com.rift.coad.lib.security.UserSession;
040:
041: /**
042: * This class is responsible for authenticating a login request against the
043: * XML user information.
044: *
045: * @author Brett Chaldecott
046: */
047: public class XMLLoginHandler implements LoginHandler {
048:
049: // the classes private member variables
050: private Logger log = Logger.getLogger(XMLLoginHandler.class
051: .getName());
052: private Map users = null;
053: private UserSession user = null;
054:
055: /**
056: * Creates a new instance of XMLLoginHandler
057: */
058: public XMLLoginHandler(Map users) {
059: this .users = users;
060: }
061:
062: /**
063: * This method returns a reference to the login manager the interface that
064: * this object was retrieved from.
065: *
066: * @return A reference to the login manager.
067: * @exception LoginException
068: */
069: public UserSession getUserInfo() throws LoginException {
070: if (user == null) {
071: throw new LoginException(
072: "No valid login request has been processed by this object.");
073: }
074: return user;
075: }
076:
077: /**
078: * This method preforms the login for a given user using the login
079: * information supplied to that user.
080: *
081: * @return TRUE if the login succeeded, FALSE if not.
082: * @param loginInfoHandler The handler containing the login information.
083: * @exception LoginException
084: */
085: public boolean login(LoginInfoHandler loginInfoHandler)
086: throws LoginException {
087: try {
088: Map parameters = loginInfoHandler.getInfo();
089: String username = (String) parameters
090: .get(AuthValues.USERNAME);
091: if (username == null) {
092: throw new LoginException(
093: "The login information handler has not supplied the "
094: + "username");
095: }
096: UserData userData = (UserData) users.get(username);
097: if (userData == null) {
098: log.debug("Username [" + username + "] was not found.");
099: return false;
100: }
101: String password = (String) parameters
102: .get(AuthValues.PASSWORD);
103: if (userData.getPassword().equals(password) == false) {
104: return false;
105: }
106: user = userData.getUser();
107: return true;
108: } catch (Exception ex) {
109: throw new LoginException(
110: "Failed to log the user in because : "
111: + ex.getMessage(), ex);
112: }
113: }
114: }
|