Perform serverside expiration of sessions on get attempt.

Also remove unnecessary conversion to int.
This commit is contained in:
voussoir 2018-01-19 21:59:50 -08:00
parent cdd8f9e248
commit cf59f96813

View file

@ -38,6 +38,8 @@ class SessionManager:
def get(self, token): def get(self, token):
token = _normalize_token(token) token = _normalize_token(token)
session = self.sessions[token] session = self.sessions[token]
if session.expired():
raise KeyError(token)
return session return session
def give_token(self, function): def give_token(self, function):
@ -92,7 +94,7 @@ class Session:
self.user = user self.user = user
self.ip_address = request.remote_addr self.ip_address = request.remote_addr
self.user_agent = request.headers.get('User-Agent', '') self.user_agent = request.headers.get('User-Agent', '')
self.last_activity = int(etiquette.helpers.now()) self.last_activity = etiquette.helpers.now()
def __repr__(self): def __repr__(self):
if self.user: if self.user:
@ -100,5 +102,10 @@ class Session:
else: else:
return 'Session %s for anonymous' % self.token return 'Session %s for anonymous' % self.token
def expired(self):
now = etiquette.helpers.now()
age = now - self.last_activity
return age > SESSION_MAX_AGE
def maintain(self): def maintain(self):
self.last_activity = int(etiquette.helpers.now()) self.last_activity = etiquette.helpers.now()