@@ -50,12 +50,12 @@ class CAS extends Auth\Source
5050
5151
5252 /**
53- * @var array<mixed> with ldap configuration
53+ * @var array<string, mixed> with ldap configuration
5454 */
5555 private array $ ldapConfig ;
5656
5757 /**
58- * @var array<mixed> cas configuration
58+ * @var array<string, mixed> cas configuration
5959 */
6060 private array $ casConfig ;
6161
@@ -75,6 +75,12 @@ class CAS extends Auth\Source
7575 */
7676 private bool $ useSlate ;
7777
78+ /**
79+ * HTTP utility class for making requests and handling redirects.
80+ * @var \SimpleSAML\Utils\HTTP
81+ */
82+ private Utils \HTTP $ httpUtils ;
83+
7884
7985 /**
8086 * Constructor for this authentication source.
@@ -89,8 +95,8 @@ public function __construct(array $info, array $config)
8995
9096 $ authsources = Configuration::loadFromArray ($ config );
9197
92- $ this ->casConfig = $ authsources ->getValue ('cas ' );
93- $ this ->ldapConfig = $ authsources ->getValue ('ldap ' );
98+ $ this ->casConfig = ( array ) $ authsources ->getValue ('cas ' );
99+ $ this ->ldapConfig = ( array ) $ authsources ->getValue ('ldap ' );
94100
95101 if (isset ($ this ->casConfig ['serviceValidate ' ])) {
96102 $ this ->validationMethod = 'serviceValidate ' ;
@@ -110,6 +116,22 @@ public function __construct(array $info, array $config)
110116 }
111117
112118
119+ /**
120+ * Initialize HTTP utilities instance
121+ *
122+ * @param \SimpleSAML\Utils\HTTP|null $httpUtils Optional HTTP utilities instance to use
123+ * @return void
124+ */
125+ protected function initHttpUtils (Utils \HTTP $ httpUtils = null ): void
126+ {
127+ if ($ httpUtils !== null ) {
128+ $ this ->httpUtils = $ httpUtils ;
129+ } else {
130+ $ this ->httpUtils = $ this ->httpUtils ?? new Utils \HTTP ();
131+ }
132+ }
133+
134+
113135 /**
114136 * This the most simple version of validating, this provides only authentication validation
115137 *
@@ -120,17 +142,17 @@ public function __construct(array $info, array $config)
120142 */
121143 private function casValidate (string $ ticket , string $ service ): array
122144 {
123- $ httpUtils = new Utils \ HTTP ();
124- $ url = $ httpUtils ->addURLParameters ($ this ->casConfig ['validate ' ], [
145+ $ this -> initHttpUtils ();
146+ $ url = $ this -> httpUtils ->addURLParameters ($ this ->casConfig ['validate ' ], [
125147 'ticket ' => $ ticket ,
126148 'service ' => $ service ,
127149 ]);
128150
129151 /** @var string $result */
130- $ result = $ httpUtils ->fetch ($ url );
152+ $ result = $ this -> httpUtils ->fetch ($ url );
131153
132- /** @var list<array{string, int<0, max>}| string> $res */
133- $ res = preg_split ("/ \r? \n/ " , $ result );
154+ /** @var list<string> $res */
155+ $ res = preg_split ("/ \r? \n/ " , $ result ) ?: [] ;
134156
135157 if (strcmp ($ res [0 ], "yes " ) == 0 ) {
136158 return [$ res [1 ], []];
@@ -150,19 +172,23 @@ private function casValidate(string $ticket, string $service): array
150172 */
151173 private function casServiceValidate (string $ ticket , string $ service ): array
152174 {
153- $ httpUtils = new Utils \ HTTP ();
154- $ url = $ httpUtils ->addURLParameters (
175+ $ this -> initHttpUtils ();
176+ $ url = $ this -> httpUtils ->addURLParameters (
155177 $ this ->casConfig ['serviceValidate ' ],
156178 [
157179 'ticket ' => $ ticket ,
158180 'service ' => $ service ,
159181 ],
160182 );
161- $ result = $ httpUtils ->fetch ($ url );
183+ $ result = $ this -> httpUtils ->fetch ($ url );
162184
163185 /** @var string $result */
164186 $ dom = DOMDocumentFactory::fromString ($ result );
165187
188+ if ($ dom ->documentElement === null ) {
189+ return [];
190+ }
191+
166192 if ($ this ->useSlate ) {
167193 $ serviceResponse = SlateServiceResponse::fromXML ($ dom ->documentElement );
168194 } else {
@@ -272,8 +298,8 @@ public function authenticate(array &$state): void
272298
273299 $ serviceUrl = Module::getModuleURL ('cas/linkback.php ' , ['stateId ' => $ stateId ]);
274300
275- $ httpUtils = new Utils \ HTTP ();
276- $ httpUtils ->redirectTrustedURL ($ this ->loginMethod , ['service ' => $ serviceUrl ]);
301+ $ this -> initHttpUtils ();
302+ $ this -> httpUtils ->redirectTrustedURL ($ this ->loginMethod , ['service ' => $ serviceUrl ]);
277303 }
278304
279305
@@ -297,8 +323,8 @@ public function logout(array &$state): void
297323 Auth \State::deleteState ($ state );
298324
299325 // we want cas to log us out
300- $ httpUtils = new Utils \ HTTP ();
301- $ httpUtils ->redirectTrustedURL ($ logoutUrl );
326+ $ this -> initHttpUtils ();
327+ $ this -> httpUtils ->redirectTrustedURL ($ logoutUrl );
302328 }
303329
304330
@@ -457,7 +483,7 @@ private function parseQueryAttributes(DOMDocument $dom): array
457483 }
458484 } else {
459485 // Relative XPath; prefer evaluating under authenticationSuccess if available
460- $ context = $ authn instanceof \ DOMElement ? $ authn : $ root ;
486+ $ context = $ authn instanceof DOMElement ? $ authn : $ root ;
461487 $ nodes = XPath::xpQuery ($ context , $ query , $ xPath );
462488 }
463489
0 commit comments