|
1 | 1 | import json |
2 | 2 | import logging |
3 | | -import os |
4 | 3 | from typing import List |
5 | 4 | from typing import Optional |
6 | 5 |
|
|
9 | 8 | from .jwe.jwe import alg2keytype as jwe_alg2keytype |
10 | 9 | from .jws.utils import alg2keytype as jws_alg2keytype |
11 | 10 | from .key_bundle import KeyBundle |
12 | | -from .key_bundle import key_diff |
13 | | -from .key_bundle import update_key_bundle |
14 | | - |
15 | | -__author__ = 'Roland Hedberg' |
16 | | - |
17 | 11 | from .key_issuer import KeyIssuer |
18 | 12 | from .key_issuer import build_keyissuer |
| 13 | +from .key_issuer import init_key_issuer |
19 | 14 | from .utils import importer |
20 | 15 | from .utils import qualified_name |
21 | 16 |
|
| 17 | +__author__ = 'Roland Hedberg' |
| 18 | + |
22 | 19 | logger = logging.getLogger(__name__) |
23 | 20 |
|
24 | 21 |
|
@@ -92,7 +89,7 @@ def _get_issuer(self, issuer_id: str) -> Optional[KeyIssuer]: |
92 | 89 |
|
93 | 90 | return self._issuers.get(issuer_id) |
94 | 91 |
|
95 | | - def _add_issuer(self, issuer_id): |
| 92 | + def _add_issuer(self, issuer_id) -> KeyIssuer: |
96 | 93 | _iss = KeyIssuer(ca_certs=self.ca_certs, name=issuer_id, |
97 | 94 | keybundle_cls=self.keybundle_cls, |
98 | 95 | remove_after=self.remove_after, |
@@ -197,51 +194,53 @@ def get(self, key_use, key_type="", issuer_id="", kid=None, **kwargs): |
197 | 194 | if _issuer is None: |
198 | 195 | return [] |
199 | 196 |
|
200 | | - lst = [] |
201 | | - for bundle in _issuer: |
202 | | - if key_type: |
203 | | - if key_use in ['ver', 'dec']: |
204 | | - _bkeys = bundle.get(key_type, only_active=False) |
205 | | - else: |
206 | | - _bkeys = bundle.get(key_type) |
207 | | - else: |
208 | | - _bkeys = bundle.keys() |
209 | | - for key in _bkeys: |
210 | | - if key.inactive_since and key_use != "sig": |
211 | | - # Skip inactive keys unless for signature verification |
212 | | - continue |
213 | | - if not key.use or use == key.use: |
214 | | - if kid: |
215 | | - if key.kid == kid: |
216 | | - lst.append(key) |
217 | | - break |
218 | | - else: |
219 | | - continue |
220 | | - else: |
221 | | - lst.append(key) |
222 | | - |
223 | | - # if elliptic curve, have to check if I have a key of the right curve |
224 | | - if key_type == "EC" and "alg" in kwargs: |
225 | | - name = "P-{}".format(kwargs["alg"][2:]) # the type |
226 | | - _lst = [] |
227 | | - for key in lst: |
228 | | - if name != key.crv: |
229 | | - continue |
230 | | - _lst.append(key) |
231 | | - lst = _lst |
232 | | - |
233 | | - if use == 'enc' and key_type == 'oct' and issuer_id != '': |
234 | | - # Add my symmetric keys |
235 | | - _issuer = self._get_issuer('') |
236 | | - if _issuer: |
237 | | - for kb in _issuer: |
238 | | - for key in kb.get(key_type): |
239 | | - if key.inactive_since: |
240 | | - continue |
241 | | - if not key.use or key.use == use: |
242 | | - lst.append(key) |
243 | | - |
244 | | - return lst |
| 197 | + return _issuer.get(key_use=key_use, key_type=key_type, kid=kid, **kwargs) |
| 198 | + |
| 199 | + # lst = [] |
| 200 | + # for bundle in _issuer: |
| 201 | + # if key_type: |
| 202 | + # if key_use in ['ver', 'dec']: |
| 203 | + # _bkeys = bundle.get(key_type, only_active=False) |
| 204 | + # else: |
| 205 | + # _bkeys = bundle.get(key_type) |
| 206 | + # else: |
| 207 | + # _bkeys = bundle.keys() |
| 208 | + # for key in _bkeys: |
| 209 | + # if key.inactive_since and key_use != "sig": |
| 210 | + # # Skip inactive keys unless for signature verification |
| 211 | + # continue |
| 212 | + # if not key.use or use == key.use: |
| 213 | + # if kid: |
| 214 | + # if key.kid == kid: |
| 215 | + # lst.append(key) |
| 216 | + # break |
| 217 | + # else: |
| 218 | + # continue |
| 219 | + # else: |
| 220 | + # lst.append(key) |
| 221 | + # |
| 222 | + # # if elliptic curve, have to check if I have a key of the right curve |
| 223 | + # if key_type == "EC" and "alg" in kwargs: |
| 224 | + # name = "P-{}".format(kwargs["alg"][2:]) # the type |
| 225 | + # _lst = [] |
| 226 | + # for key in lst: |
| 227 | + # if name != key.crv: |
| 228 | + # continue |
| 229 | + # _lst.append(key) |
| 230 | + # lst = _lst |
| 231 | + # |
| 232 | + # if use == 'enc' and key_type == 'oct' and issuer_id != '': |
| 233 | + # # Add my symmetric keys |
| 234 | + # _issuer = self._get_issuer('') |
| 235 | + # if _issuer: |
| 236 | + # for kb in _issuer: |
| 237 | + # for key in kb.get(key_type): |
| 238 | + # if key.inactive_since: |
| 239 | + # continue |
| 240 | + # if not key.use or key.use == use: |
| 241 | + # lst.append(key) |
| 242 | + # |
| 243 | + # return lst |
245 | 244 |
|
246 | 245 | def get_signing_key(self, key_type="", issuer_id="", kid=None, **kwargs): |
247 | 246 | """ |
@@ -472,12 +471,7 @@ def __eq__(self, other): |
472 | 471 |
|
473 | 472 | # Keys per issuer must be the same |
474 | 473 | for iss in self.owners(): |
475 | | - sk = self.get_issuer_keys(iss) |
476 | | - ok = other.get_issuer_keys(iss) |
477 | | - if len(sk) != len(ok): |
478 | | - return False |
479 | | - |
480 | | - if not any(k in ok for k in sk): |
| 474 | + if self[iss] != other[iss]: |
481 | 475 | return False |
482 | 476 |
|
483 | 477 | return True |
@@ -825,73 +819,9 @@ def init_key_jar(public_path='', private_path='', key_defs='', issuer_id='', rea |
825 | 819 | :return: An instantiated :py:class;`oidcmsg.key_jar.KeyJar` instance |
826 | 820 | """ |
827 | 821 |
|
828 | | - if private_path: |
829 | | - if os.path.isfile(private_path): |
830 | | - _jwks = open(private_path, 'r').read() |
831 | | - _issuer = KeyIssuer(name=issuer_id) |
832 | | - _issuer.import_jwks(json.loads(_jwks)) |
833 | | - if key_defs: |
834 | | - _kb = _issuer[0] |
835 | | - _diff = key_diff(_kb, key_defs) |
836 | | - if _diff: |
837 | | - update_key_bundle(_kb, _diff) |
838 | | - if read_only: |
839 | | - logger.error('Not allowed to write to disc!') |
840 | | - else: |
841 | | - _issuer.set([_kb]) |
842 | | - jwks = _issuer.export_jwks(private=True) |
843 | | - fp = open(private_path, 'w') |
844 | | - fp.write(json.dumps(jwks)) |
845 | | - fp.close() |
846 | | - else: |
847 | | - _issuer = build_keyissuer(key_defs, issuer_id=issuer_id) |
848 | | - if not read_only: |
849 | | - jwks = _issuer.export_jwks(private=True) |
850 | | - head, tail = os.path.split(private_path) |
851 | | - if head and not os.path.isdir(head): |
852 | | - os.makedirs(head) |
853 | | - fp = open(private_path, 'w') |
854 | | - fp.write(json.dumps(jwks)) |
855 | | - fp.close() |
856 | | - |
857 | | - if public_path and not read_only: |
858 | | - jwks = _issuer.export_jwks() # public part |
859 | | - head, tail = os.path.split(public_path) |
860 | | - if head and not os.path.isdir(head): |
861 | | - os.makedirs(head) |
862 | | - fp = open(public_path, 'w') |
863 | | - fp.write(json.dumps(jwks)) |
864 | | - fp.close() |
865 | | - elif public_path: |
866 | | - if os.path.isfile(public_path): |
867 | | - _jwks = open(public_path, 'r').read() |
868 | | - _issuer = KeyIssuer(name=issuer_id) |
869 | | - _issuer.import_jwks(json.loads(_jwks)) |
870 | | - if key_defs: |
871 | | - _kb = _issuer[0] |
872 | | - _diff = key_diff(_kb, key_defs) |
873 | | - if _diff: |
874 | | - if read_only: |
875 | | - logger.error('Not allowed to write to disc!') |
876 | | - else: |
877 | | - update_key_bundle(_kb, _diff) |
878 | | - _issuer.set([_kb]) |
879 | | - jwks = _issuer.export_jwks() |
880 | | - fp = open(public_path, 'w') |
881 | | - fp.write(json.dumps(jwks)) |
882 | | - fp.close() |
883 | | - else: |
884 | | - _issuer = build_keyissuer(key_defs, issuer_id=issuer_id) |
885 | | - if not read_only: |
886 | | - _jwks = _issuer.export_jwks(issuer=issuer_id) |
887 | | - head, tail = os.path.split(public_path) |
888 | | - if head and not os.path.isdir(head): |
889 | | - os.makedirs(head) |
890 | | - fp = open(public_path, 'w') |
891 | | - fp.write(json.dumps(_jwks)) |
892 | | - fp.close() |
893 | | - else: |
894 | | - _issuer = build_keyissuer(key_defs, issuer_id=issuer_id) |
| 822 | + _issuer = init_key_issuer(public_path=public_path, private_path=private_path, |
| 823 | + key_defs=key_defs, read_only=read_only, |
| 824 | + storage_conf=storage_conf, abstract_storage_cls=abstract_storage_cls) |
895 | 825 |
|
896 | 826 | if _issuer is None: |
897 | 827 | raise ValueError('Could not find any keys') |
|
0 commit comments