|
| 1 | +from satosa.micro_services.base import ResponseMicroService |
| 2 | +from satosa.response import Redirect |
| 3 | + |
| 4 | +class AttributeCheck(ResponseMicroService): |
| 5 | + """ |
| 6 | + A microservice that performs simple presence checks on response attributes. |
| 7 | +
|
| 8 | + Example configuration: |
| 9 | +
|
| 10 | + ```yaml |
| 11 | + config: |
| 12 | + mandatory_attributes: |
| 13 | + - sub |
| 14 | + redirect: http://error.domain.tld/services?errorType=missing_attributes&attributes={attributes} |
| 15 | + ``` |
| 16 | +
|
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, config, *args, **kwargs): |
| 20 | + super().__init__(*args, **kwargs) |
| 21 | + self.internal_attributes = kwargs["internal_attributes"] |
| 22 | + self.mandatory_attributes = config.get("mandatory_attributes", {}) |
| 23 | + self.redirect_url = config.get("redirect_url", {}) |
| 24 | + |
| 25 | + def process(self, context, data): |
| 26 | + """ |
| 27 | + Manage consent and attribute filtering |
| 28 | +
|
| 29 | + :type context: satosa.context.Context |
| 30 | + :type data: satosa.internal.InternalData |
| 31 | + :rtype: satosa.response.Response |
| 32 | +
|
| 33 | + :param context: response context |
| 34 | + :param data: the response |
| 35 | + :return: response |
| 36 | + """ |
| 37 | + missing_attributes = [] |
| 38 | + for attribute in self.mandatory_attributes: |
| 39 | + values = data.attributes.get(attribute) |
| 40 | + if values is None: |
| 41 | + missing_attributes.append(self.internal_attributes["attributes"][attribute]["saml"]) |
| 42 | + |
| 43 | + if missing_attributes: |
| 44 | + parameters = [] |
| 45 | + for missing_attribute in missing_attributes: |
| 46 | + parameters.append("attributes[]={}".format(", ".join(missing_attribute))) |
| 47 | + |
| 48 | + if "?" in self.redirect_url: |
| 49 | + # query string already present |
| 50 | + url = self.redirect_url + "&" + "&".join(parameters) |
| 51 | + else: |
| 52 | + # no query string |
| 53 | + url = self.redirect_url + "?" + "&".join(parameters) |
| 54 | + |
| 55 | + return Redirect(url) |
| 56 | + else: |
| 57 | + return super().process(context, data) |
0 commit comments