Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw TokenVerifificationError if the key ID is not known by cognito #259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +238 to +239
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
return None
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
Loading