Passed
Branch master (4b23d6)
by Tim
04:40
created

Entity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SimpleSAML\Module\Monitor\TestSuite\Metadata;
4
5
use SimpleSAML\Module\Monitor\State;
6
use SimpleSAML\Module\Monitor\TestConfiguration;
7
use SimpleSAML\Module\Monitor\TestCase;
8
use SimpleSAML\Module\Monitor\TestData;
9
use SimpleSAML\Module\Monitor\TestResult;
10
11
final class Entity extends \SimpleSAML\Module\Monitor\TestSuiteFactory
12
{
13
    /** @var array */
14
    private $entityMetadata;
15
16
    /** @var string */
17
    private $entityId;
18
19
    /** @var integer|null */
20
    private $certExpirationWarning = null;
21
22
23
    /**
24
     * @param \SimpleSAML\Module\Monitor\TestConfiguration $configuration
25
     * @param \SimpleSAML\Module\Monitor\TestData $testData
26
     */
27
    public function __construct(TestConfiguration $configuration, TestData $testData)
28
    {
29
        $moduleConfig = $configuration->getModuleConfig();
30
        $entityMetadata = $testData->getInputItem('entityMetadata');
31
        $entityId = $testData->getInputItem('entityId');
32
33
        assert(is_array($entityMetadata));
34
        assert(is_string($entityId));
35
36
        $this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28);
37
        $this->entityMetadata = $entityMetadata;
38
        $this->entityId = $entityId;
39
40
        $this->setCategory('Metadata entity');
41
        parent::__construct($configuration);
42
    }
43
44
45
    /**
46
     * @return void
47
     */
48
    public function invokeTest(): void
49
    {
50
        $input = [
51
            'entityId' => $this->entityId,
52
            'entityMetadata' => $this->entityMetadata,
53
        ];
54
        $testData = new TestData($input);
55
56
        $expTest = new TestCase\Metadata\Expiration($testData);
57
        $expTestResult = $expTest->getTestResult();
58
        $expTestResult->setSubject($this->entityId);
59
        $this->addTestResult($expTestResult);
60
61
        if (array_key_exists('keys', $this->entityMetadata)) {
62
            $keys = $this->entityMetadata['keys'];
63
64
65
            $signing = array_filter($keys, [self::class, 'getSigning']);
0 ignored issues
show
Unused Code introduced by
The assignment to $signing is dead and can be removed.
Loading history...
66
            $encryption = array_filter($keys, [self::class, 'getEncryption']);
0 ignored issues
show
Unused Code introduced by
The assignment to $encryption is dead and can be removed.
Loading history...
67
68
            foreach ($keys as $key) {
69
                $input = [
70
                    'category' => $this->getType($key),
71
                    'certData' => "-----BEGIN CERTIFICATE-----\n"
72
                        . chunk_split($key['X509Certificate'], 64)
73
                        . "-----END CERTIFICATE-----\n",
74
                    'certExpirationWarning' => $this->certExpirationWarning,
75
                ];
76
                $testData = new TestData($input);
77
78
                $certTest = new TestCase\Cert\Data($testData);
79
                $certTestResult = $certTest->getTestResult();
80
81
                $this->addTestResult($certTestResult);
82
            }
83
        } else {
84
            // saml20-idp-hosted
85
            $files = [];
86
            if (array_key_exists('certificate', $this->entityMetadata)) {
87
                $files[] = $this->entityMetadata['certificate'];
88
            }
89
            if (array_key_exists('new_certificate', $this->entityMetadata)) {
90
                $files[] = $this->entityMetadata['new_certificate'];
91
            }
92
93
            foreach ($files as $file) {
94
                $input = [
95
                    'category' => $this->getType(['signing' => true, 'encryption' => false]),
96
                    'certFile' => \SimpleSAML\Utils\Config::getCertPath($file),
97
                    'certExpirationWarning' => $this->certExpirationWarning,
98
                ];
99
100
                $testData = new TestData($input);
101
102
                $certTest = new TestCase\Cert\File($testData);
103
                $certTestResult = $certTest->getTestResult();
104
105
                $this->addTestResult($certTestResult);
106
            }
107
        }
108
109
        $state = $this->calculateState();
110
111
        $testResult = new TestResult('Metadata endpoint');
112
        $testResult->setState($state);
113
        $this->setTestResult($testResult);
114
    }
115
116
117
    /**
118
     * @param array $key
119
     * @return bool
120
     */
121
    private function getSigning(array $key): bool
122
    {
123
        return ($key['signing'] === true) && ($key['encryption'] === false);
124
    }
125
126
127
    /**
128
     * @param array $key
129
     * @return bool
130
     */
131
    private function getEncryption(array $key): bool
132
    {
133
        return ($key['signing'] === false) && ($key['encryption'] === true);
134
    }
135
136
137
    /**
138
     * @param array $key
139
     *
140
     * @return string
141
     */
142
    public function getType(array $key): string
143
    {
144
        if ($key['encryption'] === true && $key['signing'] === false) {
145
            $category = 'Encryption certificate';
146
        } elseif ($key['encryption'] === false && $key['signing'] === true) {
147
            $category = 'Signing certificate';
148
        } else {
149
            $category = 'Unknown type';
150
        }
151
        return $category;
152
    }
153
}
154