77use SimpleSAML \Auth \State ;
88use SimpleSAML \Configuration ;
99use SimpleSAML \Error \Exception ;
10- use SimpleSAML \Locale \Translate ;
1110use SimpleSAML \Logger ;
1211use SimpleSAML \Module ;
1312use SimpleSAML \Module \discopower \PowerIdPDisco ;
@@ -34,6 +33,10 @@ class Disco extends PowerIdPDisco
3433 # ROOT CONFIGURATION ENTRY
3534 public const WAYF = 'wayf_config ' ;
3635
36+ public const INTERFACE = 'interface ' ;
37+
38+ public const RPC = 'rpc ' ;
39+
3740 # CONFIGURATION ENTRIES
3841 public const BOXED = 'boxed ' ;
3942
@@ -43,7 +46,7 @@ class Disco extends PowerIdPDisco
4346
4447 public const DISABLE_WHITELISTING = 'disable_whitelisting ' ;
4548
46- public const DISPLAY_SP_NAME = 'display_sp_name ' ;
49+ public const DISPLAY_SP = 'display_sp_name ' ;
4750
4851 # CONFIGURATION ENTRIES IDP BLOCKS
4952 public const IDP_BLOCKS = 'idp_blocks_config ' ;
@@ -128,13 +131,17 @@ class Disco extends PowerIdPDisco
128131
129132 # DISPLAY SERVICE NAME KEYS
130133
131- public const CLIENT_ID_PREFIX = 'urn:cesnet:proxyidp:Client_id : ' ;
134+ public const CLIENT_ID_PREFIX = 'urn:cesnet:proxyidp:client_id : ' ;
132135
133- public const INTERFACE = 'interface ' ;
136+ public const SERVICE_NAME_ATTR = 'service_name_attr ' ;
134137
135- public const RPC = 'rpc ' ;
138+ public const SERVICE_NAME_DEFAULT_ATTR_NAME = 'perunFacilityAttr_spname ' ;
139+
140+ public const CLIENT_ID_ATTR = 'client_id_attr ' ;
136141
137- public const SERVICE_NAME_ATTR = 'urn:perun:facility:attribute-def:def:serviceName ' ;
142+ public const ENTITY_ID_ATTR = 'entity_id_attr ' ;
143+
144+ # VARIABLES
138145
139146 private $ originalsp ;
140147
@@ -185,11 +192,6 @@ public function __construct(array $metadataSets, $instance)
185192 State::saveState ($ state , self ::SAML_SP_SSO );
186193 }
187194
188- $ this ->displaySpName = $ this ->wayfConfiguration ->getBoolean (self ::DISPLAY_SP_NAME , false );
189- if ($ this ->displaySpName ) {
190- $ this ->fillSpName ($ state );
191- }
192-
193195 $ e = explode ('= ' , $ returnURL )[0 ];
194196 $ newReturnURL = $ e . '= ' . urlencode ($ id );
195197 $ _GET [self ::RETURN ] = $ newReturnURL ;
@@ -267,6 +269,11 @@ public function handleRequest()
267269 }
268270
269271 $ t = new DiscoTemplate ($ this ->config );
272+ $ this ->displaySpName = $ this ->wayfConfiguration ->getBoolean (self ::DISPLAY_SP , false );
273+ if ($ this ->displaySpName ) {
274+ $ this ->fillSpName ($ t );
275+ }
276+
270277 $ t ->data [self ::ORIGINAL_SP ] = $ this ->originalsp ;
271278 $ t ->data [self ::IDP_LIST ] = $ this ->idplistStructured ($ idpList );
272279 $ t ->data [self ::PREFERRED_IDP ] = $ preferredIdP ;
@@ -277,7 +284,7 @@ public function handleRequest()
277284 $ t ->data [self ::WARNING_ATTRIBUTES ] = $ warningAttributes ;
278285 $ t ->data [self ::WAYF ] = $ this ->wayfConfiguration ;
279286 $ t ->data [self ::NAME ] = $ this ->spName ;
280- $ t ->data [self ::DISPLAY_SP_NAME ] = $ this ->displaySpName ;
287+ $ t ->data [self ::DISPLAY_SP ] = $ this ->displaySpName ;
281288 $ t ->show ();
282289 }
283290
@@ -886,31 +893,61 @@ private static function substrInArray($needle, array $haystack)
886893 return null ;
887894 }
888895
889- private function fillSpName ($ state )
896+ private function fillSpName ($ t )
890897 {
891- $ translate = new Translate (Configuration::getInstance ());
892-
893898 $ clientIdWithPrefix = self ::substrInArray (self ::CLIENT_ID_PREFIX , $ this ->originalAuthnContextClassRef );
894899
895- if ( $ clientIdWithPrefix !== null ) {
896- $ parts = explode ( ' : ' , $ clientIdWithPrefix );
897- $ clientId = end ( $ parts );
898-
899- $ this -> adapter = Adapter:: getInstance ( $ this -> wayfConfiguration -> getString ( self :: INTERFACE , self :: RPC ) );
900+ $ this -> adapter = Adapter:: getInstance ( $ this -> wayfConfiguration -> getString ( self :: INTERFACE , self :: RPC ));
901+ try {
902+ if ( $ clientIdWithPrefix !== null ) {
903+ $ parts = explode ( ' : ' , $ clientIdWithPrefix );
904+ $ clientId = end ( $ parts );
900905
901- $ facility = $ this ->adapter ->getFacilityByClientId ($ clientId );
906+ $ clientIdAttr = $ this ->wayfConfiguration ->getString (self ::CLIENT_ID_ATTR , null );
907+ if ($ clientIdAttr === null ) {
908+ $ facility = $ this ->adapter ->getFacilityByClientId ($ clientId );
909+ } else {
910+ $ facility = $ this ->adapter ->getFacilityByClientId ($ clientId , $ clientIdAttr );
911+ }
902912
903- if ($ facility !== null ) {
904- $ spNameMap = $ this ->adapter ->getFacilityAttribute ($ facility , self ::SERVICE_NAME_ATTR );
905- }
913+ if ($ facility !== null ) {
914+ $ spNameAttrName = $ this ->wayfConfiguration ->getString (
915+ self ::SERVICE_NAME_ATTR ,
916+ self ::SERVICE_NAME_DEFAULT_ATTR_NAME
917+ );
918+ $ spNameMap = $ this ->adapter ->getFacilityAttribute ($ facility , $ spNameAttrName );
919+ if (! empty ($ spNameMap )) {
920+ $ this ->spName = $ t ->getTranslation ($ spNameMap );
921+ }
922+ }
923+ } else {
924+ $ entityId = $ this ->originalsp ['entityid ' ];
925+ $ entityIdAttr = $ this ->wayfConfiguration ->getString (self ::ENTITY_ID_ATTR , null );
926+ if ($ entityIdAttr === null ) {
927+ $ facility = $ this ->adapter ->getFacilityByEntityId ($ entityId );
928+ } else {
929+ $ facility = $ this ->adapter ->getFacilityByEntityId ($ entityId , $ entityIdAttr );
930+ }
906931
907- if (! empty ($ spNameMap )) {
908- $ this ->spName = $ translate ->getPreferredTranslation ($ spNameMap );
909- }
910- } else {
911- if (! empty ($ state [self ::STATE_SP_METADATA ][self ::NAME ])) {
912- $ this ->spName = $ translate ->getPreferredTranslation ($ state [self ::STATE_SP_METADATA ][self ::NAME ]);
932+ if ($ facility !== null ) {
933+ $ spNameAttr = $ this ->wayfConfiguration ->getString (
934+ self ::SERVICE_NAME_ATTR ,
935+ self ::SERVICE_NAME_DEFAULT_ATTR_NAME
936+ );
937+ $ spNameMap = $ this ->adapter ->getFacilityAttribute ($ facility , $ spNameAttr );
938+ if (! empty ($ spNameMap )) {
939+ $ this ->spName = $ t ->getTranslation ($ spNameMap );
940+ }
941+ }
942+ if (empty ($ entityId )) {
943+ if (! empty ($ this ->originalsp [self ::NAME ])) {
944+ $ this ->spName = $ t ->translate ->getTranslation ($ this ->originalsp [self ::NAME ]);
945+ }
946+ }
913947 }
948+ } catch (\Exception $ e ) {
949+ Logger::warning ("Fill SP name - caught exception $ {e}" );
950+ //OK, we will just display the disco
914951 }
915952 }
916953}
0 commit comments