66
77use DOMXpath ;
88use Exception ;
9- use SAML2 \DOMDocumentFactory ;
109use SimpleSAML \Auth ;
10+ use SimpleSAML \CAS \XML \cas \AuthenticationFailure ;
11+ use SimpleSAML \CAS \XML \cas \AuthenticationSuccess ;
12+ use SimpleSAML \CAS \XML \cas \ServiceResponse ;
13+ use SimpleSAML \CAS \Utils \XPath ;
1114use SimpleSAML \Configuration ;
1215use SimpleSAML \Module ;
1316use SimpleSAML \Module \ldap \Auth \Ldap ;
1417use SimpleSAML \Utils ;
18+ use SimpleSAML \XML \DOMDocumentFactory ;
1519
1620use function array_key_exists ;
1721use function array_merge_recursive ;
@@ -149,32 +153,34 @@ private function casServiceValidate(string $ticket, string $service): array
149153
150154 /** @var string $result */
151155 $ dom = DOMDocumentFactory::fromString ($ result );
152- $ xPath = new DOMXpath ($ dom );
153- $ xPath ->registerNamespace ("cas " , 'http://www.yale.edu/tp/cas ' );
154- $ success = $ xPath ->query ("/cas:serviceResponse/cas:authenticationSuccess/cas:user " );
155- if ($ success ->length == 0 ) {
156- $ failure = $ xPath ->evaluate ("/cas:serviceResponse/cas:authenticationFailure " );
157- throw new Exception ("Error when validating CAS service ticket: " . $ failure ->item (0 )->textContent );
158- } else {
156+
157+ $ serviceResponse = ServiceResponse::fromXML ($ dom ->documentElement );
158+ $ message = $ serviceResponse ->getResponse ();
159+ if ($ message instanceof AuthenticationFailure) {
160+ throw new Exception (sprintf (
161+ "Error when validating CAS service ticket: %s (%s) " ,
162+ $ message ->getContent (),
163+ $ message ->getCode (),
164+ ));
165+ } elseif ($ message instanceof AuthenticationSuccess) {
166+ $ user = $ message ->getUser ()->getContent ();
167+ $ xPath = XPath::getXPath ();
168+
159169 $ attributes = [];
160170 if ($ casattributes = $ this ->casConfig ['attributes ' ]) {
161- // Some has attributes in the xml - attributes is a list of XPath expressions to get them
171+ // Some have attributes in the xml - attributes is a list of XPath expressions to get them
162172 foreach ($ casattributes as $ name => $ query ) {
163- $ attrs = $ xPath ->query ($ query );
173+ $ attrs = $ xPath ->xpQuery ($ query, $ xPath );
164174 foreach ($ attrs as $ attrvalue ) {
165175 $ attributes [$ name ][] = $ attrvalue ->textContent ;
166176 }
167177 }
168178 }
169179
170- $ item = $ success ->item (0 );
171- if (is_null ($ item )) {
172- throw new Exception ("Error parsing serviceResponse. " );
173- }
174- $ casusername = $ item ->textContent ;
175-
176- return [$ casusername , $ attributes ];
180+ return [$ user , $ attributes ];
177181 }
182+
183+ throw new Exception ("Error parsing serviceResponse. " );
178184 }
179185
180186
@@ -208,8 +214,8 @@ public function finalStep(array &$state): void
208214 $ ticket = $ state ['cas:ticket ' ];
209215 $ stateId = Auth \State::saveState ($ state , self ::STAGE_INIT );
210216 $ service = Module::getModuleURL ('cas/linkback.php ' , ['stateId ' => $ stateId ]);
211- list ($ username , $ casattributes ) = $ this ->casValidation ($ ticket , $ service );
212- $ ldapattributes = [];
217+ list ($ username , $ casAttributes ) = $ this ->casValidation ($ ticket , $ service );
218+ $ ldapAttributes = [];
213219
214220 $ config = Configuration::loadFromArray (
215221 $ this ->ldapConfig ,
@@ -224,12 +230,13 @@ public function finalStep(array &$state): void
224230 $ config ->getOptionalInteger ('port ' , 389 ),
225231 $ config ->getOptionalBoolean ('referrals ' , true ),
226232 );
227- $ ldapattributes = $ ldap ->validate ($ this ->ldapConfig , $ username );
228- if ($ ldapattributes === false ) {
233+
234+ $ ldapAttributes = $ ldap ->validate ($ this ->ldapConfig , $ username );
235+ if ($ ldapAttributes === false ) {
229236 throw new Exception ("Failed to authenticate against LDAP-server. " );
230237 }
231238 }
232- $ attributes = array_merge_recursive ($ casattributes , $ ldapattributes );
239+ $ attributes = array_merge_recursive ($ casAttributes , $ ldapAttributes );
233240 $ state ['Attributes ' ] = $ attributes ;
234241 }
235242
0 commit comments