Cancelled
Branch master (244451)
by Tim
10:31
created

Metadata::invokeTest()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 5
nop 0
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite;
4
5
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration;
6
use \SimpleSAML\Module\monitor\TestCase as TestCase;
7
use \SimpleSAML\Module\monitor\TestData as TestData;
8
9
final class Metadata extends \SimpleSAML\Module\monitor\TestSuiteFactory
10
{
11
    /**
12
     * @param array
13
     */
14
    private $metadata = array();
15
16
    /**
17
     * @param TestConfiguration $configuration
18
     */
19
    public function __construct($configuration)
20
    {
21
        $moduleConfig = $configuration->getModuleConfig();
22
        $metadataConfig = $configuration->getMetadataConfig();
23
24
        $checkMetadata = $moduleConfig->getValue('checkMetadata', true);
25
        if ($checkMetadata === true) {
26
            $metadata = $metadataConfig;
27
        } else {
28
            $metadata = array();
29
            if (is_array($checkMetadata)) {
30
                foreach ($checkMetadata as $set => $entityId) {
31
                    if (array_key_exists($set, $metadataConfig)) {
32
                        if (array_key_exists($entityId, $metadataConfig[$set])) {
33
                            $metadata[$set][$entityId] = $metadataConfig[$set][$entityId];
34
                        }
35
                    }
36
                }
37
            }
38
        } 
39
40
        $this->fixEntityIds($metadata);
41
        $this->metadata = $metadata;
42
        $this->setCategory('Metadata');
43
44
        parent::__construct($configuration);
45
    }
46
47
    /**
48
     * @return void
49
     */
50
    public function invokeTest()
51
    {
52
        foreach ($this->metadata as $set => $metadataSet) {
53
            foreach ($metadataSet as $entityId => $entityMetadata) {
54
                $input = array(
55
                    'entityId' => $entityId,
56
                    'metadata' => $entityMetadata
57
                );
58
                $testData = new TestData($input);
59
60
                $expTest = new TestCase\Metadata\Expiration($this, $testData);
61
                $expTestResult = $expTest->getTestResult();
62
                $expTestResult->setSubject($entityId);
63
                $this->addTestResult($expTestResult);
64
65
                if (array_key_exists('keys', $entityMetadata)) {
66
                    $keys = $entityMetadata['keys'];
67
                    foreach ($keys as $key) {
68
                        $input = array(
69
                            'category' => $this->getType($key),
70
                            'certData' => "-----BEGIN CERTIFICATE-----\n" . $key['X509Certificate'] . "\n-----END CERTIFICATE-----"
71
                        );
72
                        $testData = new TestData($input);
73
74
                        $certTest = new TestCase\Cert\Data($this, $testData);
75
                        $certTestResult = $certTest->getTestResult();
76
                        $certTestResult->setSubject($entityId);
77
                        $this->addTestResult($certTestResult);
78
                    }
79
                }
80
            }
81
        }
82
83
        $this->calculateState();
84
    }
85
86
87
    /**
88
     * @param array $key
89
     *
90
     * @return string
91
     */
92
    public function getType($key)
93
    {
94
        if ($key['encryption'] === true && $key['signing'] === false) {
95
            $category = 'Encryption certificate';
96
        } elseif ($key['encryption'] === false && $key['signing'] === true) {
97
            $category = 'Signing certificate';
98
        } else {
99
            $category = 'Unknown type';
100
        }
101
        return $category;
102
    }
103
104
    /**
105
     * @param array $metadata
106
     *
107
     * @return void
108
     */
109
    private function fixEntityIds(&$metadata)
110
    {
111
        foreach ($metadata as $set => $metadataSet) {
112
            foreach ($metadataSet as $entityId => $entityMetadata) {
113
                if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
114
                    // Remove old entry and create a new one based on new entityId
115
                    unset($metadata[$set][$entityId]);
116
                    $newEntityId = $this->generateDynamicHostedEntityID($set);
117
                    $metadata[$set][$newEntityId] = $entityMetadata;
118
                }
119
            }
120
        }
121
    }
122
123
    // Borrowed this from lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php until we take care of different sources properly
124
    /**
125
     * @return string
126
     */
127
    private function generateDynamicHostedEntityID($set)
128
    {
129
        // get the configuration
130
        $baseurl = \SimpleSAML\Utils\HTTP::getBaseURL();
131
132
        if ($set === 'saml20-idp-hosted') {
133
            return $baseurl.'saml2/idp/metadata.php';
134
        } elseif ($set === 'shib13-idp-hosted') {
135
            return $baseurl.'shib13/idp/metadata.php';
136
        } elseif ($set === 'wsfed-sp-hosted') {
137
            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
138
        } elseif ($set === 'adfs-idp-hosted') {
139
            return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp';
140
        } else {
141
            throw new \Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']');
142
        }
143
    }
144
}
145
146