001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: DatabaseSessionValidator.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.sessionvalidators;
009:
010: import com.uwyn.rife.authentication.CredentialsManager;
011: import com.uwyn.rife.authentication.RememberManager;
012: import com.uwyn.rife.authentication.SessionAttributes;
013: import com.uwyn.rife.authentication.SessionManager;
014: import com.uwyn.rife.authentication.SessionValidator;
015: import com.uwyn.rife.authentication.exceptions.SessionValidatorException;
016: import com.uwyn.rife.authentication.sessionvalidators.exceptions.SessionValidityCheckErrorException;
017: import com.uwyn.rife.database.Datasource;
018: import com.uwyn.rife.database.DbPreparedStatement;
019: import com.uwyn.rife.database.DbPreparedStatementHandler;
020: import com.uwyn.rife.database.DbQueryManager;
021: import com.uwyn.rife.database.exceptions.DatabaseException;
022: import com.uwyn.rife.database.queries.Select;
023:
024: public abstract class DatabaseSessionValidator extends DbQueryManager
025: implements SessionValidator {
026: public static final int SESSION_INVALID = 0;
027: public static final int SESSION_VALID = 1;
028:
029: protected CredentialsManager mCredentialsManager = null;
030: protected SessionManager mSessionManager = null;
031: protected RememberManager mRememberManager = null;
032:
033: protected DatabaseSessionValidator(Datasource datasource) {
034: super (datasource);
035: }
036:
037: public void setCredentialsManager(
038: CredentialsManager credentialsManager) {
039: assert credentialsManager != null;
040:
041: mCredentialsManager = credentialsManager;
042: }
043:
044: public CredentialsManager getCredentialsManager() {
045: return mCredentialsManager;
046: }
047:
048: public void setSessionManager(SessionManager sessionManager) {
049: assert sessionManager != null;
050:
051: mSessionManager = sessionManager;
052: }
053:
054: public void setRememberManager(RememberManager rememberManager) {
055: assert rememberManager != null;
056:
057: mRememberManager = rememberManager;
058: }
059:
060: public RememberManager getRememberManager() {
061: return mRememberManager;
062: }
063:
064: public SessionManager getSessionManager() {
065: return mSessionManager;
066: }
067:
068: public boolean isAccessAuthorized(int id) {
069: return SESSION_VALID == id;
070: }
071:
072: protected int _validateSession(Select sessionValidityNoRole,
073: Select sessionValidityNoRoleRestrictHostIp,
074: Select sessionValidityRole,
075: Select sessionValidityRoleRestrictHostIp,
076: ProcessSessionValidity processSessionValidity,
077: final String authId, final String hostIp,
078: final SessionAttributes attributes)
079: throws SessionValidatorException {
080: if (null == authId || 0 == authId.length() || null == hostIp
081: || 0 == hostIp.length() || null == attributes) {
082: return SESSION_INVALID;
083: }
084:
085: int result = SESSION_INVALID;
086:
087: Select query = null;
088:
089: // select which query to use according to the role attribute
090: if (attributes.hasAttribute("role")) {
091: if (mSessionManager.getRestrictHostIp()) {
092: query = sessionValidityRoleRestrictHostIp;
093: } else {
094: query = sessionValidityRole;
095: }
096: } else {
097: if (mSessionManager.getRestrictHostIp()) {
098: query = sessionValidityNoRoleRestrictHostIp;
099: } else {
100: query = sessionValidityNoRole;
101: }
102: }
103:
104: // role has been specified, use optimized validity check to limit the amount of db queries
105: try {
106: executeFetchFirst(query, processSessionValidity,
107: new DbPreparedStatementHandler() {
108: public void setParameters(
109: DbPreparedStatement statement) {
110: statement
111: .setString("authId", authId)
112: .setLong(
113: "sessStart",
114: System.currentTimeMillis()
115: - mSessionManager
116: .getSessionDuration());
117:
118: if (attributes.hasAttribute("role")) {
119: statement.setString("role", attributes
120: .getAttribute("role"));
121: }
122: if (mSessionManager.getRestrictHostIp()) {
123: statement.setString("hostIp", hostIp);
124: }
125: }
126: });
127: result = processSessionValidity.getValidity();
128: } catch (DatabaseException e) {
129: throw new SessionValidityCheckErrorException(authId,
130: hostIp, e);
131: }
132:
133: return result;
134: }
135: }
|