Total Complexity | 56 |
Total Lines | 328 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like jwtValidate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use jwtValidate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class jwtValidate extends jwt |
||
26 | { |
||
27 | /** |
||
28 | * [$TIMESTAMP] |
||
29 | * @var int |
||
30 | */ |
||
31 | protected static $TIMESTAMP; |
||
32 | |||
33 | /** |
||
34 | * [$LEEWAY] |
||
35 | * @var int |
||
36 | */ |
||
37 | protected static $LEEWAY = 0; |
||
38 | |||
39 | /** |
||
40 | * [$ALGORITHM] |
||
41 | * @var string |
||
42 | */ |
||
43 | private static $ALGORITHM; |
||
|
|||
44 | |||
45 | /** |
||
46 | * [$isPayloadValid Validation placeholders] |
||
47 | * @var array |
||
48 | */ |
||
49 | protected static $isPayloadValid = [ |
||
50 | "iss" => false, |
||
51 | "sub" => false, |
||
52 | "iat" => false, |
||
53 | "nbf" => false, |
||
54 | "exp" => false, |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * [unauthorised Set unauthorized headers] |
||
59 | * @throws \responsible\core\exception\responsibleException |
||
60 | * @return void |
||
61 | */ |
||
62 | private static function unauthorised() |
||
63 | { |
||
64 | (new jwt())->setUnauthorised(); |
||
65 | // @codeCoverageIgnoreStart |
||
66 | } |
||
67 | // @codeCoverageIgnoreEnd |
||
68 | |||
69 | /** |
||
70 | * [header Validate the header object] |
||
71 | * First segment of the token |
||
72 | * @return void |
||
73 | * @throws \responsible\core\exception\responsibleException |
||
74 | */ |
||
75 | public static function header(array $headObject = []) |
||
76 | { |
||
77 | if ( |
||
78 | empty($headObject) || |
||
79 | !self::typ($headObject) || |
||
80 | !self::alg($headObject) |
||
81 | ) { |
||
82 | self::unauthorised(); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * [payload Validate the payload object] |
||
88 | * Second segment of the token |
||
89 | * @return bool |
||
90 | * @throws \responsible\core\exception\responsibleException |
||
91 | */ |
||
92 | public static function payload(array $payloadObject = []) |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Check if the scope is anonymous |
||
112 | * @return boolean |
||
113 | */ |
||
114 | private static function isAnonymousScope($payloadObject) |
||
115 | { |
||
116 | return (isset($payloadObject['scope']) && $payloadObject['scope'] == 'anonymous'); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * [signature - Validate the signature object] |
||
121 | * Third segment of the token |
||
122 | * @return void |
||
123 | * @throws \responsible\core\exception\responsibleException |
||
124 | */ |
||
125 | public static function signature($jwtHead, $jwtPayload, $signature, $key) |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * [typ Issuer claim] |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | public static function typ($headObject) |
||
158 | { |
||
159 | if ( |
||
160 | !isset($headObject['typ']) || |
||
161 | (isset($headObject['typ']) && empty($headObject)) |
||
162 | ) { |
||
163 | return false; |
||
164 | } |
||
165 | return true; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * [alg Issuer claim] |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | public static function alg($headObject) |
||
174 | { |
||
175 | if ( |
||
176 | !isset($headObject['alg']) || |
||
177 | (isset($headObject['alg']) && empty($headObject)) && |
||
178 | (self::getAlgorithm()['header'] === $headObject['alg']) |
||
179 | ) { |
||
180 | return false; |
||
181 | } |
||
182 | |||
183 | return true; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * [iss Issuer claim] |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | public static function iss($payloadObject) |
||
192 | { |
||
193 | if ( |
||
194 | !isset($payloadObject['iss']) || |
||
195 | (isset($payloadObject['iss']) && empty($payloadObject)) |
||
196 | ) { |
||
197 | return false; |
||
198 | } |
||
199 | |||
200 | $router = new route\router(); |
||
201 | $router->route(); |
||
202 | |||
203 | if ($payloadObject['iss'] !== $router->getIssuer(true)) { |
||
204 | return false; |
||
205 | } |
||
206 | |||
207 | self::$isPayloadValid['iss'] = true; |
||
208 | return true; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * [sub Subject claim] |
||
213 | * The Responsible API uses the "Subject" claim as a placeholder for account Ids |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | public static function sub($payloadObject) |
||
218 | { |
||
219 | $server = new server([], parent::$options); |
||
220 | if ($server->isMockTest()) { |
||
221 | return self::$isPayloadValid['sub'] = true; |
||
222 | } |
||
223 | |||
224 | // @codeCoverageIgnoreStart |
||
225 | if ( |
||
226 | !isset($payloadObject['sub']) || |
||
227 | (isset($payloadObject['sub']) && empty($payloadObject)) |
||
228 | ) { |
||
229 | return false; |
||
230 | } |
||
231 | |||
232 | $account = (object) (new user\user()) |
||
233 | ->setOptions(parent::$options) |
||
234 | ->load($payloadObject['sub'], ['refreshToken' => false]) |
||
235 | ; |
||
236 | |||
237 | if (empty($account)) { |
||
238 | return false; |
||
239 | } else { |
||
240 | if (!isset($account->account_id)) { |
||
241 | return false; |
||
242 | } |
||
243 | |||
244 | if ((int) $account->account_id !== (int) $payloadObject['sub']) { |
||
245 | return false; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | self::$isPayloadValid['sub'] = true; |
||
250 | return true; |
||
251 | // @codeCoverageIgnoreEnd |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * [iat Issued at claim] |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public static function iat($payloadObject) |
||
260 | { |
||
261 | if ( |
||
262 | !isset($payloadObject['iat']) || |
||
263 | (isset($payloadObject['iat']) && empty($payloadObject)) |
||
264 | ) { |
||
265 | return false; |
||
266 | } |
||
267 | |||
268 | if ($payloadObject['iat'] > self::getTimestamp()) { |
||
269 | (new exception\errorException()) |
||
270 | ->setOptions(parent::$options) |
||
271 | ->message(self::messages('not_ready')) |
||
272 | ->error('NO_CONTENT'); |
||
273 | } |
||
274 | |||
275 | self::$isPayloadValid['iat'] = true; |
||
276 | return true; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * [nbf Not before claim] |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | public static function nbf($payloadObject) |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * [exp Expiration claim this optional so if its not set return true] |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | public static function exp($payloadObject) |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * [leeway Inherit the leeway offset] |
||
328 | * @param int $leeway [integer in seconds] |
||
329 | * @return void |
||
330 | */ |
||
331 | public static function leeway($leeway) |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * [timestamp Inherit the current timestamp (now)] |
||
338 | * @param int $timestamp [integer in seconds] |
||
339 | * @return void |
||
340 | */ |
||
341 | public static function timestamp($timestamp) |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * [getTimestamp Add both the timestamp (now) and the leeway] |
||
348 | * @return int |
||
349 | */ |
||
350 | private static function getTimestamp() |
||
355 |