77use SimpleSAML \Auth \State ;
88use SimpleSAML \Configuration ;
99use SimpleSAML \Error \Exception ;
10+ use SimpleSAML \Locale \Translate ;
1011use SimpleSAML \Logger ;
1112use SimpleSAML \Module ;
1213use SimpleSAML \Module \discopower \PowerIdPDisco ;
@@ -42,6 +43,8 @@ class Disco extends PowerIdPDisco
4243
4344 public const DISABLE_WHITELISTING = 'disable_whitelisting ' ;
4445
46+ public const DISPLAY_SP_NAME = 'display_sp_name ' ;
47+
4548 # CONFIGURATION ENTRIES IDP BLOCKS
4649 public const IDP_BLOCKS = 'idp_blocks_config ' ;
4750
@@ -121,6 +124,18 @@ class Disco extends PowerIdPDisco
121124
122125 public const SAML_SP_SSO = 'saml:sp:sso ' ;
123126
127+ public const NAME = 'name ' ;
128+
129+ # DISPLAY SERVICE NAME KEYS
130+
131+ public const CLIENT_ID_PREFIX = 'urn:cesnet:proxyidp:Client_id: ' ;
132+
133+ public const INTERFACE = 'interface ' ;
134+
135+ public const RPC = 'rpc ' ;
136+
137+ public const SERVICE_NAME_ATTR = 'urn:perun:facility:attribute-def:def:serviceName ' ;
138+
124139 private $ originalsp ;
125140
126141 private array $ originalAuthnContextClassRef = [];
@@ -129,6 +144,12 @@ class Disco extends PowerIdPDisco
129144
130145 private $ perunModuleConfiguration ;
131146
147+ private $ displaySpName ;
148+
149+ private $ spName ;
150+
151+ private $ adapter ;
152+
132153 private $ proxyIdpEntityId ;
133154
134155 public function __construct (array $ metadataSets , $ instance )
@@ -156,12 +177,19 @@ public function __construct(array $metadataSets, $instance)
156177 if ($ state !== null ) {
157178 if (isset ($ state [self ::SAML_REQUESTED_AUTHN_CONTEXT ][self ::AUTHN_CONTEXT_CLASS_REF ])) {
158179 $ this ->originalAuthnContextClassRef = $ state [self ::SAML_REQUESTED_AUTHN_CONTEXT ][self ::AUTHN_CONTEXT_CLASS_REF ];
180+
159181 $ this ->removeAuthContextClassRefWithPrefixes ($ state );
160182 if (isset ($ state ['IdPMetadata ' ]['entityid ' ])) {
161183 $ this ->proxyIdpEntityId = $ state ['IdPMetadata ' ]['entityid ' ];
162184 }
163185 State::saveState ($ state , self ::SAML_SP_SSO );
164186 }
187+
188+ $ this ->displaySpName = $ this ->wayfConfiguration ->getBoolean (self ::DISPLAY_SP_NAME , false );
189+ if ($ this ->displaySpName ) {
190+ $ this ->fillSpName ($ state );
191+ }
192+
165193 $ e = explode ('= ' , $ returnURL )[0 ];
166194 $ newReturnURL = $ e . '= ' . urlencode ($ id );
167195 $ _GET [self ::RETURN ] = $ newReturnURL ;
@@ -248,6 +276,8 @@ public function handleRequest()
248276 $ t ->data [self ::AUTHN_CONTEXT_CLASS_REF ] = $ this ->originalAuthnContextClassRef ;
249277 $ t ->data [self ::WARNING_ATTRIBUTES ] = $ warningAttributes ;
250278 $ t ->data [self ::WAYF ] = $ this ->wayfConfiguration ;
279+ $ t ->data [self ::NAME ] = $ this ->spName ;
280+ $ t ->data [self ::DISPLAY_SP_NAME ] = $ this ->displaySpName ;
251281 $ t ->show ();
252282 }
253283
@@ -844,4 +874,43 @@ private static function constructSearchData($idpMetadata): string
844874
845875 return strtolower (str_replace ('" ' , '' , iconv ('UTF-8 ' , 'US-ASCII//TRANSLIT ' , $ res )));
846876 }
877+
878+ private static function substrInArray ($ needle , array $ haystack )
879+ {
880+ foreach ($ haystack as $ item ) {
881+ if (strpos ($ item , $ needle ) !== false ) {
882+ return $ item ;
883+ }
884+ }
885+
886+ return null ;
887+ }
888+
889+ private function fillSpName ($ state )
890+ {
891+ $ translate = new Translate (Configuration::getInstance ());
892+
893+ $ clientIdWithPrefix = self ::substrInArray (self ::CLIENT_ID_PREFIX , $ this ->originalAuthnContextClassRef );
894+
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+
901+ $ facility = $ this ->adapter ->getFacilityByClientId ($ clientId );
902+
903+ if ($ facility !== null ) {
904+ $ spNameMap = $ this ->adapter ->getFacilityAttribute ($ facility , self ::SERVICE_NAME_ATTR );
905+ }
906+
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 ]);
913+ }
914+ }
915+ }
847916}
0 commit comments