| Total Complexity | 40 |
| Total Lines | 157 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 0 |
Complex classes like jwtAuthRoles 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 jwtAuthRoles, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class jwtAuthRoles |
||
| 16 | { |
||
| 17 | |||
| 18 | private static function getKid(string $jwt): ?string |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | private static function getClaims(string $jwt): ?object |
||
| 33 | { |
||
| 34 | if (Str::is('*.*.*', $jwt)) { |
||
| 35 | $claims = explode('.', $jwt); |
||
| 36 | $claims = JWT::jsonDecode(JWT::urlsafeB64Decode($claims[1])); |
||
| 37 | return $claims ?? null; |
||
| 38 | } else { |
||
| 39 | throw authException::auth(422, 'Malformed JWT'); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param object $jwk |
||
| 45 | * @return bool|string|null |
||
| 46 | */ |
||
| 47 | private static function jwkToPem(object $jwk) |
||
| 48 | { |
||
| 49 | if (isset($jwk->e) && isset($jwk->n)) { |
||
| 50 | $rsa = new RSA(); |
||
| 51 | $rsa->loadKey([ |
||
| 52 | 'e' => new BigInteger(JWT::urlsafeB64Decode($jwk->e), 256), |
||
| 53 | 'n' => new BigInteger(JWT::urlsafeB64Decode($jwk->n), 256), |
||
| 54 | ]); |
||
| 55 | |||
| 56 | return $rsa->getPublicKey(); |
||
| 57 | } |
||
| 58 | throw authException::auth(500, 'Malformed jwk'); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $kid |
||
| 63 | * @param string $uri |
||
| 64 | * @return bool|string|null |
||
| 65 | */ |
||
| 66 | private static function getJwk(string $kid, string $uri) |
||
| 81 | } |
||
| 82 | |||
| 83 | private static function getPem(string $kid, string $uri): ?string |
||
| 98 | } |
||
| 99 | |||
| 100 | private static function verifyToken(string $jwt, string $uri, bool $jwk = false): object |
||
| 101 | { |
||
| 102 | $kid = self::getKid($jwt); |
||
| 103 | if (! $kid) { |
||
| 104 | throw authException::auth(422, 'Malformed JWT'); |
||
| 105 | } |
||
| 106 | if (config('jwtAuthRoles.cache.enabled')) { |
||
| 107 | if (config('jwtAuthRoles.cache.type') === 'database') { |
||
| 108 | $row = JwtKey::where('kid', $kid) |
||
| 109 | ->orderBy('created_at', 'desc') |
||
| 110 | ->first('key'); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | $publicKey = $row->key |
||
| 115 | ?? $jwk |
||
| 116 | ? self::getJwk($kid, $uri) |
||
| 117 | : self::getPem($kid, $uri); |
||
| 118 | |||
| 119 | if (! isset($publicKey) || ! $publicKey) { |
||
| 120 | throw authException::auth(500, 'Unable to validate JWT'); |
||
| 121 | } |
||
| 122 | |||
| 123 | if (config('jwtAuthRoles.cache.enabled')) { |
||
| 124 | if (config('jwtAuthRoles.cache.type') === 'database') { |
||
| 125 | $row = $row ?? JwtKey::create(['kid' => $kid, 'key' => $publicKey]); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | return JWT::decode($jwt, $publicKey, [config('jwtAuthRoles.alg')]); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** @return mixed */ |
||
| 133 | public static function authUser(object $request) |
||
| 172 | } |
||
| 173 | } |
||
| 174 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths