Skip to content

Commit f61ca6c

Browse files
committed
Removed JWKS since KeyBundle does everything that JWKS did and more.
1 parent 8166763 commit f61ca6c

File tree

12 files changed

+162
-308
lines changed

12 files changed

+162
-308
lines changed

doc/keyhandling.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ The file now should contain 2 keys::
234234
>>> len(_keys)
235235
3
236236

237-
???
237+
What ???
238238
The key that was removed has not disappeared from the key bundle, but it is
239239
marked as *inactive*. Which means that it should not be used for signing and
240240
encryption but can be used for decryption and signature verification. ::
@@ -248,6 +248,8 @@ encryption but can be used for decryption and signature verification. ::
248248
Key Jar
249249
-------
250250

251+
A key jar keeps keys sorted by owner/issuer. The keys in a key jar are all
252+
part of key bundles.
251253

252254

253255
.. _cryptography: https://cryptography.io/en/latest/

src/cryptojwt/jwk/jwks.py

Lines changed: 0 additions & 142 deletions
This file was deleted.

src/cryptojwt/jwx.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import logging
33

44
import requests
5+
from cryptojwt.key_bundle import KeyBundle
56

67
from .jwk.jwk import key_from_jwk_dict
7-
from .jwk.jwks import JWKS
88
from .jwk.rsa import import_rsa_key
99
from .jwk.rsa import load_x509_cert
1010
from .jwk.rsa import RSAKey
@@ -79,8 +79,7 @@ def __init__(self, msg=None, with_digest=False, httpc=None, **kwargs):
7979
_pub_key = import_rsa_key(_val)
8080
self._jwk = RSAKey(_pub_key)
8181
elif key == "jku":
82-
self._jwks = JWKS(httpc=self.httpc)
83-
self._jwks.load_from_url(_val)
82+
self._jwks = KeyBundle(source=_val, httpc=self.httpc)
8483
self._dict['jku'] = _val
8584
elif "x5u" in self:
8685
try:
@@ -141,7 +140,7 @@ def _get_keys(self):
141140
_keys = []
142141
if self._jwk:
143142
_keys.append(self._jwk)
144-
if self._jwks:
143+
if self._jwks is not None:
145144
_keys.extend(self._jwks.keys())
146145
return _keys
147146

src/cryptojwt/key_bundle.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def ec_init(spec):
107107

108108
class KeyBundle(object):
109109
def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
110-
fileformat="jwks", keytype="RSA", keyusage=None, kid=''):
110+
fileformat="jwks", keytype="RSA", keyusage=None, kid='',
111+
httpc=None):
111112
"""
112113
Contains a set of keys that have a common origin.
113114
The sources can be serveral:
@@ -125,6 +126,7 @@ def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
125126
presently only 'rsa' is supported.
126127
:param keyusage: What the key loaded from file should be used for.
127128
Only applicable for DER files
129+
:param httpc: A HTTP client function
128130
"""
129131

130132
self._keys = []
@@ -139,11 +141,18 @@ def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
139141
self.keyusage = keyusage
140142
self.imp_jwks = None
141143
self.last_updated = 0
144+
if httpc:
145+
self.httpc = httpc
146+
else:
147+
self.httpc = requests.request
142148

143149
if keys:
144150
self.source = None
145151
if isinstance(keys, dict):
146-
self.do_keys([keys])
152+
if 'keys' in keys:
153+
self.do_keys(keys['keys'])
154+
else:
155+
self.do_keys([keys])
147156
else:
148157
self.do_keys(keys)
149158
else:
@@ -254,7 +263,7 @@ def do_remote(self):
254263

255264
try:
256265
logging.debug('KeyBundle fetch keys from: {}'.format(self.source))
257-
r = requests.get(self.source, **args)
266+
r = self.httpc('GET', self.source, **args)
258267
except Exception as err:
259268
logger.error(err)
260269
raise UpdateFailed(
@@ -547,6 +556,9 @@ def copy(self):
547556

548557
return kb
549558

559+
def __iter__(self):
560+
return self._keys.__iter__()
561+
550562

551563
def keybundle_from_local_file(filename, typ, usage):
552564
"""

src/cryptojwt/key_jar.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,14 @@ def add_symmetric(self, issuer, key, usage=None):
9797
if issuer not in self.issuer_keys:
9898
self.issuer_keys[issuer] = []
9999

100-
_key = b64e(as_bytes(key))
101100
if usage is None:
102101
self.issuer_keys[issuer].append(
103-
self.keybundle_cls([{"kty": "oct", "k": _key}]))
102+
self.keybundle_cls([{"kty": "oct", "key": key}]))
104103
else:
105104
for use in usage:
106105
self.issuer_keys[issuer].append(
107106
self.keybundle_cls([{"kty": "oct",
108-
"k": _key,
107+
"key": key,
109108
"use": use}]))
110109

111110
def add_kb(self, issuer, kb):
@@ -188,7 +187,10 @@ def get(self, key_use, key_type="", owner="", kid=None, **kwargs):
188187
lst = []
189188
for bundle in _kj:
190189
if key_type:
191-
_bkeys = bundle.get(key_type)
190+
if key_use in ['ver', 'dec']:
191+
_bkeys = bundle.get(key_type, only_active=False)
192+
else:
193+
_bkeys = bundle.get(key_type)
192194
else:
193195
_bkeys = bundle.keys()
194196
for key in _bkeys:
@@ -283,6 +285,12 @@ def __contains__(self, item):
283285
return False
284286

285287
def __getitem__(self, owner):
288+
"""
289+
Get all the key bundles that belong to an entity.
290+
291+
:param owner: The entity ID
292+
:return: A possibly empty list of key bundles
293+
"""
286294
try:
287295
return self.issuer_keys[owner]
288296
except KeyError:
@@ -344,9 +352,8 @@ def load_keys(self, issuer, jwks_uri='', jwks=None, replace=False):
344352
self.add_url(issuer, jwks_uri)
345353
elif jwks:
346354
# jwks should only be considered if no jwks_uri is present
347-
_keys = jwks["keys"]
348-
self.issuer_keys[issuer].append(
349-
self.keybundle_cls(_keys, verify_ssl=self.verify_ssl))
355+
_keys = jwks['keys']
356+
self.issuer_keys[issuer].append(self.keybundle_cls(_keys))
350357

351358
def find(self, source, issuer):
352359
"""

0 commit comments

Comments
 (0)