2525
2626namespace SimpleSAML \Module \casserver \Cas \Protocol ;
2727
28- use DateTimeImmutable ;
28+ use Beste \Clock \LocalizedClock ;
29+ use DateTimeZone ;
30+ use InvalidArgumentException ;
31+ use SimpleSAML \CAS \Type \CodeValue ;
2932use SimpleSAML \CAS \XML \cas \Attributes ;
3033use SimpleSAML \CAS \XML \cas \AuthenticationDate ;
3134use SimpleSAML \CAS \XML \cas \AuthenticationFailure ;
4245use SimpleSAML \Logger ;
4346use SimpleSAML \XML \Chunk ;
4447use SimpleSAML \XML \DOMDocumentFactory ;
48+ use SimpleSAML \XMLSchema \Type \BooleanValue ;
49+ use SimpleSAML \XMLSchema \Type \DateTimeValue ;
50+ use SimpleSAML \XMLSchema \Type \StringValue ;
4551
4652use function base64_encode ;
4753use function count ;
@@ -121,23 +127,24 @@ public function getProxyGrantingTicketIOU(): ?string
121127 */
122128 public function getValidateSuccessResponse (string $ username ): ServiceResponse
123129 {
124- $ user = new User ($ username );
130+ $ user = new User (StringValue:: fromString ( $ username) );
125131
126132 $ proxyGrantingTicket = null ;
127133 if (is_string ($ this ->proxyGrantingTicketIOU )) {
128- $ proxyGrantingTicket = new ProxyGrantingTicket ($ this ->proxyGrantingTicketIOU );
134+ $ proxyGrantingTicket = new ProxyGrantingTicket (StringValue:: fromString ( $ this ->proxyGrantingTicketIOU ) );
129135 }
130136
131137 $ attr = [];
132138 if ($ this ->sendAttributes && count ($ this ->attributes ) > 0 ) {
133139 foreach ($ this ->attributes as $ name => $ values ) {
134140 // Fix the most common cause of invalid XML elements
135141 $ _name = str_replace (': ' , '_ ' , $ name );
136- if ($ this ->isValidXmlName ($ _name ) === true ) {
142+ try {
143+ Assert::validNCName ($ _name );
137144 foreach ($ values as $ value ) {
138145 $ attr [] = $ this ->generateCas20Attribute ($ _name , $ value );
139146 }
140- } else {
147+ } catch ( InvalidArgumentException ) {
141148 Logger::warning ("DOMException creating attribute ' $ _name'. Continuing without attribute' " );
142149 }
143150 }
@@ -150,10 +157,11 @@ public function getValidateSuccessResponse(string $username): ServiceResponse
150157 }
151158 }
152159
160+ $ systemClock = LocalizedClock::in (new DateTimeZone ('Z ' ));
153161 $ attributes = new Attributes (
154- new AuthenticationDate (new DateTimeImmutable ( ' now ' )),
155- new LongTermAuthenticationRequestTokenUsed (' true ' ),
156- new IsFromNewLogin (' true ' ),
162+ new AuthenticationDate (DateTimeValue:: now ( $ systemClock )),
163+ new LongTermAuthenticationRequestTokenUsed (BooleanValue:: fromBoolean ( true ) ),
164+ new IsFromNewLogin (BooleanValue:: fromBoolean ( true ) ),
157165 $ attr ,
158166 );
159167
@@ -171,7 +179,10 @@ public function getValidateSuccessResponse(string $username): ServiceResponse
171179 */
172180 public function getValidateFailureResponse (string $ errorCode , string $ explanation ): ServiceResponse
173181 {
174- $ authenticationFailure = new AuthenticationFailure ($ explanation , $ errorCode );
182+ $ authenticationFailure = new AuthenticationFailure (
183+ StringValue::fromString ($ explanation ),
184+ CodeValue::fromString ($ errorCode ),
185+ );
175186 $ serviceResponse = new ServiceResponse ($ authenticationFailure );
176187
177188 return $ serviceResponse ;
@@ -184,7 +195,7 @@ public function getValidateFailureResponse(string $errorCode, string $explanatio
184195 */
185196 public function getProxySuccessResponse (string $ proxyTicketId ): ServiceResponse
186197 {
187- $ proxyTicket = new ProxyTicket ($ proxyTicketId );
198+ $ proxyTicket = new ProxyTicket (StringValue:: fromString ( $ proxyTicketId) );
188199 $ proxySuccess = new ProxySuccess ($ proxyTicket );
189200 $ serviceResponse = new ServiceResponse ($ proxySuccess );
190201
@@ -199,7 +210,10 @@ public function getProxySuccessResponse(string $proxyTicketId): ServiceResponse
199210 */
200211 public function getProxyFailureResponse (string $ errorCode , string $ explanation ): ServiceResponse
201212 {
202- $ proxyFailure = new ProxyFailure ($ explanation , $ errorCode );
213+ $ proxyFailure = new ProxyFailure (
214+ StringValue::fromString ($ explanation ),
215+ CodeValue::fromString ($ errorCode ),
216+ );
203217 $ serviceResponse = new ServiceResponse ($ proxyFailure );
204218
205219 return $ serviceResponse ;
@@ -222,26 +236,4 @@ private function generateCas20Attribute(
222236
223237 return new Chunk ($ attributeElement );
224238 }
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- }
247239}
0 commit comments