Skip to content

Commit

Permalink
Remove arbitrary kwargs. (#657)
Browse files Browse the repository at this point in the history
* Remove arbitrary kwargs.

* Update CHANGELOG.
  • Loading branch information
dajiaji committed Aug 8, 2021
1 parent fdd795a commit 5fe7f2b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
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

0 comments on commit 5fe7f2b

Please sign in to comment.