Completed
Push — master ( 484ff8...780e48 )
by Jaime Pérez
15s queued 12s
created

SignatureAlgorithmFactory::updateCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Alg\Signature;
6
7
use SimpleSAML\XMLSecurity\Alg\AbstractAlgorithmFactory;
8
use SimpleSAML\XMLSecurity\Constants as C;
9
use SimpleSAML\XMLSecurity\Key\AbstractKey;
10
11
/**
12
 * Factory class to create and configure digital signature algorithms.
13
 *
14
 * @package simplesamlphp/xml-security
15
 */
16
final class SignatureAlgorithmFactory extends AbstractAlgorithmFactory
17
{
18
    /**
19
     * A cache of algorithm implementations indexed by algorithm ID.
20
     *
21
     * @var string[]
22
     */
23
    protected static array $cache = [];
24
25
    /**
26
     * Whether the factory has been initialized or not.
27
     *
28
     * @var bool
29
     */
30
    protected static bool $initialized = false;
31
32
    /**
33
     * An array of blacklisted algorithms.
34
     *
35
     * Defaults to RSA-SHA1 & HMAC-SHA1 due to the weakness of SHA1.
36
     *
37
     * @var string[]
38
     */
39
    protected array $blacklist = [
40
        C::SIG_RSA_SHA1,
41
        C::SIG_HMAC_SHA1,
42
    ];
43
44
45
    /**
46
     * Build a factory that creates signature algorithms.
47
     *
48
     * @param array|null $blacklist A list of algorithms forbidden for their use.
49
     */
50
    public function __construct(array $blacklist = null)
51
    {
52
        parent::__construct(
53
            $blacklist,
54
            [
55
                RSA::class,
56
                HMAC::class,
57
            ]
58
        );
59
    }
60
61
62
    /**
63
     * Get the name of the abstract class our algorithm implementations must extend.
64
     *
65
     * @return string
66
     */
67
    protected static function getExpectedParent(): string
68
    {
69
        return SignatureAlgorithmInterface::class;
70
    }
71
72
73
    /**
74
     * Get a new object implementing the given digital signature algorithm.
75
     *
76
     * @param string $algId The identifier of the algorithm desired.
77
     * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use with the given algorithm.
78
     *
79
     * @return \SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmInterface An object implementing the given
80
     * algorithm.
81
     *
82
     * @throws \SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException If an error occurs, e.g. the given algorithm
83
     * is blacklisted, unknown or the given key is not suitable for it.
84
     */
85
    public function getAlgorithm(string $algId, AbstractKey $key): SignatureAlgorithmInterface
86
    {
87
        return parent::getAlgorithm($algId, $key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getAlgorithm($algId, $key) returns the type SimpleSAML\XMLSecurity\Alg\AlgorithmInterface which includes types incompatible with the type-hinted return SimpleSAML\XMLSecurity\A...atureAlgorithmInterface.
Loading history...
88
    }
89
}
90