@@ -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 /**
0 commit comments