001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software 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 GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021: package org.josso.gateway.identity.service;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.josso.Lookup;
026: import org.josso.auth.Authenticator;
027: import org.josso.auth.AuthenticatorImpl;
028: import org.josso.gateway.identity.SSORole;
029: import org.josso.gateway.identity.SSOUser;
030: import org.josso.gateway.identity.exceptions.NoSuchUserException;
031: import org.josso.gateway.identity.exceptions.SSOIdentityException;
032: import org.josso.gateway.identity.service.store.IdentityStore;
033: import org.josso.gateway.identity.service.store.IdentityStoreKeyAdapter;
034: import org.josso.gateway.identity.service.store.SimpleUserKey;
035: import org.josso.gateway.identity.service.store.UserKey;
036: import org.josso.gateway.session.exceptions.NoSuchSessionException;
037: import org.josso.gateway.session.exceptions.SSOSessionException;
038: import org.josso.gateway.session.service.BaseSession;
039: import org.josso.gateway.session.service.SSOSessionManager;
040:
041: /**
042: * This is the default implementation of an SSOIdentityManager.
043: * This implementation keeps track of user and session associations in memory.
044: *
045: * @author <a href="mailto:sgonzalez@josso.org">Sebastian Gonzalez Oyuela</a>
046: * @version $Id: SSOIdentityManagerImpl.java 508 2008-02-18 13:32:29Z sgonzalez $
047: */
048:
049: public class SSOIdentityManagerImpl implements SSOIdentityManager {
050:
051: private static final Log logger = LogFactory
052: .getLog(SSOIdentityManagerImpl.class);
053:
054: // Identity store used by the manager.
055: private IdentityStore _store;
056: private IdentityStoreKeyAdapter _keyAdapter;
057: private SSOSessionManager _sessionManager;
058:
059: /**
060: *
061: */
062: public SSOIdentityManagerImpl() {
063: }
064:
065: /**
066: * Finds a user based on its name.
067: *
068: * @param name the user login name, wich is unique for a domain.
069: *
070: *
071: * @throws NoSuchUserException if the user does not exist for the domain.
072: */
073: public SSOUser findUser(String name) throws NoSuchUserException,
074: SSOIdentityException {
075:
076: // Find user in store
077: UserKey key = getIdentityStoreKeyAdapter().getKeyForUsername(
078: name);
079: BaseUser user = getIdentityStore().loadUser(key);
080: if (user == null)
081: throw new NoSuchUserException(key);
082:
083: // Done ... user found.
084: return user;
085: }
086:
087: /**
088: * Finds the user associated to a sso session
089: *
090: * @param sessionId the sso session identifier
091: *
092: * @throws SSOIdentityException if no user is associated to this session id.
093: */
094: public SSOUser findUserInSession(String sessionId)
095: throws SSOIdentityException {
096:
097: BaseUser user = null;
098: UserKey key = null;
099:
100: try {
101: BaseSession s = (BaseSession) getSessionManager()
102: .getSession(sessionId);
103: key = new SimpleUserKey(s.getUsername());
104: user = getIdentityStore().loadUser(key);
105:
106: if (logger.isDebugEnabled())
107: logger.debug("[findUserInSession(" + sessionId
108: + ")] Found : " + user);
109:
110: return user;
111:
112: } catch (NoSuchSessionException e) {
113: throw new SSOIdentityException("Invalid session : "
114: + sessionId);
115:
116: } catch (SSOSessionException e) {
117: throw new SSOIdentityException(e.getMessage(), e);
118: }
119:
120: }
121:
122: /**
123: * Finds a collection of user's roles.
124: * Elements in the collection are SSORole instances.
125: *
126: * @param username
127: *
128: * @throws SSOIdentityException
129: */
130: public SSORole[] findRolesByUsername(String username)
131: throws SSOIdentityException {
132:
133: UserKey key = getIdentityStoreKeyAdapter().getKeyForUsername(
134: username);
135: return getIdentityStore().findRolesByUserKey(key);
136: }
137:
138: /**
139: * Checks if current user exists in this manager.
140: *
141: * @throws NoSuchUserException if the user does not exists.
142: * @throws SSOIdentityException if an error occurs
143: */
144: public void userExists(String username) throws NoSuchUserException,
145: SSOIdentityException {
146: UserKey key = getIdentityStoreKeyAdapter().getKeyForUsername(
147: username);
148: if (!getIdentityStore().userExists(key))
149: throw new NoSuchUserException(key);
150: }
151:
152: // --------------------------------------------------------------------
153: // Public utils
154: // --------------------------------------------------------------------
155:
156: /**
157: * Used to set the store for this manager.
158: * @param s
159: */
160: public void setIdentityStore(IdentityStore s) {
161: _store = s;
162: }
163:
164: public void setIdentityStoreKeyAdapter(IdentityStoreKeyAdapter a) {
165: _keyAdapter = a;
166: }
167:
168: public void initialize() {
169:
170: }
171:
172: // --------------------------------------------------------------------
173: // Protected utils
174: // --------------------------------------------------------------------
175:
176: protected IdentityStore getIdentityStore() {
177: return _store;
178: }
179:
180: protected IdentityStoreKeyAdapter getIdentityStoreKeyAdapter() {
181: return _keyAdapter;
182: }
183:
184: protected Authenticator getAuthenticator() {
185: return new AuthenticatorImpl();
186: }
187:
188: protected SSOSessionManager getSessionManager() {
189:
190: if (_sessionManager == null) {
191:
192: try {
193: _sessionManager = Lookup.getInstance()
194: .lookupSecurityDomain().getSessionManager();
195: } catch (Exception e) {
196: logger.error("Can't find Session Manager : \n"
197: + e.getMessage() != null ? e.getMessage() : e
198: .toString(), e);
199: }
200: }
201:
202: return _sessionManager;
203: }
204:
205: }
|