SEC-588: Completed JdbcTokenRepositoryImpl and added extra update method to PersistentTokenRepository interface (additional files from failed commit).

This commit is contained in:
Luke Taylor 2007-11-10 15:56:07 +00:00
parent 7caa1587b3
commit 1a5ef2dece
1 changed files with 17 additions and 5 deletions

View File

@ -2,33 +2,45 @@ package org.springframework.security.ui.rememberme;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
/** /**
* Simple <tt>PersistentTokenRepository</tt> implementation backed by a Map. Intended for testing only.
*
* @author Luke Taylor * @author Luke Taylor
* @version $Id$ * @version $Id$
*/ */
public class InMemoryTokenRepositoryImpl implements PersistentTokenRepository { public class InMemoryTokenRepositoryImpl implements PersistentTokenRepository {
private Map seriesTokens = new HashMap(); private Map seriesTokens = new HashMap();
public synchronized void saveToken(PersistentRememberMeToken token) { public synchronized void createNewToken(PersistentRememberMeToken token) {
PersistentRememberMeToken current = (PersistentRememberMeToken) seriesTokens.get(token.getSeries()); PersistentRememberMeToken current = (PersistentRememberMeToken) seriesTokens.get(token.getSeries());
if (current != null && !token.getUsername().equals(current.getUsername())) { if (current != null) {
throw new DataIntegrityViolationException("Series Id already exists with different username"); throw new DataIntegrityViolationException("Series Id '"+ token.getSeries() +"' already exists!");
} }
// Store it, overwriting the existing one.
seriesTokens.put(token.getSeries(), token); seriesTokens.put(token.getSeries(), token);
} }
public synchronized void updateToken(String series, String tokenValue, Date lastUsed) {
PersistentRememberMeToken token = getTokenForSeries(series);
PersistentRememberMeToken newToken = new PersistentRememberMeToken(token.getUsername(), series, tokenValue,
new Date());
// Store it, overwriting the existing one.
seriesTokens.put(series, newToken);
}
public synchronized PersistentRememberMeToken getTokenForSeries(String seriesId) { public synchronized PersistentRememberMeToken getTokenForSeries(String seriesId) {
return (PersistentRememberMeToken) seriesTokens.get(seriesId); return (PersistentRememberMeToken) seriesTokens.get(seriesId);
} }
public synchronized void removeAllTokens(String username) { public synchronized void removeUserTokens(String username) {
Iterator series = seriesTokens.keySet().iterator(); Iterator series = seriesTokens.keySet().iterator();
while (series.hasNext()) { while (series.hasNext()) {