| Total Complexity | 40 |
| Total Lines | 163 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Jwt 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 Jwt, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Jwt implements EncryptionInterface |
||
| 10 | { |
||
| 11 | public function encode($payload, $key, $algo = 'HS256') |
||
| 12 | { |
||
| 13 | $header = $this->generateJwtHeader($payload, $algo); |
||
| 14 | |||
| 15 | $segments = array( |
||
| 16 | $this->urlSafeB64Encode(json_encode($header)), |
||
| 17 | $this->urlSafeB64Encode(json_encode($payload)) |
||
| 18 | ); |
||
| 19 | |||
| 20 | $signing_input = implode('.', $segments); |
||
| 21 | |||
| 22 | $signature = $this->sign($signing_input, $key, $algo); |
||
| 23 | $segments[] = $this->urlsafeB64Encode($signature); |
||
| 24 | |||
| 25 | return implode('.', $segments); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function decode($jwt, $key = null, $allowedAlgorithms = true) |
||
| 68 | } |
||
| 69 | |||
| 70 | private function verifySignature($signature, $input, $key, $algo = 'HS256') |
||
| 71 | { |
||
| 72 | // use constants when possible, for HipHop support |
||
| 73 | switch ($algo) { |
||
| 74 | case'HS256': |
||
| 75 | case'HS384': |
||
| 76 | case'HS512': |
||
| 77 | return $this->hash_equals( |
||
| 78 | $this->sign($input, $key, $algo), |
||
| 79 | $signature |
||
| 80 | ); |
||
| 81 | |||
| 82 | case 'RS256': |
||
| 83 | return openssl_verify($input, $signature, $key, defined('OPENSSL_ALGO_SHA256') ? OPENSSL_ALGO_SHA256 : 'sha256') === 1; |
||
|
|
|||
| 84 | |||
| 85 | case 'RS384': |
||
| 86 | return @openssl_verify($input, $signature, $key, defined('OPENSSL_ALGO_SHA384') ? OPENSSL_ALGO_SHA384 : 'sha384') === 1; |
||
| 87 | |||
| 88 | case 'RS512': |
||
| 89 | return @openssl_verify($input, $signature, $key, defined('OPENSSL_ALGO_SHA512') ? OPENSSL_ALGO_SHA512 : 'sha512') === 1; |
||
| 90 | |||
| 91 | default: |
||
| 92 | throw new \InvalidArgumentException("Unsupported or invalid signing algorithm."); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | private function sign($input, $key, $algo = 'HS256') |
||
| 97 | { |
||
| 98 | switch ($algo) { |
||
| 99 | case 'HS256': |
||
| 100 | return hash_hmac('sha256', $input, $key, true); |
||
| 101 | |||
| 102 | case 'HS384': |
||
| 103 | return hash_hmac('sha384', $input, $key, true); |
||
| 104 | |||
| 105 | case 'HS512': |
||
| 106 | return hash_hmac('sha512', $input, $key, true); |
||
| 107 | |||
| 108 | case 'RS256': |
||
| 109 | return $this->generateRSASignature($input, $key, defined('OPENSSL_ALGO_SHA256') ? OPENSSL_ALGO_SHA256 : 'sha256'); |
||
| 110 | |||
| 111 | case 'RS384': |
||
| 112 | return $this->generateRSASignature($input, $key, defined('OPENSSL_ALGO_SHA384') ? OPENSSL_ALGO_SHA384 : 'sha384'); |
||
| 113 | |||
| 114 | case 'RS512': |
||
| 115 | return $this->generateRSASignature($input, $key, defined('OPENSSL_ALGO_SHA512') ? OPENSSL_ALGO_SHA512 : 'sha512'); |
||
| 116 | |||
| 117 | default: |
||
| 118 | throw new \Exception("Unsupported or invalid signing algorithm."); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | private function generateRSASignature($input, $key, $algo) |
||
| 123 | { |
||
| 124 | if (!openssl_sign($input, $signature, $key, $algo)) { |
||
| 125 | throw new \Exception("Unable to sign data."); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $signature; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function urlSafeB64Encode($data) |
||
| 139 | } |
||
| 140 | |||
| 141 | public function urlSafeB64Decode($b64) |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Override to create a custom header |
||
| 152 | */ |
||
| 153 | protected function generateJwtHeader($payload, $algorithm) |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | |||
| 161 | protected function hash_equals($a, $b) |
||
| 162 | { |
||
| 163 | if (function_exists('hash_equals')) { |
||
| 164 | return hash_equals($a, $b); |
||
| 165 | } |
||
| 166 | $diff = strlen($a) ^ strlen($b); |
||
| 167 | for ($i = 0; $i < strlen($a) && $i < strlen($b); $i++) { |
||
| 168 | $diff |= ord($a[$i]) ^ ord($b[$i]); |
||
| 169 | } |
||
| 170 | |||
| 171 | return $diff === 0; |
||
| 172 | } |
||
| 174 |