1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\core\Auth\Process; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\{Auth, Error, Logger}; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
|
10
|
|
|
use function array_intersect; |
11
|
|
|
use function array_key_exists; |
12
|
|
|
use function array_merge; |
13
|
|
|
use function array_unique; |
14
|
|
|
use function count; |
15
|
|
|
use function is_array; |
16
|
|
|
use function is_int; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Filter to create target attribute based on value(s) in source attribute |
20
|
|
|
* |
21
|
|
|
* @package SimpleSAMLphp |
22
|
|
|
*/ |
23
|
|
|
class AttributeValueMap extends Auth\ProcessingFilter |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The name of the attribute we should assign values to (ie: the target attribute). |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private string $targetattribute = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The name of the attribute we should create values from. |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private string $sourceattribute = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The required $sourceattribute values and target affiliations. |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private array $values = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Whether $sourceattribute should be kept or not. |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
private bool $keep = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Whether $target attribute values should be replaced by new values or not. |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
private bool $replace = false; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initialize the filter. |
58
|
|
|
* |
59
|
|
|
* @param array &$config Configuration information about this filter. |
60
|
|
|
* @param mixed $reserved For future use. |
61
|
|
|
* @throws \SimpleSAML\Error\Exception If the configuration is not valid. |
62
|
|
|
*/ |
63
|
|
|
public function __construct(array &$config, $reserved) |
64
|
|
|
{ |
65
|
|
|
parent::__construct($config, $reserved); |
66
|
|
|
|
67
|
|
|
// parse configuration |
68
|
|
|
foreach ($config as $name => $value) { |
69
|
|
|
if (is_int($name)) { |
70
|
|
|
// check if this is an option |
71
|
|
|
if ($value === '%replace') { |
72
|
|
|
$this->replace = true; |
73
|
|
|
} elseif ($value === '%keep') { |
74
|
|
|
$this->keep = true; |
75
|
|
|
} else { |
76
|
|
|
// unknown configuration option, log it and ignore the error |
77
|
|
|
Logger::warning( |
78
|
|
|
"AttributeValueMap: unknown configuration flag '" . var_export($value, true) . "'" |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
continue; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// set the target attribute |
85
|
|
|
if ($name === 'targetattribute') { |
86
|
|
|
$this->targetattribute = $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// set the source attribute |
90
|
|
|
if ($name === 'sourceattribute') { |
91
|
|
|
$this->sourceattribute = $value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// set the values |
95
|
|
|
if ($name === 'values') { |
96
|
|
|
$this->values = $value; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// now validate it |
101
|
|
|
if (empty($this->sourceattribute)) { |
102
|
|
|
throw new Error\Exception("AttributeValueMap: 'sourceattribute' configuration option not set."); |
103
|
|
|
} |
104
|
|
|
if (empty($this->targetattribute)) { |
105
|
|
|
throw new Error\Exception("AttributeValueMap: 'targetattribute' configuration option not set."); |
106
|
|
|
} |
107
|
|
|
if (!is_array($this->values)) { |
|
|
|
|
108
|
|
|
throw new Error\Exception("AttributeValueMap: 'values' configuration option is not an array."); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Apply filter. |
115
|
|
|
* |
116
|
|
|
* @param array &$state The current request |
117
|
|
|
*/ |
118
|
|
|
public function process(array &$state): void |
119
|
|
|
{ |
120
|
|
|
Logger::debug('Processing the AttributeValueMap filter.'); |
121
|
|
|
|
122
|
|
|
Assert::keyExists($state, 'Attributes'); |
123
|
|
|
$attributes = &$state['Attributes']; |
124
|
|
|
|
125
|
|
|
if (!array_key_exists($this->sourceattribute, $attributes)) { |
126
|
|
|
// the source attribute does not exist, nothing to do here |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$sourceattribute = $attributes[$this->sourceattribute]; |
131
|
|
|
$targetvalues = []; |
132
|
|
|
|
133
|
|
|
if (is_array($sourceattribute)) { |
134
|
|
|
foreach ($this->values as $value => $values) { |
135
|
|
|
if (!is_array($values)) { |
136
|
|
|
$values = [$values]; |
137
|
|
|
} |
138
|
|
|
if (count(array_intersect($values, $sourceattribute)) > 0) { |
139
|
|
|
Logger::debug("AttributeValueMap: intersect match for '$value'"); |
140
|
|
|
$targetvalues[] = $value; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (count($targetvalues) > 0) { |
146
|
|
|
if ($this->replace || !array_key_exists($this->targetattribute, $attributes)) { |
147
|
|
|
$attributes[$this->targetattribute] = $targetvalues; |
148
|
|
|
} else { |
149
|
|
|
$attributes[$this->targetattribute] = array_unique(array_merge( |
150
|
|
|
$attributes[$this->targetattribute], |
151
|
|
|
$targetvalues |
152
|
|
|
)); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (!$this->keep) { |
157
|
|
|
// no need to keep the source attribute |
158
|
|
|
unset($attributes[$this->sourceattribute]); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|