Skip to content

Commit 441fec6

Browse files
committed
replaced try/except with get
1 parent e38f197 commit 441fec6

File tree

1 file changed

+24
-51
lines changed

1 file changed

+24
-51
lines changed

src/cryptojwt/key_jar.py

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,21 @@ class KeyJar(object):
3636
""" A keyjar contains a number of KeyBundles sorted by owner/issuer """
3737

3838
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):
4040
"""
4141
KeyJar init function
4242
4343
:param ca_certs: CA certificates, to be used for HTTPS
4444
: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
4945
:return: Keyjar instance
5046
"""
5147
self.spec2key = {}
5248
self.issuer_keys = {}
5349
self.ca_certs = ca_certs
50+
self.verify_ssl = verify_ssl
5451
self.keybundle_cls = keybundle_cls
5552
self.remove_after = remove_after
5653
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
6254

6355
def __repr__(self):
6456
issuers = list(self.issuer_keys.keys())
@@ -81,13 +73,11 @@ def add_url(self, issuer, url, **kwargs):
8173
raise KeyError("No url given")
8274

8375
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)
8878
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)
9181

9282
kb.update()
9383
self.add_kb(issuer, kb)
@@ -114,7 +104,9 @@ def add_symmetric(self, issuer, key, usage=None):
114104
else:
115105
for use in usage:
116106
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}]))
118110

119111
def add_kb(self, issuer, kb):
120112
"""
@@ -420,10 +412,10 @@ def import_jwks(self, jwks, issuer):
420412
else:
421413
try:
422414
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))
424416
except KeyError:
425417
self.issuer_keys[issuer] = [self.keybundle_cls(
426-
_keys, httpc=self.httpc, httpc_params=self.httpc_params)]
418+
_keys, verify_ssl=self.verify_ssl)]
427419

428420
def import_jwks_as_json(self, jwks, issuer):
429421
"""
@@ -466,7 +458,7 @@ def remove_outdated(self, when=0):
466458
Outdated keys are keys that has been marked as inactive at a time that
467459
is longer ago then some set number of seconds (when). If when=0 the
468460
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
470462
key jar.
471463
472464
:param when: To facilitate testing
@@ -493,7 +485,8 @@ def _add_key(self, keys, issuer, use, key_type='', kid='',
493485
issuer, key_summary(self, issuer)))
494486

495487
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):
497490
if _key and _key not in keys:
498491
keys.append(_key)
499492
return keys
@@ -578,37 +571,18 @@ def get_jwt_verify_keys(self, jwt, **kwargs):
578571
:param kwargs: Other key word arguments
579572
:return: list of usable keys
580573
"""
574+
allow_missing_kid = kwargs.get('allow_missing_kid', False)
581575

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'):
588578
_key_type = jws_alg2keytype(jwt.headers['alg'])
589-
except KeyError:
590-
_key_type = ''
591579

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', {})
602582

603583
_payload = jwt.payload()
604584

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 ""
612586

613587
if _iss:
614588
# First extend the key jar iff allowed
@@ -644,8 +618,7 @@ def copy(self):
644618
for issuer in self.owners():
645619
kj[issuer] = [kb.copy() for kb in self[issuer]]
646620

647-
kj.httpc_params = self.httpc_params
648-
kj.httpc = self.httpc
621+
kj.verify_ssl = self.verify_ssl
649622
return kj
650623

651624

@@ -672,8 +645,8 @@ def build_keyjar(key_conf, kid_template="", keyjar=None, owner=''):
672645
The type of key. Presently only 'rsa', 'oct' and 'ec' supported.
673646
674647
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
677650
678651
use
679652
What the key should be used for
@@ -838,7 +811,7 @@ def init_key_jar(public_path='', private_path='', key_defs='', owner='',
838811
update_key_bundle(_kb, _diff)
839812
_kj.issuer_keys[owner] = [_kb]
840813
jwks = _kj.export_jwks(issuer=owner)
841-
fp = open(public_path, 'w')
814+
fp = open(private_path, 'w')
842815
fp.write(json.dumps(jwks))
843816
fp.close()
844817
else:

0 commit comments

Comments
 (0)