Merge "Manually expire web sessions"

This commit is contained in:
Shawn O. Pearce
2012-05-19 20:40:49 -07:00
committed by gerrit code review
2 changed files with 26 additions and 5 deletions

View File

@@ -170,7 +170,7 @@ public final class CacheBasedWebSession implements WebSession {
/** Set the user account for this current request only. */ /** Set the user account for this current request only. */
public void setUserAccountId(Account.Id id) { public void setUserAccountId(Account.Id id) {
key = new Key("id:" + id); key = new Key("id:" + id);
val = new Val(id, 0, false, null, ""); val = new Val(id, 0, false, null, "", 0);
} }
public void logout() { public void logout() {

View File

@@ -43,6 +43,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;
@Singleton @Singleton
class WebSessionManager { class WebSessionManager {
@@ -104,7 +105,9 @@ class WebSessionManager {
final long halfAgeRefresh = sessionMaxAgeMillis >>> 1; final long halfAgeRefresh = sessionMaxAgeMillis >>> 1;
final long minRefresh = MILLISECONDS.convert(1, HOURS); final long minRefresh = MILLISECONDS.convert(1, HOURS);
final long refresh = Math.min(halfAgeRefresh, minRefresh); final long refresh = Math.min(halfAgeRefresh, minRefresh);
final long refreshCookieAt = now() + refresh; final long now = now();
final long refreshCookieAt = now + refresh;
final long expiresAt = now + sessionMaxAgeMillis;
if (xsrfToken == null) { if (xsrfToken == null) {
// If we don't yet have a token for this session, establish one. // If we don't yet have a token for this session, establish one.
@@ -115,7 +118,8 @@ class WebSessionManager {
xsrfToken = CookieBase64.encode(rnd); xsrfToken = CookieBase64.encode(rnd);
} }
Val val = new Val(who, refreshCookieAt, remember, lastLogin, xsrfToken); Val val = new Val(who, refreshCookieAt, remember,
lastLogin, xsrfToken, expiresAt);
self.put(key, val); self.put(key, val);
return val; return val;
} }
@@ -137,7 +141,12 @@ class WebSessionManager {
} }
Val get(final Key key) { Val get(final Key key) {
return self.get(key); Val val = self.get(key);
if (val != null && val.expiresAt <= now()) {
self.remove(key);
return null;
}
return val;
} }
void destroy(final Key key) { void destroy(final Key key) {
@@ -184,15 +193,18 @@ class WebSessionManager {
private transient boolean persistentCookie; private transient boolean persistentCookie;
private transient AccountExternalId.Key externalId; private transient AccountExternalId.Key externalId;
private transient String xsrfToken; private transient String xsrfToken;
private transient long expiresAt;
Val(final Account.Id accountId, final long refreshCookieAt, Val(final Account.Id accountId, final long refreshCookieAt,
final boolean persistentCookie, final AccountExternalId.Key externalId, final boolean persistentCookie, final AccountExternalId.Key externalId,
final String xsrfToken) { final String xsrfToken,
final long expiresAt) {
this.accountId = accountId; this.accountId = accountId;
this.refreshCookieAt = refreshCookieAt; this.refreshCookieAt = refreshCookieAt;
this.persistentCookie = persistentCookie; this.persistentCookie = persistentCookie;
this.externalId = externalId; this.externalId = externalId;
this.xsrfToken = xsrfToken; this.xsrfToken = xsrfToken;
this.expiresAt = expiresAt;
} }
Account.Id getAccountId() { Account.Id getAccountId() {
@@ -233,6 +245,9 @@ class WebSessionManager {
writeVarInt32(out, 5); writeVarInt32(out, 5);
writeString(out, xsrfToken); writeString(out, xsrfToken);
writeVarInt32(out, 6);
writeFixInt64(out, expiresAt);
writeVarInt32(out, 0); writeVarInt32(out, 0);
} }
@@ -257,10 +272,16 @@ class WebSessionManager {
case 5: case 5:
xsrfToken = readString(in); xsrfToken = readString(in);
continue; continue;
case 6:
expiresAt = readFixInt64(in);
continue;
default: default:
throw new IOException("Unknown tag found in object: " + tag); throw new IOException("Unknown tag found in object: " + tag);
} }
} }
if (expiresAt == 0) {
expiresAt = refreshCookieAt + TimeUnit.HOURS.toMillis(2);
}
} }
} }
} }