Skip to content

Commit 7b9920a

Browse files
committed
Do not warn if JWKS content type are per RFC 7517
1 parent 9e094e3 commit 7b9920a

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[tool.poetry]
44
name = "cryptojwt"
5-
version = "1.9.3"
5+
version = "1.9.4"
66
description = "Python implementation of JWT, JWE, JWS and JWK"
77
authors = ["Roland Hedberg <[email protected]>"]
88
license = "Apache-2.0"

Diff for: src/cryptojwt/key_bundle.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
LOGGER = logging.getLogger(__name__)
4242

43+
JWKS_CONTENT_TYPES = set(["application/json", "application/jwk-set+json"])
44+
4345
# def raise_exception(excep, descr, error='service_error'):
4446
# _err = json.dumps({'error': error, 'error_description': descr})
4547
# raise excep(_err, 'application/json')
@@ -528,8 +530,8 @@ def _parse_remote_response(self, response):
528530
"""
529531
# Check if the content type is the right one.
530532
try:
531-
if not check_content_type(response.headers["Content-Type"], "application/json"):
532-
LOGGER.warning("Wrong Content_type (%s)", response.headers["Content-Type"])
533+
if not check_content_type(response.headers["Content-Type"], JWKS_CONTENT_TYPES):
534+
LOGGER.warning("Wrong Content-Type (%s)", response.headers["Content-Type"])
533535
except KeyError:
534536
pass
535537

Diff for: src/cryptojwt/utils.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,17 @@ def httpc_params_loader(httpc_params):
262262
return httpc_params
263263

264264

265-
def check_content_type(content_type, mime_type):
265+
def check_content_type(content_type: str, mime_type: str | list[str] | set[str]):
266266
"""Return True if the content type contains the MIME type"""
267267
msg = EmailMessage()
268268
msg["content-type"] = content_type
269269
mt = msg.get_content_type()
270-
return mime_type == mt
270+
if isinstance(mime_type, str):
271+
return mt == mime_type
272+
elif isinstance(mime_type, (list, set)):
273+
return mt in mime_type
274+
else:
275+
raise ValueError("Invalid MIME type argument")
271276

272277

273278
def is_compact_jws(token):

Diff for: tests/test_31_utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from cryptojwt.utils import check_content_type
24

35

@@ -15,3 +17,19 @@ def test_check_content_type():
1517
)
1618
is False
1719
)
20+
assert (
21+
check_content_type(
22+
content_type="application/jwk-set+json;charset=UTF-8",
23+
mime_type="application/application/jwk-set+json",
24+
)
25+
is False
26+
)
27+
assert (
28+
check_content_type(
29+
content_type="application/jwk-set+json;charset=UTF-8",
30+
mime_type=set(["application/application/jwk-set+json", "application/json"]),
31+
)
32+
is False
33+
)
34+
with pytest.raises(ValueError):
35+
check_content_type(content_type="application/jwk-set+json;charset=UTF-8", mime_type=42)

0 commit comments

Comments
 (0)