-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAttributeExtractorTest.php
More file actions
353 lines (291 loc) · 12 KB
/
Copy pathAttributeExtractorTest.php
File metadata and controls
353 lines (291 loc) · 12 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
declare(strict_types=1);
namespace SimpleSAML\Casserver;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration;
use SimpleSAML\Module\casserver\Cas\AttributeExtractor;
use SimpleSAML\Module\casserver\Cas\Factories\ProcessingChainFactory;
use SimpleSAML\Utils;
class AttributeExtractorTest extends TestCase
{
/**
* An authproc filter that records the IdP and SP entity IDs the processing chain was built with,
* so that the test can assert on what a real filter would observe.
*/
private const array ENTITY_ID_OBSERVER = [
'class' => 'core:PHP',
'code' => '$attributes["observedIdpEntityId"] = [$state["Source"]["entityid"] ?? "MISSING"];'
. '$attributes["observedSpEntityId"] = [$state["Destination"]["entityid"] ?? "MISSING"];',
];
/**
* Confirm behavior of a default configuration
*/
public function testNoCasConfig(): void
{
$casConfig = [
// Default is to use eppn and copy all attributes
];
$state['Attributes'] = [
'eduPersonPrincipalName' => ['testuser@example.com'],
'additionalAttribute' => ['Taco Club'],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertEquals('testuser@example.com', $result['user']);
$this->assertEquals($state['Attributes'], $result['attributes']);
}
/**
* Test disable attribute copying
*/
public function testNoAttributeCopying(): void
{
$casConfig = [
'attributes' => false,
];
$state['Attributes'] = [
'eduPersonPrincipalName' => ['testuser@example.com'],
'additionalAttribute' => ['Taco Club'],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertEquals('testuser@example.com', $result['user']);
$this->assertEquals([], $result['attributes']);
}
/**
* Confirm customizing the attribute for user and attributes to copy
*/
public function testCustomAttributeCopy(): void
{
$casConfig = [
'attrname' => 'userNameAttribute',
'attributes_to_transfer' => [
'exampleAttribute',
'additionalAttribute',
],
];
$state['Attributes'] = [
'userNameAttribute' => ['testuser@example.com'],
'additionalAttribute' => ['Taco Club'],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertEquals('testuser@example.com', $result['user']);
$this->assertEquals(['additionalAttribute' => ['Taco Club']], $result['attributes']);
}
/**
* Confirm empty authproc has no affect
*/
public function testEmptyAuthproc(): void
{
$casConfig = [
// Default is to use eppn and copy all attributes
];
$state['Attributes'] = [
'eduPersonPrincipalName' => ['testuser@example.com'],
'additionalAttribute' => ['Taco Club'],
'authproc' => [],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertEquals('testuser@example.com', $result['user']);
$this->assertEquals($state['Attributes'], $result['attributes']);
}
/**
* Test authproc configurations can adjust the attributes.
*/
public function testAuthprocConfig(): void
{
// Authproc filters need a config.php defined
putenv('SIMPLESAMLPHP_CONFIG_DIR=' . dirname(__DIR__) . '/config/');
$casConfig = [
// Default is to use eppn and copy all attributes
'authproc' => [
[
'class' => 'core:AttributeMap',
'oid2name',
'urn:example' => 'additionalAttribute',
],
],
'attributes_to_transfer' => [
'not-affected-by-authproc',
'additionalAttribute',
],
];
$state['Attributes'] = [
'urn:oid:1.3.6.1.4.1.5923.1.1.1.6' => ['testuser@example.com'],
'urn:example' => ['Taco Club'],
'not-affected-by-authproc' => ['Value'],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
// The authproc filters will remap the attributes prior to mapping them to CAS attributes
$result = $attributeExtractor->extractUserAndAttributes($state);
$expectedAttributes = [
'additionalAttribute' => ['Taco Club'],
'not-affected-by-authproc' => ['Value'],
];
$this->assertEquals('testuser@example.com', $result['user']);
$this->assertEquals($expectedAttributes, $result['attributes']);
}
/**
* The CAS service URL seeded into the state is handed to the authproc filters as the SP entity ID,
* and the IdP entity ID falls back to the SimpleSAMLphp base URL when not configured.
*/
public function testAuthprocReceivesServiceUrlAsSpEntityId(): void
{
putenv('SIMPLESAMLPHP_CONFIG_DIR=' . dirname(__DIR__) . '/config/');
$serviceUrl = 'https://myservice.example.com/cas/callback';
$casConfig = [
'authproc' => [self::ENTITY_ID_OBSERVER],
];
$state = [
'Attributes' => [
'eduPersonPrincipalName' => ['testuser@example.com'],
],
// Seeded by LoginController from the validated ?service= / ?TARGET= parameter
'Destination' => ['entityid' => $serviceUrl],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertEquals([$serviceUrl], $result['attributes']['observedSpEntityId']);
// Regression guard: the whole failure mode is that this silently becomes an empty string.
$observedIdpEntityId = $result['attributes']['observedIdpEntityId'][0];
$this->assertIsString($observedIdpEntityId);
$this->assertNotEmpty($observedIdpEntityId);
$this->assertEquals((new Utils\HTTP())->getBaseURL(), $observedIdpEntityId);
}
/**
* The configured idp_entity_id is handed to the authproc filters as the IdP entity ID.
*/
public function testAuthprocReceivesConfiguredIdpEntityId(): void
{
putenv('SIMPLESAMLPHP_CONFIG_DIR=' . dirname(__DIR__) . '/config/');
$idpEntityId = 'https://login.example.org/idp/metadata.php';
$casConfig = [
'idp_entity_id' => $idpEntityId,
'authproc' => [self::ENTITY_ID_OBSERVER],
];
$state = [
'Attributes' => [
'eduPersonPrincipalName' => ['testuser@example.com'],
],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertEquals([$idpEntityId], $result['attributes']['observedIdpEntityId']);
}
/**
* Entity IDs already present in the state take precedence over the configuration, so that a state
* resumed from an authproc filter keeps what the first pass established.
*/
public function testAuthprocPreservesPreExistingEntityIds(): void
{
putenv('SIMPLESAMLPHP_CONFIG_DIR=' . dirname(__DIR__) . '/config/');
$stateIdpEntityId = 'https://from-state.example.org/idp/metadata.php';
$stateSpEntityId = 'https://from-state.example.com/cas/callback';
$casConfig = [
'idp_entity_id' => 'https://from-config.example.org/idp/metadata.php',
'authproc' => [self::ENTITY_ID_OBSERVER],
];
$state = [
'Attributes' => [
'eduPersonPrincipalName' => ['testuser@example.com'],
],
'Source' => ['entityid' => $stateIdpEntityId],
'Destination' => ['entityid' => $stateSpEntityId],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertEquals([$stateIdpEntityId], $result['attributes']['observedIdpEntityId']);
$this->assertEquals([$stateSpEntityId], $result['attributes']['observedSpEntityId']);
}
/**
* Only the casserver configuration may contribute authproc filters. Filters seeded into the SP
* metadata by a caller must not be executed by the processing chain.
*/
public function testAuthprocIgnoresFiltersSeededIntoSpMetadata(): void
{
putenv('SIMPLESAMLPHP_CONFIG_DIR=' . dirname(__DIR__) . '/config/');
$casConfig = [
'authproc' => [self::ENTITY_ID_OBSERVER],
];
$state = [
'Attributes' => [
'eduPersonPrincipalName' => ['testuser@example.com'],
],
'Destination' => [
'entityid' => 'https://myservice.example.com/cas/callback',
// A control key, not metadata. It must be dropped rather than added to the chain.
'authproc' => [
[
'class' => 'core:PHP',
'code' => '$attributes["filterFromSpMetadata"] = ["should not run"];',
],
],
],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertArrayNotHasKey('filterFromSpMetadata', $result['attributes']);
// The configured filters still ran.
$this->assertArrayHasKey('observedSpEntityId', $result['attributes']);
}
/**
* Without a service URL the chain still runs, and the SP entity ID stays empty.
*/
public function testAuthprocWithoutServiceUrl(): void
{
putenv('SIMPLESAMLPHP_CONFIG_DIR=' . dirname(__DIR__) . '/config/');
$casConfig = [
'idp_entity_id' => 'https://login.example.org/idp/metadata.php',
'authproc' => [self::ENTITY_ID_OBSERVER],
];
$state = [
'Attributes' => [
'eduPersonPrincipalName' => ['testuser@example.com'],
],
];
$loadedConfig = Configuration::loadFromArray($casConfig);
$attributeExtractor = new AttributeExtractor(
$loadedConfig,
new ProcessingChainFactory($loadedConfig),
);
$result = $attributeExtractor->extractUserAndAttributes($state);
$this->assertEquals([''], $result['attributes']['observedSpEntityId']);
}
}