/
NameIdDecryptionTransformerTest.php
191 lines (159 loc) · 6.53 KB
/
NameIdDecryptionTransformerTest.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
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\SAML2\XML\saml;
use DateTimeImmutable;
use DOMDocument;
use PHPUnit\Framework\TestCase;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SimpleSAML\SAML2\Assertion\Processor;
use SimpleSAML\SAML2\Assertion\ProcessorBuilder;
use SimpleSAML\SAML2\Compat\ContainerSingleton;
use SimpleSAML\SAML2\Configuration\Destination;
use SimpleSAML\SAML2\Configuration\IdentityProvider;
use SimpleSAML\SAML2\Configuration\PrivateKey;
use SimpleSAML\SAML2\Configuration\ServiceProvider;
use SimpleSAML\SAML2\Signature\Validator;
use SimpleSAML\SAML2\Utilities\ArrayCollection;
use SimpleSAML\SAML2\Utils;
use SimpleSAML\SAML2\XML\saml\Assertion;
use SimpleSAML\SAML2\XML\saml\AuthnContext;
use SimpleSAML\SAML2\XML\saml\AuthnContextClassRef;
use SimpleSAML\SAML2\XML\saml\AuthnStatement;
use SimpleSAML\SAML2\XML\saml\EncryptedID;
use SimpleSAML\SAML2\XML\saml\Issuer;
use SimpleSAML\SAML2\XML\saml\NameID;
use SimpleSAML\SAML2\XML\saml\Subject;
use SimpleSAML\SAML2\XML\samlp\Response;
use SimpleSAML\SAML2\XML\samlp\Status;
use SimpleSAML\SAML2\XML\samlp\StatusCode;
use SimpleSAML\Test\SAML2\Constants as C;
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory;
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
use function getcwd;
/**
* Tests for decryption NameIDs.
*
* @package simplesamlphp/saml2
*/
final class NameIdDecryptionTransformerTest extends TestCase
{
/** @var \Psr\Clock\ClockInterface */
protected static ClockInterface $clock;
/** @var \DOMDocument */
protected static DOMDocument $document;
/** @var \SimpleSAML\SAML2\Assertion\Processor */
protected static Processor $assertionProcessor;
/** @var \SimpleSAML\SAML2\Configuration\IdentityProvider */
protected static IdentityProvider $identityProviderConfiguration;
/** @var \SimpleSAML\SAML2\Configuration\ServiceProvider */
protected static ServiceProvider $serviceProviderConfiguration;
/** @var \Psr\Log\LoggerInterface */
protected static LoggerInterface $logger;
/** @var \SimpleSAML\SAML2\Response\Validation\Validator */
protected static Validator $validator;
/** @var \SimpleSAML\SAML2\Configuration\Destination */
protected static Destination $destination;
/** @var \SimpleSAML\SAML2\XML\samlp\Response */
protected static Response $response;
/** @var string */
private const FRAMEWORK = 'vendor/simplesamlphp/xml-security';
/**
* @return void
*/
public static function setUpBeforeClass(): void
{
self::$clock = Utils::getContainer()->getClock();
$container = ContainerSingleton::getInstance();
$container->setBlacklistedAlgorithms(null);
self::$logger = new NullLogger();
self::$validator = new Validator(self::$logger);
self::$destination = new Destination(C::ENTITY_SP);
self::$response = new Response(new Status(new StatusCode()), self::$clock->now());
self::$identityProviderConfiguration = new IdentityProvider(['assertionEncryptionEnabled' => true]);
$base = getcwd() . DIRECTORY_SEPARATOR . self::FRAMEWORK;
$keysDir = 'tests' . DIRECTORY_SEPARATOR . PEMCertificatesMock::KEYS_DIR;
self::$serviceProviderConfiguration = new ServiceProvider(
[
'entityId' => C::ENTITY_SP,
'blacklistedEncryptionAlgorithms' => [],
'privateKeys' => [
new PrivateKey(
$base . DIRECTORY_SEPARATOR . $keysDir . DIRECTORY_SEPARATOR . PEMCertificatesMock::PRIVATE_KEY,
'default',
PEMCertificatesMock::PASSPHRASE,
true,
),
],
],
);
self::$assertionProcessor = ProcessorBuilder::build(
self::$logger,
self::$validator,
self::$destination,
self::$identityProviderConfiguration,
self::$serviceProviderConfiguration,
self::$response
);
$encryptor = (new KeyTransportAlgorithmFactory([]))->getAlgorithm(
C::KEY_TRANSPORT_RSA_1_5,
PEMCertificatesMock::getPublicKey(PEMCertificatesMock::PUBLIC_KEY),
);
$nameId = new NameID('value', 'name_qualifier');
$encryptedId = new EncryptedID($nameId->encrypt($encryptor));
$assertion = new Assertion(
issuer: new Issuer(C::ENTITY_IDP),
id: '_45e42090d8cbbfa52d5a394b01049fc2221e274182',
issueInstant: new DateTimeImmutable('2023-05-27T16:20:52Z'),
subject: new Subject($encryptedId),
statements: [
new AuthnStatement(
new AuthnContext(
new AuthnContextClassRef(C::AUTHNCONTEXT_CLASS_REF_LOA1),
null,
null,
),
new DateTimeImmutable('2023-05-27T16:20:52Z'),
),
],
);
self::$document = $assertion->toXML()->ownerDocument;
}
/**
* Verifies that we can create decrypted NameIDs.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @return void
*/
public function testBasicNameIdDecryption(): void
{
$this->markTestSkipped();
$assertion = Assertion::fromXML(self::$document->documentElement);
$processed = self::$assertionProcessor->process($assertion);
$identifier = $processed->getSubject()->getIdentifier();
$this->assertInstanceOf(NameID::class, $identifier);
$this->assertEquals('value', $identifier->getContent());
$this->assertEquals('name_qualifier', $identifier->getNameQualifier());
}
/**
* Run the decoder through processAssertions.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @return void
*/
public function testDecryptionProcessAssertions(): void
{
$this->markTestSkipped();
$assertion = Assertion::fromXML(self::$document->documentElement);
$assertions = new ArrayCollection([$assertion]);
$processed = self::$assertionProcessor->processAssertions($assertions);
$this->assertCount(1, $processed);
$identifier = $processed->getOnlyElement()->getSubject()->getIdentifier();
$this->assertInstanceOf(NameID::class, $identifier);
$this->assertEquals('value', $identifier->getContent());
$this->assertEquals('name_qualifier', $identifier->getNameQualifier());
}
}