PrivateKeyLoader::loadDecryptionKeys()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 2
dl 0
loc 25
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Certificate;
6
7
use SimpleSAML\SAML2\Configuration\DecryptionProvider;
8
use SimpleSAML\SAML2\Configuration\PrivateKey as PrivateKeyConfiguration;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Configuration\PrivateKey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SimpleSAML\SAML2\Utilities\ArrayCollection;
10
use SimpleSAML\XMLSecurity\Key\PrivateKey;
11
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
12
13
class PrivateKeyLoader
14
{
15
    /**
16
     * Loads a private key based on the configuration given.
17
     *
18
     * @param \SimpleSAML\SAML2\Configuration\PrivateKey $key
19
     * @return \SimpleSAML\XMLSecurity\Key\PrivateKey
20
     */
21
    public function loadPrivateKey(PrivateKeyConfiguration $key): PrivateKey
22
    {
23
        return PrivateKey::fromFile(
24
            $key->isFile() ? $key->getFilePath() : $key->getContents(),
25
            $key->getPassPhrase(),
26
        );
27
    }
28
29
30
    /**
31
     * @param \SimpleSAML\SAML2\Configuration\DecryptionProvider $identityProvider
32
     * @param \SimpleSAML\SAML2\Configuration\DecryptionProvider $serviceProvider
33
     * @return \SimpleSAML\SAML2\Utilities\ArrayCollection
34
     *
35
     * @throws \Exception
36
     */
37
    public function loadDecryptionKeys(
38
        DecryptionProvider $identityProvider,
39
        DecryptionProvider $serviceProvider,
40
    ): ArrayCollection {
41
        $decryptionKeys = new ArrayCollection();
42
43
        $senderSharedKey = $identityProvider->getSharedKey();
44
        if ($senderSharedKey !== null) {
45
            $key = new SymmetricKey($senderSharedKey);
46
            $decryptionKeys->add($key);
47
48
            return $decryptionKeys;
49
        }
50
51
        $newPrivateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_NEW);
52
        if ($newPrivateKey instanceof PrivateKeyConfiguration) {
53
            $loadedKey = $this->loadPrivateKey($newPrivateKey);
54
            $decryptionKeys->add($loadedKey);
55
        }
56
57
        $privateKey = $serviceProvider->getPrivateKey(PrivateKeyConfiguration::NAME_DEFAULT, true);
58
        $loadedKey  = $this->loadPrivateKey($privateKey);
59
        $decryptionKeys->add($loadedKey);
60
61
        return $decryptionKeys;
62
    }
63
}
64