Completed
Push — master ( c3456a...b4bda7 )
by Damien
04:06 queued 38s
created

AssertionTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 38
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getFirstAssertion() 0 29 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\sp\Saml;
11
use LightSaml\Model\Assertion\Assertion;
12
use LightSaml\Model\Protocol\Response as SamlResponse;
13
14
trait AssertionTrait
15
{
16
    protected $isAssertionDecrypted = false;
17
    /**
18
     * @param SamlResponse $response
19
     * @return Assertion
20
     * @throws InvalidMessage
21
     */
22
    public function getFirstAssertion(\LightSaml\Model\Protocol\Response $response)
23
    {
24
25
        $ownProvider = Saml::getInstance()->getProvider()->findOwn();
26
27
        if ($ownProvider->keychain &&
28
            $response->getFirstEncryptedAssertion() &&
29
            $this->isAssertionDecrypted === false
30
        ) {
31
            Saml::getInstance()->getResponse()->decryptAssertions(
32
                $response,
33
                $ownProvider->keychain
34
            );
35
            /**
36
             * try to only do this once
37
             */
38
            $this->isAssertionDecrypted = true;
39
        }
40
41
        $assertions = $response->getAllAssertions();
42
43
        if (! isset($assertions[0])) {
44
            throw new InvalidMessage("Invalid message. No assertions found in response.");
45
        }
46
        /**
47
         * Just grab the first one for now.
48
         */
49
        return $assertions[0];
50
    }
51
}
52