2525
2626namespace SimpleSAML \Module \casserver \Cas \Protocol ;
2727
28- use DateTimeImmutable ;
29- use SimpleSAML \CAS \XML \cas \Attributes ;
30- use SimpleSAML \CAS \XML \cas \AuthenticationDate ;
31- use SimpleSAML \CAS \XML \cas \AuthenticationFailure ;
32- use SimpleSAML \CAS \XML \cas \AuthenticationSuccess ;
33- use SimpleSAML \CAS \XML \cas \IsFromNewLogin ;
34- use SimpleSAML \CAS \XML \cas \LongTermAuthenticationRequestTokenUsed ;
35- use SimpleSAML \CAS \XML \cas \ProxyFailure ;
36- use SimpleSAML \CAS \XML \cas \ProxyGrantingTicket ;
37- use SimpleSAML \CAS \XML \cas \ProxySuccess ;
38- use SimpleSAML \CAS \XML \cas \ProxyTicket ;
39- use SimpleSAML \CAS \XML \cas \ServiceResponse ;
40- use SimpleSAML \CAS \XML \cas \User ;
28+ use Beste \Clock \LocalizedClock ;
29+ use DateTimeZone ;
30+ use InvalidArgumentException ;
31+ use SimpleSAML \CAS \Type \CodeValue ;
32+ use SimpleSAML \CAS \XML \Attributes ;
33+ use SimpleSAML \CAS \XML \AuthenticationDate ;
34+ use SimpleSAML \CAS \XML \AuthenticationFailure ;
35+ use SimpleSAML \CAS \XML \AuthenticationSuccess ;
36+ use SimpleSAML \CAS \XML \IsFromNewLogin ;
37+ use SimpleSAML \CAS \XML \LongTermAuthenticationRequestTokenUsed ;
38+ use SimpleSAML \CAS \XML \ProxyFailure ;
39+ use SimpleSAML \CAS \XML \ProxyGrantingTicket ;
40+ use SimpleSAML \CAS \XML \ProxySuccess ;
41+ use SimpleSAML \CAS \XML \ProxyTicket ;
42+ use SimpleSAML \CAS \XML \ServiceResponse ;
43+ use SimpleSAML \CAS \XML \User ;
4144use SimpleSAML \Configuration ;
4245use SimpleSAML \Logger ;
46+ use SimpleSAML \XML \Assert \Assert ;
4347use SimpleSAML \XML \Chunk ;
4448use SimpleSAML \XML \DOMDocumentFactory ;
49+ use SimpleSAML \XMLSchema \Type \BooleanValue ;
50+ use SimpleSAML \XMLSchema \Type \DateTimeValue ;
51+ use SimpleSAML \XMLSchema \Type \StringValue ;
4552
4653use function base64_encode ;
4754use function count ;
@@ -121,23 +128,24 @@ public function getProxyGrantingTicketIOU(): ?string
121128 */
122129 public function getValidateSuccessResponse (string $ username ): ServiceResponse
123130 {
124- $ user = new User ($ username );
131+ $ user = new User (StringValue:: fromString ( $ username) );
125132
126133 $ proxyGrantingTicket = null ;
127134 if (is_string ($ this ->proxyGrantingTicketIOU )) {
128- $ proxyGrantingTicket = new ProxyGrantingTicket ($ this ->proxyGrantingTicketIOU );
135+ $ proxyGrantingTicket = new ProxyGrantingTicket (StringValue:: fromString ( $ this ->proxyGrantingTicketIOU ) );
129136 }
130137
131138 $ attr = [];
132139 if ($ this ->sendAttributes && count ($ this ->attributes ) > 0 ) {
133140 foreach ($ this ->attributes as $ name => $ values ) {
134141 // Fix the most common cause of invalid XML elements
135142 $ _name = str_replace (': ' , '_ ' , $ name );
136- if ($ this ->isValidXmlName ($ _name ) === true ) {
143+ try {
144+ Assert::validNCName ($ _name );
137145 foreach ($ values as $ value ) {
138146 $ attr [] = $ this ->generateCas20Attribute ($ _name , $ value );
139147 }
140- } else {
148+ } catch ( InvalidArgumentException ) {
141149 Logger::warning ("DOMException creating attribute ' $ _name'. Continuing without attribute' " );
142150 }
143151 }
@@ -150,10 +158,11 @@ public function getValidateSuccessResponse(string $username): ServiceResponse
150158 }
151159 }
152160
161+ $ systemClock = LocalizedClock::in (new DateTimeZone ('Z ' ));
153162 $ attributes = new Attributes (
154- new AuthenticationDate (new DateTimeImmutable ( ' now ' )),
155- new LongTermAuthenticationRequestTokenUsed (' true ' ),
156- new IsFromNewLogin (' true ' ),
163+ new AuthenticationDate (DateTimeValue:: now ( $ systemClock )),
164+ new LongTermAuthenticationRequestTokenUsed (BooleanValue:: fromBoolean ( true ) ),
165+ new IsFromNewLogin (BooleanValue:: fromBoolean ( true ) ),
157166 $ attr ,
158167 );
159168
@@ -171,7 +180,10 @@ public function getValidateSuccessResponse(string $username): ServiceResponse
171180 */
172181 public function getValidateFailureResponse (string $ errorCode , string $ explanation ): ServiceResponse
173182 {
174- $ authenticationFailure = new AuthenticationFailure ($ explanation , $ errorCode );
183+ $ authenticationFailure = new AuthenticationFailure (
184+ StringValue::fromString ($ explanation ),
185+ CodeValue::fromString ($ errorCode ),
186+ );
175187 $ serviceResponse = new ServiceResponse ($ authenticationFailure );
176188
177189 return $ serviceResponse ;
@@ -184,7 +196,7 @@ public function getValidateFailureResponse(string $errorCode, string $explanatio
184196 */
185197 public function getProxySuccessResponse (string $ proxyTicketId ): ServiceResponse
186198 {
187- $ proxyTicket = new ProxyTicket ($ proxyTicketId );
199+ $ proxyTicket = new ProxyTicket (StringValue:: fromString ( $ proxyTicketId) );
188200 $ proxySuccess = new ProxySuccess ($ proxyTicket );
189201 $ serviceResponse = new ServiceResponse ($ proxySuccess );
190202
@@ -199,7 +211,10 @@ public function getProxySuccessResponse(string $proxyTicketId): ServiceResponse
199211 */
200212 public function getProxyFailureResponse (string $ errorCode , string $ explanation ): ServiceResponse
201213 {
202- $ proxyFailure = new ProxyFailure ($ explanation , $ errorCode );
214+ $ proxyFailure = new ProxyFailure (
215+ StringValue::fromString ($ explanation ),
216+ CodeValue::fromString ($ errorCode ),
217+ );
203218 $ serviceResponse = new ServiceResponse ($ proxyFailure );
204219
205220 return $ serviceResponse ;
@@ -222,26 +237,4 @@ private function generateCas20Attribute(
222237
223238 return new Chunk ($ attributeElement );
224239 }
225-
226-
227- /**
228- * XML element names have a lot of rules and not every SAML attribute name can be converted.
229- * Ref: https://www.w3.org/TR/REC-xml/#NT-NameChar
230- * https://stackoverflow.com/q/2519845/54396
231- * must only start with letter or underscore
232- * cannot start with 'xml' (or maybe it can - stackoverflow commenters don't agree)
233- * cannot contain a ':' since those are for namespaces
234- * cannot contains space
235- * can only contain letters, digits, hyphens, underscores, and periods
236- * @param string $name The attribute name to be used as an element
237- * @return bool true if $name would make a valid xml element.
238- */
239- private function isValidXmlName (string $ name ): bool
240- {
241- return filter_var (
242- $ name ,
243- FILTER_VALIDATE_REGEXP ,
244- ['options ' => ['regexp ' => '/^[a-zA-Z_][\w.-]*$/ ' ]],
245- ) !== false ;
246- }
247240}
0 commit comments