Skip to content

Commit 1b58acd

Browse files
new: FilterAttributeValues: allow more filter types besides regexp
Introduce an extended filter notation where for each attribute, instead of a single value with a regexp, the filter can be a dict indexed by filter type, with (optional) filter value. Define `regexp` filter matching existing behaviour. Support existing syntax (regexp as direct filter value) by mapping it to a dict explictily pointing to regexp filter.
1 parent ad6154e commit 1b58acd

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/satosa/micro_services/attribute_modifications.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22

33
from .base import ResponseMicroService
4+
from ..exception import SATOSAError
45

56

67
class AddStaticAttributes(ResponseMicroService):
@@ -40,17 +41,27 @@ def process(self, context, data):
4041
def _apply_requester_filters(self, attributes, provider_filters, requester):
4142
# apply default requester filters
4243
default_requester_filters = provider_filters.get("", {})
43-
self._apply_filter(attributes, default_requester_filters)
44+
self._apply_filters(attributes, default_requester_filters)
4445

4546
# apply requester specific filters
4647
requester_filters = provider_filters.get(requester, {})
47-
self._apply_filter(attributes, requester_filters)
48-
49-
def _apply_filter(self, attributes, attribute_filters):
50-
for attribute_name, attribute_filter in attribute_filters.items():
51-
regex = re.compile(attribute_filter)
52-
if attribute_name == "": # default filter for all attributes
53-
for attribute, values in attributes.items():
54-
attributes[attribute] = list(filter(regex.search, attributes[attribute]))
55-
elif attribute_name in attributes:
56-
attributes[attribute_name] = list(filter(regex.search, attributes[attribute_name]))
48+
self._apply_filters(attributes, requester_filters)
49+
50+
def _apply_filters(self, attributes, attribute_filters):
51+
for attribute_name, attribute_filters in attribute_filters.items():
52+
if type(attribute_filters) == str:
53+
# convert simple notation to filter list
54+
attribute_filters = {'regexp': attribute_filters}
55+
56+
for filter_type, filter_value in attribute_filters.items():
57+
58+
if filter_type == "regexp":
59+
filter_func = re.compile(filter_value).search
60+
else:
61+
raise SATOSAError("Unknown filter type")
62+
63+
if attribute_name == "": # default filter for all attributes
64+
for attribute, values in attributes.items():
65+
attributes[attribute] = list(filter(filter_func, attributes[attribute]))
66+
elif attribute_name in attributes:
67+
attributes[attribute_name] = list(filter(filter_func, attributes[attribute_name]))

0 commit comments

Comments
 (0)