77from .jws .utils import alg2keytype as jws_alg2keytype
88from .jwe .jwe import alg2keytype as jwe_alg2keytype
99
10- from .exception import DeSerializationNotPossible
10+ from .key_bundle import build_key_bundle , key_diff , update_key_bundle
1111from .key_bundle import KeyBundle
12- from .key_bundle import ec_init
13- from .key_bundle import rsa_init
1412
1513
1614__author__ = 'Roland Hedberg'
@@ -287,7 +285,7 @@ def __contains__(self, item):
287285 else :
288286 return False
289287
290- def __getitem__ (self , owner ):
288+ def __getitem__ (self , owner = '' ):
291289 """
292290 Get all the key bundles that belong to an entity.
293291
@@ -651,8 +649,8 @@ def build_keyjar(key_conf, kid_template="", keyjar=None, owner=''):
651649
652650 keys = [
653651 {"type": "RSA", "key": "cp_keys/key.pem", "use": ["enc", "sig"]},
654- {"type": "EC", "crv": "P-256", "use": ["sig"]},
655- {"type": "EC", "crv": "P-256", "use": ["enc"]}
652+ {"type": "EC", "crv": "P-256", "use": ["sig"], "kid": "ec.1" },
653+ {"type": "EC", "crv": "P-256", "use": ["enc"], "kid": "ec.2" }
656654 ]
657655
658656 Keys in this specification are:
@@ -671,6 +669,10 @@ def build_keyjar(key_conf, kid_template="", keyjar=None, owner=''):
671669 The elliptic curve that should be used. Only applies to elliptic curve
672670 keys :-)
673671
672+ kid
673+ Key ID, can only be used with one usage type is specified. If there
674+ are more the one usage type specified 'kid' will just be ignored.
675+
674676 :param key_conf: The key configuration
675677 :param kid_template: A template by which to build the key IDs. If no
676678 kid_template is given then the built-in function add_kid() will be used.
@@ -682,39 +684,7 @@ def build_keyjar(key_conf, kid_template="", keyjar=None, owner=''):
682684 if keyjar is None :
683685 keyjar = KeyJar ()
684686
685- kid = 0
686-
687- tot_kb = KeyBundle ()
688- for spec in key_conf :
689- typ = spec ["type" ].upper ()
690-
691- if typ == "RSA" :
692- if "key" in spec :
693- error_to_catch = (OSError , IOError ,
694- DeSerializationNotPossible )
695- try :
696- kb = KeyBundle (source = "file://%s" % spec ["key" ],
697- fileformat = "der" ,
698- keytype = typ , keyusage = spec ["use" ])
699- except error_to_catch :
700- kb = rsa_init (spec )
701- except Exception :
702- raise
703- else :
704- kb = rsa_init (spec )
705- elif typ == "EC" :
706- kb = ec_init (spec )
707- else :
708- continue
709-
710- for k in kb .keys ():
711- if kid_template :
712- k .kid = kid_template % kid
713- kid += 1
714- else :
715- k .add_kid ()
716-
717- tot_kb .extend (kb .keys ())
687+ tot_kb = build_key_bundle (key_conf , kid_template )
718688
719689 keyjar .add_kb (owner , tot_kb )
720690
@@ -804,6 +774,16 @@ def init_key_jar(public_path='', private_path='', key_defs='', owner=''):
804774 _jwks = open (private_path , 'r' ).read ()
805775 _kj = KeyJar ()
806776 _kj .import_jwks (json .loads (_jwks ), owner )
777+ if key_defs :
778+ _kb = _kj .issuer_keys [owner ][0 ]
779+ _diff = key_diff (_kb , key_defs )
780+ if _diff :
781+ update_key_bundle (_kb , _diff )
782+ _kj .issuer_keys [owner ] = [_kb ]
783+ jwks = _kj .export_jwks (private = True , issuer = owner )
784+ fp = open (private_path , 'w' )
785+ fp .write (json .dumps (jwks ))
786+ fp .close ()
807787 else :
808788 _kj = build_keyjar (key_defs , owner = owner )
809789 jwks = _kj .export_jwks (private = True , issuer = owner )
@@ -824,6 +804,16 @@ def init_key_jar(public_path='', private_path='', key_defs='', owner=''):
824804 _jwks = open (public_path , 'r' ).read ()
825805 _kj = KeyJar ()
826806 _kj .import_jwks (json .loads (_jwks ), owner )
807+ if key_defs :
808+ _kb = _kj .issuer_keys [owner ][0 ]
809+ _diff = key_diff (_kb , key_defs )
810+ if _diff :
811+ update_key_bundle (_kb , _diff )
812+ _kj .issuer_keys [owner ] = [_kb ]
813+ jwks = _kj .export_jwks (issuer = owner )
814+ fp = open (private_path , 'w' )
815+ fp .write (json .dumps (jwks ))
816+ fp .close ()
827817 else :
828818 _kj = build_keyjar (key_defs , owner = owner )
829819 _jwks = _kj .export_jwks (issuer = owner )
0 commit comments