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

Remove arbitrary kwargs. #657

Merged
merged 3 commits into from Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -10,6 +10,8 @@ This project adheres to `Semantic Versioning <https://semver.org/>`__.
Changed
~~~~~~~

- Remove arbitrary kwalgs. `#657 <https://github.com/jpadilla/pyjwt/pull/657>`__

Fixed
~~~~~

Expand Down
4 changes: 1 addition & 3 deletions jwt/api_jws.py
Expand Up @@ -137,7 +137,6 @@ def decode_complete(
key: str = "",
algorithms: List[str] = None,
options: Dict = None,
**kwargs,
) -> Dict[str, Any]:
if options is None:
options = {}
Expand Down Expand Up @@ -166,9 +165,8 @@ def decode(
key: str = "",
algorithms: List[str] = None,
options: Dict = None,
**kwargs,
) -> str:
decoded = self.decode_complete(jwt, key, algorithms, options, **kwargs)
decoded = self.decode_complete(jwt, key, algorithms, options)
return decoded["payload"]

def get_unverified_header(self, jwt):
Expand Down
21 changes: 12 additions & 9 deletions jwt/api_jwt.py
Expand Up @@ -68,7 +68,9 @@ def decode_complete(
key: str = "",
algorithms: List[str] = None,
options: Dict = None,
**kwargs,
audience: Optional[Union[str, List[str]]] = None,
issuer: Optional[str] = None,
leeway: Union[float, timedelta] = 0,
) -> Dict[str, Any]:
if options is None:
options = {"verify_signature": True}
Expand All @@ -92,7 +94,6 @@ def decode_complete(
key=key,
algorithms=algorithms,
options=options,
**kwargs,
)

try:
Expand All @@ -103,7 +104,7 @@ def decode_complete(
raise DecodeError("Invalid payload string: must be a json object")

merged_options = {**self.options, **options}
self._validate_claims(payload, merged_options, **kwargs)
self._validate_claims(payload, merged_options, audience, issuer, leeway)

decoded["payload"] = payload
return decoded
Expand All @@ -114,18 +115,20 @@ def decode(
key: str = "",
algorithms: List[str] = None,
options: Dict = None,
**kwargs,
audience: Optional[Union[str, List[str]]] = None,
issuer: Optional[str] = None,
leeway: Union[float, timedelta] = 0,
) -> Dict[str, Any]:
decoded = self.decode_complete(jwt, key, algorithms, options, **kwargs)
decoded = self.decode_complete(
jwt, key, algorithms, options, audience, issuer, leeway
)
return decoded["payload"]

def _validate_claims(
self, payload, options, audience=None, issuer=None, leeway=0, **kwargs
):
def _validate_claims(self, payload, options, audience, issuer, leeway):
if isinstance(leeway, timedelta):
leeway = leeway.total_seconds()

if not isinstance(audience, (bytes, str, type(None), Iterable)):
if not isinstance(audience, (str, type(None), Iterable)):
raise TypeError("audience must be a string, iterable, or None")

self._validate_required_claims(payload, options)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_api_jwt.py
Expand Up @@ -106,6 +106,17 @@ def test_decode_with_non_mapping_payload_throws_exception(self, jwt):
exception = context.value
assert str(exception) == "Invalid payload string: must be a json object"

def test_decode_with_unknown_parameter_throws_exception(self, jwt):
secret = "secret"
example_jwt = (
b"eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
b".eyJoZWxsbyI6ICJ3b3JsZCJ9"
b".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8"
)

with pytest.raises(TypeError):
jwt.decode(example_jwt, key=secret, foo="bar", algorithms=["HS256"])

def test_decode_with_invalid_audience_param_throws_exception(self, jwt):
secret = "secret"
example_jwt = (
Expand Down