1+ from typing import Callable
2+ from typing import Optional
3+ from typing import Type
4+
15from fastapi .security import OAuth2 as FastAPIOAuth2
26from fastapi .security import OAuth2AuthorizationCodeBearer as FastAPICodeBearer
37from fastapi .security import OAuth2PasswordBearer as FastAPIPasswordBearer
48from starlette .datastructures import Headers
59from starlette .requests import Request
610
711
8- def use_cookie (cls : FastAPIOAuth2 ):
9- def _use_cookie (* args , ** kwargs ):
10- async def __call__ (self , request : Request ):
12+ def use_cookies (cls : Type [FastAPIOAuth2 ]) -> Callable [[...], FastAPIOAuth2 ]:
13+ """OAuth2 classes wrapped with this decorator will use cookies for the Authorization header."""
14+
15+ def _use_cookies (* args , ** kwargs ) -> FastAPIOAuth2 :
16+ async def __call__ (self : FastAPIOAuth2 , request : Request ) -> Optional [str ]:
1117 authorization = request .headers .get ("Authorization" , request .cookies .get ("Authorization" ))
1218 if authorization :
1319 request ._headers = Headers ({** request .headers , "Authorization" : authorization })
@@ -16,19 +22,19 @@ async def __call__(self, request: Request):
1622 cls .__call__ = __call__
1723 return cls (* args , ** kwargs )
1824
19- return _use_cookie
25+ return _use_cookies
2026
2127
22- @use_cookie
28+ @use_cookies
2329class OAuth2 (FastAPIOAuth2 ):
24- ...
30+ """Wrapper class of the `fastapi.security.OAuth2` class."""
2531
2632
27- @use_cookie
33+ @use_cookies
2834class OAuth2PasswordBearer (FastAPIPasswordBearer ):
29- ...
35+ """Wrapper class of the `fastapi.security.OAuth2PasswordBearer` class."""
3036
3137
32- @use_cookie
38+ @use_cookies
3339class OAuth2AuthorizationCodeBearer (FastAPICodeBearer ):
34- ...
40+ """Wrapper class of the `fastapi.security.OAuth2AuthorizationCodeBearer` class."""
0 commit comments