Skip to content

Commit 88bfcec

Browse files
authored
Merge pull request #69 from jschlyter/accept_invalid_keys
Add option ignore_invalid_keys to KeyBundle
2 parents c9087f9 + 013d278 commit 88bfcec

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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:

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)