|
| 1 | +import logging |
| 2 | +from urllib.parse import parse_qs |
| 3 | + |
| 4 | +from .base import RequestMicroService |
| 5 | +from ..exception import SATOSAConfigurationError |
| 6 | +from ..exception import SATOSAError |
| 7 | + |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +class IdpHintingError(SATOSAError): |
| 13 | + """ |
| 14 | + SATOSA exception raised by IdpHinting microservice |
| 15 | + """ |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +class IdpHinting(RequestMicroService): |
| 20 | + """ |
| 21 | + Detect if an idp hinting feature have been requested |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, config, *args, **kwargs): |
| 25 | + """ |
| 26 | + Constructor. |
| 27 | + :param config: microservice configuration |
| 28 | + :type config: Dict[str, Dict[str, str]] |
| 29 | + """ |
| 30 | + super().__init__(*args, **kwargs) |
| 31 | + try: |
| 32 | + self.idp_hint_param_names = config['allowed_params'] |
| 33 | + except KeyError: |
| 34 | + raise SATOSAConfigurationError( |
| 35 | + f"{self.__class__.__name__} can't find allowed_params" |
| 36 | + ) |
| 37 | + |
| 38 | + def process(self, context, data): |
| 39 | + """ |
| 40 | + This intercepts if idp_hint paramenter is in use |
| 41 | + :param context: request context |
| 42 | + :param data: the internal request |
| 43 | + """ |
| 44 | + target_entity_id = context.get_decoration(context.KEY_TARGET_ENTITYID) |
| 45 | + qs_raw = context._http_headers['QUERY_STRING'] |
| 46 | + if target_entity_id or not qs_raw: |
| 47 | + return super().process(context, data) |
| 48 | + |
| 49 | + qs = parse_qs(qs_raw) |
| 50 | + hints = ( |
| 51 | + entity_id |
| 52 | + for param in self.idp_hint_param_names |
| 53 | + for entity_id in qs.get(param, [None]) |
| 54 | + if entity_id |
| 55 | + ) |
| 56 | + hint = next(hints, None) |
| 57 | + |
| 58 | + context.decorate(context.KEY_TARGET_ENTITYID, hint) |
| 59 | + return super().process(context, data) |
0 commit comments