@@ -506,7 +506,7 @@ def _verify_and_get_jwt_optional_in_cookies(self,issuer: Optional[str] = None) -
506
506
507
507
# set token from cookie and verify jwt
508
508
self ._token = cookie
509
- self ._verify_jwt_optional_in_request (self ._token ,issuer )
509
+ self .verify_jwt_optional_in_request (self ._token ,issuer )
510
510
511
511
decoded_token = self .get_raw_jwt ()
512
512
@@ -553,7 +553,7 @@ def _verify_and_get_jwt_in_cookies(
553
553
554
554
# set token from cookie and verify jwt
555
555
self ._token = cookie
556
- self ._verify_jwt_in_request (self ._token ,type_token ,'cookies' ,issuer ,fresh )
556
+ self .verify_jwt_in_request (self ._token ,type_token ,'cookies' ,issuer ,fresh )
557
557
558
558
decoded_token = self .get_raw_jwt ()
559
559
@@ -563,7 +563,7 @@ def _verify_and_get_jwt_in_cookies(
563
563
if not hmac .compare_digest (csrf_cookie ,decoded_token ['csrf' ]):
564
564
raise CSRFError (status_code = 401 ,message = "CSRF double submit tokens do not match" )
565
565
566
- def _verify_jwt_optional_in_request (self ,token : str , issuer : Optional [str ] = None ) -> None :
566
+ def verify_jwt_optional_in_request (self ,token : str , issuer : Optional [str ] = None ) -> None :
567
567
"""
568
568
Optionally check if this request has a valid access token
569
569
@@ -573,10 +573,10 @@ def _verify_jwt_optional_in_request(self,token: str, issuer: Optional[str] = Non
573
573
if token :
574
574
self ._verifying_token (token ,issuer )
575
575
576
- if token and self ._get_type_token () != 'access' :
576
+ if token and self .get_raw_jwt ( token )[ 'type' ] != 'access' :
577
577
raise AccessTokenRequired (status_code = 422 ,message = "Only access tokens are allowed" )
578
578
579
- def _verify_jwt_in_request (
579
+ def verify_jwt_in_request (
580
580
self ,
581
581
token : str ,
582
582
type_token : str ,
@@ -599,14 +599,14 @@ def _verify_jwt_in_request(
599
599
if not token and token_from == 'headers' :
600
600
raise MissingHeaderError (status_code = 401 ,message = "Missing {} Header" .format (self ._header_name ))
601
601
602
- if self ._get_type_token () != type_token :
602
+ if self .get_raw_jwt ( token )[ 'type' ] != type_token :
603
603
msg = "Only {} tokens are allowed" .format (type_token )
604
604
if type_token == 'access' :
605
605
raise AccessTokenRequired (status_code = 422 ,message = msg )
606
606
if type_token == 'refresh' :
607
607
raise RefreshTokenRequired (status_code = 422 ,message = msg )
608
608
609
- if fresh and not self ._get_fresh_token () :
609
+ if fresh and not self .get_raw_jwt ( token )[ 'fresh' ] :
610
610
raise FreshTokenRequired (status_code = 401 ,message = "Fresh token required" )
611
611
612
612
def _verifying_token (self ,encoded_token : str , issuer : Optional [str ] = None ) -> None :
@@ -653,24 +653,18 @@ def _verified_token(self,encoded_token: str, issuer: Optional[str] = None) -> Di
653
653
except Exception as err :
654
654
raise JWTDecodeError (status_code = 422 ,message = str (err ))
655
655
656
- def _get_type_token (self ) -> str :
657
- return self .get_raw_jwt ()['type' ]
658
-
659
- def _get_fresh_token (self ) -> bool :
660
- return self .get_raw_jwt ()['fresh' ]
661
-
662
656
def jwt_required (self ) -> None :
663
657
"""
664
658
Only access token can access this function
665
659
"""
666
660
if len (self ._token_location ) == 2 :
667
661
if self ._token and self .jwt_in_headers :
668
- self ._verify_jwt_in_request (self ._token ,'access' ,'headers' ,self ._decode_issuer )
662
+ self .verify_jwt_in_request (self ._token ,'access' ,'headers' ,self ._decode_issuer )
669
663
if not self ._token and self .jwt_in_cookies :
670
664
self ._verify_and_get_jwt_in_cookies ('access' ,self ._decode_issuer )
671
665
else :
672
666
if self .jwt_in_headers :
673
- self ._verify_jwt_in_request (self ._token ,'access' ,'headers' ,self ._decode_issuer )
667
+ self .verify_jwt_in_request (self ._token ,'access' ,'headers' ,self ._decode_issuer )
674
668
if self .jwt_in_cookies :
675
669
self ._verify_and_get_jwt_in_cookies ('access' ,self ._decode_issuer )
676
670
@@ -682,12 +676,12 @@ def jwt_optional(self) -> None:
682
676
"""
683
677
if len (self ._token_location ) == 2 :
684
678
if self ._token and self .jwt_in_headers :
685
- self ._verify_jwt_optional_in_request (self ._token ,self ._decode_issuer )
679
+ self .verify_jwt_optional_in_request (self ._token ,self ._decode_issuer )
686
680
if not self ._token and self .jwt_in_cookies :
687
681
self ._verify_and_get_jwt_optional_in_cookies (self ._decode_issuer )
688
682
else :
689
683
if self .jwt_in_headers :
690
- self ._verify_jwt_optional_in_request (self ._token ,self ._decode_issuer )
684
+ self .verify_jwt_optional_in_request (self ._token ,self ._decode_issuer )
691
685
if self .jwt_in_cookies :
692
686
self ._verify_and_get_jwt_optional_in_cookies (self ._decode_issuer )
693
687
@@ -697,12 +691,12 @@ def jwt_refresh_token_required(self) -> None:
697
691
"""
698
692
if len (self ._token_location ) == 2 :
699
693
if self ._token and self .jwt_in_headers :
700
- self ._verify_jwt_in_request (self ._token ,'refresh' ,'headers' )
694
+ self .verify_jwt_in_request (self ._token ,'refresh' ,'headers' )
701
695
if not self ._token and self .jwt_in_cookies :
702
696
self ._verify_and_get_jwt_in_cookies ('refresh' )
703
697
else :
704
698
if self .jwt_in_headers :
705
- self ._verify_jwt_in_request (self ._token ,'refresh' ,'headers' )
699
+ self .verify_jwt_in_request (self ._token ,'refresh' ,'headers' )
706
700
if self .jwt_in_cookies :
707
701
self ._verify_and_get_jwt_in_cookies ('refresh' )
708
702
@@ -712,30 +706,34 @@ def fresh_jwt_required(self) -> None:
712
706
"""
713
707
if len (self ._token_location ) == 2 :
714
708
if self ._token and self .jwt_in_headers :
715
- self ._verify_jwt_in_request (self ._token ,'access' ,'headers' ,self ._decode_issuer ,True )
709
+ self .verify_jwt_in_request (self ._token ,'access' ,'headers' ,self ._decode_issuer ,True )
716
710
if not self ._token and self .jwt_in_cookies :
717
711
self ._verify_and_get_jwt_in_cookies ('access' ,self ._decode_issuer ,True )
718
712
else :
719
713
if self .jwt_in_headers :
720
- self ._verify_jwt_in_request (self ._token ,'access' ,'headers' ,self ._decode_issuer ,True )
714
+ self .verify_jwt_in_request (self ._token ,'access' ,'headers' ,self ._decode_issuer ,True )
721
715
if self .jwt_in_cookies :
722
716
self ._verify_and_get_jwt_in_cookies ('access' ,self ._decode_issuer ,True )
723
717
724
- def get_raw_jwt (self ) -> Optional [Dict [str ,Union [str ,int ,bool ]]]:
718
+ def get_raw_jwt (self , encoded_token : Optional [ str ] = None ) -> Optional [Dict [str ,Union [str ,int ,bool ]]]:
725
719
"""
726
720
this will return the python dictionary which has all of the claims of the JWT that is accessing the endpoint.
727
721
If no JWT is currently present, return None instead
728
722
723
+ :param encoded_token: The encoded JWT from parameter
729
724
:return: claims of JWT
730
725
"""
731
- if self ._token :
732
- return self ._verified_token (self ._token )
726
+ token = encoded_token or self ._token
727
+
728
+ if token :
729
+ return self ._verified_token (token )
733
730
return None
734
731
735
732
def get_jti (self ,encoded_token : str ) -> str :
736
733
"""
737
734
Returns the JTI (unique identifier) of an encoded JWT
738
735
736
+ :param encoded_token: The encoded JWT from parameter
739
737
:return: string of JTI
740
738
"""
741
739
return self ._verified_token (encoded_token )['jti' ]
0 commit comments