Completed
Push — master ( b0095b...64e902 )
by Damien
05:32
created

AssertionTrait   A

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 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