Skip to content

Commit 7381959

Browse files
Merge pull request #46 from mdsol/include-asgi-root-path
Include ASGI scope root_path in url used to validate mAuth signature
2 parents a6e8330 + 80c9ece commit 7381959

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.6.2
2+
- Fix `MAuthASGIMiddleware` signature validation when the full URL path is split
3+
between `root_path` and `path` in the request scope.
4+
15
# 1.6.1
26
- Fix `MAuthWSGIMiddleware` to return a string for "status" and to properly set
37
content-length header.

mauth_client/middlewares/asgi.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ async def __call__(
3838
if scope["type"] != "http" or path in self.exempt:
3939
return await self.app(scope, receive, send)
4040

41+
root_path = scope["root_path"]
4142
query_string = scope["query_string"]
42-
url = f"{path}?{decode(query_string)}" if query_string else path
43+
url = f"{root_path}{path}?{decode(query_string)}" if query_string else f"{root_path}{path}"
4344
headers = {decode(k): decode(v) for k, v in scope["headers"]}
4445

4546
events, body = await self._get_body(receive)

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.1"
3+
version = "1.6.2"
44
description = "MAuth Client for Python"
55
repository = "https://github.com./mdsol/mauth-client-python"
66
authors = ["Medidata Solutions <[email protected]>"]

tests/middlewares/asgi_test.py

+36
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,39 @@ async def ws(websocket: WebSocket):
179179
with self.client.websocket_connect("/ws") as websocket:
180180
data = websocket.receive_json()
181181
self.assertEqual(data, {"msg": "helloes"})
182+
183+
184+
class TestMAuthASGIMiddlewareInSubApplication(unittest.TestCase):
185+
def setUp(self):
186+
self.app_uuid = str(uuid4())
187+
Config.APP_UUID = self.app_uuid
188+
Config.MAUTH_URL = "https://mauth.com"
189+
Config.MAUTH_API_VERSION = "v1"
190+
Config.PRIVATE_KEY = "key"
191+
192+
self.app = FastAPI()
193+
sub_app = FastAPI()
194+
sub_app.add_middleware(MAuthASGIMiddleware)
195+
196+
@sub_app.get("/path")
197+
async def sub_app_path():
198+
return {"msg": "sub app path"}
199+
200+
self.app.mount("/sub_app", sub_app)
201+
202+
self.client = TestClient(self.app)
203+
204+
@patch.object(LocalAuthenticator, "is_authentic", autospec=True)
205+
def test_includes_base_application_path_in_signature_verification(self, is_authentic_mock):
206+
request_url = None
207+
208+
def is_authentic_effect(self):
209+
nonlocal request_url
210+
request_url = self.signable.attributes_for_signing["request_url"]
211+
return True, 200, ""
212+
213+
is_authentic_mock.side_effect = is_authentic_effect
214+
215+
self.client.get("/sub_app/path")
216+
217+
self.assertEqual(request_url, "/sub_app/path")

0 commit comments

Comments
 (0)