|
| 1 | +from typing import Optional |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +from cryptojwt import KeyJar |
| 5 | + |
| 6 | +from idpyoidc.configure import Configuration |
| 7 | +from idpyoidc.node import Unit |
| 8 | +from idpyoidc.server.util import execute |
| 9 | + |
| 10 | + |
| 11 | +class Combo(Unit): |
| 12 | + name = 'root' |
| 13 | + |
| 14 | + def __init__(self, |
| 15 | + config: Union[dict, Configuration], |
| 16 | + httpc: Optional[object] = None, |
| 17 | + entity_id: Optional[str] = '', |
| 18 | + keyjar: Optional[Union[KeyJar, bool]] = None, |
| 19 | + httpc_params: Optional[dict] = None |
| 20 | + ): |
| 21 | + self.entity_id = entity_id or config.get('entity_id') |
| 22 | + if not httpc_params: |
| 23 | + httpc_params = self._get_httpc_params(config) |
| 24 | + |
| 25 | + Unit.__init__(self, config=config, httpc=httpc, issuer_id=self.entity_id, keyjar=keyjar) |
| 26 | + self._part = {} |
| 27 | + for key, spec in config.items(): |
| 28 | + if isinstance(spec, dict) and 'class' in spec: |
| 29 | + if httpc_params: |
| 30 | + self._add_httpc_params(spec, httpc_params) |
| 31 | + self._part[key] = execute(spec, upstream_get=self.unit_get, |
| 32 | + entity_id=self.entity_id, httpc=httpc) |
| 33 | + |
| 34 | + def _get_httpc_params(self, config): |
| 35 | + return config.get("httpc_params") |
| 36 | + |
| 37 | + def _add_httpc_params(self, spec, httpc_params): |
| 38 | + spec_kwargs = spec.get("kwargs", {}) |
| 39 | + if "config" in spec_kwargs: |
| 40 | + if httpc_params and "httpc_params" not in spec_kwargs["config"]: |
| 41 | + spec_kwargs["config"]["httpc_params"] = httpc_params |
| 42 | + else: |
| 43 | + if "httpc_params" not in spec_kwargs: |
| 44 | + spec_kwargs["httpc_params"] = httpc_params |
| 45 | + |
| 46 | + def __getitem__(self, item): |
| 47 | + if item in self._part: |
| 48 | + return self._part[item] |
| 49 | + else: |
| 50 | + return None |
| 51 | + |
| 52 | + def __setitem__(self, key, value): |
| 53 | + self._part[key] = value |
| 54 | + |
| 55 | + def get_entity_types(self): |
| 56 | + return list(self._part.keys()) |
| 57 | + |
| 58 | + def keys(self): |
| 59 | + return self._part.keys() |
| 60 | + |
| 61 | + def items(self): |
| 62 | + return self._part.items() |
0 commit comments