@@ -33,6 +33,10 @@ class Disco extends PowerIdPDisco
3333 # ROOT CONFIGURATION ENTRY
3434 public const WAYF = 'wayf_config ' ;
3535
36+ public const INTERFACE = 'interface ' ;
37+
38+ public const RPC = 'rpc ' ;
39+
3640 # CONFIGURATION ENTRIES
3741 public const BOXED = 'boxed ' ;
3842
@@ -42,6 +46,8 @@ class Disco extends PowerIdPDisco
4246
4347 public const DISABLE_WHITELISTING = 'disable_whitelisting ' ;
4448
49+ public const DISPLAY_SP = 'display_sp_name ' ;
50+
4551 # CONFIGURATION ENTRIES IDP BLOCKS
4652 public const IDP_BLOCKS = 'idp_blocks_config ' ;
4753
@@ -121,6 +127,22 @@ class Disco extends PowerIdPDisco
121127
122128 public const SAML_SP_SSO = 'saml:sp:sso ' ;
123129
130+ public const NAME = 'name ' ;
131+
132+ # DISPLAY SERVICE NAME KEYS
133+
134+ public const CLIENT_ID_PREFIX = 'urn:cesnet:proxyidp:client_id: ' ;
135+
136+ public const SERVICE_NAME_ATTR = 'service_name_attr ' ;
137+
138+ public const SERVICE_NAME_DEFAULT_ATTR_NAME = 'perunFacilityAttr_spname ' ;
139+
140+ public const CLIENT_ID_ATTR = 'client_id_attr ' ;
141+
142+ public const ENTITY_ID_ATTR = 'entity_id_attr ' ;
143+
144+ # VARIABLES
145+
124146 private $ originalsp ;
125147
126148 private array $ originalAuthnContextClassRef = [];
@@ -129,6 +151,12 @@ class Disco extends PowerIdPDisco
129151
130152 private $ perunModuleConfiguration ;
131153
154+ private $ displaySpName ;
155+
156+ private $ spName ;
157+
158+ private $ adapter ;
159+
132160 private $ proxyIdpEntityId ;
133161
134162 public function __construct (array $ metadataSets , $ instance )
@@ -156,12 +184,14 @@ public function __construct(array $metadataSets, $instance)
156184 if ($ state !== null ) {
157185 if (isset ($ state [self ::SAML_REQUESTED_AUTHN_CONTEXT ][self ::AUTHN_CONTEXT_CLASS_REF ])) {
158186 $ this ->originalAuthnContextClassRef = $ state [self ::SAML_REQUESTED_AUTHN_CONTEXT ][self ::AUTHN_CONTEXT_CLASS_REF ];
187+
159188 $ this ->removeAuthContextClassRefWithPrefixes ($ state );
160189 if (isset ($ state ['IdPMetadata ' ]['entityid ' ])) {
161190 $ this ->proxyIdpEntityId = $ state ['IdPMetadata ' ]['entityid ' ];
162191 }
163192 State::saveState ($ state , self ::SAML_SP_SSO );
164193 }
194+
165195 $ e = explode ('= ' , $ returnURL )[0 ];
166196 $ newReturnURL = $ e . '= ' . urlencode ($ id );
167197 $ _GET [self ::RETURN ] = $ newReturnURL ;
@@ -239,6 +269,11 @@ public function handleRequest()
239269 }
240270
241271 $ 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+
242277 $ t ->data [self ::ORIGINAL_SP ] = $ this ->originalsp ;
243278 $ t ->data [self ::IDP_LIST ] = $ this ->idplistStructured ($ idpList );
244279 $ t ->data [self ::PREFERRED_IDP ] = $ preferredIdP ;
@@ -248,6 +283,8 @@ public function handleRequest()
248283 $ t ->data [self ::AUTHN_CONTEXT_CLASS_REF ] = $ this ->originalAuthnContextClassRef ;
249284 $ t ->data [self ::WARNING_ATTRIBUTES ] = $ warningAttributes ;
250285 $ t ->data [self ::WAYF ] = $ this ->wayfConfiguration ;
286+ $ t ->data [self ::NAME ] = $ this ->spName ;
287+ $ t ->data [self ::DISPLAY_SP ] = $ this ->displaySpName ;
251288 $ t ->show ();
252289 }
253290
@@ -844,4 +881,73 @@ private static function constructSearchData($idpMetadata): string
844881
845882 return strtolower (str_replace ('" ' , '' , iconv ('UTF-8 ' , 'US-ASCII//TRANSLIT ' , $ res )));
846883 }
884+
885+ private static function substrInArray ($ needle , array $ haystack )
886+ {
887+ foreach ($ haystack as $ item ) {
888+ if (strpos ($ item , $ needle ) !== false ) {
889+ return $ item ;
890+ }
891+ }
892+
893+ return null ;
894+ }
895+
896+ private function fillSpName ($ t )
897+ {
898+ $ clientIdWithPrefix = self ::substrInArray (self ::CLIENT_ID_PREFIX , $ this ->originalAuthnContextClassRef );
899+
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 );
905+
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+ }
912+
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+ }
931+
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+ }
947+ }
948+ } catch (\Exception $ e ) {
949+ Logger::warning ("Fill SP name - caught exception $ {e}" );
950+ //OK, we will just display the disco
951+ }
952+ }
847953}
0 commit comments