Skip to content

Commit 2a67e9e

Browse files
authored
Correct host in generated URLS for IdPs with 'host' config in admin/federation (#1781)
It gets quite fiddly to get this right. The actual metadata (hosted on the `host` itself) is alreafy correct because the URL generator uses the 'current' entity metadata and current URL. The admin interface of course presents all entities in one page and hence cannot rely on the current URL. Therefore we need to override the url host for this specific display case. Closes: #1774
1 parent 6346cd9 commit 2a67e9e

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

modules/admin/src/Controller/Federation.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,14 @@ private function getHostedIdP(): array
198198
$httpUtils = new Utils\HTTP();
199199
$metadataBase = Module::getModuleURL('saml/idp/metadata');
200200
if (count($idps) > 1) {
201+
$selfHost = $httpUtils->getSelfHost();
201202
foreach ($idps as $index => $idp) {
202-
$idp['url'] = $metadataBase . '?idpentityid=' . urlencode($idp['entityid']);
203+
if (isset($idp['host']) && $idp['host'] !== '__DEFAULT__') {
204+
$mdHostBase = str_replace('://' . $selfHost, '://' . $idp['host'], $metadataBase);
205+
} else {
206+
$mdHostBase = $metadataBase;
207+
}
208+
$idp['url'] = $mdHostBase . '?idpentityid=' . urlencode($idp['entityid']);
203209
$idp['metadata-set'] = 'saml20-idp-hosted';
204210
$idp['metadata-index'] = $index;
205211
$idp['metadata_array'] = SAML2_IdP::getHostedMetadata($idp['entityid']);

modules/admin/templates/federation.twig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@
2121
<dl>
2222
<dt>{{ set|entityDisplayName }}</dt>
2323

24-
<dd>EntityID: <code>{{ set.entityid }}</code></dd>
24+
<dd>EntityID: <code>{{ set.entityid }}</code>
25+
{% if set.type == 'saml20-idp-hosted' and set.host is defined %}
26+
(hostname:
27+
{% if set.host == '__DEFAULT__' %}
28+
<em>{{ 'default'|trans }}</em>
29+
{%- else %}
30+
<code>{{ set.host }}</code>
31+
{%- endif -%}
32+
)
33+
{% endif %}
34+
</dd>
2535
{%- if set.deprecated is defined and set.deprecated %}
2636

2737
<dd><span class="entity-deprecated">{{ 'Deprecated'|trans }}</span></dd>

modules/saml/src/IdP/SAML2.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,11 +762,14 @@ public static function getHostedMetadata(string $entityid, MetaDataStorageHandle
762762
}
763763
$config = $handler->getMetaDataConfig($entityid, 'saml20-idp-hosted');
764764

765+
$host = $config->getOptionalString('host', null);
766+
$host = $host === '__DEFAULT__' ? null : $host;
767+
765768
// configure endpoints
766-
$ssob = $handler->getGenerated('SingleSignOnServiceBinding', 'saml20-idp-hosted');
767-
$slob = $handler->getGenerated('SingleLogoutServiceBinding', 'saml20-idp-hosted');
768-
$ssol = $handler->getGenerated('SingleSignOnService', 'saml20-idp-hosted');
769-
$slol = $handler->getGenerated('SingleLogoutService', 'saml20-idp-hosted');
769+
$ssob = $handler->getGenerated('SingleSignOnServiceBinding', 'saml20-idp-hosted', $host);
770+
$slob = $handler->getGenerated('SingleLogoutServiceBinding', 'saml20-idp-hosted', $host);
771+
$ssol = $handler->getGenerated('SingleSignOnService', 'saml20-idp-hosted', $host);
772+
$slol = $handler->getGenerated('SingleLogoutService', 'saml20-idp-hosted', $host);
770773

771774
$sso = [];
772775
if (is_array($ssob)) {

src/SimpleSAML/Metadata/MetaDataStorageHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ protected function __construct()
8383
*
8484
* @param string $property The metadata property which should be auto-generated.
8585
* @param string $set The set we the property comes from.
86+
* @param string $overrideHost Hostname to use in the URLs
8687
*
8788
* @return string|array The auto-generated metadata property.
8889
* @throws \Exception If the metadata cannot be generated automatically.
8990
*/
90-
public function getGenerated(string $property, string $set)
91+
public function getGenerated(string $property, string $set, string $overrideHost = null)
9192
{
9293
// first we check if the user has overridden this property in the metadata
9394
try {
@@ -101,9 +102,11 @@ public function getGenerated(string $property, string $set)
101102

102103
// get the configuration
103104
$config = Configuration::getInstance();
104-
105105
$httpUtils = new Utils\HTTP();
106106
$baseurl = $httpUtils->getSelfURLHost() . $config->getBasePath();
107+
if ($overrideHost !== null) {
108+
$baseurl = str_replace('://' . $httpUtils->getSelfHost(), '://' . $overrideHost, $baseurl);
109+
}
107110

108111
if ($set == 'saml20-sp-hosted') {
109112
if ($property === 'SingleLogoutServiceBinding') {

0 commit comments

Comments
 (0)