Skip to content

Commit df00af1

Browse files
committed
sessions: addition of KVSession interface
Signed-off-by: Harris Tzovanakis <me@drjova.com>
1 parent 4d2f0f6 commit df00af1

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

invenio_accounts/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,7 @@
226226

227227
ACCOUNTS_USERINFO_HEADERS = False
228228
"""If True, add X-Session-ID and X-User-ID to the HTTP response."""
229+
230+
SESSION_INTERFACE_FACTORY = \
231+
'invenio_accounts.sessions:KVSessionInterfaceWithAnonymousSessions'
232+
"""Set the session interface factory for KVSession"""

invenio_accounts/sessions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from __future__ import absolute_import, print_function
1818

1919
from flask import after_this_request, current_app, request, session
20+
from flask_kvsession import KVSessionInterface
21+
from flask_login import current_user
2022
from geolite2 import geolite2
2123
from invenio_db import db
2224
from ua_parser import user_agent_parser
@@ -152,3 +154,14 @@ def default_session_store_factory(app):
152154
accounts_session_redis_url))
153155
from simplekv.memory import DictStore
154156
return DictStore()
157+
158+
159+
class KVSessionInterfaceWithAnonymousSessions(KVSessionInterface):
160+
"""Session interface to avoid storing anonymous sessions."""
161+
162+
def store_session(self, session):
163+
"""Store session only when user is authenticated."""
164+
if current_user.is_authenticated:
165+
super(KVSessionInterfaceWithAnonymousSessions, self).store_session(
166+
session
167+
)

0 commit comments

Comments
 (0)