11from abc import ABC
22from datetime import datetime , timedelta
33from typing import Any , Dict , Optional , Set
4- from uuid import uuid1
4+ from uuid import uuid4
55
66from fastapi .exceptions import HTTPException
77from fastapi .param_functions import Security
@@ -83,6 +83,24 @@ def __init__(
8383 self .access_expires_delta = access_expires_delta or timedelta (minutes = 15 )
8484 self .refresh_expires_delta = refresh_expires_delta or timedelta (days = 31 )
8585
86+ @classmethod
87+ def from_other (
88+ cls ,
89+ other : 'JwtAuthBase' ,
90+ secret_key : Optional [str ] = None ,
91+ auto_error : Optional [bool ] = None ,
92+ algorithm : Optional [str ] = None ,
93+ access_expires_delta : Optional [timedelta ] = None ,
94+ refresh_expires_delta : Optional [timedelta ] = None ,
95+ ) -> 'JwtAuthBase' :
96+ return cls (
97+ secret_key = secret_key or other .secret_key ,
98+ auto_error = auto_error or other .auto_error ,
99+ algorithm = algorithm or other .algorithm ,
100+ access_expires_delta = access_expires_delta or other .access_expires_delta ,
101+ refresh_expires_delta = refresh_expires_delta or other .refresh_expires_delta ,
102+ )
103+
86104 def _decode (self , token : str ) -> Optional [Dict [str , Any ]]:
87105 try :
88106 payload : Dict [str , Any ] = jwt .decode (
@@ -124,24 +142,17 @@ def _generate_payload(
124142 "jti" : unique_identifier , # uuid
125143 }
126144
127- def _get_token (
128- self ,
129- bearer : Optional [HTTPBearer ] = None ,
130- cookie : Optional [APIKeyCookie ] = None ,
131- ) -> Optional [str ]:
132- if bearer :
133- return str (bearer .credentials ) # type: ignore
134- if cookie :
135- return str (cookie )
136- return None
137-
138- def _get_payload (
145+ async def _get_payload (
139146 self , bearer : Optional [HTTPBearer ], cookie : Optional [APIKeyCookie ]
140147 ) -> Optional [Dict [str , Any ]]:
141- refresh_token = self ._get_token (bearer , cookie ) # TODO: del function
148+ token : Optional [str ] = None
149+ if bearer :
150+ token = str (bearer .credentials ) # type: ignore
151+ elif cookie :
152+ token = str (cookie )
142153
143154 # Check token exist
144- if refresh_token is None :
155+ if not token :
145156 if self .auto_error :
146157 raise HTTPException (
147158 status_code = HTTP_401_UNAUTHORIZED , detail = "Credentials are not provided"
@@ -150,7 +161,7 @@ def _get_payload(
150161 return None
151162
152163 # Try to decode jwt token. auto_error on error
153- payload = self ._decode (refresh_token )
164+ payload = self ._decode (token )
154165 return payload
155166
156167 def create_access_token (
@@ -160,7 +171,7 @@ def create_access_token(
160171 unique_identifier : Optional [str ] = None ,
161172 ) -> str :
162173 expires_delta = expires_delta or self .access_expires_delta
163- unique_identifier = unique_identifier or str (uuid1 ())
174+ unique_identifier = unique_identifier or str (uuid4 ())
164175 to_encode = self ._generate_payload (
165176 subject , expires_delta , unique_identifier , "access"
166177 )
@@ -177,7 +188,7 @@ def create_refresh_token(
177188 unique_identifier : Optional [str ] = None ,
178189 ) -> str :
179190 expires_delta = expires_delta or self .refresh_expires_delta
180- unique_identifier = unique_identifier or str (uuid1 ())
191+ unique_identifier = unique_identifier or str (uuid4 ())
181192 to_encode = self ._generate_payload (
182193 subject , expires_delta , unique_identifier , "refresh"
183194 )
@@ -252,12 +263,12 @@ def __init__(
252263 refresh_expires_delta = refresh_expires_delta ,
253264 )
254265
255- def _get_credentials (
266+ async def _get_credentials (
256267 self ,
257268 bearer : Optional [JwtAuthBase .JwtAccessBearer ],
258269 cookie : Optional [JwtAuthBase .JwtAccessCookie ],
259270 ) -> Optional [JwtAuthorizationCredentials ]:
260- payload = self ._get_payload (bearer , cookie )
271+ payload = await self ._get_payload (bearer , cookie )
261272
262273 if payload :
263274 return JwtAuthorizationCredentials (
@@ -284,10 +295,10 @@ def __init__(
284295 refresh_expires_delta = refresh_expires_delta ,
285296 )
286297
287- def __call__ (
298+ async def __call__ (
288299 self , bearer : JwtAuthBase .JwtAccessBearer = Security (JwtAccess ._bearer )
289300 ) -> Optional [JwtAuthorizationCredentials ]:
290- return self ._get_credentials (bearer = bearer , cookie = None )
301+ return await self ._get_credentials (bearer = bearer , cookie = None )
291302
292303
293304class JwtAccessCookie (JwtAccess ):
@@ -308,11 +319,11 @@ def __init__(
308319 refresh_expires_delta = refresh_expires_delta ,
309320 )
310321
311- def __call__ (
322+ async def __call__ (
312323 self ,
313324 cookie : JwtAuthBase .JwtAccessCookie = Security (JwtAccess ._cookie ),
314325 ) -> Optional [JwtAuthorizationCredentials ]:
315- return self ._get_credentials (bearer = None , cookie = cookie )
326+ return await self ._get_credentials (bearer = None , cookie = cookie )
316327
317328
318329class JwtAccessBearerCookie (JwtAccess ):
@@ -333,12 +344,12 @@ def __init__(
333344 refresh_expires_delta = refresh_expires_delta ,
334345 )
335346
336- def __call__ (
347+ async def __call__ (
337348 self ,
338349 bearer : JwtAuthBase .JwtAccessBearer = Security (JwtAccess ._bearer ),
339350 cookie : JwtAuthBase .JwtAccessCookie = Security (JwtAccess ._cookie ),
340351 ) -> Optional [JwtAuthorizationCredentials ]:
341- return self ._get_credentials (bearer = bearer , cookie = cookie )
352+ return await self ._get_credentials (bearer = bearer , cookie = cookie )
342353
343354
344355class JwtRefresh (JwtAuthBase ):
@@ -363,12 +374,12 @@ def __init__(
363374 refresh_expires_delta = refresh_expires_delta ,
364375 )
365376
366- def _get_credentials (
377+ async def _get_credentials (
367378 self ,
368379 bearer : Optional [JwtAuthBase .JwtRefreshBearer ],
369380 cookie : Optional [JwtAuthBase .JwtRefreshCookie ],
370381 ) -> Optional [JwtAuthorizationCredentials ]:
371- payload = self ._get_payload (bearer , cookie )
382+ payload = await self ._get_payload (bearer , cookie )
372383
373384 if payload is None :
374385 return None
@@ -405,10 +416,10 @@ def __init__(
405416 refresh_expires_delta = refresh_expires_delta ,
406417 )
407418
408- def __call__ (
419+ async def __call__ (
409420 self , bearer : JwtAuthBase .JwtRefreshBearer = Security (JwtRefresh ._bearer )
410421 ) -> Optional [JwtAuthorizationCredentials ]:
411- return self ._get_credentials (bearer = bearer , cookie = None )
422+ return await self ._get_credentials (bearer = bearer , cookie = None )
412423
413424
414425class JwtRefreshCookie (JwtRefresh ):
@@ -429,11 +440,11 @@ def __init__(
429440 refresh_expires_delta = refresh_expires_delta ,
430441 )
431442
432- def __call__ (
443+ async def __call__ (
433444 self ,
434445 cookie : JwtAuthBase .JwtRefreshCookie = Security (JwtRefresh ._cookie ),
435446 ) -> Optional [JwtAuthorizationCredentials ]:
436- return self ._get_credentials (bearer = None , cookie = cookie )
447+ return await self ._get_credentials (bearer = None , cookie = cookie )
437448
438449
439450class JwtRefreshBearerCookie (JwtRefresh ):
@@ -454,9 +465,9 @@ def __init__(
454465 refresh_expires_delta = refresh_expires_delta ,
455466 )
456467
457- def __call__ (
468+ async def __call__ (
458469 self ,
459470 bearer : JwtAuthBase .JwtRefreshBearer = Security (JwtRefresh ._bearer ),
460471 cookie : JwtAuthBase .JwtRefreshCookie = Security (JwtRefresh ._cookie ),
461472 ) -> Optional [JwtAuthorizationCredentials ]:
462- return self ._get_credentials (bearer = bearer , cookie = cookie )
473+ return await self ._get_credentials (bearer = bearer , cookie = cookie )
0 commit comments