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: DatabaseRemember.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.remembermanagers;
009:
010: import com.uwyn.rife.authentication.remembermanagers.exceptions.*;
011:
012: import com.uwyn.rife.authentication.RememberManager;
013: import com.uwyn.rife.authentication.exceptions.RememberManagerException;
014: import com.uwyn.rife.config.RifeConfig;
015: import com.uwyn.rife.database.Datasource;
016: import com.uwyn.rife.database.DbPreparedStatement;
017: import com.uwyn.rife.database.DbPreparedStatementHandler;
018: import com.uwyn.rife.database.DbQueryManager;
019: import com.uwyn.rife.database.exceptions.DatabaseException;
020: import com.uwyn.rife.database.queries.CreateTable;
021: import com.uwyn.rife.database.queries.Delete;
022: import com.uwyn.rife.database.queries.DropTable;
023: import com.uwyn.rife.database.queries.Insert;
024: import com.uwyn.rife.database.queries.Select;
025: import com.uwyn.rife.tools.StringEncryptor;
026: import com.uwyn.rife.tools.UniqueID;
027: import com.uwyn.rife.tools.UniqueIDGenerator;
028: import java.security.NoSuchAlgorithmException;
029:
030: public abstract class DatabaseRemember extends DbQueryManager implements
031: RememberManager {
032: private long mRememberDuration = RifeConfig.Authentication
033: .getRememberDuration();
034:
035: protected DatabaseRemember(Datasource datasource) {
036: super (datasource);
037: }
038:
039: public long getRememberDuration() {
040: return mRememberDuration;
041: }
042:
043: public void setRememberDuration(long milliseconds) {
044: mRememberDuration = milliseconds;
045: }
046:
047: public abstract boolean install() throws RememberManagerException;
048:
049: public abstract boolean remove() throws RememberManagerException;
050:
051: protected boolean _install(CreateTable createRemember,
052: String createRememberMomentIndex) {
053: executeUpdate(createRemember);
054: executeUpdate(createRememberMomentIndex);
055:
056: return true;
057: }
058:
059: protected boolean _remove(DropTable removeRemember,
060: String removeRememberMomentIndex) {
061: executeUpdate(removeRememberMomentIndex);
062: executeUpdate(removeRemember);
063:
064: return true;
065: }
066:
067: protected String _createRememberId(Insert createRememberId,
068: final long userId, String hostIp)
069: throws RememberManagerException {
070: assert createRememberId != null;
071:
072: if (userId < 0) {
073: throw new CreateRememberIdErrorException(userId);
074: }
075:
076: final UniqueID remember_id = UniqueIDGenerator.generate(hostIp);
077: final String remember_id_string = remember_id.toString();
078:
079: try {
080: if (0 == executeUpdate(createRememberId,
081: new DbPreparedStatementHandler() {
082: public void setParameters(
083: DbPreparedStatement statement) {
084: statement.setString("rememberId",
085: remember_id_string).setLong(
086: "userId", userId).setLong("moment",
087: System.currentTimeMillis());
088: }
089: })) {
090: throw new CreateRememberIdErrorException(userId);
091: }
092: } catch (DatabaseException e) {
093: throw new CreateRememberIdErrorException(userId, e);
094: }
095:
096: try {
097: return StringEncryptor.SHA.encrypt(String.valueOf(userId))
098: + "|" + remember_id_string;
099: } catch (NoSuchAlgorithmException e) {
100: throw new CreateRememberIdErrorException(userId, e);
101: }
102: }
103:
104: protected boolean _eraseRememberId(Delete eraseRememberId,
105: final String rememberId) throws RememberManagerException {
106: assert eraseRememberId != null;
107:
108: if (null == rememberId || 0 == rememberId.length()) {
109: return false;
110: }
111:
112: final int rememberid_slash = rememberId.indexOf("|");
113: if (-1 == rememberid_slash) {
114: return false;
115: }
116:
117: boolean result = false;
118: try {
119: if (0 != executeUpdate(eraseRememberId,
120: new DbPreparedStatementHandler() {
121: public void setParameters(
122: DbPreparedStatement statement) {
123: statement
124: .setString(
125: "rememberId",
126: rememberId
127: .substring(rememberid_slash + 1));
128: }
129: })) {
130: result = true;
131: }
132: } catch (DatabaseException e) {
133: throw new EraseRememberIdErrorException(rememberId, e);
134: }
135:
136: return result;
137: }
138:
139: protected boolean _eraseUserRememberIds(
140: Delete eraseUserRememberIds, final long userId)
141: throws RememberManagerException {
142: assert eraseUserRememberIds != null;
143:
144: if (userId < 0) {
145: return false;
146: }
147:
148: boolean result = false;
149: try {
150: if (0 != executeUpdate(eraseUserRememberIds,
151: new DbPreparedStatementHandler() {
152: public void setParameters(
153: DbPreparedStatement statement) {
154: statement.setLong("userId", userId);
155: }
156: })) {
157: result = true;
158: }
159: } catch (DatabaseException e) {
160: throw new EraseUserRememberIdsErrorException(userId, e);
161: }
162:
163: return result;
164: }
165:
166: protected void _eraseAllRememberIds(Delete eraseAllRememberIds)
167: throws RememberManagerException {
168: assert eraseAllRememberIds != null;
169:
170: try {
171: executeUpdate(eraseAllRememberIds);
172: } catch (DatabaseException e) {
173: throw new EraseAllRememberIdsErrorException(e);
174: }
175: }
176:
177: protected long _getRememberedUserId(Select getRememberedUserId,
178: final String rememberId) throws RememberManagerException {
179: assert getRememberedUserId != null;
180:
181: if (null == rememberId || 0 == rememberId.length()) {
182: return -1;
183: }
184:
185: final int rememberid_slash = rememberId.indexOf("|");
186: if (-1 == rememberid_slash) {
187: return -1;
188: }
189:
190: final String encrypted_userid = rememberId.substring(0,
191: rememberid_slash);
192: final String real_rememberid = rememberId
193: .substring(rememberid_slash + 1);
194:
195: long result = -1;
196:
197: try {
198: result = executeGetFirstLong(getRememberedUserId,
199: new DbPreparedStatementHandler() {
200: public void setParameters(
201: DbPreparedStatement statement) {
202: statement.setString("rememberId",
203: real_rememberid);
204: }
205: });
206: } catch (DatabaseException e) {
207: throw new GetRememberedUserIdErrorException(rememberId, e);
208: }
209:
210: try {
211: if (!encrypted_userid.equals(StringEncryptor.SHA
212: .encrypt(String.valueOf(result)))) {
213: return -1;
214: }
215: } catch (NoSuchAlgorithmException e) {
216: throw new GetRememberedUserIdErrorException(rememberId, e);
217: }
218:
219: return result;
220: }
221:
222: protected void _purgeRememberIds(Delete purgeRememberIds)
223: throws RememberManagerException {
224: try {
225: executeUpdate(purgeRememberIds,
226: new DbPreparedStatementHandler() {
227: public void setParameters(
228: DbPreparedStatement statement) {
229: statement.setLong(1, System
230: .currentTimeMillis()
231: - getRememberDuration());
232: }
233: });
234: } catch (DatabaseException e) {
235: throw new PurgeRememberIdsErrorException(e);
236: }
237: }
238: }
|