@@ -36,29 +36,21 @@ class KeyJar(object):
36
36
""" A keyjar contains a number of KeyBundles sorted by owner/issuer """
37
37
38
38
def __init__ (self , ca_certs = None , verify_ssl = True , keybundle_cls = KeyBundle ,
39
- remove_after = 3600 , httpc = None , httpc_params = None ):
39
+ remove_after = 3600 , httpc = None ):
40
40
"""
41
41
KeyJar init function
42
42
43
43
:param ca_certs: CA certificates, to be used for HTTPS
44
44
:param verify_ssl: Attempting SSL certificate verification
45
- :param keybundle_cls: The KeyBundle class
46
- :param remove_after: How long keys marked as inactive will remain in the key Jar.
47
- :param httpc: A HTTP client to use. Default is Requests request.
48
- :param httpc_params: HTTP request parameters
49
45
:return: Keyjar instance
50
46
"""
51
47
self .spec2key = {}
52
48
self .issuer_keys = {}
53
49
self .ca_certs = ca_certs
50
+ self .verify_ssl = verify_ssl
54
51
self .keybundle_cls = keybundle_cls
55
52
self .remove_after = remove_after
56
53
self .httpc = httpc or request
57
- self .httpc_params = httpc_params or {}
58
- # Now part of httpc_params
59
- # self.verify_ssl = verify_ssl
60
- if not self .httpc_params : # backward compatibility
61
- self .httpc_params ["verify" ] = verify_ssl
62
54
63
55
def __repr__ (self ):
64
56
issuers = list (self .issuer_keys .keys ())
@@ -81,13 +73,11 @@ def add_url(self, issuer, url, **kwargs):
81
73
raise KeyError ("No url given" )
82
74
83
75
if "/localhost:" in url or "/localhost/" in url :
84
- _params = self .httpc_params .copy ()
85
- _params ['verify' ] = False
86
- kb = self .keybundle_cls (source = url , httpc = self .httpc ,
87
- httpc_params = _params , ** kwargs )
76
+ kb = self .keybundle_cls (source = url , verify_ssl = False ,
77
+ httpc = self .httpc , ** kwargs )
88
78
else :
89
- kb = self .keybundle_cls (source = url , httpc = self .httpc ,
90
- httpc_params = self .httpc_params , ** kwargs )
79
+ kb = self .keybundle_cls (source = url , verify_ssl = self .verify_ssl ,
80
+ httpc = self .httpc , ** kwargs )
91
81
92
82
kb .update ()
93
83
self .add_kb (issuer , kb )
@@ -114,7 +104,9 @@ def add_symmetric(self, issuer, key, usage=None):
114
104
else :
115
105
for use in usage :
116
106
self .issuer_keys [issuer ].append (
117
- self .keybundle_cls ([{"kty" : "oct" , "key" : key , "use" : use }]))
107
+ self .keybundle_cls ([{"kty" : "oct" ,
108
+ "key" : key ,
109
+ "use" : use }]))
118
110
119
111
def add_kb (self , issuer , kb ):
120
112
"""
@@ -420,10 +412,10 @@ def import_jwks(self, jwks, issuer):
420
412
else :
421
413
try :
422
414
self .issuer_keys [issuer ].append (
423
- self .keybundle_cls (_keys , httpc = self .httpc , httpc_params = self . httpc_params ))
415
+ self .keybundle_cls (_keys , verify_ssl = self .verify_ssl ))
424
416
except KeyError :
425
417
self .issuer_keys [issuer ] = [self .keybundle_cls (
426
- _keys , httpc = self .httpc , httpc_params = self . httpc_params )]
418
+ _keys , verify_ssl = self .verify_ssl )]
427
419
428
420
def import_jwks_as_json (self , jwks , issuer ):
429
421
"""
@@ -466,7 +458,7 @@ def remove_outdated(self, when=0):
466
458
Outdated keys are keys that has been marked as inactive at a time that
467
459
is longer ago then some set number of seconds (when). If when=0 the
468
460
the base time is set to now.
469
- The number of seconds are carried in the remove_after parameter in the
461
+ The number of seconds a carried in the remove_after parameter in the
470
462
key jar.
471
463
472
464
:param when: To facilitate testing
@@ -493,7 +485,8 @@ def _add_key(self, keys, issuer, use, key_type='', kid='',
493
485
issuer , key_summary (self , issuer )))
494
486
495
487
if kid :
496
- for _key in self .get (key_use = use , owner = issuer , kid = kid , key_type = key_type ):
488
+ for _key in self .get (key_use = use , owner = issuer , kid = kid ,
489
+ key_type = key_type ):
497
490
if _key and _key not in keys :
498
491
keys .append (_key )
499
492
return keys
@@ -578,37 +571,18 @@ def get_jwt_verify_keys(self, jwt, **kwargs):
578
571
:param kwargs: Other key word arguments
579
572
:return: list of usable keys
580
573
"""
574
+ allow_missing_kid = kwargs .get ('allow_missing_kid' , False )
581
575
582
- try :
583
- allow_missing_kid = kwargs ['allow_missing_kid' ]
584
- except KeyError :
585
- allow_missing_kid = False
586
-
587
- try :
576
+ _key_type = ''
577
+ if jwt .headers .get ('alg' ):
588
578
_key_type = jws_alg2keytype (jwt .headers ['alg' ])
589
- except KeyError :
590
- _key_type = ''
591
579
592
- try :
593
- _kid = jwt .headers ['kid' ]
594
- except KeyError :
595
- logger .info ('Missing kid' )
596
- _kid = ''
597
-
598
- try :
599
- nki = kwargs ['no_kid_issuer' ]
600
- except KeyError :
601
- nki = {}
580
+ _kid = jwt .headers .get ('kid' , "" )
581
+ nki = kwargs .get ('no_kid_issuer' , {})
602
582
603
583
_payload = jwt .payload ()
604
584
605
- try :
606
- _iss = _payload ['iss' ]
607
- except KeyError :
608
- try :
609
- _iss = kwargs ['iss' ]
610
- except KeyError :
611
- _iss = ''
585
+ _iss = _payload .get ('iss' ) or kwargs .get ('iss' ) or ""
612
586
613
587
if _iss :
614
588
# First extend the key jar iff allowed
@@ -644,8 +618,7 @@ def copy(self):
644
618
for issuer in self .owners ():
645
619
kj [issuer ] = [kb .copy () for kb in self [issuer ]]
646
620
647
- kj .httpc_params = self .httpc_params
648
- kj .httpc = self .httpc
621
+ kj .verify_ssl = self .verify_ssl
649
622
return kj
650
623
651
624
@@ -672,8 +645,8 @@ def build_keyjar(key_conf, kid_template="", keyjar=None, owner=''):
672
645
The type of key. Presently only 'rsa', 'oct' and 'ec' supported.
673
646
674
647
key
675
- A name of a file where a key can be found. Works with PEM encoded
676
- RSA and EC private keys.
648
+ A name of a file where a key can be found. Only works with PEM encoded
649
+ RSA keys
677
650
678
651
use
679
652
What the key should be used for
@@ -838,7 +811,7 @@ def init_key_jar(public_path='', private_path='', key_defs='', owner='',
838
811
update_key_bundle (_kb , _diff )
839
812
_kj .issuer_keys [owner ] = [_kb ]
840
813
jwks = _kj .export_jwks (issuer = owner )
841
- fp = open (public_path , 'w' )
814
+ fp = open (private_path , 'w' )
842
815
fp .write (json .dumps (jwks ))
843
816
fp .close ()
844
817
else :
0 commit comments