Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Keep web users logged in indefinitely via rotating refresh tokens

Claude24a14276229c
diff --git a/mwmbl/settings_common.py b/mwmbl/settings_common.py
index 16a9a06..fd847e1 100644
--- a/mwmbl/settings_common.py
+++ b/mwmbl/settings_common.py
@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/
"""
import os
+from datetime import timedelta
from pathlib import Path
from urllib.parse import urlparse
@@ -240,6 +241,10 @@ def strip_query_string(event):
# Django ninja-jwt settings
NINJA_JWT = {
"USER_AUTHENTICATION_RULE": require_email_confirmation,
+ # Refresh tokens rotate on every use, so this is an inactivity limit: users who
+ # visit at least once a year stay logged in indefinitely.
+ "REFRESH_TOKEN_LIFETIME": timedelta(days=365),
+ "ROTATE_REFRESH_TOKENS": True,
}
# Database configuration (shared across all environments via DATABASE_URL env var)
diff --git a/test/test_auth.py b/test/test_auth.py
index f2723ce..6e40b68 100644
--- a/test/test_auth.py
+++ b/test/test_auth.py
@@ -107,3 +107,22 @@ def test_login_email_and_username_return_same_user(client, verified_user):
return _json.loads(base64.urlsafe_b64decode(payload))["user_id"]
assert subject(by_username["access"]) == subject(by_email["access"])
+
+
+def test_refresh_rotates_refresh_token(client, verified_user):
+ # Rotation restarts the refresh token lifetime, so active users stay logged in
+ pair = client.post(
+ TOKEN_URL,
+ {"username": "swift_falcon_379", "password": "correctpassword"},
+ content_type="application/json",
+ ).json()
+ response = client.post(
+ "/api/v1/platform/token/refresh",
+ {"refresh": pair["refresh"]},
+ content_type="application/json",
+ )
+ assert response.status_code == 200
+ data = response.json()
+ assert data["access"]
+ assert data["refresh"] != pair["refresh"]