@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * @date 2020-03-28 |
70 | 70 | * @param [type] $params [description] |
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"); |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * @param string $key Key of the item. e.g "alg", "typ". |
91 | 91 | * @param string|int $value Value of the item. |
92 | 92 | */ |
93 | - public function header(string $key, $value=null) { |
|
93 | + public function header(string $key, $value = null) { |
|
94 | 94 | if ($value === null) return $this->header[$key]; |
95 | 95 | $this->header[$key] = $value; |
96 | 96 | } |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | * @return mixed|none Value of $key if $value == null, else |
109 | 109 | * returns none[NOTHING]. |
110 | 110 | */ |
111 | - public function payload(string $key, $value=null) { |
|
111 | + public function payload(string $key, $value = null) { |
|
112 | 112 | if ($value === null) return $this->payload[$key]; |
113 | 113 | $this->payload[$key] = $value; |
114 | 114 | } |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | * @return mixed|none Value of the 'iss' claim, if the $iss argument wasn't |
119 | 119 | * supplied. Otherwise, null. |
120 | 120 | */ |
121 | - public function iss(string $iss=null) { |
|
121 | + public function iss(string $iss = null) { |
|
122 | 122 | if ($iss === null) return $this->payload['iss']; |
123 | 123 | $this->payload['iss'] = $iss; |
124 | 124 | } |
@@ -142,9 +142,9 @@ discard block |
||
142 | 142 | * @param [type] $secret [description] |
143 | 143 | * @return [type] [description] |
144 | 144 | */ |
145 | - public function sign(string $secret=null):?string { |
|
145 | + public function sign(string $secret = null): ?string { |
|
146 | 146 | // Checks. |
147 | - if (count($this->payload) == 0) return null; |
|
147 | + if (count($this->payload) == 0) return null; |
|
148 | 148 | // $key is $secret. |
149 | 149 | $key = $secret ?? $this->secret; |
150 | 150 | $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512); |
@@ -165,9 +165,9 @@ discard block |
||
165 | 165 | * [token description] |
166 | 166 | * @return string [description] |
167 | 167 | */ |
168 | - public function token():?string { |
|
168 | + public function token(): ?string { |
|
169 | 169 | // Checks. |
170 | - if (count($this->payload) == 0) return null; |
|
170 | + if (count($this->payload) == 0) return null; |
|
171 | 171 | // Begin. |
172 | 172 | $this->header["alg"] = self::NONE; |
173 | 173 | if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time(); |
@@ -181,14 +181,14 @@ discard block |
||
181 | 181 | * @param string $secret [description] |
182 | 182 | * @return bool [description] |
183 | 183 | */ |
184 | - public function verify(string $jwt, string $secret=null):bool { |
|
184 | + public function verify(string $jwt, string $secret = null):bool { |
|
185 | 185 | if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
186 | 186 | $key = $secret ?? $this->secret; |
187 | 187 | $parts = explode(".", $jwt); |
188 | - $header = json_decode(base64url_decode($parts[0]) ,true); |
|
188 | + $header = json_decode(base64url_decode($parts[0]), true); |
|
189 | 189 | if ($header == null) return false; |
190 | 190 | $alg = $this->algorithm ?? $header["alg"] ?? self::HS256; |
191 | - $payload = json_decode(base64url_decode($parts[1]) ,true); |
|
191 | + $payload = json_decode(base64url_decode($parts[1]), true); |
|
192 | 192 | if ($payload == null) return false; |
193 | 193 | if ($parts[2] == "") { |
194 | 194 | return $this->allow_unsigned; |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | * @param string $jwt [description] |
226 | 226 | * @return bool [description] |
227 | 227 | */ |
228 | - public function expired(string $jwt=null):bool { |
|
228 | + public function expired(string $jwt = null):bool { |
|
229 | 229 | $exp = $jwt == null ? ($this->payload["exp"] ?? time()) : $this->get_expired($jwt); |
230 | 230 | return time() >= $exp; |
231 | 231 | } |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | */ |
248 | 248 | private function get_expired(string $jwt):int { |
249 | 249 | $parts = explode(".", $jwt); |
250 | - return json_decode(base64url_decode($parts[1]) ,true)["exp"] ?? time(); |
|
250 | + return json_decode(base64url_decode($parts[1]), true)["exp"] ?? time(); |
|
251 | 251 | } |
252 | 252 | /** |
253 | 253 | * [sign_token Sign JWT] |
@@ -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 | /** |
@@ -91,7 +93,9 @@ discard block |
||
91 | 93 | * @param string|int $value Value of the item. |
92 | 94 | */ |
93 | 95 | public function header(string $key, $value=null) { |
94 | - if ($value === null) return $this->header[$key]; |
|
96 | + if ($value === null) { |
|
97 | + return $this->header[$key]; |
|
98 | + } |
|
95 | 99 | $this->header[$key] = $value; |
96 | 100 | } |
97 | 101 | /** |
@@ -109,7 +113,9 @@ discard block |
||
109 | 113 | * returns none[NOTHING]. |
110 | 114 | */ |
111 | 115 | public function payload(string $key, $value=null) { |
112 | - if ($value === null) return $this->payload[$key]; |
|
116 | + if ($value === null) { |
|
117 | + return $this->payload[$key]; |
|
118 | + } |
|
113 | 119 | $this->payload[$key] = $value; |
114 | 120 | } |
115 | 121 | /** |
@@ -119,7 +125,9 @@ discard block |
||
119 | 125 | * supplied. Otherwise, null. |
120 | 126 | */ |
121 | 127 | public function iss(string $iss=null) { |
122 | - if ($iss === null) return $this->payload['iss']; |
|
128 | + if ($iss === null) { |
|
129 | + return $this->payload['iss']; |
|
130 | + } |
|
123 | 131 | $this->payload['iss'] = $iss; |
124 | 132 | } |
125 | 133 | /** |
@@ -144,21 +152,33 @@ discard block |
||
144 | 152 | */ |
145 | 153 | public function sign(string $secret=null):?string { |
146 | 154 | // Checks. |
147 | - if (count($this->payload) == 0) return null; |
|
155 | + if (count($this->payload) == 0) { |
|
156 | + return null; |
|
157 | + } |
|
148 | 158 | // $key is $secret. |
149 | 159 | $key = $secret ?? $this->secret; |
150 | 160 | $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512); |
151 | 161 | $this->header["typ"] = $this->header["typ"] ?? self::JWT; |
152 | 162 | // Generate Issued At Time. |
153 | - if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time(); |
|
163 | + if ($this->set_iat) { |
|
164 | + $this->payload["iat"] = $this->payload["iat"] ?? time(); |
|
165 | + } |
|
154 | 166 | // Auto Expire. |
155 | - if ($this->auto_expire != null && !isset($this->payload["exp"])) $this->payload["exp"] = strtotime($this->auto_expire); |
|
167 | + if ($this->auto_expire != null && !isset($this->payload["exp"])) { |
|
168 | + $this->payload["exp"] = strtotime($this->auto_expire); |
|
169 | + } |
|
156 | 170 | $jwt = base64url_encode(json_encode($this->header)); |
157 | - if ($jwt === false) return null; |
|
158 | - if ($jwt != "") $jwt .= "."; |
|
171 | + if ($jwt === false) { |
|
172 | + return null; |
|
173 | + } |
|
174 | + if ($jwt != "") { |
|
175 | + $jwt .= "."; |
|
176 | + } |
|
159 | 177 | $payload = base64url_encode(json_encode($this->payload)); |
160 | 178 | $jwt .= $payload; |
161 | - if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]); |
|
179 | + if ($key != "") { |
|
180 | + return $this->sign_token($jwt, $key, $this->header["alg"]); |
|
181 | + } |
|
162 | 182 | return $jwt . "."; |
163 | 183 | } |
164 | 184 | /** |
@@ -167,11 +187,17 @@ discard block |
||
167 | 187 | */ |
168 | 188 | public function token():?string { |
169 | 189 | // Checks. |
170 | - if (count($this->payload) == 0) return null; |
|
190 | + if (count($this->payload) == 0) { |
|
191 | + return null; |
|
192 | + } |
|
171 | 193 | // Begin. |
172 | 194 | $this->header["alg"] = self::NONE; |
173 | - if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time(); |
|
174 | - if ($this->auto_expire != null) $this->payload["exp"] = strtotime($this->auto_expire); |
|
195 | + if ($this->set_iat) { |
|
196 | + $this->payload["iat"] = $this->payload["iat"] ?? time(); |
|
197 | + } |
|
198 | + if ($this->auto_expire != null) { |
|
199 | + $this->payload["exp"] = strtotime($this->auto_expire); |
|
200 | + } |
|
175 | 201 | return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . "."; |
176 | 202 | } |
177 | 203 | |
@@ -182,14 +208,21 @@ discard block |
||
182 | 208 | * @return bool [description] |
183 | 209 | */ |
184 | 210 | public function verify(string $jwt, string $secret=null):bool { |
185 | - if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
|
211 | + if (substr_count($jwt, ".") != 2) { |
|
212 | + return false; |
|
213 | + } |
|
214 | + // Invalid JWT. |
|
186 | 215 | $key = $secret ?? $this->secret; |
187 | 216 | $parts = explode(".", $jwt); |
188 | 217 | $header = json_decode(base64url_decode($parts[0]) ,true); |
189 | - if ($header == null) return false; |
|
218 | + if ($header == null) { |
|
219 | + return false; |
|
220 | + } |
|
190 | 221 | $alg = $this->algorithm ?? $header["alg"] ?? self::HS256; |
191 | 222 | $payload = json_decode(base64url_decode($parts[1]) ,true); |
192 | - if ($payload == null) return false; |
|
223 | + if ($payload == null) { |
|
224 | + return false; |
|
225 | + } |
|
193 | 226 | if ($parts[2] == "") { |
194 | 227 | return $this->allow_unsigned; |
195 | 228 | } |
@@ -213,9 +246,13 @@ discard block |
||
213 | 246 | public function decode(string $jwt):bool { |
214 | 247 | $parts = explode(".", $jwt); |
215 | 248 | $header = json_decode(base64url_decode($parts[0]), true); |
216 | - if ($header === false) return false; |
|
249 | + if ($header === false) { |
|
250 | + return false; |
|
251 | + } |
|
217 | 252 | $payload = json_decode(base64url_decode($parts[1]), true); |
218 | - if ($payload === false) return false; |
|
253 | + if ($payload === false) { |
|
254 | + return false; |
|
255 | + } |
|
219 | 256 | $this->header = $header; |
220 | 257 | $this->payload = $payload; |
221 | 258 | return true; |
@@ -257,7 +294,9 @@ discard block |
||
257 | 294 | * @return string Complete JWT. |
258 | 295 | */ |
259 | 296 | private function sign_token(string $token, string $key, string $alg):string { |
260 | - if ($alg == self::NONE) return $token . "."; |
|
297 | + if ($alg == self::NONE) { |
|
298 | + return $token . "."; |
|
299 | + } |
|
261 | 300 | $token = rtrim($token, "."); |
262 | 301 | $signature = hash_hmac(self::ALGOS[$alg], $token, $key); |
263 | 302 | return $token . "." . $signature; |