-
Notifications
You must be signed in to change notification settings - Fork 58
/
jwk.py
946 lines (760 loc) · 25.5 KB
/
jwk.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
import base64
import binascii
import hashlib
import json
import logging
import re
import sys
from binascii import a2b_base64
import six
from Cryptodome.PublicKey import RSA
from Cryptodome.PublicKey.RSA import RsaKey
from Cryptodome.PublicKey.RSA import importKey
from Cryptodome.Util.asn1 import DerSequence
from jwkest.ecc import ECCException
from requests import request
from jwkest import JWKESTException
from jwkest import UnknownAlgorithm
from jwkest import as_bytes
from jwkest import as_unicode
from jwkest import b64d
from jwkest import b64e
from jwkest import base64_to_long
from jwkest import base64url_to_long
from jwkest import long_to_base64
from jwkest.ecc import NISTEllipticCurve
from jwkest.jwt import b2s_conv
if sys.version > '3':
long = int
else:
from __builtin__ import long
__author__ = 'rohe0002'
logger = logging.getLogger(__name__)
PREFIX = "-----BEGIN CERTIFICATE-----"
POSTFIX = "-----END CERTIFICATE-----"
class JWKException(JWKESTException):
pass
class FormatError(JWKException):
pass
class SerializationNotPossible(JWKException):
pass
class DeSerializationNotPossible(JWKException):
pass
class HeaderError(JWKESTException):
pass
def dicthash(d):
return hash(repr(sorted(d.items())))
def intarr2str(arr):
return "".join([chr(c) for c in arr])
def sha256_digest(msg):
return hashlib.sha256(as_bytes(msg)).digest()
def sha384_digest(msg):
return hashlib.sha384(as_bytes(msg)).digest()
def sha512_digest(msg):
return hashlib.sha512(as_bytes(msg)).digest()
DIGEST_HASH = {
'SHA-256': sha256_digest,
'SHA-384': sha384_digest,
'SHA-512': sha512_digest
}
# =============================================================================
def import_rsa_key_from_file(filename, passphrase=None):
content = None
with open(filename, 'r') as f:
content = f.read()
return RSA.importKey(content, passphrase=passphrase)
def import_rsa_key(key, passphrase=None):
"""
Extract an RSA key from a PEM-encoded certificate
:param key: RSA key encoded in standard form
:param passphrase: Password to open the certificate (Optional)
:return: RSA key instance
"""
return importKey(key, passphrase=passphrase)
def der2rsa(der):
# Extract subjectPublicKeyInfo field from X.509 certificate (see RFC3280)
cert = DerSequence()
cert.decode(der)
tbs_certificate = DerSequence()
tbs_certificate.decode(cert[0])
subject_public_key_info = tbs_certificate[6]
# Initialize RSA key
return RSA.importKey(subject_public_key_info)
def pem_cert2rsa(pem_file):
# Convert from PEM to DER
pem = None
with open(pem_file) as f:
pem = f.read()
_rsa = RSA.importKey(pem)
lines = pem.replace(" ", '').split()
return der2rsa(a2b_base64(''.join(lines[1:-1])))
def der_cert2rsa(der):
"""
Extract an RSA key from a DER certificate
@param der: DER-encoded certificate
@return: RSA instance
"""
pem = re.sub(r'[^A-Za-z0-9+/]', '', der)
return der2rsa(base64.b64decode(pem))
def load_x509_cert(url, spec2key, *args, **kwargs):
"""
Get and transform a X509 cert into a key
:param url: Where the X509 cert can be found
:param spec2key: A dictionary over keys already seen
:return: List of 2-tuples (keytype, key)
"""
try:
r = request("GET", url, allow_redirects=True, **kwargs)
if r.status_code == 200:
cert = str(r.text)
try:
_key = spec2key[cert]
except KeyError:
_key = import_rsa_key(cert)
spec2key[cert] = _key
return [("rsa", _key)]
else:
raise Exception("HTTP Get error: %s" % r.status_code)
except Exception as err: # not a RSA key
logger.warning("Can't load key: %s" % err)
return []
def rsa_load(filename):
"""Read a PEM-encoded RSA key pair from a file."""
pem = None
with open(filename, 'r') as f:
pem = f.read()
return import_rsa_key(pem)
def rsa_eq(key1, key2):
# Check if two RSA keys are in fact the same
if key1.n == key2.n and key1.e == key2.e:
return True
else:
return False
def key_eq(key1, key2):
if type(key1) == type(key2):
if isinstance(key1, str):
return key1 == key2
elif isinstance(key1, RSA):
return rsa_eq(key1, key2)
return False
def x509_rsa_load(txt):
""" So I get the same output format as loads produces
:param txt:
:return:
"""
return [("rsa", import_rsa_key(txt))]
def key_from_jwk_dict(jwk_dict, private=True):
"""Load JWK from dictionary"""
if jwk_dict['kty'] == 'EC':
if private:
return ECKey(kid=jwk_dict['kid'],
crv=jwk_dict['crv'],
x=jwk_dict['x'],
y=jwk_dict['y'],
d=jwk_dict['d'])
else:
return ECKey(kid=jwk_dict['kid'],
crv=jwk_dict['crv'],
x=jwk_dict['x'],
y=jwk_dict['y'])
elif jwk_dict['kty'] == 'RSA':
if private:
return RSAKey(kid=jwk_dict['kid'],
n=jwk_dict['n'],
e=jwk_dict['e'],
d=jwk_dict['d'],
p=jwk_dict['p'],
q=jwk_dict['q'])
else:
return RSAKey(kid=jwk_dict['kid'],
n=jwk_dict['n'],
e=jwk_dict['e'])
elif jwk_dict['kty'] == 'oct':
return SYMKey(kid=jwk_dict['kid'],
k=jwk_dict['k'])
else:
raise UnknownAlgorithm
class Key(object):
"""
Basic JSON Web key class
"""
members = ["kty", "alg", "use", "kid", "x5c", "x5t", "x5u"]
longs = []
public_members = ["kty", "alg", "use", "kid", "x5c", "x5t", "x5u"]
required = ['kty']
def __init__(self, kty="", alg="", use="", kid="", key=None, x5c=None,
x5t="", x5u="", **kwargs):
self.key = key
self.extra_args = kwargs
# want kty, alg, use and kid to be strings
if isinstance(kty, six.string_types):
self.kty = kty
else:
self.kty = as_unicode(kty)
if isinstance(alg, six.string_types):
self.alg = alg
else:
self.alg = as_unicode(alg)
if isinstance(use, six.string_types):
self.use = use
else:
self.use = as_unicode(use)
if isinstance(kid, six.string_types):
self.kid = kid
else:
self.kid = as_unicode(kid)
self.x5c = x5c or []
self.x5t = x5t
self.x5u = x5u
self.inactive_since = 0
self._hash = None
def to_dict(self):
"""
A wrapper for to_dict the makes sure that all the private information
as well as extra arguments are included. This method should *not* be
used for exporting information about the key.
"""
res = self.serialize(private=True)
res.update(self.extra_args)
return res
def common(self):
res = {"kty": self.kty}
if self.use:
res["use"] = self.use
if self.kid:
res["kid"] = self.kid
if self.alg:
res["alg"] = self.alg
return res
def __str__(self):
return str(self.to_dict())
def deserialize(self):
"""
Starting with information gathered from the on-the-wire representation
initiate an appropriate key.
"""
pass
def serialize(self, private=False):
"""
map key characteristics into attribute values that can be used
to create an on-the-wire representation of the key
"""
pass
def get_key(self, **kwargs):
return self.key
def verify(self):
"""
Verify that the information gathered from the on-the-wire
representation is of the right types.
This is supposed to be run before the info is deserialized.
"""
for param in self.longs:
item = getattr(self, param)
if not item or isinstance(item, six.integer_types):
continue
if isinstance(item, bytes):
item = item.decode('utf-8')
setattr(self, param, item)
try:
_ = base64url_to_long(item)
except Exception:
return False
else:
if [e for e in ['+', '/', '='] if e in item]:
return False
if self.kid:
try:
assert isinstance(self.kid, six.string_types)
except AssertionError:
raise HeaderError("kid of wrong value type")
return True
def __eq__(self, other):
try:
assert isinstance(other, Key)
assert set(self.__dict__.keys()) == set(other.__dict__.keys())
for key in self.public_members:
assert getattr(other, key) == getattr(self, key)
except AssertionError:
return False
else:
return True
def keys(self):
return list(self.to_dict().keys())
def thumbprint(self, hash_function, members=None):
if members is None:
members = self.required
members.sort()
ser = self.serialize()
_se = []
for elem in members:
try:
_val = ser[elem]
except KeyError: # should never happen with the required set
pass
else:
_se.append('"{}":{}'.format(elem, json.dumps(_val)))
_json = '{{{}}}'.format(','.join(_se))
return DIGEST_HASH[hash_function](_json)
def get_hash(self, hash_function=None):
if not hash_function:
hash_function = 'SHA-256'
self._hash = int(binascii.hexlify(self.thumbprint(hash_function)), 16)
return self._hash
def add_kid(self):
_tp = self.thumbprint('SHA-256')
self.kid = b64e(_tp).decode('utf8')
self._hash = int(binascii.hexlify(_tp), 16)
def __hash__(self):
if not self._hash:
if self.kid:
self.get_hash()
else:
self.add_kid()
return self._hash
def deser(val):
if isinstance(val, str):
_val = val.encode("utf-8")
else:
_val = val
return base64_to_long(_val)
class RSAKey(Key):
"""
JSON Web key representation of a RSA key
"""
members = Key.members
members.extend(["n", "e", "d", "p", "q"])
longs = ["n", "e", "d", "p", "q", "dp", "dq", "di", "qi"]
public_members = Key.public_members
public_members.extend(["n", "e"])
required = ['kty', 'n', 'e']
def __init__(self, kty="RSA", alg="", use="", kid="", key=None,
x5c=None, x5t="", x5u="", n="", e="", d="", p="", q="",
dp="", dq="", di="", qi="", **kwargs):
Key.__init__(self, kty, alg, use, kid, key, x5c, x5t, x5u, **kwargs)
self.n = n
self.e = e
self.d = d
self.p = p
self.q = q
self.dp = dp
self.dq = dq
self.di = di
self.qi = qi
has_public_key_parts = len(self.n) > 0 and len(self.e)
has_x509_cert_chain = len(self.x5c) > 0
if not self.key and (has_public_key_parts or has_x509_cert_chain):
self.deserialize()
elif self.key and not (self.n and self.e):
self._split()
def deserialize(self):
if self.n and self.e:
try:
for param in self.longs:
item = getattr(self, param)
if not item or isinstance(item, six.integer_types):
continue
else:
try:
val = long(deser(item))
except Exception:
raise
else:
setattr(self, param, val)
lst = [self.n, self.e]
if self.d:
lst.append(self.d)
if self.p:
lst.append(self.p)
if self.q:
lst.append(self.q)
self.key = RSA.construct(tuple(lst))
else:
self.key = RSA.construct(lst)
except ValueError as err:
raise DeSerializationNotPossible("%s" % err)
elif self.x5c:
der_cert = base64.b64decode(self.x5c[0].encode("ascii"))
if self.x5t: # verify the cert
if not b64d(self.x5t.encode("ascii")) == hashlib.sha1(
der_cert).digest():
raise DeSerializationNotPossible(
"The thumbprint ('x5t') does not match the "
"certificate.")
self.key = der2rsa(der_cert)
self._split()
if len(self.x5c) > 1: # verify chain
pass
else:
raise DeSerializationNotPossible()
def serialize(self, private=False):
if not self.key:
raise SerializationNotPossible()
res = self.common()
public_longs = list(set(self.public_members) & set(self.longs))
for param in public_longs:
item = getattr(self, param)
if item:
res[param] = long_to_base64(item)
if private:
for param in self.longs:
if not private and param in ["d", "p", "q", "dp", "dq", "di",
"qi"]:
continue
item = getattr(self, param)
if item:
res[param] = long_to_base64(item)
return res
def _split(self):
if not self.key:
raise SerializationNotPossible()
self.n = self.key.n
self.e = self.key.e
try:
self.d = self.key.d
except AttributeError:
pass
else:
for param in ["p", "q"]:
try:
val = getattr(self.key, param)
except AttributeError:
pass
else:
if val:
setattr(self, param, val)
def load(self, filename):
"""
Load the key from a file.
:param filename: File name
"""
self.key = rsa_load(filename)
self._split()
return self
def load_key(self, key):
"""
Use this RSA key
:param key: An RSA key instance
"""
self.key = key
self._split()
return self
def encryption_key(self, **kwargs):
"""
Make sure there is a key instance present that can be used for
encrypting/signing.
"""
if not self.key:
self.deserialize()
return self.key
class ECKey(Key):
"""
JSON Web key representation of a Elliptic curve key
"""
members = ["kty", "alg", "use", "kid", "crv", "x", "y", "d"]
longs = ['x', 'y', 'd']
public_members = ["kty", "alg", "use", "kid", "crv", "x", "y"]
required = ['crv', 'key', 'x', 'y']
def __init__(self, kty="EC", alg="", use="", kid="", key=None,
crv="", x="", y="", d="", curve=None, **kwargs):
Key.__init__(self, kty, alg, use, kid, key, **kwargs)
self.crv = crv
self.x = x
self.y = y
self.d = d
self.curve = curve
# Initiated guess as to what state the key is in
# To be usable for encryption/signing/.. it has to be deserialized
if self.crv and not self.curve:
self.verify()
self.deserialize()
elif self.key:
if not self.crv and not self.curve:
self.load_key(key)
def deserialize(self):
"""
Starting with information gathered from the on-the-wire representation
of an elliptic curve key initiate an Elliptic Curve.
"""
if not (self.x and self.y and self.crv):
DeSerializationNotPossible()
try:
if not isinstance(self.x, six.integer_types):
self.x = deser(self.x)
if not isinstance(self.y, six.integer_types):
self.y = deser(self.y)
except TypeError:
raise DeSerializationNotPossible()
except ValueError as err:
raise DeSerializationNotPossible("%s" % err)
self.curve = NISTEllipticCurve.by_name(self.crv)
if self.d:
try:
if isinstance(self.d, six.string_types):
self.d = deser(self.d)
except ValueError as err:
raise DeSerializationNotPossible(str(err))
def get_key(self, private=False, **kwargs):
if private:
if self.d:
return self.d
else:
raise ValueError()
else:
if self.x and self.y:
return self.x, self.y
else:
raise ValueError()
def serialize(self, private=False):
if not self.crv and not self.curve:
raise SerializationNotPossible()
res = self.common()
blen = self.curve.bytes
res.update({
"crv": self.curve.name(),
"x": long_to_base64(self.x, blen),
"y": long_to_base64(self.y, blen)
})
if private and self.d:
res["d"] = long_to_base64(self.d, blen)
return res
def load_key(self, key):
self.curve = key
self.d, (self.x, self.y) = key.key_pair()
return self
def decryption_key(self):
return self.get_key(private=True)
def encryption_key(self, private=False, **kwargs):
# both for encryption and decryption.
return self.get_key(private=private)
ALG2KEYLEN = {
"A128KW": 16,
"A192KW": 24,
"A256KW": 32,
"HS256": 32,
"HS384": 48,
"HS512": 64
}
class SYMKey(Key):
members = ["kty", "alg", "use", "kid", "k"]
public_members = members[:]
required = ['k', 'kty']
def __init__(self, kty="oct", alg="", use="", kid="", key=None,
x5c=None, x5t="", x5u="", k="", mtrl="", **kwargs):
Key.__init__(self, kty, alg, use, kid, as_bytes(key), x5c, x5t, x5u,
**kwargs)
self.k = k
if not self.key and self.k:
if isinstance(self.k, str):
self.k = self.k.encode("utf-8")
self.key = b64d(bytes(self.k))
def deserialize(self):
if self.k:
self.key = b64d(bytes(self.k))
else:
raise DeSerializationNotPossible()
def serialize(self, private=True):
res = self.common()
res["k"] = as_unicode(b64e(bytes(self.key)))
return res
def encryption_key(self, alg, **kwargs):
"""
Return an encryption key as per
http://openid.net/specs/openid-connect-core-1_0.html#Encryption
:param alg: encryption algorithm
:param kwargs:
:return: encryption key as byte string
"""
if not self.key:
self.deserialize()
tsize = ALG2KEYLEN[alg]
# _keylen = len(self.key)
if tsize <= 32:
# SHA256
_enc_key = sha256_digest(self.key)[:tsize]
elif tsize <= 48:
# SHA384
_enc_key = sha384_digest(self.key)[:tsize]
elif tsize <= 64:
# SHA512
_enc_key = sha512_digest(self.key)[:tsize]
else:
raise JWKException("No support for symmetric keys > 512 bits")
logger.debug('Symmetric encryption key: {}'.format(
as_unicode(b64e(_enc_key))))
return _enc_key
# -----------------------------------------------------------------------------
def keyitems2keyreps(keyitems):
keys = []
for key_type, _keys in list(keyitems.items()):
if key_type.upper() == "RSA":
keys.extend([RSAKey(key=k) for k in _keys])
elif key_type.lower() == "oct":
keys.extend([SYMKey(key=k) for k in _keys])
elif key_type.upper() == "EC":
keys.extend([ECKey(key=k) for k in _keys])
else:
keys.extend([Key(key=k) for k in _keys])
return keys
def keyrep(kspec, enc="utf-8"):
"""
Instantiate a Key given a set of key/word arguments
:param kspec: Key specification, arguments to the Key initialization
:param enc: The encoding of the strings. If it's JSON which is the default
the encoding is utf-8.
:return: Key instance
"""
if enc:
_kwargs = {}
for key, val in kspec.items():
if isinstance(val, str):
_kwargs[key] = val.encode(enc)
else:
_kwargs[key] = val
else:
_kwargs = kspec
if kspec["kty"] == "RSA":
item = RSAKey(**_kwargs)
elif kspec["kty"] == "oct":
item = SYMKey(**_kwargs)
elif kspec["kty"] == "EC":
item = ECKey(**_kwargs)
else:
item = Key(**_kwargs)
return item
def jwk_wrap(key, use="", kid=""):
"""
Instantiate a Key instance with the given key
:param key: The keys to wrap
:param use: What the key are expected to be use for
:param kid: A key id
:return: The Key instance
"""
if isinstance(key, RsaKey):
kspec = RSAKey(use=use, kid=kid).load_key(key)
elif isinstance(key, str):
kspec = SYMKey(key=key, use=use, kid=kid)
elif isinstance(key, NISTEllipticCurve):
kspec = ECKey(use=use, kid=kid).load_key(key)
else:
raise Exception("Unknown key type:key=" + str(type(key)))
kspec.serialize()
return kspec
class KEYS(object):
def __init__(self):
self._keys = []
def load_dict(self, dikt):
for kspec in dikt["keys"]:
try:
_key = keyrep(kspec)
except ECCException as err:
logger.warning(err)
else:
self._keys.append(_key)
def load_jwks(self, jwks):
"""
Load and create keys from a JWKS JSON representation
Expects something on this form::
{"keys":
[
{"kty":"EC",
"crv":"P-256",
"x":"MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
"y":"4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
"use":"enc",
"kid":"1"},
{"kty":"RSA",
"n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFb....."
"e":"AQAB",
"kid":"2011-04-29"}
]
}
:param jwks: The JWKS JSON string representation
:return: list of 2-tuples containing key, type
"""
self.load_dict(json.loads(jwks))
return self
def dump_jwks(self):
"""
:return: A JWKS representation of the held keys
"""
res = []
for key in self._keys:
res.append(b2s_conv(key.serialize()))
return json.dumps({"keys": res})
def load_from_url(self, url, *args, **kwargs):
"""
Get and transform a JWKS into keys
:param url: Where the JWKS can be found
:param verify: SSL cert verification
:return: list of keys
"""
if 'verify' not in kwargs:
kwargs['verify'] = True
r = request("GET", url, allow_redirects=True, **kwargs)
if r.status_code == 200:
return self.load_jwks(r.text)
else:
raise Exception("HTTP Get error: %s" % r.status_code)
def __getitem__(self, item):
"""
Get all keys of a specific key type
:param kty: Key type
:return: list of keys
"""
kty = item.lower()
return [k for k in self._keys if k.kty.lower() == kty]
def __iter__(self):
for k in self._keys:
yield k
def __len__(self):
return len(self._keys)
def keys(self):
return self._keys
def key_types(self):
"""
:return: A list of key types !!! not keys
"""
return list(set([k.kty for k in self._keys]))
def __repr__(self):
return self.dump_jwks()
def __str__(self):
return self.__repr__()
def kids(self):
return [k.kid for k in self._keys if k.kid]
def by_kid(self, kid):
return [k for k in self._keys if kid == k.kid]
def wrap_add(self, keyinst, use="", kid=''):
self._keys.append(jwk_wrap(keyinst, use, kid))
def as_dict(self):
_res = {}
for kty, k in [(k.kty, k) for k in self._keys]:
if kty not in ["RSA", "EC", "oct"]:
if kty in ["rsa", "ec"]:
kty = kty.upper()
else:
kty = kty.lower()
try:
_res[kty].append(k)
except KeyError:
_res[kty] = [k]
return _res
def add(self, item, enc="utf-8"):
self._keys.append(keyrep(item, enc))
def append(self, key):
self._keys.append(key)
def load_jwks_from_url(url, verify=True):
return KEYS().load_from_url(url, verify=verify).keys()
def load_jwks(spec):
return KEYS().load_jwks(spec).keys()
def make_public_copy(key):
if not isinstance(key, Key):
raise ValueError("Wrong type of class instance")
c = key.__class__()
for attr in key.public_members:
try:
v = getattr(key, attr)
except AttributeError:
pass
else:
setattr(c, attr, v)
return c