1616
1717from typing import Any , Dict
1818import jwt
19- from jwt import PyJWKClient , ExpiredSignatureError , InvalidTokenError
19+ from jwt import PyJWKClient , ExpiredSignatureError , InvalidTokenError , DecodeError
2020from jwt import InvalidAudienceError , InvalidIssuerError , InvalidSignatureError
2121from firebase_admin import _utils
2222
@@ -38,6 +38,7 @@ def verify_token(token: str, app=None) -> Dict[str, Any]:
3838 Raises:
3939 ValueError: If the app's ``project_id`` is invalid or unspecified,
4040 or if the token's headers or payload are invalid.
41+ PyJWKClientError: If PyJWKClient fails to fetch a valid signing key.
4142 """
4243 return _get_app_check_service (app ).verify_token (token )
4344
@@ -71,9 +72,14 @@ def verify_token(self, token: str) -> Dict[str, Any]:
7172 # Obtain the Firebase App Check Public Keys
7273 # Note: It is not recommended to hard code these keys as they rotate,
7374 # but you should cache them for up to 6 hours.
74- signing_key = self ._jwks_client .get_signing_key_from_jwt (token )
75- self ._has_valid_token_headers (jwt .get_unverified_header (token ))
76- verified_claims = self ._decode_and_verify (token , signing_key .key )
75+ try :
76+ signing_key = self ._jwks_client .get_signing_key_from_jwt (token )
77+ self ._has_valid_token_headers (jwt .get_unverified_header (token ))
78+ verified_claims = self ._decode_and_verify (token , signing_key .key )
79+ except (InvalidTokenError , DecodeError ) as exception :
80+ raise ValueError (
81+ f'Verifying App Check token failed. Error: { exception } '
82+ )
7783
7884 verified_claims ['app_id' ] = verified_claims .get ('sub' )
7985 return verified_claims
0 commit comments