To comply with security guidelines, we need to destroy the session when a user logs out. This means that the session's key in the Redis cache needs to be deleted. Flask expects to _always_ have a session object. If the current session object does not exist in the Redis cache, Flask will reserialize and store it at the end of the request. In order for session deletion to work, we need to delete the key for the existing session and then replace the session object with a new, empty one. This also updates the SessionLimiter class so that the session prefix is configurable.
21 lines
650 B
Python
21 lines
650 B
Python
from atst.domain.users import Users
|
|
|
|
|
|
class SessionLimiter(object):
|
|
def __init__(self, config, session, redis):
|
|
self.limit_logins = config["LIMIT_CONCURRENT_SESSIONS"]
|
|
self.session_prefix = config.get("SESSION_KEY_PREFIX", "session:")
|
|
self.session = session
|
|
self.redis = redis
|
|
|
|
def on_login(self, user):
|
|
if not self.limit_logins:
|
|
return
|
|
|
|
session_id = self.session.sid
|
|
self._delete_session(user.last_session_id)
|
|
Users.update_last_session_id(user, session_id)
|
|
|
|
def _delete_session(self, session_id):
|
|
self.redis.delete(f"{self.session_prefix}{session_id}")
|