Completed
Push — master ( 4e360d...80bc96 )
by Daan van
07:27
created

SAML2_Assertion_Decrypter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 19.75 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 7
lcom 1
cbo 6
dl 16
loc 81
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Decrypter::__construct() 0 11 1
A Decrypter::isEncryptionRequired() 0 5 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SAML2\Assertion;
4
5
use Psr\Log\LoggerInterface;
6
use SAML2\Assertion\Exception\NotDecryptedException;
7
use SAML2\Certificate\PrivateKeyLoader;
8
use SAML2\Configuration\IdentityProvider;
9
use SAML2\Configuration\ServiceProvider;
10
use SAML2\EncryptedAssertion;
11
12
class Decrypter
13
{
14
    /**
15
     * @var \SAML2\Configuration\IdentityProvider
16
     */
17
    private $identityProvider;
18
19
    /**
20
     * @var \SAML2\Configuration\ServiceProvider
21
     */
22
    private $serviceProvider;
23
24
    /**
25
     * @var \SAML2\Certificate\PrivateKeyLoader
26
     */
27
    private $privateKeyLoader;
28
29
    /**
30
     * @var \Psr\Log\LoggerInterface
31
     */
32
    private $logger;
33
34
    public function __construct(
35
        LoggerInterface $logger,
36
        IdentityProvider $identityProvider,
37
        ServiceProvider $serviceProvider,
38
        PrivateKeyLoader $privateKeyLoader
39
    ) {
40
        $this->logger = $logger;
41
        $this->identityProvider = $identityProvider;
42
        $this->serviceProvider = $serviceProvider;
43
        $this->privateKeyLoader = $privateKeyLoader;
44
    }
45
46
    /**
47
     * Allows for checking whether either the SP or the IdP requires assertion encryption
48
     */
49
    public function isEncryptionRequired()
50
    {
51
        return $this->identityProvider->isAssertionEncryptionRequired()
52
            || $this->serviceProvider->isAssertionEncryptionRequired();
53
    }
54
55
    /**
56
     * @param \SAML2\EncryptedAssertion $assertion
57
     *
58
     * @return \SAML2\Assertion
59
     */
60
    public function decrypt(EncryptedAssertion $assertion)
61
    {
62
        $decryptionKeys = $this->privateKeyLoader->loadDecryptionKeys($this->identityProvider, $this->serviceProvider);
63
        $blacklistedKeys = $this->identityProvider->getBlacklistedAlgorithms();
64
        if (is_null($blacklistedKeys)) {
65
            $blacklistedKeys = $this->serviceProvider->getBlacklistedAlgorithms();
66
        }
67
68
        // reflects the simplesamlphp behaviour for BC, see
69
        // https://github.com/simplesamlphp/simplesamlphp/blob/3d735912342767d391297cc5e13272a76730aca0/modules/saml/lib/Message.php#L369
70 View Code Duplication
        foreach ($decryptionKeys as $index => $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            try {
72
                $decryptedAssertion = $assertion->getAssertion($key, $blacklistedKeys);
73
                $this->logger->debug(sprintf('Decrypted Assertion with key "#%d"', $index));
74
75
                return $decryptedAssertion;
76
            } catch (\Exception $e) {
77
                $this->logger->debug(sprintf(
78
                    'Could not decrypt assertion with key "#%d", "%s" thrown: "%s"',
79
                    $index,
80
                    get_class($e),
81
                    $e->getMessage()
82
                ));
83
            }
84
        }
85
86
        throw new NotDecryptedException(sprintf(
87
            'Could not decrypt the assertion, tried with "%d" keys. See the debug log for more information',
88
            count($decryptionKeys)
89
        ));
90
    }
91
}
92