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

Revert "Remove arbitrary kwargs." #701

Merged
merged 1 commit into from
Oct 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion jwt/api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ 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 @@ -162,8 +163,9 @@ def decode(
key: str = "",
algorithms: List[str] = None,
options: Dict = None,
**kwargs,
) -> str:
decoded = self.decode_complete(jwt, key, algorithms, options)
decoded = self.decode_complete(jwt, key, algorithms, options, **kwargs)
return decoded["payload"]

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

try:
Expand All @@ -104,7 +103,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, audience, issuer, leeway)
self._validate_claims(payload, merged_options, **kwargs)

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

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

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

self._validate_required_claims(payload, options)
Expand Down
11 changes: 0 additions & 11 deletions tests/test_api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,6 @@ 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