Skip to content

Commit 6c09a10

Browse files
committed
Just provide a storage instance to a KeyJar.
It doesn't have to know more then how to use it.
1 parent 5f10441 commit 6c09a10

File tree

2 files changed

+36
-53
lines changed

2 files changed

+36
-53
lines changed

src/cryptojwt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
except ImportError:
2222
pass
2323

24-
__version__ = "1.0.0"
24+
__version__ = "1.1.0"
2525

2626
logger = logging.getLogger(__name__)
2727

src/cryptojwt/key_jar.py

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ class KeyJar(object):
2525
""" A keyjar contains a number of KeyBundles sorted by owner/issuer """
2626

2727
def __init__(
28-
self,
29-
ca_certs=None,
30-
verify_ssl=True,
31-
keybundle_cls=KeyBundle,
32-
remove_after=3600,
33-
httpc=None,
34-
httpc_params=None,
35-
storage_conf=None,
36-
storage_factory=None,
28+
self,
29+
ca_certs=None,
30+
verify_ssl=True,
31+
keybundle_cls=KeyBundle,
32+
remove_after=3600,
33+
httpc=None,
34+
httpc_params=None,
35+
storage=None,
3736
):
3837
"""
3938
KeyJar init function
@@ -44,20 +43,15 @@ def __init__(
4443
:param remove_after: How long keys marked as inactive will remain in the key Jar.
4544
:param httpc: A HTTP client to use. Default is Requests request.
4645
:param httpc_params: HTTP request parameters
47-
:param storage_conf: Storage configuration
48-
:param storage_factory: A function that given the storage configuration (storage_conf)
49-
will return an instance that can store information.
46+
:param storage: An instance that can store information. It basically look like dictionary.
5047
:return: Keyjar instance
5148
"""
5249

53-
if storage_conf is None:
50+
if storage is None:
5451
self._issuers = {}
5552
else:
56-
if not storage_factory:
57-
raise ValueError("Missing storage factory specification")
58-
self._issuers = storage_factory(storage_conf)
53+
self._issuers = storage
5954

60-
self.storage_conf = storage_conf
6155
self.spec2key = {}
6256
self.ca_certs = ca_certs
6357
self.keybundle_cls = keybundle_cls
@@ -392,7 +386,7 @@ def export_jwks(self, private=False, issuer_id="", usage=None):
392386
k.serialize(private)
393387
for k in kb.keys()
394388
if k.inactive_since == 0
395-
and (usage is None or (hasattr(k, "use") and k.use == usage))
389+
and (usage is None or (hasattr(k, "use") and k.use == usage))
396390
]
397391
)
398392
return {"keys": keys}
@@ -478,14 +472,14 @@ def remove_outdated(self, when=0):
478472

479473
@deprecated_alias(issuer="issuer_id", owner="issuer_id")
480474
def _add_key(
481-
self,
482-
keys,
483-
issuer_id,
484-
use,
485-
key_type="",
486-
kid="",
487-
no_kid_issuer=None,
488-
allow_missing_kid=False,
475+
self,
476+
keys,
477+
issuer_id,
478+
use,
479+
key_type="",
480+
kid="",
481+
no_kid_issuer=None,
482+
allow_missing_kid=False,
489483
):
490484

491485
_issuer = self._get_issuer(issuer_id)
@@ -621,18 +615,14 @@ def get_jwt_verify_keys(self, jwt, **kwargs):
621615

622616
def copy(self):
623617
"""
624-
Make deep copy of this key jar.
618+
Make deep copy of the content of this key jar.
619+
620+
Note that if this key jar uses an external storage module the copy will not.
625621
626622
:return: A :py:class:`oidcmsg.key_jar.KeyJar` instance
627623
"""
628-
if self.storage_conf:
629-
_conf = self.storage_conf.get("KeyJar")
630-
if _conf:
631-
_label = self.storage_conf.get("label")
632-
if _label:
633-
self.storage_conf["KeyJar"]["label"] = "{}.copy".format(_label)
634624

635-
kj = KeyJar(storage_conf=self.storage_conf)
625+
kj = KeyJar()
636626
for _id, _issuer in self._issuers.items():
637627
_issuer_copy = KeyIssuer()
638628
_issuer_copy.set([kb.copy() for kb in _issuer])
@@ -653,7 +643,6 @@ def dump(self, exclude=None):
653643
"""
654644

655645
info = {
656-
# 'storage_conf': self.storage_conf,
657646
"spec2key": self.spec2key,
658647
"ca_certs": self.ca_certs,
659648
"keybundle_cls": qualified_name(self.keybundle_cls),
@@ -676,7 +665,6 @@ def load(self, info):
676665
:param info: A dictionary with the information
677666
:return:
678667
"""
679-
# self.storage_conf = info['storage_conf']
680668
self.spec2key = info["spec2key"]
681669
self.ca_certs = info["ca_certs"]
682670
self.keybundle_cls = importer(info["keybundle_cls"])
@@ -718,7 +706,7 @@ def rotate_keys(self, key_conf, kid_template="", issuer_id=""):
718706

719707

720708
def build_keyjar(
721-
key_conf, kid_template="", keyjar=None, issuer_id="", storage_conf=None, storage_factory=None
709+
key_conf, kid_template="", keyjar=None, issuer_id="", storage=None
722710
):
723711
"""
724712
Builds a :py:class:`oidcmsg.key_jar.KeyJar` instance or adds keys to
@@ -758,9 +746,7 @@ def build_keyjar(
758746
kid_template is given then the built-in function add_kid() will be used.
759747
:param keyjar: If an KeyJar instance the new keys are added to this key jar.
760748
:param issuer_id: The default owner of the keys in the key jar.
761-
:param storage_conf: Storage configuration
762-
:param storage_factory: A function that given the configuration can instantiate a Storage
763-
instance.
749+
:param storage: A Storage instance.
764750
:return: A KeyJar instance
765751
"""
766752

@@ -769,7 +755,7 @@ def build_keyjar(
769755
return None
770756

771757
if keyjar is None:
772-
keyjar = KeyJar(storage_conf=storage_conf, storage_factory=storage_factory)
758+
keyjar = KeyJar(storage=storage)
773759

774760
keyjar[issuer_id] = _issuer
775761

@@ -778,13 +764,12 @@ def build_keyjar(
778764

779765
@deprecated_alias(issuer="issuer_id", owner="issuer_id")
780766
def init_key_jar(
781-
public_path="",
782-
private_path="",
783-
key_defs="",
784-
issuer_id="",
785-
read_only=True,
786-
storage_conf=None,
787-
storage_factory=None,
767+
public_path="",
768+
private_path="",
769+
key_defs="",
770+
issuer_id="",
771+
read_only=True,
772+
storage=None,
788773
):
789774
"""
790775
A number of cases here:
@@ -822,9 +807,7 @@ def init_key_jar(
822807
:param key_defs: A definition of what keys should be created if they are not already available
823808
:param issuer_id: The owner of the keys
824809
:param read_only: This function should not attempt to write anything to a file system.
825-
:param storage_conf: Configuration information for the storage
826-
:param storage_factory: A function that given the configuration can instantiate a Storage
827-
instance.
810+
:param storage: A Storage instance.
828811
:return: An instantiated :py:class;`oidcmsg.key_jar.KeyJar` instance
829812
"""
830813

@@ -835,7 +818,7 @@ def init_key_jar(
835818
if _issuer is None:
836819
raise ValueError("Could not find any keys")
837820

838-
keyjar = KeyJar(storage_conf=storage_conf, storage_factory=storage_factory)
821+
keyjar = KeyJar(storage=storage)
839822
keyjar[issuer_id] = _issuer
840823
return keyjar
841824

0 commit comments

Comments
 (0)