Entity   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 65
c 0
b 0
f 0
dl 0
loc 142
rs 10
wmc 16

5 Methods

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