From 58a32c16911b5e2fb96fa75d1964cf4eb974897b Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Fri, 3 May 2024 12:06:58 -0400 Subject: [PATCH 1/3] docs: enable interactive docs in dev mode --- bento_authorization_service/main.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/bento_authorization_service/main.py b/bento_authorization_service/main.py index f131241..7ee658e 100644 --- a/bento_authorization_service/main.py +++ b/bento_authorization_service/main.py @@ -6,6 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from starlette.exceptions import HTTPException as StarletteHTTPException +from urllib.parse import urlparse from . import __version__ from .config import ConfigDependency, get_config @@ -22,7 +23,16 @@ # TODO: Find a way to DI this config_for_setup = get_config() -app = FastAPI() +DOCS_URL = "/docs" +OPENAPI_URL = "/openapi.json" + +app = FastAPI( + title=config_for_setup.service_name, + root_path=urlparse(config_for_setup.service_url_base_path).path, + docs_url=DOCS_URL, + openapi_url=OPENAPI_URL, + version=__version__, +) app.add_middleware( CORSMiddleware, allow_origins=config_for_setup.cors_origins, @@ -48,7 +58,12 @@ async def permissions_enforcement(request: Request, call_next) -> Response: about permissions and decided the request should go through (or be rejected). """ - if request.method == "OPTIONS": # Allow pre-flight responses through + # Allow pre-flight responses through + # Allow docs responses through in development mode + if request.method == "OPTIONS" or ( + config_for_setup.bento_debug + and (request.url.path.startswith(DOCS_URL) or request.url.path.startswith(OPENAPI_URL)) + ): return await call_next(request) # Set flag saying the request hasn't had its permissions determined yet. From 44495d47f2f321ee9431cf55cd82b1151bfc4283 Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Fri, 3 May 2024 12:09:33 -0400 Subject: [PATCH 2/3] docs: mention interactive docs in readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 66dbd14..04cbdf1 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@ Permissions and authorization service for the Bento platform. TODO +To see interactive documentation while in development mode in a Bento context, go to, e.g., +https://bentov2.local/api/authorization/docs. + From f3e5d30047c4b019d8187b04d6599658fa72d08f Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Fri, 3 May 2024 12:12:29 -0400 Subject: [PATCH 3/3] chore: tighten restrictions on auth passthrough for docs urls --- bento_authorization_service/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bento_authorization_service/main.py b/bento_authorization_service/main.py index 7ee658e..1de57d8 100644 --- a/bento_authorization_service/main.py +++ b/bento_authorization_service/main.py @@ -60,9 +60,10 @@ async def permissions_enforcement(request: Request, call_next) -> Response: # Allow pre-flight responses through # Allow docs responses through in development mode + req_path = request.url.path if request.method == "OPTIONS" or ( config_for_setup.bento_debug - and (request.url.path.startswith(DOCS_URL) or request.url.path.startswith(OPENAPI_URL)) + and (req_path == DOCS_URL or req_path.startswith(f"{DOCS_URL}/") or req_path == OPENAPI_URL) ): return await call_next(request)