-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAuthorize.php
More file actions
238 lines (208 loc) · 7.6 KB
/
Authorize.php
File metadata and controls
238 lines (208 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\authorize\Auth\Process;
use Exception;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Auth;
use SimpleSAML\Module;
use SimpleSAML\Utils;
use function array_diff;
use function array_key_exists;
use function array_keys;
use function array_push;
use function implode;
use function is_array;
use function is_bool;
use function is_string;
use function preg_match;
use function var_export;
/**
* Filter to authorize only certain users.
* See docs directory.
*
* @package SimpleSAMLphp
*/
class Authorize extends Auth\ProcessingFilter
{
/**
* Flag to deny/unauthorize the user a attribute filter IS found
*
* @var bool
*/
protected bool $deny = false;
/**
* Flag to turn the REGEX pattern matching on or off
*
* @var bool
*/
protected bool $regex = true;
/**
* Array of localised rejection messages
*
* @var string[]
*/
protected array $reject_msg = [];
/**
* Flag to toggle generation of errorURL
*
* @var bool
*/
protected bool $errorURL = true;
/**
* Array of valid users. Each element is a regular expression. You should
* use \ to escape special chars, like '.' etc.
*
* @var array<mixed>
*/
protected array $valid_attribute_values = [];
/**
* Flag to allow re-authentication when user is not authorized
* @var bool
*/
protected bool $allow_reauthentication = false;
/**
* The attribute to show in the error page
* @var string|null
*/
protected ?string $show_user_attribute = null;
/**
* Initialize this filter.
* Validate configuration parameters.
*
* @param array<mixed> $config Configuration information about this filter.
* @param mixed $reserved For future use.
*/
public function __construct(array $config, $reserved)
{
parent::__construct($config, $reserved);
// Check for the deny option
// Must be bool specifically, if not, it might be for an attrib filter below
if (isset($config['deny']) && is_bool($config['deny'])) {
$this->deny = $config['deny'];
unset($config['deny']);
}
// Check for the regex option
// Must be bool specifically, if not, it might be for an attrib filter below
if (isset($config['regex']) && is_bool($config['regex'])) {
$this->regex = $config['regex'];
unset($config['regex']);
}
// Check for the reject_msg option; Must be array of languages
if (isset($config['reject_msg']) && is_array($config['reject_msg'])) {
$this->reject_msg = $config['reject_msg'];
unset($config['reject_msg']);
}
// Check for the errorURL option
// Must be bool specifically, if not, it might be for an attrib filter below
if (isset($config['errorURL']) && is_bool($config['errorURL'])) {
$this->errorURL = $config['errorURL'];
unset($config['errorURL']);
}
if (isset($config['allow_reauthentication']) && is_bool($config['allow_reauthentication'])) {
$this->allow_reauthentication = $config['allow_reauthentication'];
unset($config['allow_reauthentication']);
}
if (isset($config['show_user_attribute']) && is_string($config['show_user_attribute'])) {
$this->show_user_attribute = $config['show_user_attribute'];
unset($config['show_user_attribute']);
}
foreach ($config as $attribute => $values) {
if (is_string($values)) {
$arrayUtils = new Utils\Arrays();
$values = $arrayUtils->arrayize($values);
} elseif (!is_array($values)) {
throw new Exception(sprintf(
'Filter Authorize: Attribute values is neither string nor array: %s',
var_export($attribute, true),
));
}
foreach ($values as $value) {
if (!is_string($value)) {
throw new Exception(sprintf(
'Filter Authorize: Each value should be a string for attribute: %s value: %s config: %s',
var_export($attribute, true),
var_export($value, true),
var_export($config, true),
));
}
}
$this->valid_attribute_values[$attribute] = $values;
}
}
/**
* Apply filter to validate attributes.
*
* @param array<mixed> &$state The current request
*/
public function process(array &$state): void
{
Assert::keyExists($state, 'Attributes');
$authorize = $this->deny;
$attributes = &$state['Attributes'];
$ctx = [];
// Store the rejection message array in the $state
if (!empty($this->reject_msg)) {
$state['authprocAuthorize_reject_msg'] = $this->reject_msg;
}
$state['authprocAuthorize_errorURL'] = $this->errorURL;
$state['authprocAuthorize_allow_reauthentication'] = $this->allow_reauthentication;
$arrayUtils = new Utils\Arrays();
foreach ($this->valid_attribute_values as $name => $patterns) {
if (array_key_exists($name, $attributes)) {
foreach ($patterns as $pattern) {
$values = $arrayUtils->arrayize($attributes[$name]);
foreach ($values as $value) {
if ($this->regex) {
$matched = preg_match($pattern, $value);
} else {
$matched = ($value === $pattern);
}
if ($matched) {
$authorize = ($this->deny ? false : true);
array_push($ctx, $name);
break 3;
}
}
}
}
}
if (!$authorize) {
if ($this->show_user_attribute !== null && array_key_exists($this->show_user_attribute, $attributes)) {
$userAttribute = $attributes[$this->show_user_attribute][0] ?? null;
if ($userAttribute !== null) {
$state['authprocAuthorize_user_attribute'] = $userAttribute;
}
}
// Try to hint at which attributes may have failed as context for errorURL processing
if ($this->deny) {
$state['authprocAuthorize_ctx'] = implode(' ', $ctx);
} else {
$state['authprocAuthorize_ctx'] = implode(
' ',
array_diff(array_keys($this->valid_attribute_values), $ctx),
);
}
$this->unauthorized($state);
}
}
/**
* When the process logic determines that the user is not
* authorized for this service, then forward the user to
* an 403 unauthorized page.
*
* Separated this code into its own method so that child
* classes can override it and change the action. Forward
* thinking in case a "chained" ACL is needed, more complex
* permission logic.
*
* @param array<mixed> $state
*/
protected function unauthorized(array &$state): void
{
// Save state and redirect to 403 page
$id = Auth\State::saveState($state, 'authorize:Authorize');
$url = Module::getModuleURL('authorize/error/forbidden');
$httpUtils = new Utils\HTTP();
$httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
}
}