| Conditions | 19 |
| Paths | 53 |
| Total Lines | 120 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 93 | public function validate( |
||
| 94 | Request $request, |
||
| 95 | string $method, |
||
| 96 | bool $renew = false, |
||
| 97 | ?string $target = null, |
||
| 98 | ?string $ticket = null, |
||
| 99 | ?string $service = null, |
||
| 100 | ?string $pgtUrl = null, |
||
| 101 | ): XmlResponse { |
||
| 102 | $forceAuthn = $renew; |
||
| 103 | $serviceUrl = $service ?? $target ?? null; |
||
| 104 | |||
| 105 | // Check if any of the required query parameters are missing |
||
| 106 | if ($serviceUrl === null || $ticket === null) { |
||
| 107 | $messagePostfix = $serviceUrl === null ? 'service' : 'ticket'; |
||
| 108 | $message = "casserver: Missing service parameter: [{$messagePostfix}]"; |
||
| 109 | Logger::debug($message); |
||
| 110 | |||
| 111 | return new XmlResponse( |
||
| 112 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), |
||
| 113 | Response::HTTP_BAD_REQUEST, |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | |||
| 117 | try { |
||
| 118 | // Get the service ticket |
||
| 119 | // `getTicket` uses the unserializable method and Objects may throw Throwables in their |
||
| 120 | // unserialization handlers. |
||
| 121 | $serviceTicket = $this->ticketStore->getTicket($ticket); |
||
| 122 | // Delete the ticket |
||
| 123 | $this->ticketStore->deleteTicket($ticket); |
||
| 124 | } catch (\Exception $e) { |
||
| 125 | $message = 'casserver:serviceValidate: internal server error. ' . var_export($e->getMessage(), true); |
||
| 126 | Logger::error($message); |
||
| 127 | |||
| 128 | return new XmlResponse( |
||
| 129 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), |
||
| 130 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | $failed = false; |
||
| 135 | $message = ''; |
||
| 136 | if (empty($serviceTicket)) { |
||
| 137 | // No ticket |
||
| 138 | $message = 'ticket: ' . var_export($ticket, true) . ' not recognized'; |
||
| 139 | $failed = true; |
||
| 140 | } elseif ($method === 'serviceValidate' && $this->ticketFactory->isProxyTicket($serviceTicket)) { |
||
| 141 | $message = 'Ticket ' . var_export($_GET['ticket'], true) . |
||
| 142 | ' is a proxy ticket. Use proxyValidate instead.'; |
||
| 143 | $failed = true; |
||
| 144 | } elseif (!$this->ticketFactory->isServiceTicket($serviceTicket)) { |
||
| 145 | // This is not a service ticket |
||
| 146 | $message = 'ticket: ' . var_export($ticket, true) . ' is not a service ticket'; |
||
| 147 | $failed = true; |
||
| 148 | } elseif ($this->ticketFactory->isExpired($serviceTicket)) { |
||
| 149 | // the ticket has expired |
||
| 150 | $message = 'Ticket has ' . var_export($ticket, true) . ' expired'; |
||
| 151 | $failed = true; |
||
| 152 | } elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($serviceUrl)) { |
||
| 153 | // The service url we passed to the query parameters does not match the one in the ticket. |
||
| 154 | $message = 'Mismatching service parameters: expected ' . |
||
| 155 | var_export($serviceTicket['service'], true) . |
||
| 156 | ' but was: ' . var_export($serviceUrl, true); |
||
| 157 | $failed = true; |
||
| 158 | } elseif ($forceAuthn && !$serviceTicket['forceAuthn']) { |
||
| 159 | // If `forceAuthn` is required but not set in the ticket |
||
| 160 | $message = 'Ticket was issued from single sign on session'; |
||
| 161 | $failed = true; |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($failed) { |
||
| 165 | $finalMessage = 'casserver:validate: ' . $message; |
||
| 166 | Logger::error($finalMessage); |
||
| 167 | |||
| 168 | return new XmlResponse( |
||
| 169 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), |
||
| 170 | Response::HTTP_BAD_REQUEST, |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | |||
| 174 | $attributes = $serviceTicket['attributes']; |
||
| 175 | $this->cas20Protocol->setAttributes($attributes); |
||
| 176 | |||
| 177 | if (isset($pgtUrl)) { |
||
| 178 | $sessionTicket = $this->ticketStore->getTicket($serviceTicket['sessionId']); |
||
| 179 | if ( |
||
| 180 | $sessionTicket !== null |
||
| 181 | && $this->ticketFactory->isSessionTicket($sessionTicket) |
||
| 182 | && !$this->ticketFactory->isExpired($sessionTicket) |
||
| 183 | ) { |
||
| 184 | $proxyGrantingTicket = $this->ticketFactory->createProxyGrantingTicket( |
||
| 185 | [ |
||
| 186 | 'userName' => $serviceTicket['userName'], |
||
| 187 | 'attributes' => $attributes, |
||
| 188 | 'forceAuthn' => false, |
||
| 189 | 'proxies' => array_merge( |
||
| 190 | [$serviceUrl], |
||
| 191 | $serviceTicket['proxies'], |
||
| 192 | ), |
||
| 193 | 'sessionId' => $serviceTicket['sessionId'], |
||
| 194 | ], |
||
| 195 | ); |
||
| 196 | try { |
||
| 197 | $this->httpUtils->fetch( |
||
| 198 | $pgtUrl . '?pgtIou=' . $proxyGrantingTicket['iou'] . '&pgtId=' . $proxyGrantingTicket['id'], |
||
| 199 | ); |
||
| 200 | |||
| 201 | $this->cas20Protocol->setProxyGrantingTicketIOU($proxyGrantingTicket['iou']); |
||
| 202 | |||
| 203 | $this->ticketStore->addTicket($proxyGrantingTicket); |
||
| 204 | } catch (\Exception $e) { |
||
| 205 | // Fall through |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | return new XmlResponse( |
||
| 211 | (string)$this->cas20Protocol->getValidateSuccessResponse($serviceTicket['userName']), |
||
| 212 | Response::HTTP_OK, |
||
| 213 | ); |
||
| 216 |