tiangolo / fastapi

FastAPI framework, high performance, easy to learn, fast to code, ready for production
https://fastapi.tiangolo.com/
MIT License
73.13k stars 6.16k forks source link

[feature] Add CookieToAuth2 middleware to convert cookies to Authorization header #11684

Open LainezDev opened 4 weeks ago

LainezDev commented 4 weeks ago

Allows developers to use cookies and authorization headers without changing authentication settings. OAuth2PasswordBearer is designed to look for the token in the authorization header of the request. The middleware that converts the token cookie into an Authorization header ensuring that when OAuth2PasswordBearer inspects the request, it finds the necessary header in the correct format. It is a simple proposal to several discussions about the use of cookies for authorization of web pages with FastAPI.

I'm looking forward to your feedback

from fastapi import FastAPI
from fastapi.middleware.cookietoauth2 import CookieToAuth2Middleware

app = FastAPI()

app.add_middleware(CookieToAuth2Middleware, cookie_name="cookie_name")