File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ from fastapi .security import OAuth2 as FastAPIOAuth2
2
+ from fastapi .security import OAuth2AuthorizationCodeBearer as FastAPICodeBearer
3
+ from fastapi .security import OAuth2PasswordBearer as FastAPIPasswordBearer
4
+ from starlette .datastructures import Headers
5
+ from starlette .requests import Request
6
+
7
+
8
+ def use_cookie (cls : FastAPIOAuth2 ):
9
+ def _use_cookie (* args , ** kwargs ):
10
+ async def __call__ (self , request : Request ):
11
+ authorization = request .headers .get ("Authorization" , request .cookies .get ("Authorization" ))
12
+ if authorization :
13
+ request ._headers = Headers ({** request .headers , "Authorization" : authorization })
14
+ return await super (cls , self ).__call__ (request )
15
+
16
+ cls .__call__ = __call__
17
+ return cls (* args , ** kwargs )
18
+
19
+ return _use_cookie
20
+
21
+
22
+ @use_cookie
23
+ class OAuth2 (FastAPIOAuth2 ):
24
+ ...
25
+
26
+
27
+ @use_cookie
28
+ class OAuth2PasswordBearer (FastAPIPasswordBearer ):
29
+ ...
30
+
31
+
32
+ @use_cookie
33
+ class OAuth2AuthorizationCodeBearer (FastAPICodeBearer ):
34
+ ...
You can’t perform that action at this time.
0 commit comments