01: /*
02: * Copyright 2001-2007 Steven Grimm <koreth[remove] at midwinter dot com> and
03: * Geert Bevin <gbevin[remove] at uwyn dot com>
04: * Distributed under the terms of either:
05: * - the common development and distribution license (CDDL), v1.0; or
06: * - the GNU Lesser General Public License, v2.1 or later
07: * $Id$
08: */
09: package com.uwyn.rife.authentication.sessionvalidators;
10:
11: import com.uwyn.rife.authentication.SessionAttributes;
12: import com.uwyn.rife.authentication.SessionManager;
13: import com.uwyn.rife.authentication.credentialsmanagers.RoleUsersManager;
14: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
15: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
16: import com.uwyn.rife.authentication.exceptions.SessionValidatorException;
17: import com.uwyn.rife.authentication.sessionvalidators.exceptions.RoleCheckErrorException;
18: import com.uwyn.rife.authentication.sessionvalidators.exceptions.SessionValidityCheckErrorException;
19:
20: /**
21: * Non-optimized session validator. This is a naive implementation of the
22: * {@link com.uwyn.rife.authentication.SessionValidator} interface, suitable for cases where there is no
23: * need for optimization of session validity checking. For example, it is
24: * used as the session validator for RIFE's built-in "mixed" and "memory"
25: * authentication elements.
26: *
27: * @author Steven Grimm (koreth[remove] at midwinter dot com)
28: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
29: * @version $Revision: $
30: * @see com.uwyn.rife.authentication.SessionValidator
31: * @since 1.6
32: */
33: public class BasicSessionValidator extends AbstractSessionValidator {
34: public boolean isAccessAuthorized(int id) {
35: return SESSION_VALID == id;
36: }
37:
38: public int validateSession(String authId, String hostIp,
39: SessionAttributes attributes)
40: throws SessionValidatorException {
41: if (null == authId || 0 == authId.length() || null == hostIp
42: || 0 == hostIp.length() || null == attributes) {
43: return SESSION_INVALID;
44: }
45:
46: SessionManager sessions = getSessionManager();
47:
48: try {
49: if (!sessions.isSessionValid(authId, hostIp)) {
50: return SESSION_INVALID;
51: }
52: } catch (SessionManagerException e) {
53: throw new SessionValidityCheckErrorException(authId,
54: hostIp, e);
55: }
56:
57: if (attributes.hasAttribute("role")) {
58: long user_id = -1;
59: String role = attributes.getAttribute("role");
60:
61: try {
62: user_id = sessions.getSessionUserId(authId);
63: } catch (SessionManagerException e) {
64: user_id = -1;
65: }
66:
67: if (-1 == user_id) {
68: return SESSION_INVALID;
69: }
70:
71: try {
72: if (mCredentialsManager instanceof RoleUsersManager
73: && !((RoleUsersManager) mCredentialsManager)
74: .isUserInRole(user_id, attributes
75: .getAttribute("role"))) {
76: return SESSION_INVALID;
77: }
78: } catch (CredentialsManagerException e) {
79: throw new RoleCheckErrorException(authId, hostIp, role,
80: e);
81: }
82:
83: return SESSION_VALID;
84: }
85:
86: return SESSION_VALID;
87: }
88:
89: }
|