Skip to content

Commit

Permalink
Return powersync public key(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandgeider committed Aug 11, 2024
1 parent 0ba5d2c commit 5e2bd2b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
7 changes: 7 additions & 0 deletions extras/docker/development/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
30 changes: 26 additions & 4 deletions wger/core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -79,7 +82,6 @@
from wger.utils.api_token import create_token
from wger.utils.permissions import WgerPermission


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -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,
)
6 changes: 3 additions & 3 deletions wger/nutrition/migrations/0025_create_publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Migration(migrations.Migration):

dependencies = [
('nutrition', '0024_remove_ingredient_status'),
]
Expand All @@ -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;
"""
""",
),
]
7 changes: 6 additions & 1 deletion wger/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 5e2bd2b

Please sign in to comment.