Self hosted FLOSS fitness/workout, nutrition and weight tracker
Index the long-lived sessions of the headless refresh tokens
Roland Geider2b30c0589b1b
diff --git a/wger/core/migrations/0028_longlivedsession.py b/wger/core/migrations/0028_longlivedsession.pynew file mode 100644index 000000000..2cf83eba7--- /dev/null+++ b/wger/core/migrations/0028_longlivedsession.py@@ -0,0 +1,124 @@+from django.conf import settings+from django.contrib.sessions.backends.db import SessionStore+from django.db import migrations, models+from django.utils import timezone+from django.utils.dateparse import parse_datetime++import django.db.models.deletion+++"""+Indexes the long-lived sessions of the headless refresh tokens.++The session table has no user column, so the overview at /user/api-key used to+decode every unexpired session of the whole instance to find the ones of the+logged-in user. New sessions are indexed when they are minted, the ones that+already exist are collected here once.+"""++# The one place where the whole table has to be read. Sessions are loaded in+# chunks and without the columns nobody needs to keep the memory flat.+CHUNK_SIZE = 2000++# Copies of the keys in wger.utils.headless_long_lived, a migration has to keep+# working when those change.+SESSION_KEY = '_auth_user_id'+LONG_LIVED_FLAG = 'wger_long_lived_refresh'+LONG_LIVED_CREATED_AT = 'wger_long_lived_refresh_created_at'+++def index_existing_sessions(session_model, index_model, user_model):+ """+ Create an index row for every unexpired long-lived session+ """+ store = SessionStore()+ now = timezone.now()+ found = []++ queryset = session_model.objects.filter(expire_date__gt=now).only(+ 'session_key',+ 'session_data',+ )+ for session in queryset.iterator(chunk_size=CHUNK_SIZE):+ try:+ data = store.decode(session.session_data)+ except Exception:+ continue++ if not data.get(LONG_LIVED_FLAG) or not data.get(SESSION_KEY):+ continue++ found.append(+ (+ data[SESSION_KEY],+ session.session_key,+ parse_datetime(data.get(LONG_LIVED_CREATED_AT) or '') or now,+ )+ )++ # Sessions of users that were deleted in the meantime would violate the+ # foreign key.+ known = {+ str(pk)+ for pk in user_model.objects.filter(pk__in={row[0] for row in found}).values_list(+ 'pk', flat=True+ )+ }+ index_model.objects.bulk_create(+ [+ index_model(+ user_id=user_id,+ session_key=session_key,+ created=created,+ )+ for user_id, session_key, created in found+ if user_id in known+ ],+ ignore_conflicts=True,+ batch_size=CHUNK_SIZE,+ )+++def forwards(apps, schema_editor):+ index_existing_sessions(+ apps.get_model('sessions', 'Session'),+ apps.get_model('core', 'LongLivedSession'),+ apps.get_model(settings.AUTH_USER_MODEL),+ )+++class Migration(migrations.Migration):+ dependencies = [+ ('sessions', '0001_initial'),+ ('core', '0027_powersync_publication'),+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),+ ]++ operations = [+ migrations.CreateModel(+ name='LongLivedSession',+ fields=[+ (+ 'id',+ models.AutoField(+ auto_created=True, primary_key=True, serialize=False, verbose_name='ID'+ ),+ ),+ ('session_key', models.CharField(editable=False, max_length=40, unique=True)),+ ('created', models.DateTimeField(default=timezone.now, editable=False)),+ (+ 'user',+ models.ForeignKey(+ editable=False,+ on_delete=django.db.models.deletion.CASCADE,+ related_name='long_lived_sessions',+ to=settings.AUTH_USER_MODEL,+ ),+ ),+ ],+ options={+ 'ordering': ['-created'],+ },+ ),+ migrations.RunPython(forwards, migrations.RunPython.noop, elidable=True),+ ]diff --git a/wger/core/models/__init__.py b/wger/core/models/__init__.pyindex 371e86cf2..82ecb50e0 100644--- a/wger/core/models/__init__.py+++ b/wger/core/models/__init__.py@@ -18,6 +18,7 @@from .cache import UserCachefrom .language import Languagefrom .license import License+from .long_lived_session import LongLivedSessionfrom .profile import UserProfilefrom .rep_unit import RepetitionUnitfrom .weight_unit import WeightUnitdiff --git a/wger/core/models/long_lived_session.py b/wger/core/models/long_lived_session.pynew file mode 100644index 000000000..052c0c797--- /dev/null+++ b/wger/core/models/long_lived_session.py@@ -0,0 +1,60 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2021 wger Team+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# Django+from django.contrib.auth.models import User+from django.db import models+from django.utils import timezone+++class LongLivedSession(models.Model):+ """+ Index of the long-lived sessions backing a user's headless refresh tokens++ The Django session table has no user column and its payload is an opaque+ signed blob, so listing the sessions of one user would mean decoding every+ unexpired row of the whole instance. The mapping is therefore recorded here+ when the session is minted, see wger.utils.headless_long_lived.+ """++ user = models.ForeignKey(+ User,+ editable=False,+ on_delete=models.CASCADE,+ related_name='long_lived_sessions',+ )+ """+ The owner of the session+ """++ session_key = models.CharField(max_length=40, editable=False, unique=True)+ """+ Primary key of the row in the session table+ """++ created = models.DateTimeField(default=timezone.now, editable=False)+ """+ When the token was generated, used to sort the overview+ """++ class Meta:+ ordering = ['-created']++ def __str__(self):+ """+ Return a more human-readable representation+ """+ return f'Long-lived session for user {self.user}'diff --git a/wger/core/tasks.py b/wger/core/tasks.pyindex fdfeca4ed..f9f354b7b 100644--- a/wger/core/tasks.py+++ b/wger/core/tasks.py@@ -18,13 +18,19 @@ import random# Djangofrom django.contrib.sessions.backends.db import SessionStore+from django.contrib.sessions.models import Sessionfrom django.core.management import call_command+from django.db.models import (+ Exists,+ OuterRef,+)# Third Partyfrom celery.schedules import crontab# wgerfrom wger.celery_configuration import app+from wger.core.models import LongLivedSessionlogger = logging.getLogger(__name__)@@ -47,10 +53,15 @@ def flush_expired_jwt_tokens_task():@app.taskdef flush_expired_long_lived_sessions_task():"""- Delete all expired DB-backed sessions+ Delete all expired DB-backed sessions and their index rows"""SessionStore.clear_expired()+ # Index rows of sessions that are gone, the session table is the truth.+ LongLivedSession.objects.filter(+ ~Exists(Session.objects.filter(session_key=OuterRef('session_key')))+ ).delete()+@app.taskdef flush_expired_oidc_tokens_task():diff --git a/wger/core/templates/user/api_key.html b/wger/core/templates/user/api_key.htmlindex 5698483a2..986892803 100644--- a/wger/core/templates/user/api_key.html+++ b/wger/core/templates/user/api_key.html@@ -69,25 +69,23 @@</thead><tbody>{% for session in long_lived_sessions %}- {% with data=session.get_decoded %}- <tr>- <td>{{ data.wger_long_lived_refresh_created_at|default:"—" }}</td>- <td>{{ session.expire_date|date:"Y-m-d H:i" }}</td>- <td>- <button- type="button"- class="btn btn-sm btn-outline-danger"- hx-post="{% url 'core:user:api-key' %}"- hx-vals='{"revoke_refresh_token": "{{ session.session_key }}"}'- hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'- hx-target="body"- hx-push-url="true"- >- {% translate "Revoke" %}- </button>- </td>- </tr>- {% endwith %}+ <tr>+ <td>{{ session.long_lived.created|date:"Y-m-d H:i" }}</td>+ <td>{{ session.expire_date|date:"Y-m-d H:i" }}</td>+ <td>+ <button+ type="button"+ class="btn btn-sm btn-outline-danger"+ hx-post="{% url 'core:user:api-key' %}"+ hx-vals='{"revoke_refresh_token": "{{ session.session_key }}"}'+ hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'+ hx-target="body"+ hx-push-url="true"+ >+ {% translate "Revoke" %}+ </button>+ </td>+ </tr>{% endfor %}</tbody></table>diff --git a/wger/core/tests/test_apikey.py b/wger/core/tests/test_apikey.pyindex 6c2e4ed04..292067e89 100644--- a/wger/core/tests/test_apikey.py+++ b/wger/core/tests/test_apikey.py@@ -13,9 +13,11 @@# You should have received a copy of the GNU Affero General Public License# Standard Library+import importlibimport jsonimport loggingfrom datetime import timedelta+from unittest import mock# Djangofrom django.contrib.auth.models import User@@ -23,6 +25,7 @@ from django.contrib.sessions.models import Sessionfrom django.test import override_settingsfrom django.urls import reversefrom django.utils import timezone+from django.utils.dateparse import parse_datetime# Third Partyfrom rest_framework.authtoken.models import Token@@ -34,9 +37,11 @@ from rest_framework_simplejwt.token_blacklist.models import (from rest_framework_simplejwt.tokens import RefreshToken# wger+from wger.core.models import LongLivedSessionfrom wger.core.tasks import flush_expired_long_lived_sessions_taskfrom wger.core.tests.base_testcase import WgerTestCasefrom wger.utils.headless_long_lived import (+ LONG_LIVED_CREATED_AT,LONG_LIVED_FLAG,list_long_lived_sessions,mint_long_lived_refresh_token,@@ -442,7 +447,7 @@ class LongLivedRefreshTokenTestCase(WgerTestCase):def test_flush_expired_long_lived_sessions_task(self):"""The periodic cleanup task deletes expired DB-backed long-lived sessions- while leaving still-valid ones untouched.+ and their index rows, while leaving still-valid ones untouched."""user = User.objects.get(username='test')@@ -460,3 +465,128 @@ class LongLivedRefreshTokenTestCase(WgerTestCase):self.assertFalse(Session.objects.filter(session_key=expired_key).exists())self.assertTrue(Session.objects.filter(session_key=live_key).exists())++ self.assertFalse(LongLivedSession.objects.filter(session_key=expired_key).exists())+ self.assertTrue(LongLivedSession.objects.filter(session_key=live_key).exists())++ def test_overview_does_not_decode_any_session(self):+ """+ Rendering the overview must not decode session payloads, no matter how+ many sessions of other users are in the table. The dates it shows come+ from the index and from the session row itself.+ """+ admin = User.objects.get(username='admin')+ for _ in range(20):+ mint_long_lived_refresh_token(admin, lifetime_seconds=120 * 86400)++ self.user_login('test')+ user = User.objects.get(username='test')+ mint_long_lived_refresh_token(user, lifetime_seconds=120 * 86400)+ entry = LongLivedSession.objects.get(user=user)++ decoded = []+ original = Session.get_decoded++ def counting_get_decoded(session):+ decoded.append(session.session_key)+ return original(session)++ with mock.patch.object(Session, 'get_decoded', counting_get_decoded):+ response = self.client.get(reverse('core:user:api-key'))++ self.assertEqual(response.status_code, 200)+ self.assertEqual(decoded, [])+ self.assertContains(response, timezone.localtime(entry.created).strftime('%Y-%m-%d %H:%M'))++ def test_deleting_the_user_removes_the_index(self):+ """+ The index rows of a deleted user are removed with them.+ """+ user = User.objects.get(username='test')+ mint_long_lived_refresh_token(user, lifetime_seconds=120 * 86400)+ self.assertEqual(LongLivedSession.objects.filter(user=user).count(), 1)++ user.delete()+ self.assertEqual(LongLivedSession.objects.count(), 0)++ def test_session_deleted_outside_the_app_is_not_listed(self):+ """+ A session that was removed without going through the revoke views does+ not show up in the overview any more.+ """+ user = User.objects.get(username='test')+ mint_long_lived_refresh_token(user, lifetime_seconds=120 * 86400)+ session_key = list_long_lived_sessions(user)[0].session_key++ Session.objects.filter(session_key=session_key).delete()++ self.assertEqual(list_long_lived_sessions(user), [])+++class LongLivedSessionBackfillTestCase(WgerTestCase):+ """+ Tests the data migration that indexes the sessions created before the index+ existed. The test settings skip migrations, so the function is called with+ the current models instead of the historical ones.+ """++ MIGRATION = 'wger.core.migrations.0028_longlivedsession'++ def index_existing_sessions(self):+ module = importlib.import_module(self.MIGRATION)+ module.index_existing_sessions(Session, LongLivedSession, User)++ def test_existing_sessions_are_indexed(self):+ """+ Long-lived sessions without an index row get one, with the user and the+ creation date taken from the session payload.+ """+ user = User.objects.get(username='test')++ # A regular browser session that must be left alone.+ self.user_login('test')++ mint_long_lived_refresh_token(user, lifetime_seconds=120 * 86400)+ mint_long_lived_refresh_token(user, lifetime_seconds=120 * 86400)+ keys = set(LongLivedSession.objects.values_list('session_key', flat=True))+ self.assertEqual(len(keys), 2)++ LongLivedSession.objects.all().delete()+ self.index_existing_sessions()++ self.assertEqual(+ set(LongLivedSession.objects.values_list('session_key', flat=True)),+ keys,+ )+ for entry in LongLivedSession.objects.all():+ data = Session.objects.get(session_key=entry.session_key).get_decoded()+ self.assertEqual(entry.user, user)+ self.assertEqual(entry.created, parse_datetime(data[LONG_LIVED_CREATED_AT]))++ def test_expired_sessions_are_skipped(self):+ """+ Sessions that already expired are not indexed, the cleanup task would+ drop them right away.+ """+ user = User.objects.get(username='test')+ mint_long_lived_refresh_token(user, lifetime_seconds=120 * 86400)+ session_key = LongLivedSession.objects.get().session_key++ Session.objects.filter(session_key=session_key).update(+ expire_date=timezone.now() - timedelta(days=1),+ )+ LongLivedSession.objects.all().delete()+ self.index_existing_sessions()++ self.assertEqual(LongLivedSession.objects.count(), 0)++ def test_running_twice_does_not_duplicate(self):+ """+ Sessions that are already indexed are not indexed a second time.+ """+ user = User.objects.get(username='test')+ mint_long_lived_refresh_token(user, lifetime_seconds=120 * 86400)++ self.index_existing_sessions()++ self.assertEqual(LongLivedSession.objects.count(), 1)diff --git a/wger/utils/headless_long_lived.py b/wger/utils/headless_long_lived.pyindex d3a45a2b7..6a65071de 100644--- a/wger/utils/headless_long_lived.py+++ b/wger/utils/headless_long_lived.py@@ -35,6 +35,9 @@ from django.contrib.sessions.backends.db import SessionStorefrom django.contrib.sessions.models import Sessionfrom django.utils import timezone+# wger+from wger.core.models import LongLivedSession+# Marker stored on the session payload to identify long-lived sessions later.LONG_LIVED_FLAG = 'wger_long_lived_refresh'@@ -76,54 +79,60 @@ def mint_long_lived_refresh_token(user, lifetime_seconds: int) -> str:# second save, /tokens/refresh would not find the jti and reject the# token as unknown.session.save()++ # Index the session so it can be listed and revoked without scanning the+ # whole session table.+ LongLivedSession.objects.update_or_create(+ session_key=session.session_key,+ defaults={'user': user},+ )return tokendef list_long_lived_sessions(user):"""- Iterate over the unexpired long-lived sessions belonging to *user*. Yields- ``Session`` model instances, sorted by creation time (newest first).+ Return the unexpired long-lived sessions belonging to *user* as ``Session``+ model instances, sorted by creation time (newest first). Each one carries+ its index row as ``long_lived``."""- user_id = str(user.pk)- rows = []- for s in Session.objects.filter(expire_date__gt=timezone.now()):- try:- data = s.get_decoded()- except Exception:- # A corrupted session row should not break the page. Django's- # signed_cookies/db backends both protect against tampering, so- # decode failures only happen for legitimate edge cases (e.g. a- # cleared SECRET_KEY across deployments).- continue- if data.get(SESSION_KEY) != user_id:- continue- if not data.get(LONG_LIVED_FLAG):+ entries = list(LongLivedSession.objects.filter(user=user))+ sessions = {+ s.session_key: s+ for s in Session.objects.filter(+ session_key__in=[entry.session_key for entry in entries],+ expire_date__gt=timezone.now(),+ )+ }++ result = []+ for entry in entries:+ session = sessions.get(entry.session_key)++ # Index rows without a session (deleted outside of the app) are skipped+ if session is None:continue- rows.append((s, data.get(LONG_LIVED_CREATED_AT) or ''))- rows.sort(key=lambda row: row[1], reverse=True)- return [row[0] for row in rows]++ session.long_lived = entry+ result.append(session)+ return resultdef revoke_long_lived_session(user, session_key: str) -> bool:"""Delete a single long-lived session by key, but only when it actually- belongs to *user* and is marked long-lived. Returns ``True`` if a row was- deleted.+ belongs to *user*. Returns ``True`` if a session was deleted."""try:- s = Session.objects.get(- session_key=session_key,- expire_date__gt=timezone.now(),- )- except Session.DoesNotExist:+ entry = LongLivedSession.objects.get(user=user, session_key=session_key)+ except LongLivedSession.DoesNotExist:return False- data = s.get_decoded()- if data.get(SESSION_KEY) != str(user.pk):- return False- if not data.get(LONG_LIVED_FLAG):- return False- s.delete()- return True++ deleted, _ = Session.objects.filter(+ session_key=session_key,+ expire_date__gt=timezone.now(),+ ).delete()+ entry.delete()+ return bool(deleted)def revoke_all_long_lived_sessions(user) -> int:@@ -131,6 +140,11 @@ def revoke_all_long_lived_sessions(user) -> int:Delete every long-lived session belonging to *user*. Returns the numberof revoked sessions."""- sessions = list_long_lived_sessions(user)- Session.objects.filter(session_key__in=[s.session_key for s in sessions]).delete()- return len(sessions)+ entries = LongLivedSession.objects.filter(user=user)+ keys = list(entries.values_list('session_key', flat=True))+ deleted, _ = Session.objects.filter(+ session_key__in=keys,+ expire_date__gt=timezone.now(),+ ).delete()+ entries.delete()+ return deleted