Skip to content

Commit 151a418

Browse files
committed
Make processor aware of assertion types
Both the Encrypted and regular Assertion classes should be passable in the decryptAssertion method. If not, you would never be able to process a SAML Response consisting of regular Assertion objects. The test merely verifies the behaviour that was changed in this commit. No additional processor tests where added. But that should be simple enough in the future. For some odd reason using a data provider for the processor_correctly_encrypts_assertions caused issues in other tests. The test suite does not seem to be fully idempotent.
1 parent 51562b0 commit 151a418

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/SAML2/Assertion/Processor.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace SAML2\Assertion;
66

77
use Psr\Log\LoggerInterface;
8-
98
use SAML2\Assertion;
109
use SAML2\Assertion\Exception\InvalidAssertionException;
1110
use SAML2\Assertion\Exception\InvalidSubjectConfirmationException;
@@ -15,7 +14,6 @@
1514
use SAML2\Configuration\IdentityProvider;
1615
use SAML2\EncryptedAssertion;
1716
use SAML2\Response\Exception\InvalidSignatureException;
18-
use SAML2\Response\Exception\UnencryptedAssertionFoundException;
1917
use SAML2\Signature\Validator;
2018
use SAML2\Utilities\ArrayCollection;
2119

@@ -95,7 +93,13 @@ public function decryptAssertions(ArrayCollection $assertions)
9593
{
9694
$decrypted = new ArrayCollection();
9795
foreach ($assertions->getIterator() as $assertion) {
98-
$decrypted->add($this->decryptAssertion($assertion));
96+
if ($assertion instanceof EncryptedAssertion) {
97+
$decrypted->add($this->decryptAssertion($assertion));
98+
} elseif ($assertion instanceof Assertion) {
99+
$decrypted->add($assertion);
100+
} else {
101+
throw new InvalidAssertionException('The assertion must be of type: EncryptedAssertion or Assertion');
102+
}
99103
}
100104

101105
return $decrypted;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SAML2\Assertion;
6+
7+
use Mockery as m;
8+
use Mockery\Adapter\Phpunit\MockeryTestCase;
9+
10+
/**
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class ProcessorTest extends MockeryTestCase
14+
{
15+
/**
16+
* @var Processor
17+
*/
18+
private $processor;
19+
20+
/**
21+
* @var m\MockInterface&Decrypter
22+
*/
23+
private $decrypter;
24+
25+
protected function setUp(): void
26+
{
27+
$this->decrypter = m::mock(Decrypter::class);
28+
$validator = m::mock(\SAML2\Signature\Validator::class);
29+
$assertionValidator = m::mock(\SAML2\Assertion\Validation\AssertionValidator::class);
30+
$subjectConfirmationValidator = m::mock(\SAML2\Assertion\Validation\SubjectConfirmationValidator::class);
31+
$transformer = m::mock(\SAML2\Assertion\Transformer\Transformer::class);
32+
$identityProvider = new \SAML2\Configuration\IdentityProvider([]);
33+
$logger = m::mock(\Psr\Log\LoggerInterface::class);
34+
35+
$this->processor = new Processor(
36+
$this->decrypter,
37+
$validator,
38+
$assertionValidator,
39+
$subjectConfirmationValidator,
40+
$transformer,
41+
$identityProvider,
42+
$logger
43+
);
44+
}
45+
46+
/**
47+
* @test
48+
*/
49+
public function processor_correctly_encrypts_assertions(): void
50+
{
51+
$testData = [
52+
[new \SAML2\Assertion()],
53+
[new \SAML2\EncryptedAssertion()],
54+
[new \SAML2\Assertion(), new \SAML2\EncryptedAssertion(), new \SAML2\Assertion()],
55+
[new \SAML2\EncryptedAssertion(), new \SAML2\EncryptedAssertion(), new \SAML2\EncryptedAssertion()],
56+
];
57+
58+
foreach ($testData as $assertions) {
59+
$this->decrypter
60+
->shouldReceive('decrypt')
61+
->andReturn(new \SAML2\Assertion());
62+
63+
$collection = new \SAML2\Utilities\ArrayCollection($assertions);
64+
$result = $this->processor->decryptAssertions($collection);
65+
self::assertInstanceOf(\SAML2\Utilities\ArrayCollection::class, $result);
66+
foreach ($result as $assertion) {
67+
self::assertInstanceOf(\SAML2\Assertion::class, $assertion);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* @test
74+
*/
75+
public function unsuported_assertions_are_rejected(): void
76+
{
77+
$this->expectException('\SAML2\Assertion\Exception\InvalidAssertionException');
78+
$this->expectExceptionMessage('The assertion must be of type: EncryptedAssertion or Assertion');
79+
$this->processor->decryptAssertions(new \SAML2\Utilities\ArrayCollection([new \stdClass()]));
80+
}
81+
}

0 commit comments

Comments
 (0)