drop last modified extra field from session, do not keep user profile in session

This commit is contained in:
Jörg Prante 2024-04-19 17:12:22 +02:00
parent 715ee9cd2a
commit 7058cfa211
3 changed files with 18 additions and 41 deletions

View file

@ -6,6 +6,7 @@ import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.xbib.datastructures.common.LRUCache; import org.xbib.datastructures.common.LRUCache;
import org.xbib.net.UserProfile; import org.xbib.net.UserProfile;
@ -13,8 +14,6 @@ public class BaseSession implements Session {
static String CREATED_FIELD = "_created_"; static String CREATED_FIELD = "_created_";
static String LAST_MODIFIED_FIELD = "_lastmodified_";
static String CACHE_PREFIX = "_cache_"; static String CACHE_PREFIX = "_cache_";
private final SessionListener sessionListener; private final SessionListener sessionListener;
@ -83,42 +82,26 @@ public class BaseSession implements Session {
@Override @Override
public boolean isExpired() { public boolean isExpired() {
String string = (String) get(LAST_MODIFIED_FIELD); return getAge().compareTo(lifetime) > 0;
if (string == null) {
string = (String) get(CREATED_FIELD);
}
if (string == null) {
return false;
}
Instant now = Instant.now();
Instant lastModified = Instant.parse(string);
return Duration.between(lastModified, now).compareTo(lifetime) > 0;
}
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
public UserProfile getUserProfile() {
return userProfile;
}
@Override
public boolean isAuthenticated() {
return userProfile != null && userProfile.getUserId() != null;
}
@Override
public boolean hasPayload() {
return !isEmpty() &&
!(size() == 1 && containsKey(CREATED_FIELD)) &&
!(size() == 2 && containsKey(CREATED_FIELD) && containsKey(LAST_MODIFIED_FIELD));
} }
@Override @Override
public Duration getAge() { public Duration getAge() {
Instant instant = containsKey(CREATED_FIELD) ? Instant.parse(get(CREATED_FIELD).toString()) : Instant.now(); return Duration.between(getLastModified(), Instant.now());
return Duration.between(instant, Instant.now()); }
public Instant getLastModified() {
String string = (String) get(CREATED_FIELD);
return string == null ? Instant.now() : Instant.parse(string);
}
public Duration getLifetime() {
return lifetime;
}
@Override
public boolean hasPayload() {
return !isEmpty() && !(size() == 1 && containsKey(CREATED_FIELD));
} }
@Override @Override
@ -201,10 +184,6 @@ public class BaseSession implements Session {
return map.toString(); return map.toString();
} }
public void setLastModified() {
put(LAST_MODIFIED_FIELD, Instant.now().toString());
}
public int getCacheSize() { public int getCacheSize() {
return cacheSize; return cacheSize;
} }

View file

@ -73,7 +73,7 @@ public class IncomingContextHandler implements HttpHandler {
payload = decodeCookie(cookie); payload = decodeCookie(cookie);
logger.log(Level.FINE, "payload from cookie = " + payload); logger.log(Level.FINE, "payload from cookie = " + payload);
session = toSession(payload); session = toSession(payload);
logger.log(Level.FINE, "session from payload = " + session); logger.log(Level.FINE, "session from payload = " + session + " expired = " + session.isExpired() + " valid = " + session.isValid());
userProfile = toUserProfile(session); userProfile = toUserProfile(session);
logger.log(Level.FINE, "userprofile from session = " + userProfile); logger.log(Level.FINE, "userprofile from session = " + userProfile);
} catch (CookieSignatureException e) { } catch (CookieSignatureException e) {

View file

@ -15,8 +15,6 @@ public interface Session extends Map<String, Object> {
boolean isExpired(); boolean isExpired();
boolean isAuthenticated();
boolean hasPayload(); boolean hasPayload();
Duration getAge(); Duration getAge();