-
Notifications
You must be signed in to change notification settings - Fork 135
/
AuthnContextTest.php
487 lines (383 loc) · 18.3 KB
/
AuthnContextTest.php
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\SAML2\XML\saml;
use DOMDocument;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\SAML2\Constants as C;
use SimpleSAML\SAML2\Utils\XPath;
use SimpleSAML\SAML2\XML\saml\AuthenticatingAuthority;
use SimpleSAML\SAML2\XML\saml\AuthnContext;
use SimpleSAML\SAML2\XML\saml\AuthnContextDecl;
use SimpleSAML\SAML2\XML\saml\AuthnContextDeclRef;
use SimpleSAML\SAML2\XML\saml\AuthnContextClassRef;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\Exception\TooManyElementsException;
use XMLReader;
use function dirname;
use function strval;
/**
* Class \SAML2\XML\saml\AuthnContextTest
*
* @covers \SimpleSAML\SAML2\XML\saml\AuthnContext
* @covers \SimpleSAML\SAML2\XML\saml\AbstractSamlElement
* @package simplesamlphp/saml2
*/
final class AuthnContextTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;
/** @var \DOMDocument */
private DOMDocument $classRef;
/** @var \DOMDocument */
private DOMDocument $declRef;
/** @var \DOMDocument */
private DOMDocument $decl;
/** @var \DOMDocument */
private DOMDocument $authority;
/**
*/
protected function setUp(): void
{
$this->schema = dirname(__FILE__, 5) . '/resources/schemas/saml-schema-assertion-2.0.xsd';
$this->testedClass = AuthnContext::class;
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/saml_AuthnContext.xml',
);
$this->classRef = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/saml_AuthnContextClassRef.xml',
);
$this->declRef = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/saml_AuthnContextDeclRef.xml',
);
$this->decl = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/saml_AuthnContextDecl.xml',
);
$this->authority = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/saml_AuthenticatingAuthority.xml',
);
}
// marshalling
/**
*/
public function testMarshallingWithClassRef(): void
{
$authnContext = new AuthnContext(
new AuthnContextClassRef(C::AC_PASSWORD_PROTECTED_TRANSPORT),
null,
new AuthnContextDeclRef('https://example.org/relative/path/to/document.xml'),
[new AuthenticatingAuthority('https://idp.example.com/SAML2')],
);
$this->assertEquals(
new AuthnContextClassRef(C::AC_PASSWORD_PROTECTED_TRANSPORT),
$authnContext->getAuthnContextClassRef(),
);
$this->assertNull($authnContext->getAuthnContextDecl());
$this->assertEquals(
new AuthnContextDeclRef('https://example.org/relative/path/to/document.xml'),
$authnContext->getAuthnContextDeclRef(),
);
$authorities = $authnContext->getAuthenticatingAuthorities();
$this->assertCount(1, $authorities);
$this->assertEquals('https://idp.example.com/SAML2', $authorities[0]->getContent());
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->classRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->declRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->authority->documentElement, true));
$this->assertXmlStringEqualsXmlString($document->saveXML(), strval($authnContext));
}
/**
*/
public function testMarshallingWithoutClassRef(): void
{
$authnContextDecl = AuthnContextDecl::fromXML($this->decl->documentElement);
$authenticatingAuthority = AuthenticatingAuthority::fromXML($this->authority->documentElement);
$authnContext = new AuthnContext(
null,
$authnContextDecl,
null,
[$authenticatingAuthority],
);
$this->assertNull($authnContext->getAuthnContextClassRef());
$this->assertEquals($authnContextDecl, $authnContext->getAuthnContextDecl());
$this->assertNull($authnContext->getAuthnContextDeclRef());
$authorities = $authnContext->getAuthenticatingAuthorities();
$this->assertCount(1, $authorities);
$this->assertEquals('https://idp.example.com/SAML2', $authorities[0]->getContent());
$document = $this->xmlRepresentation;
AuthnContextDecl::fromXML($this->decl->documentElement)->toXML($document->documentElement);
AuthenticatingAuthority::fromXML($this->authority->documentElement)->toXML($document->documentElement);
$this->assertXmlStringEqualsXmlString($document->saveXML(), strval($authnContext));
}
/**
*/
public function testMarshallingWithClassRefAndClassDeclElementOrdering(): void
{
$authnContextDecl = AuthnContextDecl::fromXML($this->decl->documentElement);
$authenticatingAuthority = new AuthenticatingAuthority('https://idp.example.com/SAML2');
$authnContext = new AuthnContext(
new AuthnContextClassRef(C::AC_PASSWORD_PROTECTED_TRANSPORT),
$authnContextDecl,
null,
[$authenticatingAuthority],
);
// Marshall it to a \DOMElement
$authnContextElement = $authnContext->toXML();
// Test for a AuthnContextClassRef
$xpCache = XPath::getXPath($authnContextElement);
$authnContextElements = XPath::xpQuery($authnContextElement, './saml_assertion:AuthnContextClassRef', $xpCache);
$this->assertCount(1, $authnContextElements);
// Test ordering of AuthnContext contents
/** @psalm-var \DOMElement[] $authnContextElements */
$authnContextElements = XPath::xpQuery(
$authnContextElement,
'./saml_assertion:AuthnContextClassRef/following-sibling::*',
$xpCache,
);
$this->assertCount(2, $authnContextElements);
$this->assertEquals('saml:AuthnContextDecl', $authnContextElements[0]->tagName);
$this->assertEquals('saml:AuthenticatingAuthority', $authnContextElements[1]->tagName);
}
/**
*/
public function testMarshallingWithoutClassRefAndClassDeclElementOrdering(): void
{
$authnContextDecl = AuthnContextDecl::fromXML($this->decl->documentElement);
$authenticatingAuthority = AuthenticatingAuthority::fromXML($this->authority->documentElement);
$authnContext = new AuthnContext(
null,
$authnContextDecl,
null,
[$authenticatingAuthority],
);
// Marshall it to a \DOMElement
$authnContextElement = $authnContext->toXML();
// Test for a AuthnContextClassRef
$xpCache = XPath::getXPath($authnContextElement);
$authnContextElements = XPath::xpQuery($authnContextElement, './saml_assertion:AuthnContextDecl', $xpCache);
$this->assertCount(1, $authnContextElements);
// Test ordering of AuthnContext contents
/** @psalm-var \DOMElement[] $authnContextElements */
$authnContextElements = XPath::xpQuery(
$authnContextElement,
'./saml_assertion:AuthnContextDecl/following-sibling::*',
$xpCache,
);
$this->assertCount(1, $authnContextElements);
$this->assertEquals('saml:AuthenticatingAuthority', $authnContextElements[0]->tagName);
}
/**
*/
public function testMarshallingWithClassRefAndDeclRefElementOrdering(): void
{
$authnContextDecl = AuthnContextDecl::fromXML($this->decl->documentElement);
$authenticatingAuthority = AuthenticatingAuthority::fromXML($this->authority->documentElement);
$authnContext = new AuthnContext(
new AuthnContextClassRef(C::AC_PASSWORD_PROTECTED_TRANSPORT),
null,
new AuthnContextDeclRef('https://example.org/relative/path/to/document.xml'),
[$authenticatingAuthority],
);
// Marshall it to a \DOMElement
$authnContextElement = $authnContext->toXML();
// Test for a AuthnContextClassRef
$xpCache = XPath::getXPath($authnContextElement);
$authnContextElements = XPath::xpQuery($authnContextElement, './saml_assertion:AuthnContextClassRef', $xpCache);
$this->assertCount(1, $authnContextElements);
// Test ordering of AuthnContext contents
/** @psalm-var \DOMElement[] $authnContextElements */
$authnContextElements = XPath::xpQuery(
$authnContextElement,
'./saml_assertion:AuthnContextClassRef/following-sibling::*',
$xpCache,
);
$this->assertCount(2, $authnContextElements);
$this->assertEquals('saml:AuthnContextDeclRef', $authnContextElements[0]->tagName);
$this->assertEquals('saml:AuthenticatingAuthority', $authnContextElements[1]->tagName);
}
/**
*/
public function testMarshallingWithoutClassRefAndDeclRefElementOrdering(): void
{
$authnContextDecl = AuthnContextDecl::fromXML($this->decl->documentElement);
$authenticatingAuthority = AuthenticatingAuthority::fromXML($this->authority->documentElement);
$authnContext = new AuthnContext(
null,
null,
new AuthnContextDeclRef('https://example.org/relative/path/to/document.xml'),
[$authenticatingAuthority],
);
// Marshall it to a \DOMElement
$authnContextElement = $authnContext->toXML();
// Test for a AuthnContextClassRef
$xpCache = XPath::getXPath($authnContextElement);
$authnContextElements = XPath::xpQuery($authnContextElement, './saml_assertion:AuthnContextDeclRef', $xpCache);
$this->assertCount(1, $authnContextElements);
// Test ordering of AuthnContext contents
/** @psalm-var \DOMElement[] $authnContextElements */
$authnContextElements = XPath::xpQuery(
$authnContextElement,
'./saml_assertion:AuthnContextDeclRef/following-sibling::*',
$xpCache,
);
$this->assertCount(1, $authnContextElements);
$this->assertEquals('saml:AuthenticatingAuthority', $authnContextElements[0]->tagName);
}
/**
*/
public function testMarshallingIllegalCombination(): void
{
$authnContextClassRef = new AuthnContextClassRef(C::AC_PASSWORD_PROTECTED_TRANSPORT);
$authnContextDecl = AuthnContextDecl::fromXML($this->decl->documentElement);
$authnContextDeclRef = new AuthnContextDeclRef('https://example.org/relative/path/to/document.xml');
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage('Can only have one of AuthnContextDecl/AuthnContextDeclRef');
new AuthnContext(
$authnContextClassRef,
$authnContextDecl,
$authnContextDeclRef,
[
new AuthenticatingAuthority('https://idp.example.com/SAML2'),
],
);
}
/**
*/
public function testMarshallingEmpty(): void
{
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage('You need either an AuthnContextDecl or an AuthnContextDeclRef');
new AuthnContext(null, null, null);
}
// unmarshalling
/**
*/
public function testUnmarshallingWithClassRef(): void
{
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->classRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->declRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->authority->documentElement, true));
$authnContext = AuthnContext::fromXML($document->documentElement);
/** @psalm-var \SimpleSAML\SAML2\XML\saml\AuthnContextClassRef $classRef */
$classRef = $authnContext->getAuthnContextClassRef();
$this->assertEquals(C::AC_PASSWORD_PROTECTED_TRANSPORT, $classRef->getContent());
/** @psalm-var \SimpleSAML\SAML2\XML\saml\AuthnContextDeclRef $declRef */
$declRef = $authnContext->getAuthnContextDeclRef();
$this->assertEquals('https://example.org/relative/path/to/document.xml', $declRef->getContent());
$authorities = $authnContext->getAuthenticatingAuthorities();
$this->assertEquals('https://idp.example.com/SAML2', $authorities[0]->getContent());
}
/**
*/
public function testUnmarshallingWithoutClassRef(): void
{
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->decl->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->authority->documentElement, true));
$authnContext = AuthnContext::fromXML($document->documentElement);
$this->assertFalse($authnContext->isEmptyElement());
$contextDeclObj = $authnContext->getAuthnContextDecl();
$this->assertInstanceOf(AuthnContextDecl::class, $contextDeclObj);
/** @psalm-var \DOMElement $authnContextDecl[1] */
$authnContextDecl = $contextDeclObj->getDecl();
$this->assertEquals('samlacpass:AuthenticationContextDeclaration', $authnContextDecl[1]->tagName);
}
/**
*/
public function testUnmarshallingIllegalCombination(): void
{
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->classRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->decl->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->declRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->authority->documentElement, true));
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage('Can only have one of AuthnContextDecl/AuthnContextDeclRef');
AuthnContext::fromXML($document->documentElement);
}
/**
* More than one AuthnContextClassRef inside AuthnContext will throw Exception.
*/
public function testMoreThanOneAuthnContextClassRefThrowsException(): void
{
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->classRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->classRef->documentElement, true));
$this->expectException(TooManyElementsException::class);
$this->expectExceptionMessage("More than one <saml:AuthnContextClassRef> found");
AuthnContext::fromXML($document->documentElement);
}
/**
* More than one AuthnContextDeclRef inside AuthnContext will throw Exception.
*/
public function testMoreThanOneAuthnContextDeclRefThrowsException(): void
{
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->declRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->declRef->documentElement, true));
$this->expectException(TooManyElementsException::class);
$this->expectExceptionMessage("More than one <saml:AuthnContextDeclRef> found");
AuthnContext::fromXML($document->documentElement);
}
/**
* More than one AuthnContextDecl inside AuthnContext will throw Exception.
*/
public function testMoreThanOneAuthnContextDeclThrowsException(): void
{
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->decl->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->decl->documentElement, true));
$this->expectException(TooManyElementsException::class);
$this->expectExceptionMessage("More than one <saml:AuthnContextDecl> found");
AuthnContext::fromXML($document->documentElement);
}
/**
*/
public function testUnmarshallingEmpty(): void
{
$document = $this->xmlRepresentation;
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage('You need either an AuthnContextDecl or an AuthnContextDeclRef');
AuthnContext::fromXML($document->documentElement);
}
/**
* Test serialization / unserialization
*/
public function testSerialization(): void
{
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->classRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->declRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->authority->documentElement, true));
$this->assertXmlStringEqualsXmlString(
$this->xmlRepresentation->saveXML($document->documentElement),
strval(unserialize(serialize(AuthnContext::fromXML($document->documentElement))))
);
}
/**
* Test schema validation.
*/
public function testSchemaValidation(): void
{
$document = $this->xmlRepresentation;
$document->documentElement->appendChild($document->importNode($this->classRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->declRef->documentElement, true));
$document->documentElement->appendChild($document->importNode($this->authority->documentElement, true));
$predoc = XMLReader::XML($document->saveXML());
Assert::notFalse($predoc);
/** @psalm-var \XMLReader $predoc */
$pre = $this->validateDocument($predoc);
$this->assertTrue($pre);
$class = $this->testedClass::fromXML($document->documentElement);
$serializedClass = $class->toXML();
$postdoc = XMLReader::XML($serializedClass->ownerDocument->saveXML());
Assert::notFalse($postdoc);
/** @psalm-var \XMLReader $postdoc */
$post = $this->validateDocument($postdoc);
$this->assertTrue($post);
}
}