Skip to content

Commit 8166763

Browse files
committed
Added some useful functionality
1 parent 73a1f57 commit 8166763

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

doc/keyhandling.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,44 @@ bundle::
207207
**Note** that you will get a JWKS representing the public keys unless you
208208
specify that you want a representation of the private keys.
209209

210+
As an example of the special functionality of
211+
:py:class:`cryptojwt.key_bundle.KeyBundle` assume you have imported a file
212+
containing a JWKS with one key into a key bundle and then some time later
213+
another key is added to the file.
214+
This is how key bundle deals with that::
215+
216+
>>> from cryptojwt.key_bundle import KeyBundle
217+
>>> kb = KeyBundle(source="file://{}".format(fname), fileformat='jwks')
218+
>>> len(kb)
219+
1
220+
221+
Now if we add one key to the file and then some time later we ask for the
222+
keys in the key bundle::
223+
224+
>>> _keys = kb.keys()
225+
>>> len(_keys)
226+
2
227+
228+
It turns out the it contains the 2 keys that are in the file.
229+
If the change is that one key is removed then something else happens.
230+
Assume we add one key and remove one of the ones that was there before.
231+
The file now should contain 2 keys::
232+
233+
>>> _keys = kb.keys()
234+
>>> len(_keys)
235+
3
236+
237+
???
238+
The key that was removed has not disappeared from the key bundle, but it is
239+
marked as *inactive*. Which means that it should not be used for signing and
240+
encryption but can be used for decryption and signature verification. ::
241+
242+
>>> len(kb.get('rsa'))
243+
1
244+
>>> len(kb.get('rsa', only_active=False))
245+
2
246+
247+
210248
Key Jar
211249
-------
212250

src/cryptojwt/key_bundle.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def update(self):
353353

354354
return res
355355

356-
def get(self, typ=""):
356+
def get(self, typ="", only_active=True):
357357
"""
358358
Return a list of keys. Either all keys or only keys of a specific type
359359
@@ -365,9 +365,14 @@ def get(self, typ=""):
365365
_typs = [typ.lower(), typ.upper()]
366366

367367
if typ:
368-
return [k for k in self._keys if k.kty in _typs]
368+
_keys = [k for k in self._keys if k.kty in _typs]
369369
else:
370-
return self._keys
370+
_keys = self._keys
371+
372+
if only_active:
373+
return [k for k in _keys if not k.inactive_since]
374+
else:
375+
return _keys
371376

372377
def keys(self):
373378
"""

tests/test_10_key_bundle.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,6 @@ def test_update_mark_inactive():
500500
# 2 active and 1 inactive
501501
assert len(kb) == 3
502502
assert len(kb.active_keys()) == 2
503+
504+
assert len(kb.get('rsa')) == 1
505+
assert len(kb.get('rsa', only_active=False)) == 2

0 commit comments

Comments
 (0)