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

Validate claims if configured and verify_signature is not. #608

Merged
merged 3 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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: 9 additions & 3 deletions jwt/api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ def decode_complete(
else:
options.setdefault("verify_signature", True)

if not options["verify_signature"]:
options.setdefault("verify_exp", False)
options.setdefault("verify_nbf", False)
options.setdefault("verify_iat", False)
options.setdefault("verify_aud", False)
options.setdefault("verify_iss", False)

if options["verify_signature"] and not algorithms:
raise DecodeError(
'It is required that you pass in a value for the "algorithms" argument when calling decode().'
Expand All @@ -95,9 +102,8 @@ def decode_complete(
if not isinstance(payload, dict):
raise DecodeError("Invalid payload string: must be a json object")

if options["verify_signature"]:
merged_options = {**self.options, **options}
self._validate_claims(payload, merged_options, **kwargs)
merged_options = {**self.options, **options}
self._validate_claims(payload, merged_options, **kwargs)

decoded["payload"] = payload
return decoded
Expand Down
16 changes: 16 additions & 0 deletions tests/test_api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,22 @@ def test_decode_with_verify_exp_option(self, jwt, payload):
options={"verify_exp": True},
)

def test_decode_with_verify_exp_option_and_signature_off(self, jwt, payload):
payload["exp"] = utc_timestamp() - 1
secret = "secret"
jwt_message = jwt.encode(payload, secret)

jwt.decode(
jwt_message,
options={"verify_signature": False},
)

with pytest.raises(ExpiredSignatureError):
jwt.decode(
jwt_message,
options={"verify_signature": False, "verify_exp": True},
)

def test_decode_with_optional_algorithms(self, jwt, payload):
secret = "secret"
jwt_message = jwt.encode(payload, secret)
Expand Down