Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

auth: Add "next" to SOCIAL_AUTH_FIELDS_STORED_IN_SESSION.

Mateusz Mandera18acc6dc8a7f
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py
index 48ba979879c..8cb93faaaa9 100644
--- a/zerver/tests/test_auth_backends.py
+++ b/zerver/tests/test_auth_backends.py
@@ -1345,10 +1345,13 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase, ABC):
def test_social_auth_session_fields_cleared_correctly(self) -> None:
mobile_flow_otp = "1234abcd" * 8
+ next = "/some/path"
- def initiate_auth(mobile_flow_otp: str | None = None) -> None:
+ def initiate_auth(mobile_flow_otp: str | None = None, next: str | None = None) -> None:
url, headers = self.prepare_login_url_and_headers(
- subdomain="zulip", mobile_flow_otp=mobile_flow_otp
+ subdomain="zulip",
+ mobile_flow_otp=mobile_flow_otp,
+ next=next or "",
)
result = self.client_get(url, **headers)
self.assertEqual(result.status_code, 302)
@@ -1356,15 +1359,17 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase, ABC):
result = self.client_get(result["Location"], **headers)
self.assertEqual(result.status_code, 302)
- # Start social auth with mobile_flow_otp param. It should get saved into the session
+ # Start social auth with mobile_flow_otp and next params. They should get saved into the session
# on SOCIAL_AUTH_SUBDOMAIN.
- initiate_auth(mobile_flow_otp)
+ initiate_auth(mobile_flow_otp, next)
self.assertEqual(self.client.session["mobile_flow_otp"], mobile_flow_otp)
+ self.assertEqual(self.client.session["next"], next)
- # Make a request without mobile_flow_otp param and verify the field doesn't persist
+ # Make a request without params and verify the fields don't persist
# in the session from the previous request.
initiate_auth()
self.assertEqual(self.client.session.get("mobile_flow_otp"), None)
+ self.assertEqual(self.client.session["next"], None)
def test_social_auth_mobile_and_desktop_flow_in_one_request_error(self) -> None:
otp = "1234abcd" * 8
diff --git a/zproject/backends.py b/zproject/backends.py
index 6e517b9aaa7..fa1d8396eea 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -2475,7 +2475,17 @@ class SocialAuthMixin(ZulipAuthMixin, ExternalAuthMethod, BaseAuth):
# it should be False.
full_name_validated = False
- standard_relay_params = [*settings.SOCIAL_AUTH_FIELDS_STORED_IN_SESSION, "next"]
+ # Several backends need to store information about the parameters of a user's
+ # initiated authentication session, while the user is redirected to a third party
+ # authentication provider.
+ # See also DEFAULT_REDIS_EXPIRATION_SECONDS_FOR_TRANSIENT_STATE.
+ #
+ # The params of interest in practice overlap with SOCIAL_AUTH_FIELDS_STORED_IN_SESSION,
+ # but the objective of the storage is somewhat different, and the storage is not in
+ # request.session, but instead in redis.
+ # Thus for readability of code, we want to name this standard_relay_params, as a distinct
+ # concept.
+ standard_relay_params = [*settings.SOCIAL_AUTH_FIELDS_STORED_IN_SESSION]
@override
def auth_complete(self, *args: Any, **kwargs: Any) -> HttpResponse | None:
diff --git a/zproject/computed_settings.py b/zproject/computed_settings.py
index 42ee52cb8db..3752e63a473 100644
--- a/zproject/computed_settings.py
+++ b/zproject/computed_settings.py
@@ -1133,6 +1133,11 @@ SOCIAL_AUTH_FIELDS_STORED_IN_SESSION = [
"mobile_flow_otp",
"desktop_flow_otp",
"multiuse_object_key",
+ # Include "next" so python-social-auth treats it like other session fields
+ # and clears it when absent in subsequent auth requests. Otherwise,
+ # it saves it in the session due to its being the redirect field,
+ # but doesn't clear it from the session when the "next" is absent.
+ "next",
]
SOCIAL_AUTH_LOGIN_ERROR_URL = "/login/"