Skip to content

Commit

Permalink
Merge pull request #45 from mdsol/fix/status-should-be-string
Browse files Browse the repository at this point in the history
[MCC-1080766] Status for `start_response` must be `str`
  • Loading branch information
ejinotti-mdsol authored Jun 29, 2023
2 parents 6d9ad4e + 62e08f9 commit a6e8330
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 20 additions & 4 deletions mauth_client/middlewares/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ 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
environ[ENV_AUTHENTIC] = True
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)
Expand Down Expand Up @@ -135,3 +133,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]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
Expand Down
1 change: 1 addition & 0 deletions tests/middlewares/wsgi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [(
Expand Down

0 comments on commit a6e8330

Please sign in to comment.