From cb3a4c2a8cdb2cf8a594eabb368284465613e1b8 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Mon, 13 Jun 2022 07:33:06 -0700 Subject: [PATCH 1/4] Replace BaseHTTPMiddleware with pure ASGI middleware --- backend/middleware.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/backend/middleware.py b/backend/middleware.py index f74091b9..a555b254 100644 --- a/backend/middleware.py +++ b/backend/middleware.py @@ -5,27 +5,35 @@ from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request from starlette.responses import JSONResponse, Response +from starlette.types import ASGIApp, Scope, Receive, Send from backend.constants import DATABASE_URL, DOCS_PASSWORD, MONGO_DATABASE -class DatabaseMiddleware(BaseHTTPMiddleware): - async def dispatch(self, request: Request, call_next: t.Callable) -> Response: +class DatabaseMiddleware: + + def __init__(self, app: ASGIApp) -> None: + self._app = app + + async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: client: AsyncIOMotorClient = AsyncIOMotorClient( DATABASE_URL, ssl_cert_reqs=ssl.CERT_NONE ) db = client[MONGO_DATABASE] - request.state.db = db - response = await call_next(request) - return response + scope["state"].db = db + await self._app(scope, send, receive) + +class ProtectedDocsMiddleware: -class ProtectedDocsMiddleware(BaseHTTPMiddleware): - async def dispatch(self, request: Request, call_next: t.Callable) -> Response: + def __init__(self, app: ASGIApp) -> None: + self._app = app + + async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: + request = Request(scope) if DOCS_PASSWORD and request.url.path.startswith("/docs"): if request.cookies.get("docs_password") != DOCS_PASSWORD: - return JSONResponse({"status": "unauthorized"}, status_code=403) - - resp = await call_next(request) - return resp + await JSONResponse({"status": "unauthorized"}, status_code=403)(scope, receive, send) + return + await self._app(scope, receive, send) From 97f05b0b849105ad2b87a9eb779b4e8abe013f8b Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Mon, 13 Jun 2022 12:27:22 -0700 Subject: [PATCH 2/4] fix linters --- backend/middleware.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/backend/middleware.py b/backend/middleware.py index a555b254..517055ea 100644 --- a/backend/middleware.py +++ b/backend/middleware.py @@ -1,10 +1,7 @@ -import typing as t - import ssl from motor.motor_asyncio import AsyncIOMotorClient -from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request -from starlette.responses import JSONResponse, Response +from starlette.responses import JSONResponse from starlette.types import ASGIApp, Scope, Receive, Send from backend.constants import DATABASE_URL, DOCS_PASSWORD, MONGO_DATABASE @@ -34,6 +31,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: request = Request(scope) if DOCS_PASSWORD and request.url.path.startswith("/docs"): if request.cookies.get("docs_password") != DOCS_PASSWORD: - await JSONResponse({"status": "unauthorized"}, status_code=403)(scope, receive, send) + resp = JSONResponse({"status": "unauthorized"}, status_code=403) + await resp(scope, receive, send) return await self._app(scope, receive, send) From 8b67d41361e56dcbdd029f8e84cd1cc431a64b86 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Fri, 17 Jun 2022 17:28:17 -0700 Subject: [PATCH 3/4] Update backend/middleware.py Co-authored-by: Hassan Abouelela --- backend/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/middleware.py b/backend/middleware.py index 517055ea..f4ea9c6b 100644 --- a/backend/middleware.py +++ b/backend/middleware.py @@ -19,7 +19,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: ) db = client[MONGO_DATABASE] scope["state"].db = db - await self._app(scope, send, receive) + await self._app(scope, receive, send) class ProtectedDocsMiddleware: From 5d0da6be01eadc8f5a39d8bb1418fe435e05c6d1 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Fri, 17 Jun 2022 17:30:04 -0700 Subject: [PATCH 4/4] use Request to generate the state --- backend/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/middleware.py b/backend/middleware.py index f4ea9c6b..3a932fde 100644 --- a/backend/middleware.py +++ b/backend/middleware.py @@ -18,7 +18,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: ssl_cert_reqs=ssl.CERT_NONE ) db = client[MONGO_DATABASE] - scope["state"].db = db + Request(scope).state.db = db await self._app(scope, receive, send)