From 5e2bd2bb6d5f26b083b02aba228c87698acd914f Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sun, 11 Aug 2024 17:11:34 +0200 Subject: [PATCH] Return powersync public key(s) --- extras/docker/development/settings.py | 7 +++++ wger/core/api/views.py | 30 ++++++++++++++++--- .../migrations/0025_create_publication.py | 6 ++-- wger/urls.py | 7 ++++- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/extras/docker/development/settings.py b/extras/docker/development/settings.py index 23a81c51d..5aa78ebc4 100644 --- a/extras/docker/development/settings.py +++ b/extras/docker/development/settings.py @@ -168,3 +168,10 @@ # EXPOSE_PROMETHEUS_METRICS = env.bool('EXPOSE_PROMETHEUS_METRICS', False) PROMETHEUS_URL_PATH = env.str('PROMETHEUS_URL_PATH', 'super-secret-path') + +# +# PowerSync configuration +# +POWERSYNC_JWKS_PUBLIC_KEY = env.str('POWERSYNC_JWKS_PUBLIC_KEY', '') +POWERSYNC_JWKS_PRIVATE_KEY = env.str('POWERSYNC_JWKS_PRIVATE_KEY', '') +POWERSYNC_URL = env.str('POWERSYNC_URL', 'http://powersync:8080') diff --git a/wger/core/api/views.py b/wger/core/api/views.py index fa36eea4f..15a5b1e80 100644 --- a/wger/core/api/views.py +++ b/wger/core/api/views.py @@ -39,7 +39,10 @@ status, viewsets, ) -from rest_framework.decorators import action +from rest_framework.decorators import ( + action, + api_view, +) from rest_framework.fields import ( BooleanField, CharField, @@ -79,7 +82,6 @@ from wger.utils.api_token import create_token from wger.utils.permissions import WgerPermission - logger = logging.getLogger(__name__) @@ -409,7 +411,27 @@ class RoutineWeightUnitViewSet(viewsets.ReadOnlyModelViewSet): @login_required -def get_tokens_for_user(request): +@api_view(['GET']) +def get_token_for_user(request): token = AccessToken.for_user(request.user) - return JsonResponse(data={'access': str(token), 'type': str(token.token_type)}) + return JsonResponse( + data={ + 'token': str(token), + 'type': str(token.token_type), + 'powersync_url': 'http://powersync:8080', + } + ) + + +@login_required +@api_view(['GET']) +def get_powersync_keys(request): + return JsonResponse( + { + 'keys': [ + settings.POWERSYNC_JWKS_PUBLIC_KEY, + ] + }, + status=200, + ) diff --git a/wger/nutrition/migrations/0025_create_publication.py b/wger/nutrition/migrations/0025_create_publication.py index c0f818c0e..5d20a8827 100644 --- a/wger/nutrition/migrations/0025_create_publication.py +++ b/wger/nutrition/migrations/0025_create_publication.py @@ -2,7 +2,6 @@ class Migration(migrations.Migration): - dependencies = [ ('nutrition', '0024_remove_ingredient_status'), ] @@ -15,12 +14,13 @@ class Migration(migrations.Migration): IF NOT EXISTS ( SELECT 1 FROM pg_publication WHERE pubname = 'powersync' ) THEN - CREATE PUBLICATION powersync FOR TABLE nutrition_ingredient, auth_user; + -- CREATE PUBLICATION powersync FOR ALL TABLES + CREATE PUBLICATION powersync FOR TABLE nutrition_ingredient, exercises_muscle, auth_user; END IF; END $$; """, reverse_sql=""" DROP PUBLICATION IF EXISTS powersync; - """ + """, ), ] diff --git a/wger/urls.py b/wger/urls.py index 80feda1cd..95ff0604c 100644 --- a/wger/urls.py +++ b/wger/urls.py @@ -279,9 +279,14 @@ ), path( 'api/v2/get-token', - core_api_views.get_tokens_for_user, + core_api_views.get_token_for_user, name='get_token', ), + path( + 'api/v2/powersync-keys', + core_api_views.get_powersync_keys, + name='powersync-keys', + ), # Api documentation path( 'api/v2/schema',