-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add check for enforcing jwt and lms user email match (#419)
* feat: add check for enforcing jwt and lms user email match --------- Co-authored-by: Robert Raposa <[email protected]>
- Loading branch information
1 parent
de055fb
commit 97bc367
Showing
6 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
""" edx Django REST Framework extensions. """ | ||
|
||
__version__ = '10.0.0' # pragma: no cover | ||
__version__ = '10.1.0' # pragma: no cover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,10 @@ | |
generate_jwt_token, | ||
generate_latest_version_payload, | ||
) | ||
from edx_rest_framework_extensions.config import ENABLE_SET_REQUEST_USER_FOR_JWT_COOKIE | ||
from edx_rest_framework_extensions.config import ( | ||
ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH, | ||
ENABLE_SET_REQUEST_USER_FOR_JWT_COOKIE, | ||
) | ||
from edx_rest_framework_extensions.tests import factories | ||
|
||
|
||
|
@@ -534,6 +537,117 @@ def test_authenticate_jwt_and_no_session_and_set_request_user(self, mock_set_cus | |
assert 'jwt_auth_mismatch_jwt_cookie_username' not in set_custom_attribute_keys | ||
assert response.status_code == 200 | ||
|
||
@override_settings( | ||
MIDDLEWARE=( | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'edx_rest_framework_extensions.auth.jwt.middleware.JwtAuthCookieMiddleware', | ||
), | ||
ROOT_URLCONF='edx_rest_framework_extensions.auth.jwt.tests.test_authentication', | ||
) | ||
def test_authenticate_user_lms_and_jwt_email_mismatch_toggle_disabled(self): | ||
""" | ||
Test success for JwtAuthentication when ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH is disabled. | ||
""" | ||
user = factories.UserFactory(email='[email protected]') | ||
jwt_header_payload, jwt_signature = self._get_test_jwt_token_payload_and_signature(user=user) | ||
|
||
# Cookie parts will be recombined by JwtAuthCookieMiddleware | ||
self.client.cookies = SimpleCookie({ | ||
jwt_cookie_header_payload_name(): jwt_header_payload, | ||
jwt_cookie_signature_name(): jwt_signature, | ||
}) | ||
|
||
# simulating email change | ||
user.email = '[email protected]' | ||
user.save() # pylint: disable=no-member | ||
|
||
self.client.force_login(user) | ||
|
||
response = self.client.get(reverse('authenticated-view')) | ||
|
||
assert response.status_code == 200 | ||
|
||
@override_settings( | ||
EDX_DRF_EXTENSIONS={ | ||
ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH: True, | ||
'JWT_PAYLOAD_USER_ATTRIBUTE_MAPPING': {}, | ||
'JWT_PAYLOAD_MERGEABLE_USER_ATTRIBUTES': [] | ||
}, | ||
MIDDLEWARE=( | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'edx_rest_framework_extensions.auth.jwt.middleware.JwtAuthCookieMiddleware', | ||
), | ||
ROOT_URLCONF='edx_rest_framework_extensions.auth.jwt.tests.test_authentication', | ||
) | ||
@mock.patch('edx_rest_framework_extensions.auth.jwt.authentication.set_custom_attribute') | ||
def test_authenticate_user_lms_and_jwt_email_match_failure(self, mock_set_custom_attribute): | ||
""" | ||
Test failure for JwtAuthentication when ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH | ||
is enabled and the lms and jwt user email do not match. | ||
""" | ||
user = factories.UserFactory(email='[email protected]') | ||
jwt_header_payload, jwt_signature = self._get_test_jwt_token_payload_and_signature(user=user) | ||
|
||
# Cookie parts will be recombined by JwtAuthCookieMiddleware | ||
self.client.cookies = SimpleCookie({ | ||
jwt_cookie_header_payload_name(): jwt_header_payload, | ||
jwt_cookie_signature_name(): jwt_signature, | ||
}) | ||
|
||
# simulating email change | ||
user.email = '[email protected]' | ||
user.save() # pylint: disable=no-member | ||
|
||
self.client.force_login(user) | ||
|
||
response = self.client.get(reverse('authenticated-view')) | ||
|
||
assert response.status_code == 401 | ||
mock_set_custom_attribute.assert_any_call( | ||
'jwt_auth_failed', | ||
"Exception:JwtUserEmailMismatchError('Failing JWT authentication due to jwt user email mismatch with lms " | ||
"user email.')" | ||
) | ||
|
||
@override_settings( | ||
EDX_DRF_EXTENSIONS={ | ||
ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH: True, | ||
'JWT_PAYLOAD_USER_ATTRIBUTE_MAPPING': {}, | ||
'JWT_PAYLOAD_MERGEABLE_USER_ATTRIBUTES': [] | ||
}, | ||
MIDDLEWARE=( | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'edx_rest_framework_extensions.auth.jwt.middleware.JwtAuthCookieMiddleware', | ||
), | ||
ROOT_URLCONF='edx_rest_framework_extensions.auth.jwt.tests.test_authentication', | ||
) | ||
@mock.patch('edx_rest_framework_extensions.auth.jwt.authentication.set_custom_attribute') | ||
def test_authenticate_user_lms_and_jwt_email_match_success(self, mock_set_custom_attribute): | ||
""" | ||
Test success for JwtAuthentication when ENABLE_JWT_AND_LMS_USER_EMAIL_MATCH | ||
is enabled and the lms and jwt user email match. | ||
""" | ||
user = factories.UserFactory(email='[email protected]') | ||
jwt_header_payload, jwt_signature = self._get_test_jwt_token_payload_and_signature(user=user) | ||
|
||
# Cookie parts will be recombined by JwtAuthCookieMiddleware | ||
self.client.cookies = SimpleCookie({ | ||
jwt_cookie_header_payload_name(): jwt_header_payload, | ||
jwt_cookie_signature_name(): jwt_signature, | ||
}) | ||
|
||
# Not changing email | ||
|
||
self.client.force_login(user) | ||
|
||
response = self.client.get(reverse('authenticated-view')) | ||
|
||
assert response.status_code == 200 | ||
mock_set_custom_attribute.assert_any_call('jwt_auth_result', 'success-cookie') | ||
|
||
def _get_test_jwt_token(self, user=None, is_valid_signature=True, lms_user_id=None): | ||
""" Returns a test jwt token for the provided user """ | ||
test_user = factories.UserFactory() if user is None else user | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters