fix: authorize in swagger (#1034)

<!-- .github/pull_request_template.md -->

## Description
<!-- Provide a clear description of the changes in this PR -->

## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.
This commit is contained in:
Boris 2025-06-30 15:56:30 +02:00 committed by GitHub
commit da14497ddc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

View file

@ -12,6 +12,7 @@ from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.exceptions import RequestValidationError
from fastapi.openapi.utils import get_openapi
from cognee.exceptions import CogneeApiError
from cognee.shared.logging_utils import get_logger, setup_logging
@ -75,6 +76,36 @@ app.add_middleware(
)
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Cognee API",
version="1.0.0",
description="Cognee API with Bearer token and Cookie auth",
routes=app.routes,
)
openapi_schema["components"]["securitySchemes"] = {
"BearerAuth": {"type": "http", "scheme": "bearer"},
"CookieAuth": {
"type": "apiKey",
"in": "cookie",
"name": os.getenv("AUTH_TOKEN_COOKIE_NAME", "auth_token"),
},
}
openapi_schema["security"] = [{"BearerAuth": []}, {"CookieAuth": []}]
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
@app.exception_handler(RequestValidationError)
async def request_validation_exception_handler(request: Request, exc: RequestValidationError):
if request.url.path == "/api/v1/auth/login":

View file

@ -56,6 +56,7 @@ class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
response.body = json.dumps(
{"access_token": access_token, "token_type": "bearer"}
).encode(encoding="utf-8")
response.headers.append("Content-Type", "application/json")
async def on_after_register(self, user: User, request: Optional[Request] = None):
print(f"User {user.id} has registered.")