From eb9820318913b7d3b0d1434139da206b046092a0 Mon Sep 17 00:00:00 2001 From: "Edward J. Jinotti" Date: Mon, 26 Jun 2023 19:22:47 -0400 Subject: [PATCH 1/3] fix status to be string --- CHANGELOG.md | 4 ++++ mauth_client/middlewares/wsgi.py | 25 +++++++++++++++++++++---- pyproject.toml | 2 +- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5ce9d4..bfff0de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.6.1 +- Fix `MAuthWSGIMiddleware` to return a string for "status" and to properly set + content-length header. + # 1.6.0 - Fix bug with reading request body in `MAuthWSGIMiddleware`. - Remove Support for EOL Python 3.7 diff --git a/mauth_client/middlewares/wsgi.py b/mauth_client/middlewares/wsgi.py index 72c86f2..8e05117 100644 --- a/mauth_client/middlewares/wsgi.py +++ b/mauth_client/middlewares/wsgi.py @@ -37,7 +37,7 @@ def __call__(self, environ, start_response): ) signed = Signed.from_headers(self._extract_headers(environ)) authenticator = LocalAuthenticator(signable, signed, logger) - is_authentic, status, message = authenticator.is_authentic() + is_authentic, code, message = authenticator.is_authentic() if is_authentic: environ[ENV_APP_UUID] = signed.app_uuid @@ -45,9 +45,8 @@ def __call__(self, environ, start_response): environ[ENV_PROTOCOL_VERSION] = signed.protocol_version() return self.app(environ, start_response) - start_response(status, [("content-type", "application/json")]) - body = {"errors": {"mauth": [message]}} - return [json.dumps(body).encode("utf-8")] + return self._send_response(code, message, start_response) + def _validate_configs(self): # Validate the client settings (APP_UUID, PRIVATE_KEY) @@ -135,3 +134,21 @@ def _extract_url(self, environ): url_parts.append(f"?{quote(qs, safe=self.SAFE_CHARS)}") return "".join(url_parts) + + _STATUS_STRS = { + 401: "401 Unauthorized", + 500: "500 Internal Server Error", + } + + def _send_response(self, code, msg, start_response): + status = self._STATUS_STRS[code] + body = {"errors": {"mauth": [msg]}} + body_bytes = json.dumps(body).encode("utf-8") + + headers = [ + ("Content-Type", "application/json"), + ("Content-Length", str(len(body_bytes))), + ] + start_response(status, headers) + + return [body_bytes] diff --git a/pyproject.toml b/pyproject.toml index a6cade4..2a51a8f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "mauth-client" -version = "1.6.0" +version = "1.6.1" description = "MAuth Client for Python" repository = "https://github.com/mdsol/mauth-client-python" authors = ["Medidata Solutions "] From 4cf20dccbf743354615cb2359521ac8b353f738d Mon Sep 17 00:00:00 2001 From: "Edward J. Jinotti" Date: Mon, 26 Jun 2023 19:31:07 -0400 Subject: [PATCH 2/3] flake8 --- mauth_client/middlewares/wsgi.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mauth_client/middlewares/wsgi.py b/mauth_client/middlewares/wsgi.py index 8e05117..44657bf 100644 --- a/mauth_client/middlewares/wsgi.py +++ b/mauth_client/middlewares/wsgi.py @@ -47,7 +47,6 @@ def __call__(self, environ, start_response): return self._send_response(code, message, start_response) - def _validate_configs(self): # Validate the client settings (APP_UUID, PRIVATE_KEY) if not all([Config.APP_UUID, Config.PRIVATE_KEY]): From 62e08f9d6976cdcf03e4c23cd0a77f8def1ac929 Mon Sep 17 00:00:00 2001 From: Yohei Kitamura Date: Thu, 29 Jun 2023 10:09:35 -0400 Subject: [PATCH 3/3] Update unit test --- tests/middlewares/wsgi_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/middlewares/wsgi_test.py b/tests/middlewares/wsgi_test.py index 7024bcf..aecac85 100644 --- a/tests/middlewares/wsgi_test.py +++ b/tests/middlewares/wsgi_test.py @@ -99,6 +99,7 @@ def test_401_response_when_not_authenticated(self): response = self.client.get("/") self.assertEqual(response.status_code, 401) + self.assertEqual(response.headers["Content-Length"], "151") self.assertEqual(response.json, { "errors": { "mauth": [(