Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
auth: Use REQ for getting token value and rename param in jwt paths.
Mateusz Manderad466da106460
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.pyindex 0ed68901e74..65bd0390c32 100644--- a/zerver/tests/test_auth_backends.py+++ b/zerver/tests/test_auth_backends.py@@ -5424,7 +5424,7 @@ class TestJWTLogin(ZulipTestCase):web_token = jwt.encode(payload, key, algorithm)user_profile = get_user_by_delivery_email(email, realm)- data = {"json_web_token": web_token}+ data = {"token": web_token}result = self.client_post("/accounts/login/jwt/", data)self.assertEqual(result.status_code, 302)self.assert_logged_in_user_id(user_profile.id)@@ -5435,7 +5435,7 @@ class TestJWTLogin(ZulipTestCase):key = settings.JWT_AUTH_KEYS["zulip"]["key"][algorithm] = settings.JWT_AUTH_KEYS["zulip"]["algorithms"]web_token = jwt.encode(payload, key, algorithm)- data = {"json_web_token": web_token}+ data = {"token": web_token}result = self.client_post("/accounts/login/jwt/", data)self.assert_json_error_contains(result, "No email specified in JSON web token claims", 400@@ -5443,7 +5443,7 @@ class TestJWTLogin(ZulipTestCase):def test_login_failure_when_key_does_not_exist(self) -> None:with self.settings(JWT_AUTH_KEYS={"acme": {"key": "key", "algorithms": ["HS256"]}}):- data = {"json_web_token": "not relevant"}+ data = {"token": "not relevant"}result = self.client_post("/accounts/login/jwt/", data)self.assert_json_error_contains(result, "JWT authentication is not enabled for this organization", 400@@ -5458,7 +5458,7 @@ class TestJWTLogin(ZulipTestCase):with self.settings(JWT_AUTH_KEYS={"zulip": {"key": "key", "algorithms": ["HS256"]}}):result = self.client_post("/accounts/login/jwt/")self.assert_json_error_contains(result, "No JSON web token passed in request", 400)- data = {"json_web_token": "bad token"}+ data = {"token": "bad token"}result = self.client_post("/accounts/login/jwt/", data)self.assert_json_error_contains(result, "Bad JSON web token", 400)@@ -5468,7 +5468,7 @@ class TestJWTLogin(ZulipTestCase):key = settings.JWT_AUTH_KEYS["zulip"]["key"][algorithm] = settings.JWT_AUTH_KEYS["zulip"]["algorithms"]web_token = jwt.encode(payload, key, algorithm)- data = {"json_web_token": web_token}+ data = {"token": web_token}result = self.client_post("/accounts/login/jwt/", data)self.assertEqual(result.status_code, 200) # This should ideally be not 200.self.assert_logged_in_user_id(None)@@ -5481,7 +5481,7 @@ class TestJWTLogin(ZulipTestCase):[algorithm] = settings.JWT_AUTH_KEYS["acme"]["algorithms"]web_token = jwt.encode(payload, key, algorithm)- data = {"json_web_token": web_token}+ data = {"token": web_token}result = self.client_post("/accounts/login/jwt/", data)self.assert_json_error_contains(result, "Invalid subdomain", 404)self.assert_logged_in_user_id(None)@@ -5494,7 +5494,7 @@ class TestJWTLogin(ZulipTestCase):[algorithm] = settings.JWT_AUTH_KEYS["zulip"]["algorithms"]web_token = jwt.encode(payload, key, algorithm)- data = {"json_web_token": web_token}+ data = {"token": web_token}result = self.client_post("/accounts/login/jwt/", data)self.assertEqual(result.status_code, 302)user_profile = self.example_user("hamlet")diff --git a/zerver/views/auth.py b/zerver/views/auth.pyindex a7a023a3bcf..57ab2add56d 100644--- a/zerver/views/auth.py+++ b/zerver/views/auth.py@@ -472,8 +472,10 @@ def remote_user_sso(return login_or_register_remote_user(request, result)+@has_request_variablesdef get_email_and_realm_from_jwt_authentication_request(request: HttpRequest,+ json_web_token: str = REQ("token", default=""),) -> Tuple[str, Realm]:realm = get_realm_from_request(request)if realm is None:@@ -485,12 +487,12 @@ def get_email_and_realm_from_jwt_authentication_request(except KeyError:raise JsonableError(_("JWT authentication is not enabled for this organization"))+ if not json_web_token:+ raise JsonableError(_("No JSON web token passed in request"))+try:- json_web_token = request.POST["json_web_token"]options = {"verify_signature": True}payload = jwt.decode(json_web_token, key, algorithms=algorithms, options=options)- except KeyError:- raise JsonableError(_("No JSON web token passed in request"))except jwt.InvalidTokenError:raise JsonableError(_("Bad JSON web token"))