Skip to content

Commit a6e8330

Browse files
Merge pull request #45 from mdsol/fix/status-should-be-string
[MCC-1080766] Status for `start_response` must be `str`
2 parents 6d9ad4e + 62e08f9 commit a6e8330

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.6.1
2+
- Fix `MAuthWSGIMiddleware` to return a string for "status" and to properly set
3+
content-length header.
4+
15
# 1.6.0
26
- Fix bug with reading request body in `MAuthWSGIMiddleware`.
37
- Remove Support for EOL Python 3.7

mauth_client/middlewares/wsgi.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@ def __call__(self, environ, start_response):
3737
)
3838
signed = Signed.from_headers(self._extract_headers(environ))
3939
authenticator = LocalAuthenticator(signable, signed, logger)
40-
is_authentic, status, message = authenticator.is_authentic()
40+
is_authentic, code, message = authenticator.is_authentic()
4141

4242
if is_authentic:
4343
environ[ENV_APP_UUID] = signed.app_uuid
4444
environ[ENV_AUTHENTIC] = True
4545
environ[ENV_PROTOCOL_VERSION] = signed.protocol_version()
4646
return self.app(environ, start_response)
4747

48-
start_response(status, [("content-type", "application/json")])
49-
body = {"errors": {"mauth": [message]}}
50-
return [json.dumps(body).encode("utf-8")]
48+
return self._send_response(code, message, start_response)
5149

5250
def _validate_configs(self):
5351
# Validate the client settings (APP_UUID, PRIVATE_KEY)
@@ -135,3 +133,21 @@ def _extract_url(self, environ):
135133
url_parts.append(f"?{quote(qs, safe=self.SAFE_CHARS)}")
136134

137135
return "".join(url_parts)
136+
137+
_STATUS_STRS = {
138+
401: "401 Unauthorized",
139+
500: "500 Internal Server Error",
140+
}
141+
142+
def _send_response(self, code, msg, start_response):
143+
status = self._STATUS_STRS[code]
144+
body = {"errors": {"mauth": [msg]}}
145+
body_bytes = json.dumps(body).encode("utf-8")
146+
147+
headers = [
148+
("Content-Type", "application/json"),
149+
("Content-Length", str(len(body_bytes))),
150+
]
151+
start_response(status, headers)
152+
153+
return [body_bytes]

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "mauth-client"
3-
version = "1.6.0"
3+
version = "1.6.1"
44
description = "MAuth Client for Python"
55
repository = "https://github.com/mdsol/mauth-client-python"
66
authors = ["Medidata Solutions <[email protected]>"]

tests/middlewares/wsgi_test.py

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def test_401_response_when_not_authenticated(self):
9999
response = self.client.get("/")
100100

101101
self.assertEqual(response.status_code, 401)
102+
self.assertEqual(response.headers["Content-Length"], "151")
102103
self.assertEqual(response.json, {
103104
"errors": {
104105
"mauth": [(

0 commit comments

Comments
 (0)