From bfbc350448604bd4d55debc472f7f317366ffee7 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Mon, 18 May 2026 18:51:21 +0200 Subject: [PATCH] Add app-handoff endpoint This is needed to use social login, etc. in the app via the browser --- .../core/templates/user/app_auth_handoff.html | 27 ++++++++ wger/core/tests/test_app_auth_handoff.py | 61 +++++++++++++++++++ wger/core/urls.py | 1 + wger/core/views/user.py | 38 ++++++++++-- 4 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 wger/core/templates/user/app_auth_handoff.html create mode 100644 wger/core/tests/test_app_auth_handoff.py diff --git a/wger/core/templates/user/app_auth_handoff.html b/wger/core/templates/user/app_auth_handoff.html new file mode 100644 index 000000000..fe7987d8a --- /dev/null +++ b/wger/core/templates/user/app_auth_handoff.html @@ -0,0 +1,27 @@ +{% load i18n %} + + + + + + {% translate "Return to app" %} + + + +

{% translate "Returning to the wger app…" %}

+

+ {% blocktranslate trimmed %} + If nothing happens, tap the button below. Make sure the wger app + is installed on this device. + {% endblocktranslate %} +

+

+ + {% translate "Tap to return to app" %} + +

+ + diff --git a/wger/core/tests/test_app_auth_handoff.py b/wger/core/tests/test_app_auth_handoff.py new file mode 100644 index 000000000..7c7768409 --- /dev/null +++ b/wger/core/tests/test_app_auth_handoff.py @@ -0,0 +1,61 @@ +# This file is part of wger Workout Manager. +# +# 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 General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License + +# Standard Library +from urllib.parse import ( + parse_qs, + urlparse, +) + +# Django +from django.urls import reverse + +# wger +from wger.core.tests.base_testcase import WgerTestCase + + +class AppAuthHandoffTestCase(WgerTestCase): + """ + Tests for the /user/app-auth/ web-handoff view that mints a long-lived + refresh token and redirects to a custom URL scheme. + """ + + def test_anonymous_user_is_redirected_to_login(self): + response = self.client.get(reverse('core:user:app-auth-handoff')) + self.assertEqual(response.status_code, 302) + self.assertIn('login', response['Location']) + + def test_authenticated_user_gets_redirect_page(self): + self.user_login('test') + response = self.client.get(reverse('core:user:app-auth-handoff')) + self.assertEqual(response.status_code, 200) + # Meta-refresh carries the custom-scheme URL. + self.assertContains(response, 'http-equiv="refresh"') + self.assertContains(response, 'wger://app-auth#token=') + + def test_token_is_in_fragment_not_query(self): + self.user_login('test') + response = self.client.get(reverse('core:user:app-auth-handoff')) + content = response.content.decode() + # Extract the return_uri from the meta-refresh content attribute. + start = content.index('0;url=') + len('0;url=') + end = content.index('"', start) + return_uri = content[start:end] + parsed = urlparse(return_uri) + self.assertEqual(parsed.scheme, 'wger') + # Fragment carries the token; query is empty. + self.assertEqual(parsed.query, '') + self.assertTrue(parsed.fragment.startswith('token=')) + self.assertGreater(len(parsed.fragment), len('token=')) + diff --git a/wger/core/urls.py b/wger/core/urls.py index 4e63ebdce..d6ccbc250 100644 --- a/wger/core/urls.py +++ b/wger/core/urls.py @@ -75,6 +75,7 @@ patterns_user = [ path('registration', user.WgerSignupView.as_view(), name='registration'), path('preferences', user.preferences, name='preferences'), path('api-key', user.api_key, name='api-key'), + path('app-auth/', user.app_auth_handoff, name='app-auth-handoff'), path('demo-entries', misc.demo_entries, name='demo-entries'), path('/activate', user.UserActivateView.as_view(), name='activate'), path('/deactivate', user.UserDeactivateView.as_view(), name='deactivate'), diff --git a/wger/core/views/user.py b/wger/core/views/user.py index d92eb1bed..af2e2d8a2 100644 --- a/wger/core/views/user.py +++ b/wger/core/views/user.py @@ -16,6 +16,7 @@ # Standard Library import logging +from urllib.parse import quote # Django from django.conf import settings @@ -118,6 +119,15 @@ from wger.weight.models import WeightEntry logger = logging.getLogger(__name__) +# Session key used to carry a freshly minted refresh token across the +# post-redirect step on the api-key page. The token is shown once and then +# immediately popped. +_NEW_REFRESH_TOKEN_SESSION_KEY = '_wger_new_long_lived_refresh_token' + + +# Custom URL scheme registered by the flutter app in Info.plist, AndroidManifest.xml, etc. +_APP_AUTH_SCHEME = 'wger' + @login_required() def delete(request, user_pk=None): """ @@ -518,10 +528,30 @@ def api_key(request): return render(request, 'user/api_key.html', context) -# Session key used to carry a freshly minted refresh token across the -# post-redirect step on the api-key page. The token is shown once and then -# immediately popped. -_NEW_REFRESH_TOKEN_SESSION_KEY = '_wger_new_long_lived_refresh_token' +@login_required +def app_auth_handoff(request): + """ + Browser-side endpoint that mints a long-lived headless refresh token for + the authenticated user and redirects to a custom URL scheme so the wger + mobile app can pick it up. + + The user must already be authenticated; @login_required redirects through + the normal allauth login flow first + """ + token = mint_long_lived_refresh_token( + request.user, + settings.HEADLESS_JWT_REFRESH_TOKEN_EXPIRES_IN, + ) + + # Token goes in the URL fragment, not the query string, so it never lands + # in server access logs or referer headers. Refresh-token rotation is + # the backstop against the one remaining leak surface (browser history). + return_uri = f'{_APP_AUTH_SCHEME}://app-auth#token={quote(token, safe="")}' + return render( + request, + 'user/app_auth_handoff.html', + {'return_uri': return_uri}, + ) class UserDetailView(LoginRequiredMixin, WgerMultiplePermissionRequiredMixin, DetailView):