@@ -27,7 +27,7 @@ |
||
| 27 | 27 | * |
| 28 | 28 | * @return string|bool Decoded String. |
| 29 | 29 | */ |
| 30 | - function base64url_decode(string $data, bool $strict=false) { |
|
| 30 | + function base64url_decode(string $data, bool $strict = false) { |
|
| 31 | 31 | $b64 = strtr($data, '-_', '+/'); |
| 32 | 32 | return base64_decode($b64, $strict); |
| 33 | 33 | } |
@@ -11,7 +11,9 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | function base64url_encode(string $data) { |
| 13 | 13 | $b64 = base64_encode($data); |
| 14 | - if ($b64 === false) return false; |
|
| 14 | + if ($b64 === false) { |
|
| 15 | + return false; |
|
| 16 | + } |
|
| 15 | 17 | $url = strtr($b64, '+/', '-_'); |
| 16 | 18 | return rtrim($url, '='); |
| 17 | 19 | } |
@@ -20,7 +20,7 @@ |
||
| 20 | 20 | * @covers JWT::__construct |
| 21 | 21 | */ |
| 22 | 22 | public static function setUpBeforeClass(): void { |
| 23 | - self::$ci =& get_instance(); |
|
| 23 | + self::$ci = & get_instance(); |
|
| 24 | 24 | /** |
| 25 | 25 | * [$params Config Items.] |
| 26 | 26 | * |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * @date 2020-03-28 |
| 70 | 70 | * @param [type] $params Config array. |
| 71 | 71 | */ |
| 72 | - public function __construct(?array $params=null) |
|
| 72 | + public function __construct(?array $params = null) |
|
| 73 | 73 | { |
| 74 | 74 | if ($params != null) $this->init($params); |
| 75 | 75 | get_instance()->load->splint("francis94c/ci-jwt", "%base64"); |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | * if specified, or the header array if no key is |
| 111 | 111 | * specified. Method chaining is supported. |
| 112 | 112 | */ |
| 113 | - public function header(?string $key=null, $value=null) |
|
| 113 | + public function header(?string $key = null, $value = null) |
|
| 114 | 114 | { |
| 115 | 115 | if (!$key) return $this->header; |
| 116 | 116 | if ($value === null) return $this->header[$key]; |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | * @return mixed|null Value of $key if $value == null, payload array if $key |
| 137 | 137 | * == null. Method chaining supported. |
| 138 | 138 | */ |
| 139 | - public function payload(?string $key=null, $value=null) |
|
| 139 | + public function payload(?string $key = null, $value = null) |
|
| 140 | 140 | { |
| 141 | 141 | if (!$key) return $this->payload; |
| 142 | 142 | if ($value === null) return $this->payload[$key]; |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | * @return mixed|none Value of the 'iss' claim, if the $iss argument wasn't |
| 165 | 165 | * supplied. Otherwise, null. |
| 166 | 166 | */ |
| 167 | - public function iss(string $iss=null) |
|
| 167 | + public function iss(string $iss = null) |
|
| 168 | 168 | { |
| 169 | 169 | if ($iss === null) return $this->payload['iss']; |
| 170 | 170 | $this->payload['iss'] = $iss; |
@@ -197,9 +197,9 @@ discard block |
||
| 197 | 197 | * @param [type] $secret [description] |
| 198 | 198 | * @return [type] [description] |
| 199 | 199 | */ |
| 200 | - public function sign(string $secret=null):?string { |
|
| 200 | + public function sign(string $secret = null): ?string { |
|
| 201 | 201 | // Checks. |
| 202 | - if (count($this->payload) == 0) return null; |
|
| 202 | + if (count($this->payload) == 0) return null; |
|
| 203 | 203 | // $key is $secret. |
| 204 | 204 | $key = $secret ?? $this->secret; |
| 205 | 205 | $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512); |
@@ -221,10 +221,10 @@ discard block |
||
| 221 | 221 | * [token description] |
| 222 | 222 | * @return string [description] |
| 223 | 223 | */ |
| 224 | - public function token():?string |
|
| 224 | + public function token(): ?string |
|
| 225 | 225 | { |
| 226 | 226 | // Checks. |
| 227 | - if (count($this->payload) == 0) return null; |
|
| 227 | + if (count($this->payload) == 0) return null; |
|
| 228 | 228 | // Begin. |
| 229 | 229 | $this->header["alg"] = self::NONE; |
| 230 | 230 | if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time(); |
@@ -238,14 +238,14 @@ discard block |
||
| 238 | 238 | * @param string $secret [description] |
| 239 | 239 | * @return bool [description] |
| 240 | 240 | */ |
| 241 | - public function verify(string $jwt, string $secret=null):bool { |
|
| 241 | + public function verify(string $jwt, string $secret = null):bool { |
|
| 242 | 242 | if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
| 243 | 243 | $key = $secret ?? $this->secret; |
| 244 | 244 | $parts = explode(".", $jwt); |
| 245 | - $header = json_decode(base64url_decode($parts[0]) ,true); |
|
| 245 | + $header = json_decode(base64url_decode($parts[0]), true); |
|
| 246 | 246 | if ($header == null) return false; |
| 247 | 247 | $alg = $this->algorithm ?? $header["alg"] ?? self::HS256; |
| 248 | - $payload = json_decode(base64url_decode($parts[1]) ,true); |
|
| 248 | + $payload = json_decode(base64url_decode($parts[1]), true); |
|
| 249 | 249 | if ($payload == null) return false; |
| 250 | 250 | if ($parts[2] == "") { |
| 251 | 251 | return $this->allow_unsigned; |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * @param string $jwt [description] |
| 286 | 286 | * @return bool [description] |
| 287 | 287 | */ |
| 288 | - public function expired(string $jwt=null):bool { |
|
| 288 | + public function expired(string $jwt = null):bool { |
|
| 289 | 289 | $exp = $jwt == null ? ($this->payload["exp"] ?? time() + 4) : $this->get_expired($jwt); |
| 290 | 290 | return time() >= $exp; |
| 291 | 291 | } |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | private function get_expired(string $jwt):int |
| 311 | 311 | { |
| 312 | 312 | $parts = explode(".", $jwt); |
| 313 | - return json_decode(base64url_decode($parts[1]) ,true)["exp"] ?? time() + 4; |
|
| 313 | + return json_decode(base64url_decode($parts[1]), true)["exp"] ?? time() + 4; |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | /** |
@@ -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; |