|
| 1 | +import re |
| 2 | + |
| 3 | +from .base import ResponseMicroService |
| 4 | +from ..exception import SATOSAAuthenticationError |
| 5 | + |
| 6 | +def _filters(f, requester, provider): |
| 7 | + pf = f.get(provider, f.get("", f.get("default", {}))) |
| 8 | + rf = pf.get(requester, pf.get("", pf.get("default", {}))) |
| 9 | + return rf.items() |
| 10 | + |
| 11 | +class AttributeAuthorization(ResponseMicroService): |
| 12 | + |
| 13 | + def __init__(self, config, *args, **kwargs): |
| 14 | + super().__init__(*args, **kwargs) |
| 15 | + self.attribute_allow = config.get("attribute_allow", {}) |
| 16 | + self.attribute_deny = config.get("attribute_deny", {}) |
| 17 | + |
| 18 | + def _check_authz(self, context, attributes, requester, provider): |
| 19 | + for attribute_name, attribute_filter in _filters(self.attribute_allow, requester, provider): |
| 20 | + regex = re.compile(attribute_filter) |
| 21 | + if attribute_name in attributes: |
| 22 | + print(repr(regex)) |
| 23 | + print(list(filter(regex.search, attributes[attribute_name]))) |
| 24 | + if not list(filter(regex.search, attributes[attribute_name])): |
| 25 | + raise SATOSAAuthenticationError(context.state, "Permission denied") |
| 26 | + |
| 27 | + for attribute_name, attribute_filter in _filters(self.attribute_deny, requester, provider): |
| 28 | + regex = re.compile(attribute_filter) |
| 29 | + if attribute_name in attributes: |
| 30 | + if len(list(filter(regex.search, attributes[attribute_name]))) != len(attributes[attribute_name]): |
| 31 | + raise SATOSAAuthenticationError(context.state, "Permission denied") |
| 32 | + |
| 33 | + def process(self, context, data): |
| 34 | + self._check_authz(context, data.attributes, data.requester, data.auth_info.issuer) |
| 35 | + return super().process(context, data) |
0 commit comments