Passed
Push — master ( 2d0f29...158660 )
by Francis
06:26
created
libraries/JWT.php 2 patches
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
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");
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
    * @param  string     $key   Key of the item. e.g "alg", "typ".
105 105
    * @param  string|int $value Value of the item.
106 106
    */
107
-  public function header(string $key, $value=null)
107
+  public function header(string $key, $value = null)
108 108
   {
109 109
     if ($value === null) return $this->header[$key];
110 110
     $this->header[$key] = $value;
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
    * @return mixed|none         Value of $key if $value == null, else
128 128
    *                            returns none[NOTHING].
129 129
    */
130
-  public function payload(string $key, $value=null)
130
+  public function payload(string $key, $value = null)
131 131
   {
132 132
     if ($value === null) return $this->payload[$key];
133 133
     $this->payload[$key] = $value;
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
    * @return mixed|none      Value of the 'iss' claim, if the $iss argument wasn't
155 155
    *                         supplied. Otherwise, null.
156 156
    */
157
-  public function iss(string $iss=null)
157
+  public function iss(string $iss = null)
158 158
   {
159 159
     if ($iss === null) return $this->payload['iss'];
160 160
     $this->payload['iss'] = $iss;
@@ -186,9 +186,9 @@  discard block
 block discarded – undo
186 186
    * @param  [type] $secret [description]
187 187
    * @return [type]         [description]
188 188
    */
189
-  public function sign(string $secret=null):?string {
189
+  public function sign(string $secret = null): ?string {
190 190
     // Checks.
191
-    if  (count($this->payload) == 0) return null;
191
+    if (count($this->payload) == 0) return null;
192 192
     // $key is $secret.
193 193
     $key = $secret ?? $this->secret;
194 194
     $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512);
@@ -210,10 +210,10 @@  discard block
 block discarded – undo
210 210
    * [token description]
211 211
    * @return string [description]
212 212
    */
213
-  public function token():?string
213
+  public function token(): ?string
214 214
   {
215 215
     // Checks.
216
-    if  (count($this->payload) == 0) return null;
216
+    if (count($this->payload) == 0) return null;
217 217
     // Begin.
218 218
     $this->header["alg"] = self::NONE;
219 219
     if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
@@ -227,14 +227,14 @@  discard block
 block discarded – undo
227 227
    * @param  string $secret [description]
228 228
    * @return bool           [description]
229 229
    */
230
-  public function verify(string $jwt, string $secret=null):bool {
230
+  public function verify(string $jwt, string $secret = null):bool {
231 231
     if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
232 232
     $key = $secret ?? $this->secret;
233 233
     $parts = explode(".", $jwt);
234
-    $header = json_decode(base64url_decode($parts[0]) ,true);
234
+    $header = json_decode(base64url_decode($parts[0]), true);
235 235
     if ($header == null) return false;
236 236
     $alg = $this->algorithm ?? $header["alg"] ?? self::HS256;
237
-    $payload = json_decode(base64url_decode($parts[1]) ,true);
237
+    $payload = json_decode(base64url_decode($parts[1]), true);
238 238
     if ($payload == null) return false;
239 239
     if ($parts[2] == "") {
240 240
       return $this->allow_unsigned;
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
    * @param  string $jwt [description]
275 275
    * @return bool        [description]
276 276
    */
277
-  public function expired(string $jwt=null):bool {
277
+  public function expired(string $jwt = null):bool {
278 278
     $exp = $jwt == null ? ($this->payload["exp"] ?? time() + 4) : $this->get_expired($jwt);
279 279
     return time() >= $exp;
280 280
   }
@@ -299,7 +299,7 @@  discard block
 block discarded – undo
299 299
   private function get_expired(string $jwt):int
300 300
   {
301 301
     $parts = explode(".", $jwt);
302
-    return json_decode(base64url_decode($parts[1]) ,true)["exp"] ?? time() + 4;
302
+    return json_decode(base64url_decode($parts[1]), true)["exp"] ?? time() + 4;
303 303
   }
304 304
 
305 305
   /**
Please login to merge, or discard this patch.
Braces   +61 added lines, -20 removed lines patch added patch discarded remove patch
@@ -71,7 +71,9 @@  discard block
 block discarded – undo
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
 
@@ -106,7 +108,9 @@  discard block
 block discarded – undo
106 108
    */
107 109
   public function header(string $key, $value=null)
108 110
   {
109
-    if ($value === null) return $this->header[$key];
111
+    if ($value === null) {
112
+     return $this->header[$key];
113
+    }
110 114
     $this->header[$key] = $value;
111 115
     return $this;
112 116
   }
@@ -129,7 +133,9 @@  discard block
 block discarded – undo
129 133
    */
130 134
   public function payload(string $key, $value=null)
131 135
   {
132
-    if ($value === null) return $this->payload[$key];
136
+    if ($value === null) {
137
+     return $this->payload[$key];
138
+    }
133 139
     $this->payload[$key] = $value;
134 140
     return $this;
135 141
   }
@@ -143,7 +149,9 @@  discard block
 block discarded – undo
143 149
    */
144 150
   public function __call(string $method, array $args)
145 151
   {
146
-    if (count($args) == 0) return $this->payload[$method];
152
+    if (count($args) == 0) {
153
+     return $this->payload[$method];
154
+    }
147 155
     $this->payload[$method] = $args[0];
148 156
     return $this;
149 157
   }
@@ -156,7 +164,9 @@  discard block
 block discarded – undo
156 164
    */
157 165
   public function iss(string $iss=null)
158 166
   {
159
-    if ($iss === null) return $this->payload['iss'];
167
+    if ($iss === null) {
168
+     return $this->payload['iss'];
169
+    }
160 170
     $this->payload['iss'] = $iss;
161 171
     return $this;
162 172
   }
@@ -188,21 +198,33 @@  discard block
 block discarded – undo
188 198
    */
189 199
   public function sign(string $secret=null):?string {
190 200
     // Checks.
191
-    if  (count($this->payload) == 0) return null;
201
+    if  (count($this->payload) == 0) {
202
+     return null;
203
+    }
192 204
     // $key is $secret.
193 205
     $key = $secret ?? $this->secret;
194 206
     $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512);
195 207
     $this->header["typ"] = $this->header["typ"] ?? self::JWT;
196 208
     // Generate Issued At Time.
197
-    if ($this->set_iat) $this->payload["iat"] = $this->payload['iat'] ?? time();
209
+    if ($this->set_iat) {
210
+     $this->payload["iat"] = $this->payload['iat'] ?? time();
211
+    }
198 212
     // Auto Expire.
199
-    if ($this->auto_expire != null && !isset($this->payload['exp'])) $this->payload['exp'] = strtotime($this->auto_expire);
213
+    if ($this->auto_expire != null && !isset($this->payload['exp'])) {
214
+     $this->payload['exp'] = strtotime($this->auto_expire);
215
+    }
200 216
     $jwt = base64url_encode(json_encode($this->header));
201
-    if ($jwt === false) return null;
202
-    if ($jwt != "") $jwt .= ".";
217
+    if ($jwt === false) {
218
+     return null;
219
+    }
220
+    if ($jwt != "") {
221
+     $jwt .= ".";
222
+    }
203 223
     $payload = base64url_encode(json_encode($this->payload));
204 224
     $jwt .= $payload;
205
-    if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]);
225
+    if ($key != "") {
226
+     return $this->sign_token($jwt, $key, $this->header["alg"]);
227
+    }
206 228
     return $jwt . ".";
207 229
   }
208 230
 
@@ -213,11 +235,17 @@  discard block
 block discarded – undo
213 235
   public function token():?string
214 236
   {
215 237
     // Checks.
216
-    if  (count($this->payload) == 0) return null;
238
+    if  (count($this->payload) == 0) {
239
+     return null;
240
+    }
217 241
     // Begin.
218 242
     $this->header["alg"] = self::NONE;
219
-    if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
220
-    if ($this->auto_expire != null) $this->payload["exp"] = strtotime($this->auto_expire);
243
+    if ($this->set_iat) {
244
+     $this->payload["iat"] = $this->payload["iat"] ?? time();
245
+    }
246
+    if ($this->auto_expire != null) {
247
+     $this->payload["exp"] = strtotime($this->auto_expire);
248
+    }
221 249
     return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . ".";
222 250
   }
223 251
 
@@ -228,14 +256,21 @@  discard block
 block discarded – undo
228 256
    * @return bool           [description]
229 257
    */
230 258
   public function verify(string $jwt, string $secret=null):bool {
231
-    if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
259
+    if (substr_count($jwt, ".") != 2) {
260
+     return false;
261
+    }
262
+    // Invalid JWT.
232 263
     $key = $secret ?? $this->secret;
233 264
     $parts = explode(".", $jwt);
234 265
     $header = json_decode(base64url_decode($parts[0]) ,true);
235
-    if ($header == null) return false;
266
+    if ($header == null) {
267
+     return false;
268
+    }
236 269
     $alg = $this->algorithm ?? $header["alg"] ?? self::HS256;
237 270
     $payload = json_decode(base64url_decode($parts[1]) ,true);
238
-    if ($payload == null) return false;
271
+    if ($payload == null) {
272
+     return false;
273
+    }
239 274
     if ($parts[2] == "") {
240 275
       return $this->allow_unsigned;
241 276
     }
@@ -262,9 +297,13 @@  discard block
 block discarded – undo
262 297
   public function decode(string $jwt):bool {
263 298
     $parts = explode(".", $jwt);
264 299
     $header = json_decode(base64url_decode($parts[0]), true);
265
-    if ($header === false) return false;
300
+    if ($header === false) {
301
+     return false;
302
+    }
266 303
     $payload = json_decode(base64url_decode($parts[1]), true);
267
-    if ($payload === false) return false;
304
+    if ($payload === false) {
305
+     return false;
306
+    }
268 307
     $this->header = $header;
269 308
     $this->payload = $payload;
270 309
     return true;
@@ -311,7 +350,9 @@  discard block
 block discarded – undo
311 350
    */
312 351
   private function sign_token(string $token, string $key, string $alg):string
313 352
   {
314
-    if ($alg == self::NONE) return $token . ".";
353
+    if ($alg == self::NONE) {
354
+     return $token . ".";
355
+    }
315 356
     $token = rtrim($token, ".");
316 357
     $signature = hash_hmac(self::ALGOS[$alg], $token, $key);
317 358
     return $token . "." . $signature;
Please login to merge, or discard this patch.