Conditions | 19 |
Paths | 53 |
Total Lines | 135 |
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 |
||
131 | public function validate( |
||
132 | Request $request, |
||
133 | string $method, |
||
134 | bool $renew = false, |
||
135 | ?string $ticket = null, |
||
136 | ?string $service = null, |
||
137 | ?string $pgtUrl = null, |
||
138 | ): XmlResponse { |
||
139 | $forceAuthn = $renew; |
||
140 | $serviceUrl = $service ?? $_GET['TARGET'] ?? null; |
||
141 | |||
142 | // Check if any of the required query parameters are missing |
||
143 | if ($serviceUrl === null || $ticket === null) { |
||
144 | $messagePostfix = $serviceUrl === null ? 'service' : 'ticket'; |
||
145 | $message = "casserver: Missing service parameter: [{$messagePostfix}]"; |
||
146 | Logger::debug($message); |
||
147 | |||
148 | ob_start(); |
||
149 | echo $this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message); |
||
150 | $responseContent = ob_get_clean(); |
||
151 | |||
152 | return new XmlResponse( |
||
153 | $responseContent, |
||
154 | Response::HTTP_BAD_REQUEST, |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | try { |
||
159 | // Get the service ticket |
||
160 | // `getTicket` uses the unserializable method and Objects may throw Throwables in their |
||
161 | // unserialization handlers. |
||
162 | $serviceTicket = $this->ticketStore->getTicket($ticket); |
||
163 | // Delete the ticket |
||
164 | $this->ticketStore->deleteTicket($ticket); |
||
165 | } catch (\Exception $e) { |
||
166 | $message = 'casserver:serviceValidate: internal server error. ' . var_export($e->getMessage(), true); |
||
167 | Logger::error($message); |
||
168 | |||
169 | ob_start(); |
||
170 | echo $this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message); |
||
171 | $responseContent = ob_get_clean(); |
||
172 | |||
173 | return new XmlResponse( |
||
174 | $responseContent, |
||
175 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
176 | ); |
||
177 | } |
||
178 | |||
179 | $failed = false; |
||
180 | $message = ''; |
||
181 | if (empty($serviceTicket)) { |
||
182 | // No ticket |
||
183 | $message = 'ticket: ' . var_export($ticket, true) . ' not recognized'; |
||
184 | $failed = true; |
||
185 | } elseif ($method === 'serviceValidate' && $this->ticketFactory->isProxyTicket($serviceTicket)) { |
||
186 | $message = 'Ticket ' . var_export($_GET['ticket'], true) . |
||
187 | ' is a proxy ticket. Use proxyValidate instead.'; |
||
188 | $failed = true; |
||
189 | } elseif (!$this->ticketFactory->isServiceTicket($serviceTicket)) { |
||
190 | // This is not a service ticket |
||
191 | $message = 'ticket: ' . var_export($ticket, true) . ' is not a service ticket'; |
||
192 | $failed = true; |
||
193 | } elseif ($this->ticketFactory->isExpired($serviceTicket)) { |
||
194 | // the ticket has expired |
||
195 | $message = 'Ticket has ' . var_export($ticket, true) . ' expired'; |
||
196 | $failed = true; |
||
197 | } elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($serviceUrl)) { |
||
198 | // The service url we passed to the query parameters does not match the one in the ticket. |
||
199 | $message = 'Mismatching service parameters: expected ' . |
||
200 | var_export($serviceTicket['service'], true) . |
||
201 | ' but was: ' . var_export($serviceUrl, true); |
||
202 | $failed = true; |
||
203 | } elseif ($forceAuthn && !$serviceTicket['forceAuthn']) { |
||
204 | // If `forceAuthn` is required but not set in the ticket |
||
205 | $message = 'Ticket was issued from single sign on session'; |
||
206 | $failed = true; |
||
207 | } |
||
208 | |||
209 | if ($failed) { |
||
210 | $finalMessage = 'casserver:validate: ' . $message; |
||
211 | Logger::error($finalMessage); |
||
212 | |||
213 | ob_start(); |
||
214 | echo $this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message); |
||
215 | $responseContent = ob_get_clean(); |
||
216 | |||
217 | return new XmlResponse( |
||
218 | $responseContent, |
||
219 | Response::HTTP_BAD_REQUEST, |
||
220 | ); |
||
221 | } |
||
222 | |||
223 | $attributes = $serviceTicket['attributes']; |
||
224 | $this->cas20Protocol->setAttributes($attributes); |
||
225 | |||
226 | if (isset($pgtUrl)) { |
||
227 | $sessionTicket = $this->ticketStore->getTicket($serviceTicket['sessionId']); |
||
228 | if ( |
||
229 | $sessionTicket !== null |
||
230 | && $this->ticketFactory->isSessionTicket($sessionTicket) |
||
231 | && !$this->ticketFactory->isExpired($sessionTicket) |
||
232 | ) { |
||
233 | $proxyGrantingTicket = $this->ticketFactory->createProxyGrantingTicket( |
||
234 | [ |
||
235 | 'userName' => $serviceTicket['userName'], |
||
236 | 'attributes' => $attributes, |
||
237 | 'forceAuthn' => false, |
||
238 | 'proxies' => array_merge( |
||
239 | [$serviceUrl], |
||
240 | $serviceTicket['proxies'], |
||
241 | ), |
||
242 | 'sessionId' => $serviceTicket['sessionId'], |
||
243 | ], |
||
244 | ); |
||
245 | try { |
||
246 | $this->httpUtils->fetch( |
||
247 | $pgtUrl . '?pgtIou=' . $proxyGrantingTicket['iou'] . '&pgtId=' . $proxyGrantingTicket['id'], |
||
248 | ); |
||
249 | |||
250 | $this->cas20Protocol->setProxyGrantingTicketIOU($proxyGrantingTicket['iou']); |
||
251 | |||
252 | $this->ticketStore->addTicket($proxyGrantingTicket); |
||
253 | } catch (Exception $e) { |
||
254 | // Fall through |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | |||
259 | ob_start(); |
||
260 | echo $this->cas20Protocol->getValidateSuccessResponse($serviceTicket['userName']); |
||
261 | $successContent = ob_get_clean(); |
||
262 | |||
263 | return new XmlResponse( |
||
264 | $successContent, |
||
265 | Response::HTTP_OK, |
||
266 | ); |
||
269 |