Conditions | 20 |
Paths | 62 |
Total Lines | 137 |
Code Lines | 81 |
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 | $messagePostfix = ''; |
||
126 | if (!empty($e->getMessage())) { |
||
127 | $messagePostfix = ': ' . var_export($e->getMessage(), true); |
||
128 | } |
||
129 | $message = 'casserver:serviceValidate: internal server error' . $messagePostfix; |
||
130 | Logger::error($message); |
||
131 | |||
132 | return new XmlResponse( |
||
133 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), |
||
134 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | $failed = false; |
||
139 | $message = ''; |
||
140 | if (empty($serviceTicket)) { |
||
141 | // No ticket |
||
142 | $message = 'Ticket ' . var_export($ticket, true) . ' not recognized'; |
||
143 | $failed = true; |
||
144 | } elseif ($method === 'serviceValidate' && $this->ticketFactory->isProxyTicket($serviceTicket)) { |
||
145 | $message = 'Ticket ' . var_export($ticket, true) . |
||
146 | ' is a proxy ticket. Use proxyValidate instead.'; |
||
147 | $failed = true; |
||
148 | } elseif (!$this->ticketFactory->isServiceTicket($serviceTicket)) { |
||
149 | // This is not a service ticket |
||
150 | $message = 'Ticket ' . var_export($ticket, true) . ' is not a service ticket'; |
||
151 | $failed = true; |
||
152 | } elseif ($this->ticketFactory->isExpired($serviceTicket)) { |
||
153 | // the ticket has expired |
||
154 | $message = 'Ticket ' . var_export($ticket, true) . ' has expired'; |
||
155 | $failed = true; |
||
156 | } elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($serviceUrl)) { |
||
157 | // The service url we passed to the query parameters does not match the one in the ticket. |
||
158 | $message = 'Mismatching service parameters: expected ' . |
||
159 | var_export($serviceTicket['service'], true) . |
||
160 | ' but was: ' . var_export($serviceUrl, true); |
||
161 | $failed = true; |
||
162 | } elseif ($forceAuthn && !$serviceTicket['forceAuthn']) { |
||
163 | // If `forceAuthn` is required but not set in the ticket |
||
164 | $message = 'Ticket was issued from single sign on session'; |
||
165 | $failed = true; |
||
166 | } |
||
167 | |||
168 | if ($failed) { |
||
169 | $finalMessage = 'casserver:validate: ' . $message; |
||
170 | Logger::error($finalMessage); |
||
171 | |||
172 | return new XmlResponse( |
||
173 | (string)$this->cas20Protocol->getValidateFailureResponse(C::ERR_INVALID_SERVICE, $message), |
||
174 | Response::HTTP_BAD_REQUEST, |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | $attributes = $serviceTicket['attributes']; |
||
179 | $this->cas20Protocol->setAttributes($attributes); |
||
180 | |||
181 | if (isset($pgtUrl)) { |
||
182 | $sessionTicket = $this->ticketStore->getTicket($serviceTicket['sessionId']); |
||
183 | if ( |
||
184 | $sessionTicket !== null |
||
185 | && $this->ticketFactory->isSessionTicket($sessionTicket) |
||
186 | && !$this->ticketFactory->isExpired($sessionTicket) |
||
187 | ) { |
||
188 | $proxyGrantingTicket = $this->ticketFactory->createProxyGrantingTicket( |
||
189 | [ |
||
190 | 'userName' => $serviceTicket['userName'], |
||
191 | 'attributes' => $attributes, |
||
192 | 'forceAuthn' => false, |
||
193 | 'proxies' => array_merge( |
||
194 | [$serviceUrl], |
||
195 | $serviceTicket['proxies'], |
||
196 | ), |
||
197 | 'sessionId' => $serviceTicket['sessionId'], |
||
198 | ], |
||
199 | ); |
||
200 | try { |
||
201 | // Here we assume that the fetch will throw on any error. |
||
202 | // The generation of the proxy-granting-ticket or the corresponding proxy granting ticket IOU may |
||
203 | // fail due to the proxy callback url failing to meet the minimum security requirements such as |
||
204 | // failure to establish trust between peers or unresponsiveness of the endpoint, etc. |
||
205 | // In case of failure, no proxy-granting ticket will be issued and the CAS service response |
||
206 | // as described in Section 2.5.2 MUST NOT contain a <proxyGrantingTicket> block. |
||
207 | // At this point, the issuance of a proxy-granting ticket is halted and service ticket |
||
208 | // validation will fail. |
||
209 | $data = $this->httpUtils->fetch( |
||
210 | $pgtUrl . '?pgtIou=' . $proxyGrantingTicket['iou'] . '&pgtId=' . $proxyGrantingTicket['id'], |
||
211 | ); |
||
212 | Logger::debug(__METHOD__ . '::data: ' . var_export($data, true)); |
||
213 | $this->cas20Protocol->setProxyGrantingTicketIOU($proxyGrantingTicket['iou']); |
||
214 | $this->ticketStore->addTicket($proxyGrantingTicket); |
||
215 | } catch (\Exception $e) { |
||
216 | return new XmlResponse( |
||
217 | (string)$this->cas20Protocol->getValidateFailureResponse( |
||
218 | C::ERR_INVALID_SERVICE, |
||
219 | 'Proxy callback url is failing.', |
||
220 | ), |
||
221 | Response::HTTP_BAD_REQUEST, |
||
222 | ); |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | |||
227 | return new XmlResponse( |
||
228 | (string)$this->cas20Protocol->getValidateSuccessResponse($serviceTicket['userName']), |
||
229 | Response::HTTP_OK, |
||
230 | ); |
||
233 |