@@ -153,6 +153,26 @@ def ec_init(spec):
153153class KeyBundle :
154154 """The Key Bundle"""
155155
156+ params = {
157+ "cache_time" : 0 ,
158+ "etag" : "" ,
159+ "fileformat" : "jwks" ,
160+ "httpc_params" : {},
161+ "ignore_errors_period" : 0 ,
162+ "ignore_errors_until" : None ,
163+ "ignore_invalid_keys" : True ,
164+ "imp_jwks" : None ,
165+ "keytype" : "RSA" ,
166+ "keyusage" : None ,
167+ "last_local" : None ,
168+ "last_remote" : None ,
169+ "last_updated" : 0 ,
170+ "local" : False ,
171+ "remote" : False ,
172+ "source" : None ,
173+ "time_out" : 0 ,
174+ }
175+
156176 def __init__ (
157177 self ,
158178 keys = None ,
@@ -491,6 +511,7 @@ def update(self):
491511
492512 # reread everything
493513 self ._keys = []
514+ updated = None
494515
495516 try :
496517 if self .local :
@@ -753,66 +774,67 @@ def difference(self, bundle):
753774 return [k for k in self ._keys if k not in bundle ]
754775
755776 def dump (self , exclude_attributes : Optional [List [str ]] = None ):
756- _keys = []
757- for _k in self ._keys :
758- _ser = _k .to_dict ()
759- if _k .inactive_since :
760- _ser ["inactive_since" ] = _k .inactive_since
761- _keys .append (_ser )
762-
763- res = {
764- "keys" : _keys ,
765- "cache_time" : self .cache_time ,
766- "etag" : self .etag ,
767- "fileformat" : self .fileformat ,
768- "httpc_params" : self .httpc_params ,
769- "ignore_errors_period" : self .ignore_errors_period ,
770- "ignore_errors_until" : self .ignore_errors_until ,
771- "ignore_invalid_keys" : self .ignore_invalid_keys ,
772- "imp_jwks" : self .imp_jwks ,
773- "keytype" : self .keytype ,
774- "keyusage" : self .keyusage ,
775- "last_local" : self .last_local ,
776- "last_remote" : self .last_remote ,
777- "last_updated" : self .last_updated ,
778- "local" : self .local ,
779- "remote" : self .remote ,
780- "time_out" : self .time_out ,
781- }
782-
783- if self .source :
784- res ["source" ] = self .source
785-
786- if exclude_attributes :
787- for attr in exclude_attributes :
788- try :
789- del res [attr ]
790- except KeyError :
791- pass
777+ if exclude_attributes is None :
778+ exclude_attributes = []
779+
780+ res = {}
781+
782+ if "keys" not in exclude_attributes :
783+ _keys = []
784+ for _k in self ._keys :
785+ _ser = _k .to_dict ()
786+ if _k .inactive_since :
787+ _ser ["inactive_since" ] = _k .inactive_since
788+ _keys .append (_ser )
789+ res ["keys" ] = _keys
790+
791+ for attr , default in self .params .items ():
792+ if attr in exclude_attributes :
793+ continue
794+ val = getattr (self , attr )
795+ res [attr ] = val
796+
797+ # res = {
798+ # "cache_time": self.cache_time,
799+ # "etag": self.etag,
800+ # "fileformat": self.fileformat,
801+ # "httpc_params": self.httpc_params,
802+ # "ignore_errors_period": self.ignore_errors_period,
803+ # "ignore_errors_until": self.ignore_errors_until,
804+ # "ignore_invalid_keys": self.ignore_invalid_keys,
805+ # "imp_jwks": self.imp_jwks,
806+ # "keytype": self.keytype,
807+ # "keyusage": self.keyusage,
808+ # "last_local": self.last_local,
809+ # "last_remote": self.last_remote,
810+ # "last_updated": self.last_updated,
811+ # "local": self.local,
812+ # "remote": self.remote,
813+ # "time_out": self.time_out,
814+ # }
815+
816+ # if self.source:
817+ # res["source"] = self.source
792818
793819 return res
794820
795821 def load (self , spec ):
822+ """
823+ Sets attributes according to a specification.
824+ Does not overwrite an existing attributes value with a default value.
825+
826+ :param spec: Dictionary with attributes and value to populate the instance with
827+ :return: The instance itself
828+ """
796829 _keys = spec .get ("keys" , [])
797830 if _keys :
798831 self .do_keys (_keys )
799- self .cache_time = spec .get ("cache_time" , 0 )
800- self .etag = spec .get ("etag" , "" )
801- self .fileformat = spec .get ("fileformat" , "jwks" )
802- self .httpc_params = spec .get ("httpc_params" , {})
803- self .ignore_errors_period = spec .get ("ignore_errors_period" , 0 )
804- self .ignore_errors_until = spec .get ("ignore_errors_until" , None )
805- self .ignore_invalid_keys = spec .get ("ignore_invalid_keys" , True )
806- self .imp_jwks = spec .get ("imp_jwks" , None )
807- self .keytype = (spec .get ("keytype" , "RSA" ),)
808- self .keyusage = (spec .get ("keyusage" , None ),)
809- self .last_local = spec .get ("last_local" , None )
810- self .last_remote = spec .get ("last_remote" , None )
811- self .last_updated = spec .get ("last_updated" , 0 )
812- self .local = spec .get ("local" , False )
813- self .remote = spec .get ("remote" , False )
814- self .source = spec .get ("source" , None )
815- self .time_out = spec .get ("time_out" , 0 )
832+
833+ for attr , default in self .params .items ():
834+ val = spec .get (attr )
835+ if val :
836+ setattr (self , attr , val )
837+
816838 return self
817839
818840 def flush (self ):
0 commit comments