@@ -315,12 +315,18 @@ def _get_csrf_token(self,encoded_token: str) -> str:
315315 """
316316 return self ._verified_token (encoded_token )['csrf' ]
317317
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 :
319324 """
320325 Configures the response to set access token in a cookie.
321326 this will also set the CSRF double submit values in a separate cookie
322327
323328 :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
324330 :param max_age: The max age of the cookie value should be the number of seconds (integer)
325331 """
326332 if not self .jwt_in_cookies :
@@ -330,9 +336,13 @@ def set_access_cookies(self,encoded_access_token: str, max_age: Optional[int] =
330336
331337 if max_age and not isinstance (max_age ,int ):
332338 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
333343
334344 # Set the access JWT in the cookie
335- self . _response .set_cookie (
345+ response .set_cookie (
336346 self ._access_cookie_key ,
337347 encoded_access_token ,
338348 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] =
345355
346356 # If enabled, set the csrf double submit access cookie
347357 if self ._cookie_csrf_protect :
348- self . _response .set_cookie (
358+ response .set_cookie (
349359 self ._access_csrf_cookie_key ,
350360 self ._get_csrf_token (encoded_access_token ),
351361 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] =
356366 samesite = self ._cookie_samesite
357367 )
358368
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 :
360375 """
361376 Configures the response to set refresh token in a cookie.
362377 this will also set the CSRF double submit values in a separate cookie
363378
364379 :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
365381 :param max_age: The max age of the cookie value should be the number of seconds (integer)
366382 """
367383 if not self .jwt_in_cookies :
@@ -371,9 +387,13 @@ def set_refresh_cookies(self, encoded_refresh_token: str, max_age: Optional[int]
371387
372388 if max_age and not isinstance (max_age ,int ):
373389 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
374394
375395 # Set the refresh JWT in the cookie
376- self . _response .set_cookie (
396+ response .set_cookie (
377397 self ._refresh_cookie_key ,
378398 encoded_refresh_token ,
379399 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]
386406
387407 # If enabled, set the csrf double submit refresh cookie
388408 if self ._cookie_csrf_protect :
389- self . _response .set_cookie (
409+ response .set_cookie (
390410 self ._refresh_csrf_cookie_key ,
391411 self ._get_csrf_token (encoded_refresh_token ),
392412 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]
397417 samesite = self ._cookie_samesite
398418 )
399419
400- def unset_jwt_cookies (self ) -> None :
420+ def unset_jwt_cookies (self , response : Optional [ Response ] = None ) -> None :
401421 """
402422 Unset (delete) all jwt stored in a cookie
423+
424+ :param response: The FastAPI response object to delete the JWT cookies in.
403425 """
404- self .unset_access_cookies ()
405- self .unset_refresh_cookies ()
426+ self .unset_access_cookies (response )
427+ self .unset_refresh_cookies (response )
406428
407- def unset_access_cookies (self ) -> None :
429+ def unset_access_cookies (self , response : Optional [ Response ] = None ) -> None :
408430 """
409431 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.
410434 """
411435 if not self .jwt_in_cookies :
412436 raise RuntimeWarning (
413437 "unset_access_cookies() called without 'authjwt_token_location' configured to use cookies"
414438 )
415439
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 (
417446 self ._access_cookie_key ,
418447 path = self ._access_cookie_path ,
419448 domain = self ._cookie_domain
420449 )
421450
422451 if self ._cookie_csrf_protect :
423- self . _response .delete_cookie (
452+ response .delete_cookie (
424453 self ._access_csrf_cookie_key ,
425454 path = self ._access_csrf_cookie_path ,
426455 domain = self ._cookie_domain
427456 )
428457
429- def unset_refresh_cookies (self ) -> None :
458+ def unset_refresh_cookies (self , response : Optional [ Response ] = None ) -> None :
430459 """
431460 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.
432463 """
433464 if not self .jwt_in_cookies :
434465 raise RuntimeWarning (
435466 "unset_refresh_cookies() called without 'authjwt_token_location' configured to use cookies"
436467 )
437468
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 (
439475 self ._refresh_cookie_key ,
440476 path = self ._refresh_cookie_path ,
441477 domain = self ._cookie_domain
442478 )
443479
444480 if self ._cookie_csrf_protect :
445- self . _response .delete_cookie (
481+ response .delete_cookie (
446482 self ._refresh_csrf_cookie_key ,
447483 path = self ._refresh_csrf_cookie_path ,
448484 domain = self ._cookie_domain
0 commit comments