@@ -71,7 +71,9 @@ discard block |
||
71 | 71 | */ |
72 | 72 | public function __construct(?array $params=null) |
73 | 73 | { |
74 | - if ($params != null) $this->init($params); |
|
74 | + if ($params != null) { |
|
75 | + $this->init($params); |
|
76 | + } |
|
75 | 77 | get_instance()->load->splint("francis94c/ci-jwt", "%base64"); |
76 | 78 | } |
77 | 79 | |
@@ -112,8 +114,12 @@ discard block |
||
112 | 114 | */ |
113 | 115 | public function header(?string $key=null, $value=null) |
114 | 116 | { |
115 | - if (!$key) return $this->header; |
|
116 | - if ($value === null) return $this->header[$key]; |
|
117 | + if (!$key) { |
|
118 | + return $this->header; |
|
119 | + } |
|
120 | + if ($value === null) { |
|
121 | + return $this->header[$key]; |
|
122 | + } |
|
117 | 123 | $this->header[$key] = $value; |
118 | 124 | return $this; |
119 | 125 | } |
@@ -138,8 +144,12 @@ discard block |
||
138 | 144 | */ |
139 | 145 | public function payload(?string $key=null, $value=null) |
140 | 146 | { |
141 | - if (!$key) return $this->payload; |
|
142 | - if ($value === null) return $this->payload[$key]; |
|
147 | + if (!$key) { |
|
148 | + return $this->payload; |
|
149 | + } |
|
150 | + if ($value === null) { |
|
151 | + return $this->payload[$key]; |
|
152 | + } |
|
143 | 153 | $this->payload[$key] = $value; |
144 | 154 | return $this; |
145 | 155 | } |
@@ -153,7 +163,9 @@ discard block |
||
153 | 163 | */ |
154 | 164 | public function __call(string $method, array $args) |
155 | 165 | { |
156 | - if (count($args) == 0) return $this->payload[$method]; |
|
166 | + if (count($args) == 0) { |
|
167 | + return $this->payload[$method]; |
|
168 | + } |
|
157 | 169 | $this->payload[$method] = $args[0]; |
158 | 170 | return $this; |
159 | 171 | } |
@@ -166,7 +178,9 @@ discard block |
||
166 | 178 | */ |
167 | 179 | public function iss(string $iss=null) |
168 | 180 | { |
169 | - if ($iss === null) return $this->payload['iss']; |
|
181 | + if ($iss === null) { |
|
182 | + return $this->payload['iss']; |
|
183 | + } |
|
170 | 184 | $this->payload['iss'] = $iss; |
171 | 185 | return $this; |
172 | 186 | } |
@@ -199,21 +213,33 @@ discard block |
||
199 | 213 | */ |
200 | 214 | public function sign(string $secret=null):?string { |
201 | 215 | // Checks. |
202 | - if (count($this->payload) == 0) return null; |
|
216 | + if (count($this->payload) == 0) { |
|
217 | + return null; |
|
218 | + } |
|
203 | 219 | // $key is $secret. |
204 | 220 | $key = $secret ?? $this->secret; |
205 | 221 | $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512); |
206 | 222 | $this->header["typ"] = $this->header["typ"] ?? self::JWT; |
207 | 223 | // Generate Issued At Time. |
208 | - if ($this->set_iat) $this->payload["iat"] = $this->payload['iat'] ?? time(); |
|
224 | + if ($this->set_iat) { |
|
225 | + $this->payload["iat"] = $this->payload['iat'] ?? time(); |
|
226 | + } |
|
209 | 227 | // Auto Expire. |
210 | - if ($this->auto_expire != null && !isset($this->payload['exp'])) $this->payload['exp'] = strtotime($this->auto_expire); |
|
228 | + if ($this->auto_expire != null && !isset($this->payload['exp'])) { |
|
229 | + $this->payload['exp'] = strtotime($this->auto_expire); |
|
230 | + } |
|
211 | 231 | $jwt = base64url_encode(json_encode($this->header)); |
212 | - if ($jwt === false) return null; |
|
213 | - if ($jwt != "") $jwt .= "."; |
|
232 | + if ($jwt === false) { |
|
233 | + return null; |
|
234 | + } |
|
235 | + if ($jwt != "") { |
|
236 | + $jwt .= "."; |
|
237 | + } |
|
214 | 238 | $payload = base64url_encode(json_encode($this->payload)); |
215 | 239 | $jwt .= $payload; |
216 | - if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]); |
|
240 | + if ($key != "") { |
|
241 | + return $this->sign_token($jwt, $key, $this->header["alg"]); |
|
242 | + } |
|
217 | 243 | return $jwt . "."; |
218 | 244 | } |
219 | 245 | |
@@ -224,11 +250,17 @@ discard block |
||
224 | 250 | public function token():?string |
225 | 251 | { |
226 | 252 | // Checks. |
227 | - if (count($this->payload) == 0) return null; |
|
253 | + if (count($this->payload) == 0) { |
|
254 | + return null; |
|
255 | + } |
|
228 | 256 | // Begin. |
229 | 257 | $this->header["alg"] = self::NONE; |
230 | - if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time(); |
|
231 | - if ($this->auto_expire != null) $this->payload["exp"] = strtotime($this->auto_expire); |
|
258 | + if ($this->set_iat) { |
|
259 | + $this->payload["iat"] = $this->payload["iat"] ?? time(); |
|
260 | + } |
|
261 | + if ($this->auto_expire != null) { |
|
262 | + $this->payload["exp"] = strtotime($this->auto_expire); |
|
263 | + } |
|
232 | 264 | return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . "."; |
233 | 265 | } |
234 | 266 | |
@@ -239,14 +271,21 @@ discard block |
||
239 | 271 | * @return bool [description] |
240 | 272 | */ |
241 | 273 | public function verify(string $jwt, string $secret=null):bool { |
242 | - if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
|
274 | + if (substr_count($jwt, ".") != 2) { |
|
275 | + return false; |
|
276 | + } |
|
277 | + // Invalid JWT. |
|
243 | 278 | $key = $secret ?? $this->secret; |
244 | 279 | $parts = explode(".", $jwt); |
245 | 280 | $header = json_decode(base64url_decode($parts[0]) ,true); |
246 | - if ($header == null) return false; |
|
281 | + if ($header == null) { |
|
282 | + return false; |
|
283 | + } |
|
247 | 284 | $alg = $this->algorithm ?? $header["alg"] ?? self::HS256; |
248 | 285 | $payload = json_decode(base64url_decode($parts[1]) ,true); |
249 | - if ($payload == null) return false; |
|
286 | + if ($payload == null) { |
|
287 | + return false; |
|
288 | + } |
|
250 | 289 | if ($parts[2] == "") { |
251 | 290 | return $this->allow_unsigned; |
252 | 291 | } |
@@ -273,9 +312,13 @@ discard block |
||
273 | 312 | public function decode(string $jwt):bool { |
274 | 313 | $parts = explode(".", $jwt); |
275 | 314 | $header = json_decode(base64url_decode($parts[0]), true); |
276 | - if ($header === false) return false; |
|
315 | + if ($header === false) { |
|
316 | + return false; |
|
317 | + } |
|
277 | 318 | $payload = json_decode(base64url_decode($parts[1]), true); |
278 | - if ($payload === false) return false; |
|
319 | + if ($payload === false) { |
|
320 | + return false; |
|
321 | + } |
|
279 | 322 | $this->header = $header; |
280 | 323 | $this->payload = $payload; |
281 | 324 | return true; |
@@ -322,7 +365,9 @@ discard block |
||
322 | 365 | */ |
323 | 366 | private function sign_token(string $token, string $key, string $alg):string |
324 | 367 | { |
325 | - if ($alg == self::NONE) return $token . "."; |
|
368 | + if ($alg == self::NONE) { |
|
369 | + return $token . "."; |
|
370 | + } |
|
326 | 371 | $token = rtrim($token, "."); |
327 | 372 | $signature = hash_hmac(self::ALGOS[$alg], $token, $key); |
328 | 373 | return $token . "." . $signature; |