Conditions | 13 |
Paths | 22 |
Total Lines | 88 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
79 | public function validate( |
||
80 | Request $request, |
||
81 | #[MapQueryParameter] ?string $ticket = null, |
||
82 | #[MapQueryParameter] bool $renew = false, |
||
83 | #[MapQueryParameter] ?string $service = null, |
||
84 | ): Response { |
||
85 | $forceAuthn = $renew; |
||
86 | // Check if any of the required query parameters are missing |
||
87 | // Even though we can delegate the check to Symfony's `MapQueryParameter` we cannot return |
||
88 | // the failure response needed. As a result, we allow a default value, and we handle the missing |
||
89 | // values afterwards. |
||
90 | if ($service === null || $ticket === null) { |
||
91 | $messagePostfix = $service === null ? 'service' : 'ticket'; |
||
92 | Logger::debug("casserver: Missing service parameter: [{$messagePostfix}]"); |
||
93 | return new Response( |
||
94 | $this->cas10Protocol->getValidateFailureResponse(), |
||
95 | Response::HTTP_BAD_REQUEST, |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | try { |
||
100 | // Get the service ticket |
||
101 | // `getTicket` uses the unserializable method and Objects may throw Throwables in their |
||
102 | // unserialization handlers. |
||
103 | $serviceTicket = $this->ticketStore->getTicket($ticket); |
||
104 | // Delete the ticket |
||
105 | $this->ticketStore->deleteTicket($ticket); |
||
106 | } catch (\Exception $e) { |
||
107 | Logger::error('casserver:validate: internal server error. ' . var_export($e->getMessage(), true)); |
||
108 | return new Response( |
||
109 | $this->cas10Protocol->getValidateFailureResponse(), |
||
110 | Response::HTTP_INTERNAL_SERVER_ERROR, |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | $failed = false; |
||
115 | $message = ''; |
||
116 | if (empty($serviceTicket)) { |
||
117 | // No ticket |
||
118 | $message = 'ticket: ' . var_export($ticket, true) . ' not recognized'; |
||
119 | $failed = true; |
||
120 | } elseif (!$this->ticketFactory->isServiceTicket($serviceTicket)) { |
||
121 | // This is not a service ticket |
||
122 | $message = 'ticket: ' . var_export($ticket, true) . ' is not a service ticket'; |
||
123 | $failed = true; |
||
124 | } elseif ($this->ticketFactory->isExpired($serviceTicket)) { |
||
125 | // the ticket has expired |
||
126 | $message = 'Ticket has ' . var_export($ticket, true) . ' expired'; |
||
127 | $failed = true; |
||
128 | } elseif ($this->sanitize($serviceTicket['service']) !== $this->sanitize($service)) { |
||
129 | // The service url we passed to the query parameters does not match the one in the ticket. |
||
130 | $message = 'Mismatching service parameters: expected ' . |
||
131 | var_export($serviceTicket['service'], true) . |
||
132 | ' but was: ' . var_export($service, true); |
||
133 | $failed = true; |
||
134 | } elseif ($forceAuthn && !$serviceTicket['forceAuthn']) { |
||
135 | // If `forceAuthn` is required but not set in the ticket |
||
136 | $message = 'Ticket was issued from single sign on session'; |
||
137 | $failed = true; |
||
138 | } |
||
139 | |||
140 | if ($failed) { |
||
141 | Logger::error('casserver:validate: ' . $message); |
||
142 | return new Response( |
||
143 | $this->cas10Protocol->getValidateFailureResponse(), |
||
144 | Response::HTTP_BAD_REQUEST, |
||
145 | ); |
||
146 | } |
||
147 | |||
148 | // Get the username field |
||
149 | $usernameField = $this->casConfig->getOptionalValue('attrname', 'eduPersonPrincipalName'); |
||
150 | |||
151 | // Fail if the username field is not present in the attribute list |
||
152 | if (!\array_key_exists($usernameField, $serviceTicket['attributes'])) { |
||
153 | Logger::error( |
||
154 | 'casserver:validate: internal server error. Missing user name attribute: ' |
||
155 | . var_export($usernameField, true), |
||
156 | ); |
||
157 | return new Response( |
||
158 | $this->cas10Protocol->getValidateFailureResponse(), |
||
159 | Response::HTTP_BAD_REQUEST, |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | // Successful validation |
||
164 | return new Response( |
||
165 | $this->cas10Protocol->getValidateSuccessResponse($serviceTicket['attributes'][$usernameField][0]), |
||
166 | Response::HTTP_OK, |
||
167 | ); |
||
180 |