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