Self hosted FLOSS fitness/workout, nutrition and weight tracker
Refuse OAuth authorization during trainer-login sessions
Roland Geidera0822f75ab10
diff --git a/wger/core/tests/test_oidc_provider.py b/wger/core/tests/test_oidc_provider.pyindex 636b93d46..acb7ba601 100644--- a/wger/core/tests/test_oidc_provider.py+++ b/wger/core/tests/test_oidc_provider.py@@ -930,3 +930,33 @@ class ConnectedApplicationsTestCase(OidcTestCase):self.assertEqual(response.status_code, 302)self.assertIn(settings.LOGIN_URL, response['location'])+++class OidcTrainerLoginTestCase(OidcTestCase):+ """+ A trainer logged in as one of their members can't connect an application+ to the member's account+ """++ def setUp(self):+ super().setUp()+ member = User.objects.get(username='test')+ self.user_login('trainer1')+ self.client.post(reverse('core:user:trainer-login', kwargs={'user_pk': member.pk}))+ self.assertTrue(self.client.session.get('trainer.identity'))++ def test_authorization_is_refused(self):+ query = urlencode(+ {+ 'client_id': self.oidc_client.id,+ 'redirect_uri': REDIRECT_URI,+ 'response_type': 'code',+ 'scope': f'openid {SCOPE_READ}',+ 'state': 'some-state',+ 'code_challenge': generate_pkce_pair()[1],+ 'code_challenge_method': 'S256',+ }+ )+ response = self.client.get(f'{reverse("idp:oidc:authorization")}?{query}')++ self.assertEqual(response.status_code, 403)diff --git a/wger/utils/oidc_auth.py b/wger/utils/oidc_auth.pyindex 7ca54eb74..a3dcd7636 100644--- a/wger/utils/oidc_auth.py+++ b/wger/utils/oidc_auth.py@@ -17,7 +17,10 @@# Djangofrom django.conf import settings-from django.http import Http404+from django.http import (+ Http404,+ HttpResponseForbidden,+)from django.urls import reversefrom django.utils.translation import gettext_lazy as _@@ -61,6 +64,10 @@ def authorization_view(request, *args, **kwargs):if not is_provider_configured():raise Http404('The OAuth2 provider is not configured')+ # A trainer logged in as a member must not get credentials that outlive the session+ if request.session.get('trainer.identity'):+ return HttpResponseForbidden()+return authorization(request, *args, **kwargs)