forked from IdentityPython/JWTConnect-Python-CryptoJWT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_bundle.py
executable file
·1371 lines (1118 loc) · 39.4 KB
/
key_bundle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Implementation of a Key Bundle."""
import contextlib
import copy
import json
import logging
import os
import threading
import time
from datetime import datetime
from functools import cmp_to_key
from typing import List, Optional
import requests
from cryptojwt.jwk.ec import NIST2SEC
from cryptojwt.jwk.hmac import new_sym_key
from cryptojwt.jwk.okp import OKP_CRV2PUBLIC
from cryptojwt.jwk.x509 import import_private_key_from_pem_file
from .exception import (
JWKException,
UnknownKeyType,
UnsupportedAlgorithm,
UnsupportedECurve,
UpdateFailed,
)
from .jwk.ec import ECKey, new_ec_key
from .jwk.hmac import SYMKey
from .jwk.jwk import dump_jwk, import_jwk
from .jwk.okp import OKPKey, new_okp_key
from .jwk.rsa import RSAKey, new_rsa_key
from .utils import as_unicode, check_content_type, httpc_params_loader
__author__ = "Roland Hedberg"
KEYLOADERR = "Failed to load %s key from '%s' (%s)"
REMOTE_FAILED = "Remote key update from '{}' failed, HTTP status {}"
MALFORMED = "Remote key update from {} failed, malformed JWKS."
LOGGER = logging.getLogger(__name__)
JWKS_CONTENT_TYPES = set(["application/json", "application/jwk-set+json"])
# def raise_exception(excep, descr, error='service_error'):
# _err = json.dumps({'error': error, 'error_description': descr})
# raise excep(_err, 'application/json')
# Make sure the keys are all uppercase
K2C = {"RSA": RSAKey, "EC": ECKey, "oct": SYMKey, "OKP": OKPKey}
MAP = {"dec": "enc", "enc": "enc", "ver": "sig", "sig": "sig"}
def harmonize_usage(use):
"""
:param use:
:return: list of usage
"""
if isinstance(use, str):
return [MAP[use]]
if isinstance(use, list):
_ul = list(MAP.keys())
_us = {MAP[u] for u in use if u in _ul}
return list(_us)
return None
def rsa_init(spec):
"""
Initiates a :py:class:`oidcmsg.keybundle.KeyBundle` instance
containing newly minted RSA keys according to a spec.
Example of specification::
{'size':2048, 'use': ['enc', 'sig'] }
Using the spec above 2 RSA keys would be minted, one for
encryption and one for signing.
:param spec:
:return: KeyBundle
"""
try:
size = spec["size"]
except KeyError:
size = 2048
_kb = KeyBundle(keytype="RSA")
if "use" in spec:
for use in harmonize_usage(spec["use"]):
_key = new_rsa_key(use=use, key_size=size)
_kb.append(_key)
else:
_key = new_rsa_key(key_size=size)
_kb.append(_key)
return _kb
def sym_init(spec):
"""
Initiates a :py:class:`oidcmsg.keybundle.KeyBundle` instance
containing newly minted SYM keys according to a spec.
Example of specification::
{'bytes':24, 'use': ['enc', 'sig'] }
Using the spec above 2 SYM keys would be minted, one for
encryption and one for signing.
:param spec:
:return: KeyBundle
"""
try:
size = int(spec["bytes"])
except KeyError:
size = 24
_kb = KeyBundle(keytype="oct")
if "use" in spec:
for use in harmonize_usage(spec["use"]):
_key = new_sym_key(use=use, bytes=size)
_kb.append(_key)
else:
_key = new_sym_key(bytes=size)
_kb.append(_key)
return _kb
def ec_init(spec):
"""
Initiate a key bundle with an elliptic curve key.
:param spec: Key specifics of the form::
{"type": "EC", "crv": "P-256", "use": ["sig"]}
:return: A KeyBundle instance
"""
curve = spec.get("crv", "P-256")
_kb = KeyBundle(keytype="EC")
if "use" in spec:
for use in spec["use"]:
eck = new_ec_key(crv=curve, use=use)
_kb.append(eck)
else:
eck = new_ec_key(crv=curve)
_kb.append(eck)
return _kb
def okp_init(spec):
"""
Initiate a key bundle with an Octet Key Pair.
:param spec: Key specifics of the form::
{"type": "OKP", "crv": "Ed25519", "use": ["sig"]}
:return: A KeyBundle instance
"""
curve = spec.get("crv", "Ed25519")
_kb = KeyBundle(keytype="OKP")
if "use" in spec:
for use in spec["use"]:
eck = new_okp_key(crv=curve, use=use)
_kb.append(eck)
else:
eck = new_okp_key(crv=curve)
_kb.append(eck)
return _kb
def keys_writer(func):
def wrapper(self, *args, **kwargs):
with self._lock_writer:
return func(self, *args, **kwargs)
return wrapper
class KeyBundle:
"""The Key Bundle"""
params = {
"cache_time": 0,
"etag": "",
"fileformat": "jwks",
"httpc_params": {},
"ignore_errors_period": 0,
"ignore_errors_until": None,
"ignore_invalid_keys": True,
"imp_jwks": None,
"keytype": "RSA",
"keyusage": None,
"last_local": None,
"last_remote": None,
"last_updated": 0,
"local": False,
"remote": False,
"source": None,
"time_out": 0,
}
def __init__(
self,
keys=None,
source="",
cache_time=300,
ignore_errors_period=0,
fileformat="jwks",
keytype="RSA",
keyusage=None,
kid="",
ignore_invalid_keys=True,
httpc=None,
httpc_params=None,
):
"""
Contains a set of keys that have a common origin.
The sources can be serveral:
- A dictionary provided at the initialization, see keys below.
- A list of dictionaries provided at initialization
- A file containing one of: JWKS, DER encoded key
- A URL pointing to a webpages from which an JWKS can be downloaded
:param keys: A dictionary or a list of dictionaries
with the keys ["kty", "key", "alg", "use", "kid"]
:param source: Where the key set can be fetched from
:param fileformat: For a local file either "jwks" or "der"
:param keytype: Iff local file and 'der' format what kind of key it is.
presently 'rsa' and 'ec' are supported.
:param keyusage: What the key loaded from file should be used for.
Only applicable for DER files
:param ignore_invalid_keys: Ignore invalid keys
:param httpc: A HTTP client function
:param httpc_params: Additional parameters to pass to the HTTP client
function
"""
self._keys = []
self.cache_time = cache_time
self.etag = ""
self.fileformat = fileformat.lower()
self.ignore_errors_period = ignore_errors_period
self.ignore_errors_until = None # UNIX timestamp of last error
self.ignore_invalid_keys = ignore_invalid_keys
self.imp_jwks = None
self.keytype = keytype
self.keyusage = keyusage
self.last_local = None # UNIX timestamp of last local update
self.last_remote = None # HTTP Date of last remote update
self.last_updated = 0
self.local = False
self.remote = False
self.source = None
self.time_out = 0
self._lock_writer = threading.Lock()
if httpc:
self.httpc = httpc
else:
self.httpc = requests.request
self.httpc_params = httpc_params_loader(httpc_params)
if keys:
self.source = None
initial_keys = keys.get("keys", [keys]) if isinstance(keys, dict) else keys
self._keys = self.jwk_dicts_as_keys(initial_keys)
else:
self._set_source(source, fileformat)
if self.local:
self._keys = self._do_local(kid)
def _set_source(self, source, fileformat):
if source.startswith("file://"):
self.source = source[7:]
self.local = True
elif source.startswith("http://") or source.startswith("https://"):
self.source = source
self.remote = True
elif source == "":
return
else:
if fileformat.lower() in ["rsa", "der", "jwks"]:
if os.path.isfile(source):
self.source = source
self.local = True
else:
raise ImportError("No such file")
else:
raise ImportError("Unknown source")
def _do_local(self, kid):
if self.fileformat in ["jwks", "jwk"]:
updated, keys = self._do_local_jwk(self.source)
elif self.fileformat == "der":
updated, keys = self._do_local_der(self.source, self.keytype, self.keyusage, kid)
return keys
def _local_update_required(self) -> bool:
stat = os.stat(self.source)
if self.last_local and stat.st_mtime < self.last_local:
LOGGER.debug("%s not modfied", self.source)
return False
else:
LOGGER.debug("%s modfied", self.source)
self.last_local = stat.st_mtime
return True
def do_keys(self, keys):
"""Compatibility function for add_jwk_dicts()"""
self.add_jwk_dicts(keys)
@keys_writer
def add_jwk_dicts(self, keys):
"""
Add JWK dictionaries
:param keys: List of JWK dictionaries
:return:
"""
self._keys.extend(self.jwk_dicts_as_keys(keys))
self.last_updated = time.time()
def jwk_dicts_as_keys(self, keys):
"""
Return JWK dictionaries as list of JWK objects
:param keys: List of JWK dictionaries
:return: List of JWK objects
"""
_new_keys = []
for inst in keys:
if inst["kty"].lower() in K2C:
inst["kty"] = inst["kty"].lower()
elif inst["kty"].upper() in K2C:
inst["kty"] = inst["kty"].upper()
else:
if not self.ignore_invalid_keys:
raise UnknownKeyType(inst)
LOGGER.warning("While loading keys, unknown key type: %s", inst["kty"])
continue
_typ = inst["kty"]
try:
_usage = harmonize_usage(inst["use"])
except KeyError:
_usage = [""]
else:
del inst["use"]
_error = ""
for _use in _usage:
try:
_key = K2C[_typ](use=_use, **inst)
except KeyError as exc:
if not self.ignore_invalid_keys:
raise UnknownKeyType(inst) from exc
_error = f"UnknownKeyType: {_typ}"
continue
except (UnsupportedECurve, UnsupportedAlgorithm) as err:
if not self.ignore_invalid_keys:
raise err
_error = str(err)
break
except JWKException as err:
if not self.ignore_invalid_keys:
raise err
LOGGER.warning("While loading keys: %s", err)
_error = str(err)
else:
if not _key.kid:
_key.add_kid()
_new_keys.append(_key)
_error = ""
if _error:
LOGGER.warning("While loading keys, %s", _error)
return _new_keys
def _do_local_jwk(self, filename):
"""
Load a JWKS from a local file
:param filename: Name of the file from which the JWKS should be loaded
:return: True if load was successful or False if file hasn't been modified
"""
if not self._local_update_required():
return False, None
LOGGER.info("Reading local JWKS from %s", filename)
with open(filename) as input_file:
_info = json.load(input_file)
if "keys" in _info:
new_keys = self.jwk_dicts_as_keys(_info["keys"])
else:
new_keys = self.jwk_dicts_as_keys([_info])
self.last_local = time.time()
self.time_out = self.last_local + self.cache_time
return True, new_keys
def _do_local_der(self, filename, keytype, keyusage=None, kid=""):
"""
Load a DER encoded file amd create a key from it.
:param filename: Name of the file
:param keytype: Presently 'rsa' and 'ec' supported
:param keyusage: encryption ('enc') or signing ('sig') or both
:return: True if load was successful or False if file hasn't been modified
"""
if not self._local_update_required():
return False, None
LOGGER.info("Reading local DER from %s", filename)
key_args = {}
_kty = keytype.lower()
if _kty in ["rsa", "ec"]:
key_args["kty"] = _kty
_key = import_private_key_from_pem_file(filename)
key_args["priv_key"] = _key
key_args["pub_key"] = _key.public_key()
else:
raise NotImplementedError(f"No support for DER decoding of key type {_kty}")
if not keyusage:
key_args["use"] = ["enc", "sig"]
else:
key_args["use"] = harmonize_usage(keyusage)
if kid:
key_args["kid"] = kid
new_keys = self.jwk_dicts_as_keys([key_args])
self.last_local = time.time()
self.time_out = self.last_local + self.cache_time
return True, new_keys
def _do_remote(self, set_keys=True):
"""
Load a JWKS from a webpage.
:return: True if load was successful or False if remote hasn't been modified
"""
# if self.verify_ssl is not None:
# self.httpc_params["verify"] = self.verify_ssl
if self.ignore_errors_until and time.time() < self.ignore_errors_until:
LOGGER.warning(
"Not reading remote JWKS from %s (in error hold down until %s)",
self.source,
datetime.fromtimestamp(self.ignore_errors_until),
)
return False, None
LOGGER.info("Reading remote JWKS from %s", self.source)
try:
LOGGER.debug("KeyBundle fetch keys from: %s", self.source)
httpc_params = self.httpc_params.copy()
if self.last_remote is not None:
if "headers" not in httpc_params:
httpc_params["headers"] = {}
httpc_params["headers"]["If-Modified-Since"] = self.last_remote
_http_resp = self.httpc("GET", self.source, **httpc_params)
except Exception as err:
LOGGER.error(err)
raise UpdateFailed(REMOTE_FAILED.format(self.source, str(err))) from err
new_keys = None
load_successful = _http_resp.status_code == 200
not_modified = _http_resp.status_code == 304
if load_successful:
self.time_out = time.time() + self.cache_time
self.imp_jwks = self._parse_remote_response(_http_resp)
if not isinstance(self.imp_jwks, dict) or "keys" not in self.imp_jwks:
raise UpdateFailed(MALFORMED.format(self.source))
LOGGER.debug("Loaded JWKS: %s from %s", _http_resp.text, self.source)
try:
new_keys = self.jwk_dicts_as_keys(self.imp_jwks["keys"])
except KeyError as exc:
LOGGER.error("No 'keys' keyword in JWKS")
self.ignore_errors_until = time.time() + self.ignore_errors_period
raise UpdateFailed(MALFORMED.format(self.source)) from exc
if hasattr(_http_resp, "headers"):
headers = _http_resp.headers
self.last_remote = headers.get("last-modified") or headers.get("date")
elif not_modified:
LOGGER.debug("%s not modified since %s", self.source, self.last_remote)
self.time_out = time.time() + self.cache_time
else:
LOGGER.warning(
"HTTP status %d reading remote JWKS from %s",
_http_resp.status_code,
self.source,
)
self.ignore_errors_until = time.time() + self.ignore_errors_period
raise UpdateFailed(REMOTE_FAILED.format(self.source, _http_resp.status_code))
if set_keys and new_keys:
self._keys = new_keys
self.last_updated = time.time()
self.ignore_errors_until = None
return load_successful, new_keys
def _parse_remote_response(self, response):
"""
Parse JWKS from the HTTP response.
Should be overriden by subclasses for adding support of e.g. signed
JWKS.
:param response: HTTP response from the 'jwks_uri' endpoint
:return: response parsed as JSON
"""
# Check if the content type is the right one.
try:
if not check_content_type(response.headers["Content-Type"], JWKS_CONTENT_TYPES):
LOGGER.warning("Wrong Content-Type (%s)", response.headers["Content-Type"])
except KeyError:
pass
LOGGER.debug("Loaded JWKS: %s from %s", response.text, self.source)
try:
return json.loads(response.text)
except ValueError:
return None
def _uptodate(self):
if self.remote or self.local:
if time.time() > self.time_out:
return self.update()
return False
@keys_writer
def update(self):
"""
Reload the keys if necessary.
This is a forced update, will happen even if cache time has not elapsed.
Replaced keys will be marked as inactive and not removed.
:return: True if update was ok or False if we encountered an error during update.
"""
if self.source:
try:
if self.local:
if self.fileformat in ["jwks", "jwk"]:
updated, new_keys = self._do_local_jwk(self.source)
elif self.fileformat == "der":
updated, new_keys = self._do_local_der(
self.source, self.keytype, self.keyusage
)
elif self.remote:
updated, new_keys = self._do_remote(set_keys=False)
else:
new_keys = None
updated = False
except Exception as err:
LOGGER.error("Key bundle update failed: %s", err)
return False
if updated:
now = time.time()
for _key in self._keys:
if _key not in new_keys:
if not _key.inactive_since: # If already marked don't mess
_key.inactive_since = now
new_keys.append(_key)
self._keys = new_keys
return True
def get(self, typ="", only_active=True):
"""
Return a list of keys. Either all keys or only keys of a specific type
:param typ: Type of key (rsa, ec, oct, ..)
:return: If typ is undefined all the keys as a dictionary
otherwise the appropriate keys in a list
"""
self._uptodate()
if typ:
_typs = [typ.lower(), typ.upper()]
_keys = [k for k in self._keys if k.kty in _typs]
else:
_keys = self._keys
if only_active:
return [k for k in _keys if not k.inactive_since]
return _keys
def keys(self, update: bool = True):
"""
Return all keys after having updated them
:return: List of all keys
"""
if update:
self._uptodate()
return self._keys
def active_keys(self):
"""Return the set of active keys."""
_res = []
for k in self.keys():
try:
ias = k.inactive_since
except ValueError:
_res.append(k)
else:
if ias == 0:
_res.append(k)
return _res
@keys_writer
def remove_keys_by_type(self, typ):
"""
Remove keys that are of a specific type.
:param typ: Type of key (rsa, ec, oct, ..)
"""
_typs = [typ.lower(), typ.upper()]
self._keys = [k for k in self._keys if k.kty not in _typs]
def __str__(self):
return str(self.jwks())
def jwks(self, private=False):
"""
Create a JWKS as a JSON document
:param private: Whether private key information should be included.
:return: A JWKS JSON representation of the keys in this bundle
"""
keys = list()
for k in self.keys():
if private:
key = k.serialize(private)
else:
key = k.serialize()
for _attr, _val in key.items():
key[_attr] = as_unicode(_val)
keys.append(key)
return json.dumps({"keys": keys})
@keys_writer
def append(self, key):
"""
Add a key to list of keys in this bundle
:param key: Key to be added
"""
self._keys.append(key)
@keys_writer
def extend(self, keys):
"""Add a list of keys to the list of keys."""
self._keys.extend(keys)
@keys_writer
def remove(self, key):
"""
Remove a specific key from this bundle
:param key: The key that should be removed
"""
with contextlib.suppress(ValueError):
self._keys.remove(key)
def __len__(self):
"""
The number of keys.
:return: The number of keys
"""
return len(self._keys)
@keys_writer
def set(self, keys):
"""Set the keys to the set provided."""
self._keys = keys
def get_key_with_kid(self, kid):
"""
Return the key that has a specific key ID (kid)
:param kid: The Key ID
:return: The key or None
"""
self._uptodate()
return self._get_key_with_kid(kid)
def _get_key_with_kid(self, kid):
for key in self._keys:
if key.kid == kid:
return key
return None
def kids(self):
"""
Return a list of key IDs.
Note that this list may be shorter then the list of keys.
The reason might be that there are some keys with no key ID.
:return: A list of all the key IDs that exists in this bundle
"""
return [key.kid for key in self.keys() if key.kid != ""]
@keys_writer
def mark_as_inactive(self, kid):
"""
Mark a specific key as inactive based on the keys KeyID.
:param kid: The Key Identifier
"""
k = self._get_key_with_kid(kid)
if k:
k.inactive_since = time.time()
return True
else:
return False
@keys_writer
def mark_all_as_inactive(self):
"""
Mark a specific key as inactive based on the keys KeyID.
"""
_keys = self._keys
_updated = []
for k in _keys:
k.inactive_since = time.time()
_updated.append(k)
self._keys = _updated
@keys_writer
def remove_outdated(self, after, when=0):
"""
Remove keys that should not be available any more.
Outdated means that the key was marked as inactive at a time
that was longer ago then what is given in 'after'.
:param after: The length of time the key will remain in the KeyBundle
before it should be removed.
:param when: To make it easier to test
"""
now = when or time.time()
if not isinstance(after, float):
after = float(after)
self._keys = [
k for k in self._keys if not k.inactive_since or k.inactive_since + after > now
]
def __contains__(self, key):
return key in self.keys()
def copy(self):
"""
Make deep copy of this KeyBundle
:return: The copy
"""
_bundle = KeyBundle()
_bundle._keys = self._keys[:]
_bundle.cache_time = self.cache_time
_bundle.httpc_params = copy.deepcopy(self.httpc_params)
if self.source:
_bundle.source = self.source
_bundle.fileformat = self.fileformat
_bundle.keytype = self.keytype
_bundle.keyusage = self.keyusage
_bundle.remote = self.remote
return _bundle
def __iter__(self):
return self.keys().__iter__()
def difference(self, bundle):
"""
Return a set of keys that appears in this key bundle but not in the other.
:param bundle: A KeyBundle instance
:return: A list of keys
"""
if not isinstance(bundle, KeyBundle):
return ValueError("Not a KeyBundle instance")
return [k for k in self.keys() if k not in bundle]
def dump(self, exclude_attributes: Optional[List[str]] = None):
if exclude_attributes is None:
exclude_attributes = []
res = {}
if "keys" not in exclude_attributes:
_keys = []
for _k in self.keys(update=False):
_ser = _k.to_dict()
if _k.inactive_since:
_ser["inactive_since"] = _k.inactive_since
_keys.append(_ser)
res["keys"] = _keys
for attr in self.params:
if attr in exclude_attributes:
continue
val = getattr(self, attr)
res[attr] = val
return res
@keys_writer
def load(self, spec):
"""
Sets attributes according to a specification.
Does not overwrite an existing attributes value with a default value.
:param spec: Dictionary with attributes and value to populate the instance with
:return: The instance itself
"""
_keys = spec.get("keys", [])
if _keys:
self._keys.extend(self.jwk_dicts_as_keys(_keys))
self.last_updated = time.time()
for attr in self.params:
val = spec.get(attr)
if val:
setattr(self, attr, val)
return self
@keys_writer
def flush(self):
self._keys = []
self.cache_time = (300,)
self.etag = ""
self.fileformat = "jwks"
# self.httpc=None,
self.httpc_params = (None,)
self.ignore_errors_period = 0
self.ignore_errors_until = None
self.ignore_invalid_keys = True
self.imp_jwks = None
self.keytype = ("RSA",)
self.keyusage = (None,)
self.last_local = None # UNIX timestamp of last local update
self.last_remote = None # HTTP Date of last remote update
self.last_updated = 0
self.local = False
self.remote = False
self.source = None
self.time_out = 0
return self
def keybundle_from_local_file(filename, typ, usage=None, keytype="RSA"):
"""
Create a KeyBundle based on the content in a local file.
:param filename: Name of the file
:param typ: Type of content
:param usage: What the keys should be used for
:param keytype: Type of key, e.g. "RSA", "EC". Only used with typ='der'
:return: The created KeyBundle
"""
usage = harmonize_usage(usage)
if typ.lower() == "jwks":
_bundle = KeyBundle(source=filename, fileformat="jwks", keyusage=usage)
elif typ.lower() == "der":
_bundle = KeyBundle(source=filename, fileformat="der", keyusage=usage, keytype=keytype)
else:
raise UnknownKeyType("Unsupported key type")
return _bundle
def dump_jwks(kbl, target, private=False, symmetric_too=False):
"""
Write a JWK to a file.
:param kbl: List of KeyBundles
:param target: Name of the file to which everything should be written
:param private: Should also the private parts be exported
:param symmetric_too: Include symmetric keys or not
"""
keys = []
for _bundle in kbl:
if symmetric_too:
keys.extend(
[
k.serialize(private)
for k in _bundle.keys() # noqa: SIM118
if not k.inactive_since
]
)
else:
keys.extend(
[
k.serialize(private)
for k in _bundle.keys() # noqa: SIM118
if k.kty != "oct" and not k.inactive_since
]
)
res = {"keys": keys}
try:
with open(target, "w") as fp:
json.dump(res, fp)
except OSError:
head, _ = os.path.split(target)
os.makedirs(head)
with open(target, "w") as fp:
json.dump(res, fp)
def _set_kid(spec, bundle, kid_template, kid):
if "kid" in spec and len(bundle) == 1:
_keys = bundle.keys()
_keys[0].kid = spec["kid"]
else:
for k in bundle.keys(): # noqa: SIM118
if kid_template:
k.kid = kid_template % kid
kid += 1
else:
k.add_kid()
def build_key_bundle(key_conf, kid_template=""):
"""
Builds a :py:class:`oidcmsg.key_bundle.KeyBundle` instance based on a key
specification.
An example of such a specification::
keys = [
{"type": "RSA", "key": "cp_keys/key.pem", "use": ["enc", "sig"], 'size': 2048},
{"type": "EC", "crv": "P-256", "use": ["sig"], "kid": "ec.1"},
{"type": "EC", "crv": "P-256", "use": ["enc"], "kid": "ec.2"},
{"type": "oct", "bytes":}
]
Keys in this specification are:
type
The type of key. Presently only 'rsa', 'ec' and 'oct' supported.
key
A name of a file where a key can be found. Works with PEM encoded
RSA and EC private keys.
use
What the key should be used for
crv
The elliptic curve that should be used. Only applies to elliptic curve
keys :-)
kid
Key ID, can only be used with one usage type is specified. If there
are more the one usage type specified 'kid' will just be ignored.
:param key_conf: The key configuration
:param kid_template: A template by which to build the key IDs. If no
kid_template is given then the built-in function add_kid() will be used.
:return: A KeyBundle instance
"""
kid = 0
_bundles = []
for spec in key_conf:
typ = spec["type"].upper()