Skip to content

Commit 214d0a8

Browse files
authored
Merge pull request #71 from IdentityPython/develop
Prepare for 1.4.0
2 parents 46df6c3 + 030b1c6 commit 214d0a8

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55

66
An implementation of the JSON cryptographic specs JWS, JWE, JWK, and JWA [RFC 7515-7518] and JSON Web Token (JWT) [RFC 7519]
77

8-
oidcmsg is the 1st layer in the
9-
JWTConnect stack (cryptojwt, oidcmsg, oidcservice, oidcrp)
8+
oidcmsg is the 1st layer in the JWTConnect stack (cryptojwt, oidcmsg, oidcservice, oidcrp).
9+
10+
Please read the [Official Documentation](https://cryptojwt.readthedocs.io/en/latest/) for getting usage examples and further informations.

src/cryptojwt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
except ImportError:
2222
pass
2323

24-
__version__ = "1.3.0"
24+
__version__ = "1.4.0"
2525

2626
logger = logging.getLogger(__name__)
2727

src/cryptojwt/key_bundle.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def __init__(
162162
keytype="RSA",
163163
keyusage=None,
164164
kid="",
165+
ignore_invalid_keys=True,
165166
httpc=None,
166167
httpc_params=None,
167168
):
@@ -181,6 +182,7 @@ def __init__(
181182
presently 'rsa' and 'ec' are supported.
182183
:param keyusage: What the key loaded from file should be used for.
183184
Only applicable for DER files
185+
:param ignore_invalid_keys: Ignore invalid keys
184186
:param httpc: A HTTP client function
185187
:param httpc_params: Additional parameters to pass to the HTTP client
186188
function
@@ -202,6 +204,7 @@ def __init__(
202204
self.last_updated = 0
203205
self.last_remote = None # HTTP Date of last remote update
204206
self.last_local = None # UNIX timestamp of last local update
207+
self.ignore_invalid_keys = ignore_invalid_keys
205208

206209
if httpc:
207210
self.httpc = httpc
@@ -274,6 +277,8 @@ def do_keys(self, keys):
274277
elif inst["kty"].upper() in K2C:
275278
inst["kty"] = inst["kty"].upper()
276279
else:
280+
if not self.ignore_invalid_keys:
281+
raise UnknownKeyType(inst)
277282
LOGGER.warning("While loading keys, unknown key type: %s", inst["kty"])
278283
continue
279284

@@ -290,12 +295,18 @@ def do_keys(self, keys):
290295
try:
291296
_key = K2C[_typ](use=_use, **inst)
292297
except KeyError:
298+
if not self.ignore_invalid_keys:
299+
raise UnknownKeyType(inst)
293300
_error = "UnknownKeyType: {}".format(_typ)
294301
continue
295302
except (UnsupportedECurve, UnsupportedAlgorithm) as err:
303+
if not self.ignore_invalid_keys:
304+
raise err
296305
_error = str(err)
297306
break
298307
except JWKException as err:
308+
if not self.ignore_invalid_keys:
309+
raise err
299310
LOGGER.warning("While loading keys: %s", err)
300311
_error = str(err)
301312
else:

src/cryptojwt/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ def modsplit(name):
209209
if ":" in name:
210210
_part = name.split(":")
211211
if len(_part) != 2:
212-
raise ValueError("Syntax error: {s}")
212+
raise ValueError(f"Syntax error: {s}")
213213
return _part[0], _part[1]
214214

215215
_part = name.split(".")
216216
if len(_part) < 2:
217-
raise ValueError("Syntax error: {s}")
217+
raise ValueError(f"Syntax error: {s}")
218218

219219
return ".".join(_part[:-1]), _part[-1]
220220

tests/test_03_key_bundle.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import responses
1111
from cryptography.hazmat.primitives.asymmetric import rsa
1212

13+
from cryptojwt.exception import UnknownKeyType
1314
from cryptojwt.jwk.ec import ECKey
1415
from cryptojwt.jwk.ec import new_ec_key
1516
from cryptojwt.jwk.hmac import SYMKey
@@ -1067,3 +1068,14 @@ def test_ignore_errors_period():
10671068
kb.source = source_good
10681069
res = kb.do_remote()
10691070
assert res == True
1071+
1072+
1073+
def test_ignore_invalid_keys():
1074+
rsa_key_dict = new_rsa_key().serialize()
1075+
rsa_key_dict["kty"] = "b0rken"
1076+
1077+
kb = KeyBundle(keys={"keys": [rsa_key_dict]}, ignore_invalid_keys=True)
1078+
assert len(kb) == 0
1079+
1080+
with pytest.raises(UnknownKeyType):
1081+
KeyBundle(keys={"keys": [rsa_key_dict]}, ignore_invalid_keys=False)

0 commit comments

Comments
 (0)