Skip to content

Commit aaf9832

Browse files
committed
Cleaned up a bit.
1 parent f518bcb commit aaf9832

File tree

4 files changed

+100
-33
lines changed

4 files changed

+100
-33
lines changed

src/cryptojwt/key_bundle.py

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,22 @@ def __init__(
189189
"""
190190

191191
self._keys = []
192-
self.remote = False
193-
self.local = False
194192
self.cache_time = cache_time
195-
self.ignore_errors_period = ignore_errors_period
196-
self.ignore_errors_until = None # UNIX timestamp of last error
197-
self.time_out = 0
198193
self.etag = ""
199-
self.source = None
200194
self.fileformat = fileformat.lower()
195+
self.ignore_errors_period = ignore_errors_period
196+
self.ignore_errors_until = None # UNIX timestamp of last error
197+
self.ignore_invalid_keys = ignore_invalid_keys
198+
self.imp_jwks = None
201199
self.keytype = keytype
202200
self.keyusage = keyusage
203-
self.imp_jwks = None
204-
self.last_updated = 0
205-
self.last_remote = None # HTTP Date of last remote update
206201
self.last_local = None # UNIX timestamp of last local update
207-
self.ignore_invalid_keys = ignore_invalid_keys
202+
self.last_remote = None # HTTP Date of last remote update
203+
self.last_updated = 0
204+
self.local = False
205+
self.remote = False
206+
self.source = None
207+
self.time_out = 0
208208

209209
if httpc:
210210
self.httpc = httpc
@@ -761,16 +761,22 @@ def dump(self, cutoff: Optional[list] = None):
761761

762762
res = {
763763
"keys": _keys,
764+
"cache_time": self.cache_time,
765+
"etag": self.etag,
764766
"fileformat": self.fileformat,
765-
"last_updated": self.last_updated,
766-
"last_remote": self.last_remote,
767-
"last_local": self.last_local,
768767
"httpc_params": self.httpc_params,
769-
"remote": self.remote,
770-
"local": self.local,
768+
"ignore_errors_period": self.ignore_errors_period,
769+
"ignore_errors_until": self.ignore_errors_until,
770+
"ignore_invalid_keys": self.ignore_invalid_keys,
771771
"imp_jwks": self.imp_jwks,
772+
"keytype": self.keytype,
773+
"keyusage": self.keyusage,
774+
"last_local": self.last_local,
775+
"last_remote": self.last_remote,
776+
"last_updated": self.last_updated,
777+
"local": self.local,
778+
"remote": self.remote,
772779
"time_out": self.time_out,
773-
"cache_time": self.cache_time,
774780
}
775781

776782
if self.source:
@@ -782,17 +788,45 @@ def load(self, spec):
782788
_keys = spec.get("keys", [])
783789
if _keys:
784790
self.do_keys(_keys)
785-
self.source = spec.get("source", None)
791+
self.cache_time = spec.get("cache_time", 0)
792+
self.etag = spec.get("etag", "")
786793
self.fileformat = spec.get("fileformat", "jwks")
787-
self.last_updated = spec.get("last_updated", 0)
788-
self.last_remote = spec.get("last_remote", None)
794+
self.httpc_params = spec.get("httpc_params", {})
795+
self.ignore_errors_period = spec.get("ignore_errors_period", 0)
796+
self.ignore_errors_until = spec.get("ignore_errors_until", None)
797+
self.ignore_invalid_keys = spec.get("ignore_invalid_keys", True)
798+
self.imp_jwks = spec.get("imp_jwks", None)
799+
self.keytype = (spec.get("keytype", "RSA"),)
800+
self.keyusage = (spec.get("keyusage", None),)
789801
self.last_local = spec.get("last_local", None)
790-
self.remote = spec.get("remote", False)
802+
self.last_remote = spec.get("last_remote", None)
803+
self.last_updated = spec.get("last_updated", 0)
791804
self.local = spec.get("local", False)
792-
self.imp_jwks = spec.get("imp_jwks", None)
805+
self.remote = spec.get("remote", False)
806+
self.source = spec.get("source", None)
793807
self.time_out = spec.get("time_out", 0)
794-
self.cache_time = spec.get("cache_time", 0)
795-
self.httpc_params = spec.get("httpc_params", {})
808+
return self
809+
810+
def flush(self):
811+
self._keys = []
812+
self.cache_time = (300,)
813+
self.etag = ""
814+
self.fileformat = "jwks"
815+
# self.httpc=None,
816+
self.httpc_params = (None,)
817+
self.ignore_errors_period = 0
818+
self.ignore_errors_until = None
819+
self.ignore_invalid_keys = True
820+
self.imp_jwks = None
821+
self.keytype = ("RSA",)
822+
self.keyusage = (None,)
823+
self.last_local = None # UNIX timestamp of last local update
824+
self.last_remote = None # HTTP Date of last remote update
825+
self.last_updated = 0
826+
self.local = False
827+
self.remote = False
828+
self.source = None
829+
self.time_out = 0
796830
return self
797831

798832

src/cryptojwt/key_issuer.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def dump(self, exclude=None, cutoff: Optional[list] = None) -> dict:
378378
def load(self, info):
379379
"""
380380
381-
:param items: A list with the information
381+
:param items: A dictionary with the information to load
382382
:return:
383383
"""
384384
self.name = info["name"]
@@ -390,6 +390,18 @@ def load(self, info):
390390
self._bundles = [KeyBundle().load(val) for val in info["bundles"]]
391391
return self
392392

393+
def flush(self):
394+
self.ca_certs = (None,)
395+
self.keybundle_cls = (KeyBundle,)
396+
self.remove_after = (3600,)
397+
self.httpc = (None,)
398+
self.httpc_params = (None,)
399+
self.name = ""
400+
self.spec2key = None
401+
self.remove_after = 0
402+
self._bundles = []
403+
return self
404+
393405
def update(self):
394406
for kb in self._bundles:
395407
kb.update()

src/cryptojwt/key_jar.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,11 @@ def dump(self, exclude: Optional[bool] = None, cutoff: Optional[list] = None) ->
636636
"""
637637

638638
info = {
639-
"spec2key": self.spec2key,
640639
"ca_certs": self.ca_certs,
640+
"httpc_params": self.httpc_params,
641641
"keybundle_cls": qualified_name(self.keybundle_cls),
642642
"remove_after": self.remove_after,
643-
"httpc_params": self.httpc_params,
643+
"spec2key": self.spec2key,
644644
}
645645

646646
_issuers = {}
@@ -668,19 +668,34 @@ def load(self, info):
668668
:param info: A dictionary with the information
669669
:return:
670670
"""
671-
self.spec2key = info["spec2key"]
672-
self.ca_certs = info["ca_certs"]
673-
self.keybundle_cls = importer(info["keybundle_cls"])
674-
self.remove_after = info["remove_after"]
675-
self.httpc_params = info["httpc_params"]
671+
self.ca_certs = info.get("ca_certs", None)
672+
self.httpc_params = info.get("httpc_params", None)
673+
self.keybundle_cls = importer(info.get("keybundle_cls", KeyBundle))
674+
self.remove_after = info.get("remove_after", 3600)
675+
self.spec2key = info.get("spec2key", {})
676676

677-
for _issuer_id, _issuer_desc in info["issuers"].items():
678-
self._issuers[_issuer_id] = KeyIssuer().load(_issuer_desc)
677+
_issuers = info.get("issuers", None)
678+
if _issuers is None:
679+
self._issuers = {}
680+
else:
681+
for _issuer_id, _issuer_desc in _issuers.items():
682+
self._issuers[_issuer_id] = KeyIssuer().load(_issuer_desc)
679683
return self
680684

681685
def loads(self, string):
682686
return self.load(json.loads(string))
683687

688+
def flush(self):
689+
self.ca_certs = None
690+
self.httpc_params = None
691+
self._issuers = {}
692+
self.keybundle_cls = KeyBundle
693+
self.remove_after = 3600
694+
self.spec2key = {}
695+
# self.httpc=None,
696+
697+
return self
698+
684699
@deprecated_alias(issuer="issuer_id", owner="issuer_id")
685700
def key_summary(self, issuer_id):
686701
_issuer = self._get_issuer(issuer_id)

tests/test_03_key_bundle.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,10 +953,16 @@ def test_export_inactive():
953953
res = kb.dump()
954954
assert set(res.keys()) == {
955955
"cache_time",
956+
"etag",
956957
"fileformat",
957958
"httpc_params",
959+
"ignore_errors_until",
960+
"ignore_errors_period",
961+
"ignore_invalid_keys",
958962
"imp_jwks",
959963
"keys",
964+
"keytype",
965+
"keyusage",
960966
"last_updated",
961967
"last_remote",
962968
"last_local",

0 commit comments

Comments
 (0)