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

Fastapi generated api do not get authentication parameters from Cookie #583

Open
filippomc opened this issue Sep 23, 2022 · 1 comment
Open

Comments

@filippomc
Copy link
Collaborator

filippomc commented Sep 23, 2022

The generated function supports the bearer token but not cookie.

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    # retrieve the bearer token from the header
    # and save it for use in the AuthClient
    authorization = request.headers.get('Authorization')
    if authorization:
        set_authentication_token(authorization)

    return await call_next(request)

The cookie authentication is handy when the application is secured by a gatekeeper, as it comes for free on every request.

It's also ignoring the parameter coming from the spec, like in

  securitySchemes:
    bearerAuth:
      scheme: bearer
      bearerFormat: JWT
      type: http
      x-bearerInfoFunc: cloudharness.auth.decode_token
    cookieAuth:
      type: apiKey
      name: kc-access
      in: cookie
      x-apikeyInfoFunc: cloudharness.auth.decode_token

This is not necessarily required as we don't have different decode token handlers, but can be confusins as the Connexion apis require those instead

@filippomc filippomc changed the title Fastapi generated api do not get authentication parameters Fastapi generated api do not get authentication parameters from Cookie Sep 23, 2022
@filippomc
Copy link
Collaborator Author

To use the cookie in place of the Bearer can change the main.jinja2 template to use the APIKeyCookie in place of the Bearer authentication

Relevant code:

...
from fastapi.security import APIKeyCookie, HTTPBasicCredentials
...
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    # retrieve the bearer token from the header
    # and save it for use in the AuthClient
    authorization = request.headers.get('Authorization') or request.cookies.get(
        'kc-access'
    )
    if authorization:
        if "Bearer" in authorization:
            authorization = authorization.split("Bearer ")[1]
        set_authentication_token(authorization)
    return await call_next(request)
security = APIKeyCookie(name="kc-access")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant