File tree Expand file tree Collapse file tree 4 files changed +30
-5
lines changed Expand file tree Collapse file tree 4 files changed +30
-5
lines changed Original file line number Diff line number Diff line change 22
33[tool .poetry ]
44name = " cryptojwt"
5- version = " 1.9.3 "
5+ version = " 1.9.4 "
66description = " Python implementation of JWT, JWE, JWS and JWK"
77authors = [" Roland Hedberg <roland@catalogix.se>" ]
88license = " Apache-2.0"
Original file line number Diff line number Diff line change 4040
4141LOGGER = 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
Original file line number Diff line number Diff 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
273278def is_compact_jws (token ):
Original file line number Diff line number Diff line change 1+ import pytest
2+
13from 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 )
You can’t perform that action at this time.
0 commit comments