diff --git a/openedx/core/djangoapps/user_authn/cookies.py b/openedx/core/djangoapps/user_authn/cookies.py index 8afb219042..b722fb8207 100644 --- a/openedx/core/djangoapps/user_authn/cookies.py +++ b/openedx/core/djangoapps/user_authn/cookies.py @@ -12,7 +12,7 @@ from django.conf import settings from django.contrib.auth.models import User from django.dispatch import Signal from django.urls import NoReverseMatch, reverse -from django.utils.http import http_date +from django.utils.http import http_date, parse_http_date from edx_rest_framework_extensions.auth.jwt import cookies as jwt_cookies from edx_rest_framework_extensions.auth.jwt.constants import JWT_DELIMITER from oauth2_provider.models import Application @@ -22,6 +22,9 @@ from openedx.core.djangoapps.oauth_dispatch.api import create_dot_access_token from openedx.core.djangoapps.oauth_dispatch.jwt import create_jwt_from_token from openedx.core.djangoapps.user_api.accounts.utils import retrieve_last_sitewide_block_completed from openedx.core.djangoapps.user_authn.exceptions import AuthFailedError +from student.models import CourseEnrollment +from util.json_request import JsonResponse + log = logging.getLogger(__name__) @@ -153,13 +156,26 @@ def set_logged_in_cookies(request, response, user): return response -def refresh_jwt_cookies(request, response, user): +def get_response_with_refreshed_jwt_cookies(request, user): """ - Resets the JWT related cookies in the response for the given user. + Generates the response and resets the JWT related cookies in the response for the given user. """ cookie_settings = standard_cookie_settings(request) + response = JsonResponse({}) _create_and_set_jwt_cookies(response, request, cookie_settings, user=user) + current_time = time.time() + expires_date = cookie_settings.get('expires', None) + expires_epoch = parse_http_date(expires_date) if expires_date else 0 + response.content = json.dumps( + { + 'success': True, + 'response_epoch_seconds': current_time, + 'response_http_date': http_date(current_time), + 'expires': expires_date if expires_date else 'not-found', + 'expires_epoch_seconds': expires_epoch, + } + ) return response diff --git a/openedx/core/djangoapps/user_authn/tests/test_cookies.py b/openedx/core/djangoapps/user_authn/tests/test_cookies.py index 95f11d1241..ecef23a646 100644 --- a/openedx/core/djangoapps/user_authn/tests/test_cookies.py +++ b/openedx/core/djangoapps/user_authn/tests/test_cookies.py @@ -1,6 +1,7 @@ # pylint: disable=missing-docstring +import json import six from django.conf import settings from django.http import HttpResponse @@ -129,7 +130,10 @@ class CookieTests(TestCase): def test_refresh_jwt_cookies(self): setup_login_oauth_client() self._set_use_jwt_cookie_header(self.request) - response = cookies_api.refresh_jwt_cookies(self.request, HttpResponse(), self.user) + response = cookies_api.get_response_with_refreshed_jwt_cookies(self.request, self.user) + data = json.loads(response.content.decode('utf8').replace("'", '"')) + self.assertGreater(data['expires_epoch_seconds'], 0) + self.assertNotEqual(data['expires'], 'not-found') self._assert_cookies_present(response, cookies_api.JWT_COOKIE_NAMES) self._assert_consistent_expires(response, num_of_unique_expires=1) self._assert_recreate_jwt_from_cookies(response, can_recreate=True) diff --git a/openedx/core/djangoapps/user_authn/views/login.py b/openedx/core/djangoapps/user_authn/views/login.py index 255265f763..d8792f056e 100644 --- a/openedx/core/djangoapps/user_authn/views/login.py +++ b/openedx/core/djangoapps/user_authn/views/login.py @@ -4,7 +4,6 @@ Views for login / logout and associated functionality Much of this file was broken out from views.py, previous history can be found there. """ - import json import logging @@ -32,7 +31,7 @@ from common.djangoapps.edxmako.shortcuts import render_to_response from openedx.core.djangoapps.password_policy import compliance as password_policy_compliance from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.djangoapps.user_authn.views.login_form import get_login_session_form -from openedx.core.djangoapps.user_authn.cookies import refresh_jwt_cookies, set_logged_in_cookies +from openedx.core.djangoapps.user_authn.cookies import get_response_with_refreshed_jwt_cookies, set_logged_in_cookies from openedx.core.djangoapps.user_authn.exceptions import AuthFailedError from openedx.core.djangoapps.user_authn.utils import should_redirect_to_logistration_mircrofrontend from openedx.core.djangoapps.util.user_messages import PageLevelMessages @@ -526,8 +525,7 @@ def login_refresh(request): return JsonResponse('Unauthorized', status=401) try: - response = JsonResponse({'success': True}) - return refresh_jwt_cookies(request, response, request.user) + return get_response_with_refreshed_jwt_cookies(request, request.user) except AuthFailedError as error: log.exception(error.get_response()) return JsonResponse(error.get_response(), status=400)