Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Change Log
Unreleased
----------

Fixed
~~~~~

* Session cleanup in ``EdXOAuth2.start()`` no longer discards the ``next`` redirect that social-auth stores before the OAuth flow begins. Users who were already logged in were sent to ``LOGIN_REDIRECT_URL`` instead of the page they asked for.
* The same cleanup also keeps any fields listed in ``FIELDS_STORED_IN_SESSION``, which social-auth stores at the same point.


[4.6.2] - 2025-10-16
--------------------
Expand Down
7 changes: 7 additions & 0 deletions src/auth_backends/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ def start(self):
existing_username
)

# logout() flushes the session, which also holds what social-auth just stored for this request:
# the sanitized redirect and any FIELDS_STORED_IN_SESSION. Carry only those across.
stored_names = ['next', *self.setting('FIELDS_STORED_IN_SESSION', [])]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also carries over a next that this request didn't set. do_auth() only writes next when the request has one, and nothing removes it after login, so when the request has no next the value here is left over from the previous login. If a different user is logging in, they get redirected to the last page the previous user logged in to, which is the kind of carry-over this cleanup is meant to stop. FIELDS_STORED_IN_SESSION is fine as is since do_auth() always overwrites those.

Suggested change
stored_names = ['next', *self.setting('FIELDS_STORED_IN_SESSION', [])]
stored_names = [*self.setting('FIELDS_STORED_IN_SESSION', [])]
# do_auth() only writes `next` when this request carries one, otherwise the value is left over
# from an earlier login and should not survive the logout.
if 'next' in self.strategy.request_data(merge=False):
stored_names.append('next')

The new test will need self.backend.strategy.set_request_data({'next': '/credentials/abc/'}, self.backend) with this, and a case where the request has no next would cover the leftover value.

stored_values = {name: self.strategy.session_get(name) for name in stored_names}
logout(request)
for name, value in stored_values.items():
if value is not None:
self.strategy.session_set(name, value)

return super().start()

Expand Down
23 changes: 23 additions & 0 deletions src/auth_backends/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,29 @@ def test_start_with_session_cleanup(self, user_authenticated, mock_logger, mock_
else:
mock_logger.info.assert_not_called()

@pytest.mark.django_db
@patch('auth_backends.backends.set_custom_attribute')
@patch('auth_backends.backends.logger')
def test_start_with_session_cleanup_preserves_next(self, _mock_logger, _mock_set_attr):
"""The post-login redirect stored by social-auth must survive the session cleanup."""
request = RequestFactory().get('/auth/login/edx-oauth2/?next=/credentials/abc/')
request.user = User.objects.create_user(username='existing_user', email='existing@example.com')
middleware = SessionMiddleware(lambda req: None)
middleware.process_request(request)
request.session.save()
self.backend.strategy.request = request
# In production the strategy's session IS request.session; the fake test strategy keeps its own dict.
self.backend.strategy._session = request.session # pylint: disable=protected-access
self.set_social_auth_setting('FIELDS_STORED_IN_SESSION', ['auth_entry'])
self.backend.strategy.session_set('next', '/credentials/abc/')
self.backend.strategy.session_set('auth_entry', 'login')

self.do_start()

self.assertTrue(request.user.is_anonymous)
self.assertEqual(self.backend.strategy.session_get('next'), '/credentials/abc/')
self.assertEqual(self.backend.strategy.session_get('auth_entry'), 'login')

def test_partial_pipeline(self):
self.do_partial_pipeline()

Expand Down
Loading