fix: add auth methods to swagger authtorize
This commit is contained in:
parent
e44840c601
commit
5e6bbef0b3
2 changed files with 44 additions and 1 deletions
|
|
@ -12,6 +12,7 @@ from fastapi.encoders import jsonable_encoder
|
||||||
from fastapi.responses import JSONResponse, Response
|
from fastapi.responses import JSONResponse, Response
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.exceptions import RequestValidationError
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from fastapi.openapi.utils import get_openapi
|
||||||
|
|
||||||
from cognee.exceptions import CogneeApiError
|
from cognee.exceptions import CogneeApiError
|
||||||
from cognee.shared.logging_utils import get_logger, setup_logging
|
from cognee.shared.logging_utils import get_logger, setup_logging
|
||||||
|
|
@ -75,6 +76,40 @@ 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)
|
@app.exception_handler(RequestValidationError)
|
||||||
async def request_validation_exception_handler(request: Request, exc: RequestValidationError):
|
async def request_validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||||
if request.url.path == "/api/v1/auth/login":
|
if request.url.path == "/api/v1/auth/login":
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,17 @@
|
||||||
|
from cognee.modules.users.authentication.get_api_auth_backend import get_api_auth_backend
|
||||||
|
from cognee.modules.users.authentication.get_client_auth_backend import get_client_auth_backend
|
||||||
from ..get_fastapi_users import get_fastapi_users
|
from ..get_fastapi_users import get_fastapi_users
|
||||||
|
|
||||||
|
|
||||||
fastapi_users = get_fastapi_users()
|
fastapi_users = get_fastapi_users()
|
||||||
|
|
||||||
get_authenticated_user = fastapi_users.current_user(active=True)
|
def get_enabled_backends():
|
||||||
|
api_auth_backend = get_api_auth_backend()
|
||||||
|
client_auth_backend = get_client_auth_backend()
|
||||||
|
|
||||||
|
return [api_auth_backend, client_auth_backend]
|
||||||
|
|
||||||
|
get_authenticated_user = fastapi_users.current_user(active=True, get_enabled_backends=get_enabled_backends)
|
||||||
|
|
||||||
# from types import SimpleNamespace
|
# from types import SimpleNamespace
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue