@@ -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 | * |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | private $set_iat = true; |
| 43 | 43 | |
| 44 | - function __construct($params=null) { |
|
| 44 | + function __construct($params = null) { |
|
| 45 | 45 | if ($params != null) $this->init($params); |
| 46 | 46 | get_instance()->load->splint("francis94c/ci-jwt", "%base64"); |
| 47 | 47 | } |
@@ -96,9 +96,9 @@ discard block |
||
| 96 | 96 | * @param [type] $secret [description] |
| 97 | 97 | * @return [type] [description] |
| 98 | 98 | */ |
| 99 | - public function sign(string $secret=null):?string { |
|
| 99 | + public function sign(string $secret = null): ?string { |
|
| 100 | 100 | // Checks. |
| 101 | - if (count($this->payload) == 0) return null; |
|
| 101 | + if (count($this->payload) == 0) return null; |
|
| 102 | 102 | // $key is $secret. |
| 103 | 103 | $key = $secret ?? $this->secret; |
| 104 | 104 | $this->header["alg"] = $this->header["alg"] ?? self::HS256; |
@@ -115,9 +115,9 @@ discard block |
||
| 115 | 115 | * [token description] |
| 116 | 116 | * @return string [description] |
| 117 | 117 | */ |
| 118 | - public function token():?string { |
|
| 118 | + public function token(): ?string { |
|
| 119 | 119 | // Checks. |
| 120 | - if (count($this->payload) == 0) return null; |
|
| 120 | + if (count($this->payload) == 0) return null; |
|
| 121 | 121 | // Begin. |
| 122 | 122 | $this->header["alg"] = self::NONE; |
| 123 | 123 | if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time(); |
@@ -129,14 +129,14 @@ discard block |
||
| 129 | 129 | * @param string $secret [description] |
| 130 | 130 | * @return bool [description] |
| 131 | 131 | */ |
| 132 | - public function verify(string $jwt, string $secret=null):bool { |
|
| 132 | + public function verify(string $jwt, string $secret = null):bool { |
|
| 133 | 133 | if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
| 134 | 134 | $key = $secret ?? $this->secret; |
| 135 | 135 | $parts = explode(".", $jwt); |
| 136 | - $header = json_decode(base64url_decode($parts[0]) ,true); |
|
| 136 | + $header = json_decode(base64url_decode($parts[0]), true); |
|
| 137 | 137 | if ($header == null) return false; |
| 138 | 138 | $alg = $header["alg"] ?? self::HS256; |
| 139 | - $payload = json_decode(base64url_decode($parts[1]) ,true); |
|
| 139 | + $payload = json_decode(base64url_decode($parts[1]), true); |
|
| 140 | 140 | if ($payload == null) return false; |
| 141 | 141 | if ($parts[2] == "") { |
| 142 | 142 | return $this->allow_unsigned; |
@@ -42,7 +42,9 @@ discard block |
||
| 42 | 42 | private $set_iat = true; |
| 43 | 43 | |
| 44 | 44 | function __construct($params=null) { |
| 45 | - if ($params != null) $this->init($params); |
|
| 45 | + if ($params != null) { |
|
| 46 | + $this->init($params); |
|
| 47 | + } |
|
| 46 | 48 | get_instance()->load->splint("francis94c/ci-jwt", "%base64"); |
| 47 | 49 | } |
| 48 | 50 | /** |
@@ -98,17 +100,25 @@ discard block |
||
| 98 | 100 | */ |
| 99 | 101 | public function sign(string $secret=null):?string { |
| 100 | 102 | // Checks. |
| 101 | - if (count($this->payload) == 0) return null; |
|
| 103 | + if (count($this->payload) == 0) { |
|
| 104 | + return null; |
|
| 105 | + } |
|
| 102 | 106 | // $key is $secret. |
| 103 | 107 | $key = $secret ?? $this->secret; |
| 104 | 108 | $this->header["alg"] = $this->header["alg"] ?? self::HS256; |
| 105 | 109 | $this->header["typ"] = $this->header["typ"] ?? self::JWT; |
| 106 | 110 | $jwt = base64url_encode(json_encode($this->header)); |
| 107 | - if ($jwt === false) return null; |
|
| 108 | - if ($jwt != "") $jwt .= "."; |
|
| 111 | + if ($jwt === false) { |
|
| 112 | + return null; |
|
| 113 | + } |
|
| 114 | + if ($jwt != "") { |
|
| 115 | + $jwt .= "."; |
|
| 116 | + } |
|
| 109 | 117 | $payload = base64url_encode(json_encode($this->payload)); |
| 110 | 118 | $jwt .= $payload; |
| 111 | - if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]); |
|
| 119 | + if ($key != "") { |
|
| 120 | + return $this->sign_token($jwt, $key, $this->header["alg"]); |
|
| 121 | + } |
|
| 112 | 122 | return $jwt . "."; |
| 113 | 123 | } |
| 114 | 124 | /** |
@@ -117,10 +127,14 @@ discard block |
||
| 117 | 127 | */ |
| 118 | 128 | public function token():?string { |
| 119 | 129 | // Checks. |
| 120 | - if (count($this->payload) == 0) return null; |
|
| 130 | + if (count($this->payload) == 0) { |
|
| 131 | + return null; |
|
| 132 | + } |
|
| 121 | 133 | // Begin. |
| 122 | 134 | $this->header["alg"] = self::NONE; |
| 123 | - if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time(); |
|
| 135 | + if ($this->set_iat) { |
|
| 136 | + $this->payload["iat"] = $this->payload["iat"] ?? time(); |
|
| 137 | + } |
|
| 124 | 138 | return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . "."; |
| 125 | 139 | } |
| 126 | 140 | /** |
@@ -130,14 +144,21 @@ discard block |
||
| 130 | 144 | * @return bool [description] |
| 131 | 145 | */ |
| 132 | 146 | public function verify(string $jwt, string $secret=null):bool { |
| 133 | - if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
|
| 147 | + if (substr_count($jwt, ".") != 2) { |
|
| 148 | + return false; |
|
| 149 | + } |
|
| 150 | + // Invalid JWT. |
|
| 134 | 151 | $key = $secret ?? $this->secret; |
| 135 | 152 | $parts = explode(".", $jwt); |
| 136 | 153 | $header = json_decode(base64url_decode($parts[0]) ,true); |
| 137 | - if ($header == null) return false; |
|
| 154 | + if ($header == null) { |
|
| 155 | + return false; |
|
| 156 | + } |
|
| 138 | 157 | $alg = $header["alg"] ?? self::HS256; |
| 139 | 158 | $payload = json_decode(base64url_decode($parts[1]) ,true); |
| 140 | - if ($payload == null) return false; |
|
| 159 | + if ($payload == null) { |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 141 | 162 | if ($parts[2] == "") { |
| 142 | 163 | return $this->allow_unsigned; |
| 143 | 164 | } |
@@ -151,9 +172,13 @@ discard block |
||
| 151 | 172 | public function decode(string $jwt):bool { |
| 152 | 173 | $parts = explode(".", $jwt); |
| 153 | 174 | $header = json_decode(base64url_decode($parts[0]), true); |
| 154 | - if ($header === false) return false; |
|
| 175 | + if ($header === false) { |
|
| 176 | + return false; |
|
| 177 | + } |
|
| 155 | 178 | $payload = json_decode(base64url_decode($parts[1]), true); |
| 156 | - if ($payload === false) return false; |
|
| 179 | + if ($payload === false) { |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 157 | 182 | $this->header = $header; |
| 158 | 183 | $this->payload = $payload; |
| 159 | 184 | return true; |
@@ -177,7 +202,9 @@ discard block |
||
| 177 | 202 | * @return string Complete JWT. |
| 178 | 203 | */ |
| 179 | 204 | private function sign_token(string $token, string $key, string $alg):string { |
| 180 | - if ($alg == self::NONE) return $token . "."; |
|
| 205 | + if ($alg == self::NONE) { |
|
| 206 | + return $token . "."; |
|
| 207 | + } |
|
| 181 | 208 | $token = rtrim($token, "."); |
| 182 | 209 | $signature = hash_hmac(self::ALGOS[$alg], $token, $key); |
| 183 | 210 | return $token . "." . $signature; |