@@ -315,12 +315,18 @@ def _get_csrf_token(self,encoded_token: str) -> str:
315
315
"""
316
316
return self ._verified_token (encoded_token )['csrf' ]
317
317
318
- def set_access_cookies (self ,encoded_access_token : str , max_age : Optional [int ] = None ) -> None :
318
+ def set_access_cookies (
319
+ self ,
320
+ encoded_access_token : str ,
321
+ response : Optional [Response ] = None ,
322
+ max_age : Optional [int ] = None
323
+ ) -> None :
319
324
"""
320
325
Configures the response to set access token in a cookie.
321
326
this will also set the CSRF double submit values in a separate cookie
322
327
323
328
:param encoded_access_token: The encoded access token to set in the cookies
329
+ :param response: The FastAPI response object to set the access cookies in
324
330
:param max_age: The max age of the cookie value should be the number of seconds (integer)
325
331
"""
326
332
if not self .jwt_in_cookies :
@@ -330,9 +336,13 @@ def set_access_cookies(self,encoded_access_token: str, max_age: Optional[int] =
330
336
331
337
if max_age and not isinstance (max_age ,int ):
332
338
raise TypeError ("max_age must be a integer" )
339
+ if response and not isinstance (response ,Response ):
340
+ raise TypeError ("The response must be an object response FastAPI" )
341
+
342
+ response = response or self ._response
333
343
334
344
# Set the access JWT in the cookie
335
- self . _response .set_cookie (
345
+ response .set_cookie (
336
346
self ._access_cookie_key ,
337
347
encoded_access_token ,
338
348
max_age = max_age or self ._cookie_max_age ,
@@ -345,7 +355,7 @@ def set_access_cookies(self,encoded_access_token: str, max_age: Optional[int] =
345
355
346
356
# If enabled, set the csrf double submit access cookie
347
357
if self ._cookie_csrf_protect :
348
- self . _response .set_cookie (
358
+ response .set_cookie (
349
359
self ._access_csrf_cookie_key ,
350
360
self ._get_csrf_token (encoded_access_token ),
351
361
max_age = max_age or self ._cookie_max_age ,
@@ -356,12 +366,18 @@ def set_access_cookies(self,encoded_access_token: str, max_age: Optional[int] =
356
366
samesite = self ._cookie_samesite
357
367
)
358
368
359
- def set_refresh_cookies (self , encoded_refresh_token : str , max_age : Optional [int ] = None ) -> None :
369
+ def set_refresh_cookies (
370
+ self ,
371
+ encoded_refresh_token : str ,
372
+ response : Optional [Response ] = None ,
373
+ max_age : Optional [int ] = None
374
+ ) -> None :
360
375
"""
361
376
Configures the response to set refresh token in a cookie.
362
377
this will also set the CSRF double submit values in a separate cookie
363
378
364
379
:param encoded_refresh_token: The encoded refresh token to set in the cookies
380
+ :param response: The FastAPI response object to set the refresh cookies in
365
381
:param max_age: The max age of the cookie value should be the number of seconds (integer)
366
382
"""
367
383
if not self .jwt_in_cookies :
@@ -371,9 +387,13 @@ def set_refresh_cookies(self, encoded_refresh_token: str, max_age: Optional[int]
371
387
372
388
if max_age and not isinstance (max_age ,int ):
373
389
raise TypeError ("max_age must be a integer" )
390
+ if response and not isinstance (response ,Response ):
391
+ raise TypeError ("The response must be an object response FastAPI" )
392
+
393
+ response = response or self ._response
374
394
375
395
# Set the refresh JWT in the cookie
376
- self . _response .set_cookie (
396
+ response .set_cookie (
377
397
self ._refresh_cookie_key ,
378
398
encoded_refresh_token ,
379
399
max_age = max_age or self ._cookie_max_age ,
@@ -386,7 +406,7 @@ def set_refresh_cookies(self, encoded_refresh_token: str, max_age: Optional[int]
386
406
387
407
# If enabled, set the csrf double submit refresh cookie
388
408
if self ._cookie_csrf_protect :
389
- self . _response .set_cookie (
409
+ response .set_cookie (
390
410
self ._refresh_csrf_cookie_key ,
391
411
self ._get_csrf_token (encoded_refresh_token ),
392
412
max_age = max_age or self ._cookie_max_age ,
@@ -397,52 +417,68 @@ def set_refresh_cookies(self, encoded_refresh_token: str, max_age: Optional[int]
397
417
samesite = self ._cookie_samesite
398
418
)
399
419
400
- def unset_jwt_cookies (self ) -> None :
420
+ def unset_jwt_cookies (self , response : Optional [ Response ] = None ) -> None :
401
421
"""
402
422
Unset (delete) all jwt stored in a cookie
423
+
424
+ :param response: The FastAPI response object to delete the JWT cookies in.
403
425
"""
404
- self .unset_access_cookies ()
405
- self .unset_refresh_cookies ()
426
+ self .unset_access_cookies (response )
427
+ self .unset_refresh_cookies (response )
406
428
407
- def unset_access_cookies (self ) -> None :
429
+ def unset_access_cookies (self , response : Optional [ Response ] = None ) -> None :
408
430
"""
409
431
Remove access token and access CSRF double submit from the response cookies
432
+
433
+ :param response: The FastAPI response object to delete the access cookies in.
410
434
"""
411
435
if not self .jwt_in_cookies :
412
436
raise RuntimeWarning (
413
437
"unset_access_cookies() called without 'authjwt_token_location' configured to use cookies"
414
438
)
415
439
416
- self ._response .delete_cookie (
440
+ if response and not isinstance (response ,Response ):
441
+ raise TypeError ("The response must be an object response FastAPI" )
442
+
443
+ response = response or self ._response
444
+
445
+ response .delete_cookie (
417
446
self ._access_cookie_key ,
418
447
path = self ._access_cookie_path ,
419
448
domain = self ._cookie_domain
420
449
)
421
450
422
451
if self ._cookie_csrf_protect :
423
- self . _response .delete_cookie (
452
+ response .delete_cookie (
424
453
self ._access_csrf_cookie_key ,
425
454
path = self ._access_csrf_cookie_path ,
426
455
domain = self ._cookie_domain
427
456
)
428
457
429
- def unset_refresh_cookies (self ) -> None :
458
+ def unset_refresh_cookies (self , response : Optional [ Response ] = None ) -> None :
430
459
"""
431
460
Remove refresh token and refresh CSRF double submit from the response cookies
461
+
462
+ :param response: The FastAPI response object to delete the refresh cookies in.
432
463
"""
433
464
if not self .jwt_in_cookies :
434
465
raise RuntimeWarning (
435
466
"unset_refresh_cookies() called without 'authjwt_token_location' configured to use cookies"
436
467
)
437
468
438
- self ._response .delete_cookie (
469
+ if response and not isinstance (response ,Response ):
470
+ raise TypeError ("The response must be an object response FastAPI" )
471
+
472
+ response = response or self ._response
473
+
474
+ response .delete_cookie (
439
475
self ._refresh_cookie_key ,
440
476
path = self ._refresh_cookie_path ,
441
477
domain = self ._cookie_domain
442
478
)
443
479
444
480
if self ._cookie_csrf_protect :
445
- self . _response .delete_cookie (
481
+ response .delete_cookie (
446
482
self ._refresh_csrf_cookie_key ,
447
483
path = self ._refresh_csrf_cookie_path ,
448
484
domain = self ._cookie_domain
0 commit comments