From f4d52e070b9bd1c5d16184d5f49601be585b6e9b Mon Sep 17 00:00:00 2001 From: jawad khan Date: Fri, 8 Aug 2025 11:01:21 +0500 Subject: [PATCH] Fixed jwt scope issue (#37134) * fix: Fixed jwt scope issue * fix: fixed test cases --- common/djangoapps/third_party_auth/tests/utils.py | 6 +++++- openedx/core/djangoapps/oauth_dispatch/jwt.py | 2 +- .../core/djangoapps/oauth_dispatch/tests/test_views.py | 9 ++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/common/djangoapps/third_party_auth/tests/utils.py b/common/djangoapps/third_party_auth/tests/utils.py index 29dc75e44f..8d1bafcdd8 100644 --- a/common/djangoapps/third_party_auth/tests/utils.py +++ b/common/djangoapps/third_party_auth/tests/utils.py @@ -57,7 +57,7 @@ class ThirdPartyOAuthTestMixin(ThirdPartyAuthTestMixin): client_type=Application.CLIENT_PUBLIC, ) - def _setup_provider_response(self, success=False, email=''): + def _setup_provider_response(self, success=False, email='', profile_data=None): """ Register a mock response for the third party user information endpoint; success indicates whether the response status code should be 200 or 400 @@ -67,6 +67,10 @@ class ThirdPartyOAuthTestMixin(ThirdPartyAuthTestMixin): response = {self.UID_FIELD: self.social_uid} if email: response.update({'email': email}) + + if profile_data: + response.update(profile_data) + body = json.dumps(response) else: status = 400 diff --git a/openedx/core/djangoapps/oauth_dispatch/jwt.py b/openedx/core/djangoapps/oauth_dispatch/jwt.py index de493715a7..e6dcc8038f 100644 --- a/openedx/core/djangoapps/oauth_dispatch/jwt.py +++ b/openedx/core/djangoapps/oauth_dispatch/jwt.py @@ -80,7 +80,7 @@ def create_jwt_token_dict(token_dict, oauth_adapter, use_asymmetric_key=None): # .. custom_attribute_name: create_jwt_grant_type # .. custom_attribute_description: The grant type of the newly created JWT. set_custom_attribute('create_jwt_grant_type', grant_type) - scopes = _get_updated_scopes(token_dict['scope'].split(' '), grant_type) + scopes = _get_updated_scopes(token_dict['scope'].split(), grant_type) jwt_access_token = _create_jwt( access_token.user, diff --git a/openedx/core/djangoapps/oauth_dispatch/tests/test_views.py b/openedx/core/djangoapps/oauth_dispatch/tests/test_views.py index ef03dab0ac..06031c37a1 100644 --- a/openedx/core/djangoapps/oauth_dispatch/tests/test_views.py +++ b/openedx/core/djangoapps/oauth_dispatch/tests/test_views.py @@ -422,7 +422,8 @@ class TestAccessTokenExchangeView(ThirdPartyOAuthTestMixinGoogle, ThirdPartyOAut """ client = getattr(self, client_attr) self.oauth_client = client - self._setup_provider_response(success=True) + profile_data = {'given_name': self.user.first_name, 'family_name': self.user.last_name} + self._setup_provider_response(success=True, profile_data=profile_data) response = self._post_request(self.user, client, token_type=token_type, headers=headers or {}, asymmetric_jwt=asymmetric_jwt) assert response.status_code == 200 @@ -451,7 +452,8 @@ class TestAccessTokenExchangeView(ThirdPartyOAuthTestMixinGoogle, ThirdPartyOAut def test_jwt_access_token_exchange_calls_dispatched_view(self, client_attr): client = getattr(self, client_attr) self.oauth_client = client - self._setup_provider_response(success=True) + profile_data = {'given_name': self.user.first_name, 'family_name': self.user.last_name} + self._setup_provider_response(success=True, profile_data=profile_data) response = self._post_request(self.user, client, token_type='jwt') assert response.status_code == 200 data = json.loads(response.content.decode('utf-8')) @@ -470,7 +472,8 @@ class TestAccessTokenExchangeView(ThirdPartyOAuthTestMixinGoogle, ThirdPartyOAut def test_asymmetric_jwt_access_token_exchange_calls_dispatched_view(self, client_attr): client = getattr(self, client_attr) self.oauth_client = client - self._setup_provider_response(success=True) + profile_data = {'given_name': self.user.first_name, 'family_name': self.user.last_name} + self._setup_provider_response(success=True, profile_data=profile_data) response = self._post_request(self.user, client, token_type='jwt', asymmetric_jwt=True) assert response.status_code == 200 data = json.loads(response.content.decode('utf-8'))