Skip to content

Commit

Permalink
sessions: addition of KVSession interface
Browse files Browse the repository at this point in the history
Signed-off-by: Harris Tzovanakis <[email protected]>
  • Loading branch information
drjova committed Nov 21, 2019
1 parent 1b4c8b6 commit 0bdc7e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions invenio_accounts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,7 @@

ACCOUNTS_USERINFO_HEADERS = False
"""If True, add X-Session-ID and X-User-ID to the HTTP response."""

SESSION_INTERFACE_FACTORY = \
'invenio_accounts.sessions:KVSessionInterfaceWithAnonymousSessions'
"""Set the session interface factory for KVSession"""
19 changes: 19 additions & 0 deletions invenio_accounts/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from __future__ import absolute_import, print_function

from flask import after_this_request, current_app, request, session
from flask_kvsession import KVSessionInterface
from flask_login import current_user
from geolite2 import geolite2
from invenio_db import db
from ua_parser import user_agent_parser
Expand Down Expand Up @@ -135,3 +137,20 @@ def delete_user_sessions(user):
SessionActivity.query.filter_by(user=user).delete()

return True


class KVSessionInterfaceWithAnonymousSessions(KVSessionInterface):
"""Session interface to avoid storing anonymous sessions."""

def save_session(self, app, session, response):
"""Save session interface."""
anonymous_session = not current_user.is_authenticated
super(
KVSessionInterfaceWithAnonymousSessions,
self
).save_session(
app,
session,
response,
anonymous_session=anonymous_session
)

0 comments on commit 0bdc7e7

Please sign in to comment.