AssertionTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 53
ccs 12
cts 17
cp 0.7059
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirstAssertion() 0 12 2
A getAssertions() 0 25 5
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;
15
use SAML2\Assertion as SamlAssertion;
16
use SAML2\EncryptedAssertion;
17
use SAML2\Response as SamlResponse;
18
19
trait AssertionTrait
20
{
21
    private $decryptedAssertions = [];
22
23
    /**
24
     * @param SamlResponse $response
25
     * @return SamlAssertion
26
     * @throws InvalidMessage
27
     */
28 9
    public function getFirstAssertion(SamlResponse $response, ProviderRecord $serviceProvider)
29
    {
30
31
32 9
        $assertions = $this->getAssertions($response, $serviceProvider);
33
34 9
        if (! count($assertions)) {
35
            throw new InvalidMessage("Invalid message. No assertions found in response.");
36
        }
37
38 9
        return $assertions[0];
39
    }
40
41
    /**
42
     * @param SamlResponse $response
43
     * @return Assertion[]
44
     * @throws \Exception
45
     */
46 15
    private function getAssertions(SamlResponse $response, ProviderRecord $ownProvider)
47
    {
48
        // is there a cache already?
49 15
        if (count($this->decryptedAssertions)) {
50 9
            return $this->decryptedAssertions;
51
        }
52
53
        // grab the first one
54 15
        foreach ($response->getAssertions() as $assertion) {
55 15
            if ($ownProvider->keychain &&
56 15
                $assertion instanceof EncryptedAssertion
57
            ) {
58
                $assertion = SecurityHelper::decryptAssertion(
59
                    $assertion,
60
                    $ownProvider->keychain->getDecryptedKey()
61
                );
62
63
                $this->decryptedAssertions[] = $assertion;
64
            } else {
65 15
                $this->decryptedAssertions[] = $assertion;
66
            }
67
        }
68
69 15
        return $this->decryptedAssertions;
70
    }
71
}
72