ValidatorChain   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A appendValidator() 0 3 1
A hasValidSignature() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Signature;
6
7
use Psr\Log\LoggerInterface;
8
use SimpleSAML\SAML2\Configuration\CertificateProvider;
9
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
10
11
use function get_class;
12
use function sprintf;
13
14
/**
15
 * Allows for validation of a signature trying different validators till a validator is found
16
 * that can validate the signature.
17
 *
18
 * If no validation is possible an exception is thrown.
19
 */
20
class ValidatorChain implements ValidatorInterface
21
{
22
    /** @var \SimpleSAML\SAML2\Signature\ChainedValidator[] */
23
    private array $validators = [];
24
25
26
    /**
27
     * @param \Psr\Log\LoggerInterface $logger
28
     * @param \SimpleSAML\SAML2\Signature\ChainedValidator[] $validators
29
     */
30
    public function __construct(
31
        private LoggerInterface $logger,
32
        array $validators,
33
    ) {
34
        // should be done through "adder" injection in the container.
35
        foreach ($validators as $validator) {
36
            $this->appendValidator($validator);
37
        }
38
    }
39
40
41
    /**
42
     * @param \SimpleSAML\SAML2\Signature\ChainedValidator $validator
43
     */
44
    public function appendValidator(ChainedValidator $validator): void
45
    {
46
        $this->validators[] = $validator;
47
    }
48
49
50
    /**
51
     * @param \SimpleSAML\XMLSecurity\XML\SignedElementInterface $signedElement
52
     * @param \SimpleSAML\SAML2\Configuration\CertificateProvider $configuration
53
     *
54
     * @return bool
55
     */
56
    public function hasValidSignature(
57
        SignedElementInterface $signedElement,
58
        CertificateProvider $configuration,
59
    ): bool {
60
        foreach ($this->validators as $validator) {
61
            if ($validator->canValidate($signedElement, $configuration)) {
62
                $this->logger->debug(sprintf(
63
                    'Validating the signed element with validator of type "%s"',
64
                    get_class($validator),
65
                ));
66
67
                return $validator->hasValidSignature($signedElement, $configuration);
68
            }
69
70
            $this->logger->debug(sprintf(
71
                'Could not validate the signed element with validator of type "%s"',
72
                get_class($validator),
73
            ));
74
        }
75
76
        throw new MissingConfigurationException(sprintf(
77
            'No certificates have been configured%s',
78
            $configuration->has('entityid') ? ' for "' . $configuration->get('entityid') . '"' : '',
79
        ));
80
    }
81
}
82