001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.dav.server.managers;
021:
022: import java.util.logging.*;
023:
024: import org.openharmonise.commons.cache.CacheException;
025: import org.openharmonise.commons.dsi.*;
026: import org.openharmonise.rm.DataAccessException;
027: import org.openharmonise.rm.dsi.DataStoreInterfaceFactory;
028: import org.openharmonise.rm.metadata.InvalidPropertyInstanceException;
029: import org.openharmonise.rm.resources.users.User;
030: import org.openharmonise.rm.security.authentication.*;
031: import org.openharmonise.rm.security.authorization.*;
032: import org.openharmonise.rm.sessions.*;
033:
034: import com.ibm.webdav.*;
035: import com.ibm.webdav.impl.*;
036: import com.ibm.webdav.impl.UserAuthenticator;
037:
038: /**
039: * Harmonise implementation of <code>UserAuthenticator</code> providing
040: * DAV4J with access to user authenticating functionality of Harmonise.
041: *
042: * @author Michael Bell
043: * @version $Revision: 1.2 $
044: *
045: */
046: public class HarmoniseSessionManager implements UserAuthenticator {
047: private AbstractDataStoreInterface m_dsi = null;
048:
049: /**
050: * Logger for this class
051: */
052: private static final Logger m_logger = Logger
053: .getLogger(HarmoniseSessionManager.class.getName());
054:
055: public HarmoniseSessionManager() {
056: try {
057: m_dsi = DataStoreInterfaceFactory.getDataStoreInterface();
058: } catch (DataStoreException e) {
059: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
060: }
061: }
062:
063: /* (non-Javadoc)
064: * @see com.ibm.webdav.impl.UserAuthenticator#authenticate(java.lang.String, java.lang.String)
065: */
066: public boolean authenticate(String user, String pwd)
067: throws WebDAVException {
068: boolean bIsValid = false;
069:
070: try {
071: if ((user != null) & (pwd != null)) {
072: org.openharmonise.rm.security.authentication.UserAuthenticator auth = UserAuthenticatorFactory
073: .getAuthenticator();
074:
075: User usr = auth.getUser(user);
076:
077: //password must be correct, user must have a role
078: //and must not be a browser
079: if (auth.authenticate(usr, pwd) == true
080: && (AuthorizationValidator.isSuperUser(usr) || (AuthorizationValidator
081: .getUserRoles(usr).size() > 0 && AuthorizationValidator
082: .isBrowser(usr) == false))) {
083:
084: String session_key = getSessionCacheKey(user, pwd);
085:
086: Session session = SessionCache.getInstance(
087: this .m_dsi).getSession(session_key);
088:
089: if (session == null) {
090: if (m_logger.isLoggable(Level.FINE)) {
091: m_logger.logp(Level.FINE, this .getClass()
092: .getName(), "authenticate",
093: "Looking for user - " + user + "("
094: + pwd + ")");
095: }
096:
097: session = new Session(m_dsi, usr, session_key,
098: 99999999);
099:
100: SessionCache.getInstance(this .m_dsi)
101: .addToCache(session_key, session);
102:
103: }
104:
105: bIsValid = true;
106:
107: }
108: }
109: } catch (SessionException e) {
110: m_logger.log(Level.WARNING, e.getMessage(), e);
111: throw new WebDAVException(
112: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
113: .getLocalizedMessage());
114: } catch (CacheException e) {
115: throw new WebDAVException(
116: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
117: .getLocalizedMessage());
118: } catch (PasswordExpiredException e) {
119: bIsValid = false;
120: } catch (LoginRetryLimitException e) {
121: bIsValid = false;
122: } catch (UserAuthenticationException e) {
123: throw new WebDAVException(
124: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
125: .getLocalizedMessage());
126: } catch (AuthorizationException e) {
127: throw new WebDAVException(
128: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
129: .getLocalizedMessage());
130: } catch (InvalidPropertyInstanceException e) {
131: throw new WebDAVException(
132: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
133: .getLocalizedMessage());
134: } catch (DataAccessException e) {
135: m_logger.log(Level.WARNING, e.getMessage(), e);
136: throw new WebDAVException(
137: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
138: .getLocalizedMessage());
139: }
140:
141: if (m_logger.isLoggable(Level.FINE)) {
142: m_logger.logp(Level.FINE, this .getClass().getName(),
143: "authenticate", "User " + user
144: + " authenticated - " + bIsValid);
145: }
146:
147: return bIsValid;
148: }
149:
150: /**
151: * Returns the appropriate session cache key for the user with
152: * the given username and password.
153: *
154: * @param user the user name
155: * @param pwd the user password
156: * @return the appropriate session cache key
157: */
158: private String getSessionCacheKey(String user, String pwd) {
159: String session_key = "dav_" + user.hashCode() + pwd.hashCode();
160: return session_key;
161: }
162:
163: /**
164: * Returns the Harmonise user assocaited to this session.
165: *
166: * @return
167: * @throws WebDAVException
168: */
169: public User getUser(ResourceImpl resource) throws WebDAVException {
170: User usr = null;
171:
172: try {
173: HTTPHeaders requestContext = resource.getContext()
174: .getRequestContext();
175:
176: String user = requestContext.getAuthorizationId();
177: String password = requestContext.getPassword();
178: String session_key = getSessionCacheKey(user, password);
179:
180: Session session = SessionCache.getInstance(this .m_dsi)
181: .getSession(session_key);
182:
183: if (session != null) {
184: try {
185: usr = session.getUser();
186: } catch (DataAccessException e) {
187: throw new WebDAVException(
188: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
189: .getLocalizedMessage());
190: }
191: }
192: } catch (CacheException e) {
193: throw new WebDAVException(
194: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
195: .getLocalizedMessage());
196: }
197:
198: return usr;
199: }
200:
201: /* (non-Javadoc)
202: * @see com.ibm.webdav.impl.UserAuthenticator#isSuperUser()
203: */
204: public boolean isSuperUser(ResourceImpl resource)
205: throws WebDAVException {
206: boolean bIsSuper = false;
207:
208: try {
209: bIsSuper = AuthorizationValidator
210: .isSuperUser(getUser(resource));
211: } catch (AuthorizationException e) {
212: throw new WebDAVException(
213: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
214: .getLocalizedMessage());
215: }
216:
217: return bIsSuper;
218: }
219: }
|