| Conditions | 24 |
| Paths | 276 |
| Total Lines | 113 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 91 | public function decode($token, $key = null) |
||
| 92 | { |
||
| 93 | $key = empty($key) ? $this->key : $key; |
||
| 94 | if (is_null($key)) { |
||
| 95 | if (class_exists('O2System\Framework', false)) { |
||
| 96 | $key = config()->getItem('security')->encryptionKey; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | $timestamp = empty($this->timestamp) ? time() : $this->timestamp; |
||
| 101 | |||
| 102 | $segments = explode('.', $token); |
||
| 103 | $segments = array_map('trim', $segments); |
||
| 104 | |||
| 105 | if (count($segments) == 3) { |
||
| 106 | list($headers, $payload, $signature) = $segments; |
||
| 107 | |||
| 108 | // Base64 decode headers |
||
| 109 | if (false === ($headers = Base64::decode($headers))) { |
||
| 110 | $this->errors[] = 'Invalid header base64 decoding'; |
||
| 111 | |||
| 112 | return false; |
||
| 113 | } |
||
| 114 | |||
| 115 | // Json decode headers |
||
| 116 | if (null === ($headers = Json::decode($headers))) { |
||
| 117 | $this->errors[] = 'Invalid header json decoding'; |
||
| 118 | |||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | // Validate algorithm header |
||
| 123 | if (empty($headers->alg)) { |
||
| 124 | $this->errors[] = 'Invalid algorithm'; |
||
| 125 | |||
| 126 | return false; |
||
| 127 | } elseif ( ! Signature::validAlgorithm($headers->alg)) { |
||
| 128 | $this->errors[] = 'Unsupported algorithm'; |
||
| 129 | |||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | // Validate algorithm key id |
||
| 134 | if (is_array($key) or $key instanceof \ArrayAccess) { |
||
| 135 | if (isset($headers->kid)) { |
||
| 136 | if ( ! isset($key[ $headers->kid ])) { |
||
| 137 | $this->errors[] = 'Invalid Key Id'; |
||
| 138 | |||
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | $key = $key[ $headers->kid ]; |
||
| 143 | } else { |
||
| 144 | $this->errors[] = 'Empty Key id'; |
||
| 145 | |||
| 146 | return false; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | // Base64 decode payload |
||
| 151 | if (false === ($payload = Base64::decode($payload))) { |
||
| 152 | $this->errors[] = 'Invalid payload base64 decoding'; |
||
| 153 | |||
| 154 | return false; |
||
| 155 | } |
||
| 156 | |||
| 157 | // Json decode payload |
||
| 158 | if (null === ($payload = Json::decode($payload))) { |
||
| 159 | $this->errors[] = 'Invalid payload json decoding'; |
||
| 160 | |||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | // Base64 decode payload |
||
| 165 | if (false === ($signature = Base64::decode($signature))) { |
||
| 166 | $this->errors[] = 'Invalid signature base64 decoding'; |
||
| 167 | |||
| 168 | return false; |
||
| 169 | } |
||
| 170 | |||
| 171 | if (Signature::verify($token, $signature, $key, $headers->alg) === false) { |
||
| 172 | $this->errors[] = 'Invalid signature'; |
||
| 173 | |||
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | // Check if the nbf if it is defined. This is the time that the |
||
| 178 | // token can actually be used. If it's not yet that time, abort. |
||
| 179 | if (isset($payload->nbf) && $payload->nbf > ($timestamp + $this->leeway)) { |
||
| 180 | $this->errors[] = 'Cannot handle token prior to ' . date(\DateTime::ISO8601, $payload->nbf); |
||
| 181 | |||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | // Check that this token has been created before 'now'. This prevents |
||
| 186 | // using tokens that have been created for later use (and haven't |
||
| 187 | // correctly used the nbf claim). |
||
| 188 | if (isset($payload->iat) && $payload->iat > ($timestamp + $this->leeway)) { |
||
| 189 | $this->errors[] = 'Cannot handle token prior to ' . date(\DateTime::ISO8601, $payload->iat); |
||
| 190 | |||
| 191 | return false; |
||
| 192 | } |
||
| 193 | // Check if this token has expired. |
||
| 194 | if (isset($payload->exp) && ($timestamp - $this->leeway) >= $payload->exp) { |
||
| 195 | $this->errors[] = 'Expired token'; |
||
| 196 | |||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | return $payload; |
||
| 201 | } |
||
| 202 | |||
| 203 | return false; |
||
| 204 | } |
||
| 205 | } |