Skip to content
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

Extend openapi.json with security schemes #2

Merged
merged 1 commit into from
Feb 28, 2024
Merged
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
32 changes: 32 additions & 0 deletions dialog_api_key/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from decouple import Csv, config
from fastapi import FastAPI, Request
from fastapi.openapi.utils import get_openapi
from fastapi.responses import JSONResponse


Expand Down Expand Up @@ -41,4 +42,35 @@ async def api_key_middleware(request: Request, call_next):


def register_plugin(app: FastAPI):
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema

schema = get_openapi(
title=app.title,
version=app.version,
description=app.description,
summary=app.summary,
routes=app.routes,
)
schema["components"]["securitySchemes"] = {
"ApiKeyAuth": {
"type": "apiKey",
"in": "header",
"name": settings.header,
}
}
schema["security"] = [
{"ApiKeyAuth": []},
]
for path in settings.except_paths:
try:
schema["paths"][path]["security"] = []
except KeyError:
continue

app.openapi_schema = schema
return schema

app.openapi = custom_openapi
app.middleware("http")(api_key_middleware)