Skip to content

feat: enable hosting custom Swagger UI #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ The application is configurable via environment variables.
- **Type:** JSON object
- **Required:** No, defaults to `null` (disabled)
- **Example:** `{"type": "http", "scheme": "bearer", "bearerFormat": "JWT", "description": "Paste your raw JWT here. This API uses Bearer token authorization.\n"}`
- **`SWAGGER_UI_ENDPOINT`**, path of Swagger UI, used for augmenting spec response with auth configuration
- **Type:** string or null
- **Required:** No, defaults to `/api.html`
- **Example:** `/api`
- **`SWAGGER_UI_INIT_OAUTH`**, initialization options for the [Swagger UI OAuth2 configuration](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/)
- **Type:** JSON object
- **Required:** No, defaults to `null` (disabled)
- **Example:** `{"clientId": "stac-auth-proxy", "usePkceWithAuthorizationCodeGrant": true}`
- Filtering
- **`ITEMS_FILTER_CLS`**, CQL2 expression generator for item-level filtering
- **Type:** JSON object with class configuration
Expand Down
14 changes: 13 additions & 1 deletion src/stac_auth_proxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from starlette_cramjam.middleware import CompressionMiddleware

from .config import Settings
from .handlers import HealthzHandler, ReverseProxyHandler
from .handlers import HealthzHandler, ReverseProxyHandler, SwaggerUI
from .middleware import (
AddProcessTimeHeaderMiddleware,
ApplyCql2FilterMiddleware,
Expand Down Expand Up @@ -78,6 +78,18 @@ async def lifespan(app: FastAPI):
# Handlers (place catch-all proxy handler last)
#

if settings.swagger_ui_endpoint:
assert (
settings.openapi_spec_endpoint
), "openapi_spec_endpoint must be set when using swagger_ui_endpoint"
app.add_route(
settings.swagger_ui_endpoint,
SwaggerUI(
openapi_url=settings.openapi_spec_endpoint,
init_oauth=settings.swagger_ui_init_oauth,
).route,
include_in_schema=False,
)
if settings.healthz_prefix:
app.include_router(
HealthzHandler(upstream_url=str(settings.upstream_url)).router,
Expand Down
3 changes: 3 additions & 0 deletions src/stac_auth_proxy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ class Settings(BaseSettings):
check_conformance: bool = True
enable_compression: bool = True

# OpenAPI / Swagger UI
openapi_spec_endpoint: Optional[str] = Field(pattern=_PREFIX_PATTERN, default=None)
openapi_auth_scheme_name: str = "oidcAuth"
openapi_auth_scheme_override: Optional[dict] = None
swagger_ui_endpoint: Optional[str] = None
swagger_ui_init_oauth: dict = Field(default_factory=dict)

# Auth
enable_authentication_extension: bool = True
Expand Down
3 changes: 2 additions & 1 deletion src/stac_auth_proxy/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

from .healthz import HealthzHandler
from .reverse_proxy import ReverseProxyHandler
from .swagger_ui import SwaggerUI

__all__ = ["ReverseProxyHandler", "HealthzHandler"]
__all__ = ["ReverseProxyHandler", "HealthzHandler", "SwaggerUI"]
41 changes: 41 additions & 0 deletions src/stac_auth_proxy/handlers/swagger_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
In order to allow customization fo the Swagger UI's OAuth2 configuration, we support
overriding the default handler. This is useful for adding custom parameters such as
`usePkceWithAuthorizationCodeGrant` or `clientId`.

See:
- https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/
"""

from dataclasses import dataclass, field
from typing import Optional

from fastapi.openapi.docs import get_swagger_ui_html
from starlette.requests import Request
from starlette.responses import HTMLResponse


@dataclass
class SwaggerUI:
"""Swagger UI handler."""

openapi_url: str
title: Optional[str] = "STAC API"
init_oauth: dict = field(default_factory=dict)
parameters: dict = field(default_factory=dict)
oauth2_redirect_url: str = "/docs/oauth2-redirect"

async def route(self, req: Request) -> HTMLResponse:
"""Route handler."""
root_path = req.scope.get("root_path", "").rstrip("/")
openapi_url = root_path + self.openapi_url
oauth2_redirect_url = self.oauth2_redirect_url
if oauth2_redirect_url:
oauth2_redirect_url = root_path + oauth2_redirect_url
return get_swagger_ui_html(
openapi_url=openapi_url,
title=f"{self.title} - Swagger UI",
oauth2_redirect_url=oauth2_redirect_url,
init_oauth=self.init_oauth,
swagger_ui_parameters=self.parameters,
)
Loading