| Total Complexity | 62 |
| Total Lines | 314 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 |
||
| 23 | class jwtValidate extends jwt |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * [$TIMESTAMP] |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected static $TIMESTAMP; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * [$LEEWAY] |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | protected static $LEEWAY = 0; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * [$ALGORITHM] |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private static $ALGORITHM; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * [header Validate the header object] |
||
| 45 | * First segment of the token |
||
| 46 | * @return bool |
||
| 47 | */ |
||
| 48 | public static function header(array $headObject = []) |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * [payload Validate the payload object] |
||
| 64 | * Second segment of the token |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | public static function payload(array $payloadObject = []) |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Check if the scope is anonymous |
||
| 90 | * @return boolean |
||
| 91 | */ |
||
| 92 | private static function isAnonymousScope($payloadObject) |
||
| 93 | { |
||
| 94 | return (isset($payloadObject['scope']) && $payloadObject['scope'] == 'anonymous'); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * [signature - Validate the signature object] |
||
| 99 | * Third segment of the token |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | public static function signature($jwtHead, $jwtPayload, $signature, $key) |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * [typ Issuer claim] |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public static function typ($headObject) |
||
| 139 | { |
||
| 140 | if (!isset($headObject['typ']) || |
||
| 141 | (isset($headObject['typ']) && empty($headObject)) |
||
| 142 | ) { |
||
| 143 | return; |
||
| 144 | } |
||
| 145 | return true; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * [alg Issuer claim] |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public static function alg($headObject) |
||
| 154 | { |
||
| 155 | if (!isset($headObject['alg']) || |
||
| 156 | (isset($headObject['alg']) && empty($headObject)) && |
||
| 157 | self::getAlgorithm($headObject['alg']) |
||
| 158 | ) { |
||
| 159 | return; |
||
| 160 | } |
||
| 161 | |||
| 162 | self::algorithm($headObject['alg']); |
||
| 163 | |||
| 164 | return true; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * [iss Issuer claim] |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public static function iss($payloadObject) |
||
| 173 | { |
||
| 174 | if (!isset($payloadObject['iss']) || |
||
| 175 | (isset($payloadObject['iss']) && empty($payloadObject)) |
||
| 176 | ) { |
||
| 177 | return; |
||
| 178 | } |
||
| 179 | |||
| 180 | $router = new route\router; |
||
| 181 | $router->route(); |
||
| 182 | |||
| 183 | if ($payloadObject['iss'] !== $router->getIssuer(true)) { |
||
| 184 | return; |
||
| 185 | } |
||
| 186 | |||
| 187 | return true; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * [sub Subject claim] |
||
| 192 | * The Responsible API uses the "Subject" claim as a placeholder for account Ids |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public static function sub($payloadObject) |
||
| 197 | { |
||
| 198 | $server = new server([], parent::$options); |
||
| 199 | if ($server->isMockTest()) { |
||
| 200 | return true; |
||
| 201 | } |
||
| 202 | |||
| 203 | if (!isset($payloadObject['sub']) || |
||
| 204 | (isset($payloadObject['sub']) && empty($payloadObject)) |
||
| 205 | ) { |
||
| 206 | return; |
||
| 207 | } |
||
| 208 | |||
| 209 | $account = (object) (new user\user) |
||
| 210 | ->setOptions(parent::$options) |
||
| 211 | ->load($payloadObject['sub'], ['refreshToken' => false]) |
||
| 212 | ; |
||
| 213 | |||
| 214 | if (empty($account)) { |
||
| 215 | return; |
||
| 216 | } else { |
||
| 217 | if (!isset($account->account_id)) { |
||
| 218 | return; |
||
| 219 | } |
||
| 220 | |||
| 221 | if ((int) $account->account_id !== (int) $payloadObject['sub']) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | return true; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * [iat Issued at claim] |
||
| 231 | * |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | public static function iat($payloadObject) |
||
| 235 | { |
||
| 236 | if (!isset($payloadObject['iat']) || |
||
| 237 | (isset($payloadObject['iat']) && empty($payloadObject)) |
||
| 238 | ) { |
||
| 239 | return; |
||
| 240 | } |
||
| 241 | |||
| 242 | if ($payloadObject['iat'] > self::getTimestamp()) { |
||
| 243 | (new exception\errorException) |
||
| 244 | ->setOptions(parent::$options) |
||
| 245 | ->message(self::messages('not_ready')) |
||
| 246 | ->error('NO_CONTENT'); |
||
| 247 | } |
||
| 248 | |||
| 249 | return true; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * [nbf Not before claim] |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public static function nbf($payloadObject) |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * [exp Expiration claim this optional so if its not set return true] |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public static function exp($payloadObject) |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * [leeway Inherit the leeway offset] |
||
| 300 | * @param int $leeway [integer in seconds] |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | public static function leeway($leeway) |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * [timestamp Inherit the current timestamp (now)] |
||
| 310 | * @param int $timestamp [integer in seconds] |
||
| 311 | * @return void |
||
| 312 | */ |
||
| 313 | public static function timestamp($timestamp) |
||
| 314 | { |
||
| 315 | self::$TIMESTAMP = $timestamp; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * [getTimestamp Add both the timestamp (now) and the leeway] |
||
| 320 | * @return int |
||
| 321 | */ |
||
| 322 | private static function getTimestamp() |
||
| 323 | { |
||
| 324 | return (int) (self::$TIMESTAMP + self::$LEEWAY); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * [algorithm Set the requested hash algorithm] |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public static function algorithm($algo = 'SHA256') |
||
| 337 | } |
||
| 338 | } |
||
| 339 |