markomirosavljev / fastapi-cognito

Basic Cognito-Auth library for Python and FastAPI.
MIT License
53 stars 17 forks source link

OpenAPI support #12

Closed gsiegman closed 1 year ago

gsiegman commented 1 year ago

Is it currently possible to make this work with FastAPIs OpenAPI implementation? I'm basically looking for a way to add the authorize button to OpenAPI docs and have the resulting JWT be passed in on OpenAPI requests. If it's not currently possible, is this something that is planned for a future release?

markomirosavljev commented 1 year ago

Hello, sorry for late response. I investigated if this case is possible and I think this could be a valid solution:

from fastapi.security import HTTPBearer
from starlette.requests import Request
from fastapi_cognito import CognitoToken

class AuthAWS(HTTPBearer):

    async def __call__(self, request: Request) -> CognitoToken:
        return await cognito.auth_required(request=request)

cog_auth = AuthAWS()

@router.get("/")
async def test_endpoint(auth: CognitoToken = Depends(cog_auth)):
    return JSONResponse(
        status_code=200, content={"detail": "Success"}
    )

This should create Authorize button in your OpenAPI Docs and you will be able to pass a token, authorization should work with this.

I think this could be great feature and I will try to implement this in future releases. :relaxed: