Skip to content

Commit

Permalink
Throw TokenVerifificationError if the key ID is not known by cognito
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hüsken committed Jun 3, 2024
1 parent 4f6c90a commit a4a8825
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pycognito/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ def get_keys(self):
def get_key(self, kid):
keys = self.get_keys().get("keys")
key = list(filter(lambda x: x.get("kid") == kid, keys))
return key[0]
if len(key) > 0:
return key[0]
else:
return None

def verify_tokens(self):
"""
Expand All @@ -249,7 +252,12 @@ def verify_token(self, token, id_name, token_use):
# https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html

kid = jwt.get_unverified_header(token).get("kid")
hmac_key = jwt.api_jwk.PyJWK(self.get_key(kid)).key
key = self.get_key(kid)
if key is None:
raise TokenVerificationException(
f"Your {id_name!r} token could not be verified (key with ID {kid} not found)."
)
hmac_key = jwt.api_jwk.PyJWK(key).key
required_claims = (["aud"] if token_use != "access" else []) + ["iss", "exp"]
try:
decoded = jwt.api_jwt.decode_complete(
Expand Down

0 comments on commit a4a8825

Please sign in to comment.