Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Make the powersync token lifetime configurable

Roland Geider27baa90e3966
diff --git a/settings/main.py b/settings/main.py
index e45c323b9..e14d18ee5 100644
--- a/settings/main.py
+++ b/settings/main.py
@@ -102,6 +102,7 @@ JWT_PUBLIC_KEY = env.str('JWT_PUBLIC_KEY', '')
JWT_PRIVATE_KEY = env.str('JWT_PRIVATE_KEY', '')
POWERSYNC_URL_PATH = env.str('POWERSYNC_URL_PATH', 'ps')
POWERSYNC_URL = env.str('POWERSYNC_URL', '')
+POWERSYNC_TOKEN_LIFETIME = env.int('POWERSYNC_TOKEN_LIFETIME', 600)
if not DEBUG and any(
key and hashlib.sha256(key.encode()).hexdigest() in _DEFAULT_JWT_KEY_HASHES
for key in (JWT_PUBLIC_KEY, JWT_PRIVATE_KEY)
diff --git a/settings/settings_global.py b/settings/settings_global.py
index ed1d87067..af29e2b3e 100644
--- a/settings/settings_global.py
+++ b/settings/settings_global.py
@@ -318,6 +318,11 @@ HEADLESS_TOKEN_STRATEGY = 'wger.utils.headless_auth.WgerJWTTokenStrategy'
HEADLESS_JWT_ALGORITHM = 'RS256'
HEADLESS_JWT_REFRESH_TOKEN_EXPIRES_IN = 120 * 24 * 3600
+#
+# PowerSync
+#
+POWERSYNC_TOKEN_LIFETIME = 600
+
#
# allauth.idp.oidc — OAuth2/OIDC provider
#
diff --git a/wger/core/api/powersync.py b/wger/core/api/powersync.py
index 5f226ba7c..65cd9a299 100644
--- a/wger/core/api/powersync.py
+++ b/wger/core/api/powersync.py
@@ -52,7 +52,7 @@ def create_token(user_id):
'sub': str(user_id),
'iat': now,
'aud': 'powersync',
- 'exp': now + 600,
+ 'exp': now + settings.POWERSYNC_TOKEN_LIFETIME,
},
_private_key(),
algorithm=jwk['alg'],
diff --git a/wger/core/tests/test_powersync.py b/wger/core/tests/test_powersync.py
index 624440d1e..721b21979 100644
--- a/wger/core/tests/test_powersync.py
+++ b/wger/core/tests/test_powersync.py
@@ -21,11 +21,17 @@ from django.db import (
IntegrityError,
OperationalError,
)
+from django.test import (
+ SimpleTestCase,
+ override_settings,
+)
# Third Party
+import jwt
from rest_framework import status
# wger
+from wger.core.api.powersync import create_token
from wger.core.models import UserProfile
from wger.core.tests import powersync_base_test
@@ -113,3 +119,19 @@ class UserProfilePowerSyncTestCase(
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json().get('error'), 'Validation failed')
+
+
+class PowerSyncTokenLifetimeTestCase(SimpleTestCase):
+ """Lifetime of the JWT handed to the sync service"""
+
+ @staticmethod
+ def lifetime():
+ claims = jwt.decode(create_token(1), options={'verify_signature': False})
+ return claims['exp'] - claims['iat']
+
+ def test_default(self):
+ self.assertEqual(self.lifetime(), 600)
+
+ @override_settings(POWERSYNC_TOKEN_LIFETIME=120)
+ def test_configured(self):
+ self.assertEqual(self.lifetime(), 120)