Skip to content

Commit c77608e

Browse files
authored
fix: Add PyJWKClientError to raised errors documentation and handle possible uncaught errors. (#733)
* fix: Add PyJWKClientError to raised error documentaion and handle possible uncaught errors * fix: grammar
1 parent 44b7568 commit c77608e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Diff for: firebase_admin/app_check.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from typing import Any, Dict
1818
import jwt
19-
from jwt import PyJWKClient, ExpiredSignatureError, InvalidTokenError
19+
from jwt import PyJWKClient, ExpiredSignatureError, InvalidTokenError, DecodeError
2020
from jwt import InvalidAudienceError, InvalidIssuerError, InvalidSignatureError
2121
from 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

Comments
 (0)