Conditions | 19 |
Paths | 53 |
Total Lines | 138 |
Code Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
141 | public function validate( |
||
142 | Request $request, |
||
143 | string $method, |
||
144 | string $target, |
||
145 | bool $renew = false, |
||
146 | ?string $ticket = null, |
||
147 | ?string $service = null, |
||
148 | ?string $pgtUrl = null, |
||
149 | ): XmlResponse { |
||
150 | $forceAuthn = $renew; |
||
151 | // todo: According to the protocol, there is no target??? Why are we using it? |
||
152 | $serviceUrl = $service ?? $target ?? null; |
||
153 | |||
154 | // Check if any of the required query parameters are missing |
||
155 | if ($serviceUrl === null || $ticket === null) { |
||
156 | $messagePostfix = $serviceUrl === null ? 'service' : 'ticket'; |
||
157 | $message = "casserver: Missing service parameter: [{$messagePostfix}]"; |
||
158 | Logger::debug($message); |
||
159 | |||
160 | ob_start(); |
||
161 | echo $this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message); |
||
162 | $responseContent = ob_get_clean(); |
||
163 | |||
164 | return new XmlResponse( |
||
165 | $responseContent, |
||
166 | Response::HTTP_BAD_REQUEST, |
||
167 | ); |
||
168 | } |
||
169 | |||
170 | try { |
||
171 | // Get the service ticket |
||
172 | // `getTicket` uses the unserializable method and Objects may throw Throwables in their |
||
173 | // unserialization handlers. |
||
174 | $serviceTicket = $this->ticketStore->getTicket($ticket); |
||
175 | // Delete the ticket |
||
176 | $this->ticketStore->deleteTicket($ticket); |
||
177 | } catch (\Exception $e) { |
||
178 | $message = 'casserver:serviceValidate: internal server error. ' . var_export($e->getMessage(), true); |
||
179 | Logger::error($message); |
||
180 | |||
181 | ob_start(); |
||
182 | echo $this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message); |
||
183 | $responseContent = ob_get_clean(); |
||
184 | |||
185 | return new XmlResponse( |
||
186 | $responseContent, |
||
187 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
188 | ); |
||
189 | } |
||
190 | |||
191 | $failed = false; |
||
192 | $message = ''; |
||
193 | if (empty($serviceTicket)) { |
||
194 | // No ticket |
||
195 | $message = 'ticket: ' . var_export($ticket, true) . ' not recognized'; |
||
196 | $failed = true; |
||
197 | } elseif ($method === 'serviceValidate' && $this->ticketFactory->isProxyTicket($serviceTicket)) { |
||
198 | $message = 'Ticket ' . var_export($_GET['ticket'], true) . |
||
199 | ' is a proxy ticket. Use proxyValidate instead.'; |
||
200 | $failed = true; |
||
201 | } elseif (!$this->ticketFactory->isServiceTicket($serviceTicket)) { |
||
202 | // This is not a service ticket |
||
203 | $message = 'ticket: ' . var_export($ticket, true) . ' is not a service ticket'; |
||
204 | $failed = true; |
||
205 | } elseif ($this->ticketFactory->isExpired($serviceTicket)) { |
||
206 | // the ticket has expired |
||
207 | $message = 'Ticket has ' . var_export($ticket, true) . ' expired'; |
||
208 | $failed = true; |
||
209 | } elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($serviceUrl)) { |
||
210 | // The service url we passed to the query parameters does not match the one in the ticket. |
||
211 | $message = 'Mismatching service parameters: expected ' . |
||
212 | var_export($serviceTicket['service'], true) . |
||
213 | ' but was: ' . var_export($serviceUrl, true); |
||
214 | $failed = true; |
||
215 | } elseif ($forceAuthn && !$serviceTicket['forceAuthn']) { |
||
216 | // If `forceAuthn` is required but not set in the ticket |
||
217 | $message = 'Ticket was issued from single sign on session'; |
||
218 | $failed = true; |
||
219 | } |
||
220 | |||
221 | if ($failed) { |
||
222 | $finalMessage = 'casserver:validate: ' . $message; |
||
223 | Logger::error($finalMessage); |
||
224 | |||
225 | ob_start(); |
||
226 | echo $this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message); |
||
227 | $responseContent = ob_get_clean(); |
||
228 | |||
229 | return new XmlResponse( |
||
230 | $responseContent, |
||
231 | Response::HTTP_BAD_REQUEST, |
||
232 | ); |
||
233 | } |
||
234 | |||
235 | $attributes = $serviceTicket['attributes']; |
||
236 | $this->cas20Protocol->setAttributes($attributes); |
||
237 | |||
238 | if (isset($pgtUrl)) { |
||
239 | $sessionTicket = $this->ticketStore->getTicket($serviceTicket['sessionId']); |
||
240 | if ( |
||
241 | $sessionTicket !== null |
||
242 | && $this->ticketFactory->isSessionTicket($sessionTicket) |
||
243 | && !$this->ticketFactory->isExpired($sessionTicket) |
||
244 | ) { |
||
245 | $proxyGrantingTicket = $this->ticketFactory->createProxyGrantingTicket( |
||
246 | [ |
||
247 | 'userName' => $serviceTicket['userName'], |
||
248 | 'attributes' => $attributes, |
||
249 | 'forceAuthn' => false, |
||
250 | 'proxies' => array_merge( |
||
251 | [$serviceUrl], |
||
252 | $serviceTicket['proxies'], |
||
253 | ), |
||
254 | 'sessionId' => $serviceTicket['sessionId'], |
||
255 | ], |
||
256 | ); |
||
257 | try { |
||
258 | $this->httpUtils->fetch( |
||
259 | $pgtUrl . '?pgtIou=' . $proxyGrantingTicket['iou'] . '&pgtId=' . $proxyGrantingTicket['id'], |
||
260 | ); |
||
261 | |||
262 | $this->cas20Protocol->setProxyGrantingTicketIOU($proxyGrantingTicket['iou']); |
||
263 | |||
264 | $this->ticketStore->addTicket($proxyGrantingTicket); |
||
265 | } catch (\Exception $e) { |
||
266 | // Fall through |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | |||
271 | // TODO: Replace with string casting |
||
272 | ob_start(); |
||
273 | echo $this->cas20Protocol->getValidateSuccessResponse($serviceTicket['userName']); |
||
274 | $successContent = ob_get_clean(); |
||
275 | |||
276 | return new XmlResponse( |
||
277 | $successContent, |
||
278 | Response::HTTP_OK, |
||
279 | ); |
||
282 |