|
| 1 | +from ..attribute_processor import AttributeProcessorError, AttributeProcessorWarning |
| 2 | +from .base_processor import BaseProcessor |
| 3 | + |
| 4 | + |
| 5 | +CONFIG_KEY_MAPPEDATTRIBUTE = 'mapped_attribute' |
| 6 | +CONFIG_DEFAULT_MAPPEDATTRIBUTE = '' |
| 7 | + |
| 8 | + |
| 9 | +class ScopeExtractorProcessor(BaseProcessor): |
| 10 | + """ |
| 11 | + Extracts the scope from a scoped attribute and maps that to |
| 12 | + another attribute |
| 13 | +
|
| 14 | + Example configuration: |
| 15 | + module: satosa.micro_services.attribute_processor.AttributeProcessor |
| 16 | + name: AttributeProcessor |
| 17 | + config: |
| 18 | + process: |
| 19 | + - attribute: scoped_affiliation |
| 20 | + processors: |
| 21 | + - name: ScopeExtractorProcessor |
| 22 | + module: satosa.micro_services.processors.scope_extractor_processor |
| 23 | + mapped_attribute: domain |
| 24 | + """ |
| 25 | + def process(self, internal_data, attribute, **kwargs): |
| 26 | + mapped_attribute = kwargs.get(CONFIG_KEY_MAPPEDATTRIBUTE, CONFIG_DEFAULT_MAPPEDATTRIBUTE) |
| 27 | + if mapped_attribute is None or mapped_attribute == '': |
| 28 | + raise AttributeProcessorError("The mapped_attribute needs to be set") |
| 29 | + |
| 30 | + attributes = internal_data.attributes |
| 31 | + values = attributes.get(attribute, []) |
| 32 | + if not values: |
| 33 | + raise AttributeProcessorWarning("Cannot apply scope_extractor to {}, it has no values".format(attribute)) |
| 34 | + if not any('@' in val for val in values): |
| 35 | + raise AttributeProcessorWarning("Cannot apply scope_extractor to {}, it's values are not scoped".format(attribute)) |
| 36 | + for value in values: |
| 37 | + if '@' in value: |
| 38 | + scope = value.split('@')[1] |
| 39 | + attributes[mapped_attribute] = [scope] |
| 40 | + break |
0 commit comments