1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace flipbox\saml\sp\services\login; |
8
|
|
|
|
9
|
|
|
use flipbox\saml\core\exceptions\InvalidMessage; |
10
|
|
|
use flipbox\saml\core\helpers\SecurityHelper; |
11
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
12
|
|
|
use flipbox\saml\sp\records\ProviderRecord; |
13
|
|
|
use flipbox\saml\sp\Saml; |
14
|
|
|
use SAML2\Assertion as SamlAssertion; |
15
|
|
|
use SAML2\EncryptedAssertion; |
16
|
|
|
use SAML2\Response as SamlResponse; |
17
|
|
|
|
18
|
|
|
trait AssertionTrait |
19
|
|
|
{ |
20
|
|
|
private $decryptedAssertions = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param SamlResponse $response |
24
|
|
|
* @return SamlAssertion |
25
|
|
|
* @throws InvalidMessage |
26
|
|
|
*/ |
27
|
9 |
|
public function getFirstAssertion(SamlResponse $response, ProviderRecord $serviceProvider) |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
|
31
|
9 |
|
$assertions = $this->getAssertions($response, $serviceProvider); |
32
|
|
|
|
33
|
9 |
|
if (! count($assertions)) { |
34
|
|
|
throw new InvalidMessage("Invalid message. No assertions found in response."); |
35
|
|
|
} |
36
|
|
|
|
37
|
9 |
|
return $assertions[0]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param SamlResponse $response |
42
|
|
|
* @return mixed |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
12 |
|
private function getAssertions(SamlResponse $response, ProviderRecord $ownProvider) |
46
|
|
|
{ |
47
|
|
|
// is there a cache already? |
48
|
12 |
|
if (count($this->decryptedAssertions)) { |
49
|
9 |
|
return $this->decryptedAssertions; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// grab the first one |
53
|
12 |
|
foreach ($response->getAssertions() as $assertion) { |
54
|
12 |
|
if ($ownProvider->keychain && |
55
|
12 |
|
$assertion instanceof EncryptedAssertion |
56
|
|
|
) { |
57
|
|
|
$assertion = SecurityHelper::decryptAssertion( |
58
|
|
|
$assertion, |
59
|
|
|
$ownProvider->keychain->getDecryptedKey() |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$this->decryptedAssertions[] = $assertion; |
63
|
|
|
} else { |
64
|
12 |
|
$this->decryptedAssertions[] = $assertion; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
12 |
|
return $this->decryptedAssertions; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|