11<?php
22
3- use \SimpleSAML_Configuration as Configuration ;
3+ declare (strict_types=1 );
4+
5+ namespace SimpleSAML \Module \stepup -sfo;
6+
7+ use Exception ;
8+ use SAML2 \AuthnRequest ;
9+ use SAML2 \Binding ;
10+ use SAML2 \Constants as C ;
11+ use SAML2 \XML \saml \NameID ;
12+ use SimpleSAML \Auth ;
13+ use SimpleSAML \Configuration ;
14+ use SimpleSAML \Error ;
15+ use SimpleSAML \Logger ;
16+ use SimpleSAML \Metadata \MetaDataStorageHandler ;
17+ use SimpleSAML \Module ;
18+ use SimpleSAML \Module \saml \Message ;
19+
20+ use function in_array ;
21+ use function sprintf ;
22+ use function substr ;
23+ use function var_export ;
424
525/**
626 * @package SimpleSAMLphp
727 */
8- class sspmod_stepupsfo_Auth_Process_SFO extends SimpleSAML_Auth_ProcessingFilter
28+ class SFO extends Auth \ProcessingFilter
929{
30+ /** @var array */
31+ private array $ metadata ;
1032
11- private $ metadata ;
12- private $ idpMetadata ;
33+ /** @var array */
34+ private array $ idpMetadata ;
35+
36+ /** @var string */
37+ private string $ subjectidattribute ;
38+
39+ /** @var array */
40+ private array $ skipentities = [];
1341
14- private $ subjectidattribute ;
15- private $ skipentities = [];
1642
1743 /**
1844 * Initialize this filter.
@@ -25,7 +51,7 @@ public function __construct(array $config, $reserved)
2551 parent ::__construct ($ config , $ reserved );
2652
2753 $ this ->subjectidattribute = $ config ['subjectattribute ' ];
28- if ( isset ($ config ['skipentities ' ]) ) {
54+ if (isset ($ config ['skipentities ' ]) ) {
2955 $ this ->skipentities = $ config ['skipentities ' ];
3056 }
3157
@@ -35,96 +61,100 @@ public function __construct(array $config, $reserved)
3561 $ this ->metadata = Configuration::loadFromArray ($ config );
3662 }
3763
64+
3865 /**
3966 * Process an authentication response.
4067 *
4168 * @param array $state The state of the response.
4269 */
43- public function process (&$ state )
70+ public function process (array &$ state ): void
4471 {
4572 foreach ($ this ->skipentities as $ skip ) {
4673 if ($ skip === $ state ['SPMetadata ' ]['entityid ' ] || in_array ($ skip , $ state ['saml:RequesterID ' ], true )) {
47- SimpleSAML \ Logger::info ('SFO - skipping SFO for entity ' . var_export ($ skip , true ));
74+ Logger::info ('SFO - skipping SFO for entity ' . var_export ($ skip , true ));
4875 return ;
4976 }
5077 }
5178
5279 $ state ['sfo:sp:metadata ' ] = $ this ->metadata ;
5380 $ state ['sfo:idp:entityid ' ] = $ this ->idpMetadata ->getString ('entityid ' );
54- $ samlstateid = SimpleSAML_Auth_State ::saveState ($ state , 'stepupsfo:pre ' );
81+ $ samlstateid = Auth \State ::saveState ($ state , 'stepupsfo:pre ' );
5582
56- if ( empty ($ state ['Attributes ' ][$ this ->subjectidattribute ]) ) {
83+ if (empty ($ state ['Attributes ' ][$ this ->subjectidattribute ]) ) {
5784 throw new Exception ("Subjectid " . $ this ->subjectidattribute . " not found in attributes. " );
5885 }
5986
6087 $ subjectid = $ state ['Attributes ' ][$ this ->subjectidattribute ][0 ];
61- if ( substr ($ subjectid ,0 ,18 ) !== 'urn:collab:person: ' ) {
88+ if (substr ($ subjectid ,0 ,18 ) !== 'urn:collab:person: ' ) {
6289 throw new Exception ("Subjectid " . var_export ($ subjectid ,true ) . " does not start with urn:collab:person: " );
6390 }
6491
65- $ nameid = new \ SAML2 \ XML \ saml \ NameID ();
92+ $ nameid = new NameID ();
6693 $ nameid ->setValue ($ subjectid );
6794
6895 // Start the authentication request
6996 $ this ->startSFO ($ this ->idpMetadata , $ nameid , $ samlstateid );
7097 }
7198
99+
72100 /**
73101 * Retrieve the metadata of an IdP.
74102 *
75103 * @param string $entityId The entity id of the IdP.
76- * @return SimpleSAML_Configuration The metadata of the IdP.
104+ * @return \SimpleSAML\Configuration The metadata of the IdP.
77105 */
78- public function getIdPMetadata ($ entityId )
106+ public function getIdPMetadata (string $ entityId ): Configuration
79107 {
80- assert (is_string ($ entityId ));
81-
82- $ metadataHandler = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler ();
108+ $ metadataHandler = MetaDataStorageHandler::getMetadataHandler ();
83109
84110 try {
85111 return $ metadataHandler ->getMetaDataConfig ($ entityId , 'saml20-idp-remote ' );
86112 } catch (Exception $ e ) {
87113 /* Metadata wasn't found. */
88- SimpleSAML \ Logger::debug ('getIdpMetadata: ' . $ e ->getMessage ());
114+ Logger::debug ('getIdpMetadata: ' . $ e ->getMessage ());
89115 }
90116
91117 /* Not found. */
92- throw new SimpleSAML_Error_Exception ('Could not find the metadata of an IdP with entity ID ' .
93- var_export ($ entityId , true ));
118+ throw new Error \Exception (sprintf (
119+ 'Could not find the metadata of an IdP with entity ID %s ' ,
120+ var_export ($ entityId , true )
121+ );
94122 }
95123
124+
96125 /**
97126 * Send a SAML2 SSO request to the SFO IdP.
98127 *
99- * @param SimpleSAML_Configuration $idpMetadata The metadata of the IdP.
128+ * @param \SimpleSAML\Configuration $idpMetadata The metadata of the IdP.
100129 * @param \SAML2\XML\saml\NameID $nameid The unspecified NameID of the principal to perform SFO for.
101130 * @param string $relay RelayState to pass
102131 */
103- private function startSFO (SimpleSAML_Configuration $ idpMetadata , \ SAML2 \ XML \ saml \ NameID $ nameid , $ relay )
132+ private function startSFO (Configuration $ idpMetadata , NameID $ nameid , $ relay ): void
104133 {
105- $ ar = sspmod_saml_Message ::buildAuthnRequest ($ this ->metadata , $ idpMetadata );
134+ $ ar = Message ::buildAuthnRequest ($ this ->metadata , $ idpMetadata );
106135
107- $ ar ->setAssertionConsumerServiceURL (SimpleSAML \ Module::getModuleURL ('stepupsfo/acs.php ' ));
136+ $ ar ->setAssertionConsumerServiceURL (Module::getModuleURL ('stepupsfo/acs.php ' ));
108137
109138 $ ar ->setNameId ($ nameid );
110139 $ ar ->setRelayState ($ relay );
111140
112- SimpleSAML \ Logger::debug ('Sending SAML 2 SFO AuthnRequest for ' . $ nameid ->getValue () . ' to ' .
141+ Logger::debug ('Sending SAML 2 SFO AuthnRequest for ' . $ nameid ->getValue () . ' to ' .
113142 var_export ($ idpMetadata ->getString ('entityid ' ), true ). ' with id ' . $ ar ->getId ());
114143
115144 $ dst = $ idpMetadata ->getEndpointPrioritizedByBinding ('SingleSignOnService ' ,
116- [ \ SAML2 \Constants ::BINDING_HTTP_REDIRECT ]
145+ [C ::BINDING_HTTP_REDIRECT ]
117146 );
118147
119148 $ ar ->setDestination ($ dst ['Location ' ]);
120149
121- $ b = \ SAML2 \ Binding::getBinding ($ dst ['Binding ' ]);
150+ $ b = Binding::getBinding ($ dst ['Binding ' ]);
122151
123152 $ this ->sendSAML2AuthnRequest ($ b , $ ar );
124153
125154 assert (false );
126155 }
127156
157+
128158 /**
129159 * Function to actually send the authentication request.
130160 *
@@ -133,7 +163,7 @@ private function startSFO(SimpleSAML_Configuration $idpMetadata, \SAML2\XML\saml
133163 * @param \SAML2\Binding $binding The binding.
134164 * @param \SAML2\AuthnRequest $ar The authentication request.
135165 */
136- private function sendSAML2AuthnRequest (\ SAML2 \ Binding $ binding , \ SAML2 \ AuthnRequest $ ar )
166+ private function sendSAML2AuthnRequest (Binding $ binding , AuthnRequest $ ar ): void
137167 {
138168 $ binding ->send ($ ar );
139169 assert (false );
0 commit comments