Skip to content

JWKS Content Type #177

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

Merged
merged 2 commits into from
Mar 17, 2025
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[tool.poetry]
name = "cryptojwt"
version = "1.9.3"
version = "1.9.4"
description = "Python implementation of JWT, JWE, JWS and JWK"
authors = ["Roland Hedberg <[email protected]>"]
license = "Apache-2.0"
Expand Down
6 changes: 4 additions & 2 deletions src/cryptojwt/key_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

LOGGER = logging.getLogger(__name__)

JWKS_CONTENT_TYPES = set(["application/json", "application/jwk-set+json"])

# def raise_exception(excep, descr, error='service_error'):
# _err = json.dumps({'error': error, 'error_description': descr})
# raise excep(_err, 'application/json')
Expand Down Expand Up @@ -528,8 +530,8 @@
"""
# Check if the content type is the right one.
try:
if not check_content_type(response.headers["Content-Type"], "application/json"):
LOGGER.warning("Wrong Content_type (%s)", response.headers["Content-Type"])
if not check_content_type(response.headers["Content-Type"], JWKS_CONTENT_TYPES):
LOGGER.warning("Wrong Content-Type (%s)", response.headers["Content-Type"])

Check warning on line 534 in src/cryptojwt/key_bundle.py

View check run for this annotation

Codecov / codecov/patch

src/cryptojwt/key_bundle.py#L534

Added line #L534 was not covered by tests
except KeyError:
pass

Expand Down
11 changes: 8 additions & 3 deletions src/cryptojwt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import warnings
from binascii import unhexlify
from email.message import EmailMessage
from typing import List
from typing import List, Set, Union

from cryptojwt.exception import BadSyntax

Expand Down Expand Up @@ -262,12 +262,17 @@ def httpc_params_loader(httpc_params):
return httpc_params


def check_content_type(content_type, mime_type):
def check_content_type(content_type: str, mime_type: Union[str, List[str], Set[str]]):
"""Return True if the content type contains the MIME type"""
msg = EmailMessage()
msg["content-type"] = content_type
mt = msg.get_content_type()
return mime_type == mt
if isinstance(mime_type, str):
return mt == mime_type
elif isinstance(mime_type, (list, set)):
return mt in mime_type
else:
raise ValueError("Invalid MIME type argument")


def is_compact_jws(token):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_31_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from cryptojwt.utils import check_content_type


Expand All @@ -15,3 +17,19 @@ def test_check_content_type():
)
is False
)
assert (
check_content_type(
content_type="application/jwk-set+json;charset=UTF-8",
mime_type="application/application/jwk-set+json",
)
is False
)
assert (
check_content_type(
content_type="application/jwk-set+json;charset=UTF-8",
mime_type=set(["application/application/jwk-set+json", "application/json"]),
)
is False
)
with pytest.raises(ValueError):
check_content_type(content_type="application/jwk-set+json;charset=UTF-8", mime_type=42)