Skip to content

Commit 73a1f57

Browse files
committed
Old keys not mark as inactive - fixed.
1 parent 587b607 commit 73a1f57

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

doc/keyhandling.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ bundle::
204204
]
205205
}
206206

207+
**Note** that you will get a JWKS representing the public keys unless you
208+
specify that you want a representation of the private keys.
207209

208210
Key Jar
209211
-------

src/cryptojwt/key_bundle.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def ec_init(spec):
107107

108108
class KeyBundle(object):
109109
def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
110-
fileformat="jwk", keytype="RSA", keyusage=None, kid=''):
110+
fileformat="jwks", keytype="RSA", keyusage=None, kid=''):
111111
"""
112112
Contains a set of keys that have a common origin.
113113
The sources can be serveral:
@@ -120,7 +120,7 @@ def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
120120
with the keys ["kty", "key", "alg", "use", "kid"]
121121
:param source: Where the key set can be fetch from
122122
:param verify_ssl: Verify the SSL cert used by the server
123-
:param fileformat: For a local file either "jwk" or "der"
123+
:param fileformat: For a local file either "jwks" or "der"
124124
:param keytype: Iff local file and 'der' format what kind of key it is.
125125
presently only 'rsa' is supported.
126126
:param keyusage: What the key loaded from file should be used for.
@@ -332,7 +332,7 @@ def update(self):
332332

333333
try:
334334
if self.remote is False:
335-
if self.fileformat == "jwks":
335+
if self.fileformat in ["jwks", "jwk"]:
336336
self.do_local_jwk(self.source)
337337
elif self.fileformat == "der":
338338
self.do_local_der(self.source, self.keytype,
@@ -347,9 +347,7 @@ def update(self):
347347
now = time.time()
348348
for _key in _keys:
349349
if _key not in self._keys:
350-
try:
351-
_key.inactive_since # If already marked don't mess
352-
except ValueError:
350+
if not _key.inactive_since: # If already marked don't mess
353351
_key.inactive_since = now
354352
self._keys.append(_key)
355353

tests/test_10_key_bundle.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import shutil
66
import time
77

8-
from cryptojwt.jwk.rsa import RSAKey
8+
from cryptojwt.jwk.ec import new_ec_key
9+
10+
from cryptojwt.jwk.rsa import RSAKey, new_rsa_key
911
from cryptojwt.jwk.hmac import SYMKey
1012

1113
from cryptojwt.key_bundle import dump_jwks
@@ -453,3 +455,48 @@ def test_remote(httpserver):
453455
assert len(kb.keys())
454456
assert len(kb.get('rsa')) == 1
455457
assert len(kb.get('oct')) == 1
458+
459+
460+
def test_update_2():
461+
rsa_key = new_rsa_key()
462+
_jwks = {"keys": [rsa_key.serialize()]}
463+
fname = 'tmp_jwks.json'
464+
with open(fname, 'w') as fp:
465+
fp.write(json.dumps(_jwks))
466+
467+
kb = KeyBundle(source="file://{}".format(fname), fileformat='jwks')
468+
assert len(kb) == 1
469+
470+
# Added one more key
471+
ec_key = new_ec_key(crv='P-256')
472+
_jwks = {'keys': [rsa_key.serialize(), ec_key.serialize()]}
473+
474+
with open(fname, 'w') as fp:
475+
fp.write(json.dumps(_jwks))
476+
477+
kb.update()
478+
assert len(kb) == 2
479+
480+
481+
def test_update_mark_inactive():
482+
rsa_key = new_rsa_key()
483+
_jwks = {"keys": [rsa_key.serialize()]}
484+
fname = 'tmp_jwks.json'
485+
with open(fname, 'w') as fp:
486+
fp.write(json.dumps(_jwks))
487+
488+
kb = KeyBundle(source="file://{}".format(fname), fileformat='jwks')
489+
assert len(kb) == 1
490+
491+
# new set of keys
492+
rsa_key = new_rsa_key()
493+
ec_key = new_ec_key(crv='P-256')
494+
_jwks = {'keys': [rsa_key.serialize(), ec_key.serialize()]}
495+
496+
with open(fname, 'w') as fp:
497+
fp.write(json.dumps(_jwks))
498+
499+
kb.update()
500+
# 2 active and 1 inactive
501+
assert len(kb) == 3
502+
assert len(kb.active_keys()) == 2

0 commit comments

Comments
 (0)