-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathMetadataFactory.php
More file actions
132 lines (106 loc) · 4.41 KB
/
MetadataFactory.php
File metadata and controls
132 lines (106 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Copyright 2014 SURFnet bv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Surfnet\SamlBundle\Metadata;
use SAML2\DOMDocumentFactory;
use SAML2\Utilities\Certificate;
use SAML2\Utilities\File;
use Surfnet\SamlBundle\Service\SigningService;
use Surfnet\SamlBundle\Signing\KeyPair;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
use Surfnet\SamlBundle\Exception\RuntimeException;
class MetadataFactory
{
private Metadata $metadata;
public function __construct(
private readonly Environment $templateEngine,
private readonly RouterInterface $router,
private readonly SigningService $signingService,
private readonly MetadataConfiguration $metadataConfiguration
) {
$this->metadata = $this->buildMetadata();
}
public function getMetadata(): Metadata
{
return $this->metadata;
}
public function generate(): Metadata
{
$metadata = $this->getMetadata();
$keyPair = $this->buildKeyPairFrom($this->metadataConfiguration);
$metadata->document = DOMDocumentFactory::create();
$metadata->document->loadXML($this->templateEngine->render(
'@SurfnetSaml/Metadata/metadata.xml.twig',
['metadata' => $metadata]
));
$this->signingService->sign($metadata, $keyPair);
return $metadata;
}
private function buildMetadata(): Metadata
{
$metadataConfiguration = $this->metadataConfiguration;
$metadata = new Metadata();
$metadata->entityId = $this->getUrl($metadataConfiguration->entityIdRoute);
if ($metadataConfiguration->isSp) {
$metadata->hasSpMetadata = true;
$metadata->assertionConsumerUrl = $this->getUrl($metadataConfiguration->assertionConsumerRoute);
if ($metadataConfiguration->spCertificate &&
$metadataConfiguration->spCertificate !== '' &&
$metadataConfiguration->spCertificate !== '0'
) {
$metadata->spCertificate = $this->getCertificateData($metadataConfiguration->spCertificate);
}
}
if ($metadataConfiguration->isIdP) {
$metadata->hasIdPMetadata = true;
$metadata->ssoUrl = $this->getUrl($metadataConfiguration->ssoRoute);
if ($metadataConfiguration->idpCertificate &&
$metadataConfiguration->idpCertificate !== '' &&
$metadataConfiguration->idpCertificate !== '0'
) {
$certificate = $this->getCertificateData($metadataConfiguration->idpCertificate);
} else {
$certificate = $this->getCertificateData($metadataConfiguration->publicKey);
}
$metadata->idpCertificate = $certificate;
}
return $metadata;
}
private function buildKeyPairFrom(MetadataConfiguration $metadataConfiguration): KeyPair
{
$keyPair = new KeyPair();
$keyPair->privateKeyFile = $metadataConfiguration->privateKey;
$keyPair->publicKeyFile = $metadataConfiguration->publicKey;
return $keyPair;
}
private function getCertificateData(string $publicKeyFile): string
{
$certificate = File::getFileContents($publicKeyFile);
$matches = [];
preg_match(Certificate::CERTIFICATE_PATTERN, $certificate, $matches);
if (! isset($matches[1])) {
throw new RuntimeException(sprintf('Could not parse PEM certificate in %s', $publicKeyFile));
}
return str_replace([' ', "\n"], '', $matches[1]);
}
private function getUrl(null|string|array $routeDefinition): string
{
$route = is_array($routeDefinition) ? $routeDefinition['route'] : $routeDefinition;
$parameters = is_array($routeDefinition) ? $routeDefinition['parameters'] : [];
return $this->router->generate($route ?? '', $parameters, RouterInterface::ABSOLUTE_URL);
}
}