File tree Expand file tree Collapse file tree 3 files changed +49
-3
lines changed Expand file tree Collapse file tree 3 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -207,6 +207,44 @@ bundle::
207
207
**Note ** that you will get a JWKS representing the public keys unless you
208
208
specify that you want a representation of the private keys.
209
209
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
+
210
248
Key Jar
211
249
-------
212
250
Original file line number Diff line number Diff line change @@ -353,7 +353,7 @@ def update(self):
353
353
354
354
return res
355
355
356
- def get (self , typ = "" ):
356
+ def get (self , typ = "" , only_active = True ):
357
357
"""
358
358
Return a list of keys. Either all keys or only keys of a specific type
359
359
@@ -365,9 +365,14 @@ def get(self, typ=""):
365
365
_typs = [typ .lower (), typ .upper ()]
366
366
367
367
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 ]
369
369
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
371
376
372
377
def keys (self ):
373
378
"""
Original file line number Diff line number Diff line change @@ -500,3 +500,6 @@ def test_update_mark_inactive():
500
500
# 2 active and 1 inactive
501
501
assert len (kb ) == 3
502
502
assert len (kb .active_keys ()) == 2
503
+
504
+ assert len (kb .get ('rsa' )) == 1
505
+ assert len (kb .get ('rsa' , only_active = False )) == 2
You can’t perform that action at this time.
0 commit comments