Skip to content

Commit 784b7dc

Browse files
27pchrislatymic
andauthored
[Saml2] Allow the administrator to choose an entity descriptor to use (#984)
Co-authored-by: atymic <atymicq@gmail.com>
1 parent aa85692 commit 784b7dc

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

Provider.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,20 +267,33 @@ protected function getIdentityProviderEntityDescriptorManually(): EntityDescript
267267
return $builder->get();
268268
}
269269

270-
protected function getFirstEntityDescriptorFromXml(string $xml): EntityDescriptor
270+
protected function getIdpEntityDescriptorFromXml(string $xml): EntityDescriptor
271271
{
272-
$descriptor = Metadata::fromXML($xml, new DeserializationContext());
272+
/** @var EntitiesDescriptor|EntityDescriptor $metadata */
273+
$metadata = Metadata::fromXML($xml, new DeserializationContext());
273274

274-
if ($descriptor instanceof EntitiesDescriptor) {
275-
return Arr::first($descriptor->getAllEntityDescriptors());
275+
if ($metadata instanceof EntityDescriptor) {
276+
return $metadata;
276277
}
277278

278-
return $descriptor;
279+
$entityId = $this->getConfig('entityid');
280+
281+
if (!$entityId) {
282+
return Arr::first($metadata->getAllEntityDescriptors());
283+
}
284+
285+
$entityDescriptor = $metadata->getByEntityId($entityId);
286+
287+
if (null === $entityDescriptor) {
288+
throw new MissingConfigException(sprintf('The IDP descriptor with entity id %s could not be found in the metadata.', $entityId));
289+
}
290+
291+
return $entityDescriptor;
279292
}
280293

281294
protected function getIdentityProviderEntityDescriptorFromXml(): EntityDescriptor
282295
{
283-
return $this->getFirstEntityDescriptorFromXml($this->getConfig('metadata'));
296+
return $this->getIdpEntityDescriptorFromXml($this->getConfig('metadata'));
284297
}
285298

286299
protected function getIdentityProviderEntityDescriptorFromUrl(): EntityDescriptor
@@ -290,7 +303,7 @@ protected function getIdentityProviderEntityDescriptorFromUrl(): EntityDescripto
290303
$ttl = Cache::get(self::METADATA_CACHE_KEY_TTL);
291304

292305
if ($xml && $ttl && $ttl + $this->getConfig('ttl', 86400) > time()) {
293-
return $this->getFirstEntityDescriptorFromXml($xml);
306+
return $this->getIdpEntityDescriptorFromXml($xml);
294307
}
295308

296309
Cache::forever(self::METADATA_CACHE_KEY_TTL, time());
@@ -307,7 +320,7 @@ protected function getIdentityProviderEntityDescriptorFromUrl(): EntityDescripto
307320
}
308321
}
309322

310-
return $this->getFirstEntityDescriptorFromXml($xml);
323+
return $this->getIdpEntityDescriptorFromXml($xml);
311324
}
312325

313326
/**

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ Using a metadata URL is highly recommended if your IDP supports it, so that cert
2929
],
3030
```
3131

32+
The provider will automatically choose the first IdP descriptor in your metadata.
33+
If your metadata contains multiple descriptors, you can choose the one to use by using both the `metadata` and `entityid`
34+
configuration options at the same time.
35+
36+
#### Using an Identity Provider metadata URL, selecting a specific descriptor
37+
```php
38+
'saml2' => [
39+
'metadata' => 'https://idp.co/metadata/xml',
40+
'entityid' => 'http://saml.to/trust',
41+
],
42+
```
43+
3244
#### Manually configuring the Identity Provider with a certificate string:
3345
```php
3446
'saml2' => [

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require": {
2424
"php": "^7.4 || ^8.0",
2525
"ext-json": "*",
26-
"litesaml/lightsaml": "^3.0",
26+
"litesaml/lightsaml": "^4.0",
2727
"socialiteproviders/manager": "~4.0"
2828
},
2929
"autoload": {

0 commit comments

Comments
 (0)