44
55use DOMXpath ;
66use Exception ;
7- use SAML2 \DOMDocumentFactory ;
87use SimpleSAML \Auth ;
8+ use SimpleSAML \CAS \XML \cas \AuthenticationFailure ;
9+ use SimpleSAML \CAS \XML \cas \AuthenticationSuccess ;
10+ use SimpleSAML \CAS \XML \cas \ServiceResponse ;
11+ use SimpleSAML \CAS \Utils \XPath ;
912use SimpleSAML \Configuration ;
1013use SimpleSAML \Module ;
1114use SimpleSAML \Module \ldap \Auth \Ldap ;
1215use SimpleSAML \Utils ;
16+ use SimpleSAML \XML \DOMDocumentFactory ;
1317
1418use function array_key_exists ;
1519use function array_merge_recursive ;
@@ -147,32 +151,34 @@ private function casServiceValidate(string $ticket, string $service): array
147151
148152 /** @var string $result */
149153 $ dom = DOMDocumentFactory::fromString ($ result );
150- $ xPath = new DOMXpath ($ dom );
151- $ xPath ->registerNamespace ("cas " , 'http://www.yale.edu/tp/cas ' );
152- $ success = $ xPath ->query ("/cas:serviceResponse/cas:authenticationSuccess/cas:user " );
153- if ($ success ->length == 0 ) {
154- $ failure = $ xPath ->evaluate ("/cas:serviceResponse/cas:authenticationFailure " );
155- throw new Exception ("Error when validating CAS service ticket: " . $ failure ->item (0 )->textContent );
156- } else {
154+
155+ $ serviceResponse = ServiceResponse::fromXML ($ dom ->documentElement );
156+ $ message = $ serviceResponse ->getResponse ();
157+ if ($ message instanceof AuthenticationFailure) {
158+ throw new Exception (sprintf (
159+ "Error when validating CAS service ticket: %s (%s) " ,
160+ $ message ->getContent (),
161+ $ message ->getCode (),
162+ ));
163+ } elseif ($ message instanceof AuthenticationSuccess) {
164+ $ user = $ message ->getUser ()->getContent ();
165+ $ xPath = XPath::getXPath ();
166+
157167 $ attributes = [];
158168 if ($ casattributes = $ this ->casConfig ['attributes ' ]) {
159- // Some has attributes in the xml - attributes is a list of XPath expressions to get them
169+ // Some have attributes in the xml - attributes is a list of XPath expressions to get them
160170 foreach ($ casattributes as $ name => $ query ) {
161- $ attrs = $ xPath ->query ($ query );
171+ $ attrs = $ xPath ->xpQuery ($ query, $ xPath );
162172 foreach ($ attrs as $ attrvalue ) {
163173 $ attributes [$ name ][] = $ attrvalue ->textContent ;
164174 }
165175 }
166176 }
167177
168- $ item = $ success ->item (0 );
169- if (is_null ($ item )) {
170- throw new Exception ("Error parsing serviceResponse. " );
171- }
172- $ casusername = $ item ->textContent ;
173-
174- return [$ casusername , $ attributes ];
178+ return [$ user , $ attributes ];
175179 }
180+
181+ throw new Exception ("Error parsing serviceResponse. " );
176182 }
177183
178184
@@ -206,8 +212,8 @@ public function finalStep(array &$state): void
206212 $ ticket = $ state ['cas:ticket ' ];
207213 $ stateId = Auth \State::saveState ($ state , self ::STAGE_INIT );
208214 $ service = Module::getModuleURL ('cas/linkback.php ' , ['stateId ' => $ stateId ]);
209- list ($ username , $ casattributes ) = $ this ->casValidation ($ ticket , $ service );
210- $ ldapattributes = [];
215+ list ($ username , $ casAttributes ) = $ this ->casValidation ($ ticket , $ service );
216+ $ ldapAttributes = [];
211217
212218 $ config = Configuration::loadFromArray (
213219 $ this ->ldapConfig ,
@@ -222,12 +228,13 @@ public function finalStep(array &$state): void
222228 $ config ->getOptionalInteger ('port ' , 389 ),
223229 $ config ->getOptionalBoolean ('referrals ' , true )
224230 );
225- $ ldapattributes = $ ldap ->validate ($ this ->ldapConfig , $ username );
226- if ($ ldapattributes === false ) {
231+
232+ $ ldapAttributes = $ ldap ->validate ($ this ->ldapConfig , $ username );
233+ if ($ ldapAttributes === false ) {
227234 throw new Exception ("Failed to authenticate against LDAP-server. " );
228235 }
229236 }
230- $ attributes = array_merge_recursive ($ casattributes , $ ldapattributes );
237+ $ attributes = array_merge_recursive ($ casAttributes , $ ldapAttributes );
231238 $ state ['Attributes ' ] = $ attributes ;
232239 }
233240
0 commit comments