Completed
Push — master ( 5ae312...cdf4e7 )
by Francis
01:36 queued 10s
created
libraries/JWT.php 2 patches
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
    */
56 56
   private $algorithm;
57 57
 
58
-  function __construct($params=null) {
58
+  function __construct($params = null) {
59 59
     if ($params != null) $this->init($params);
60 60
     get_instance()->load->splint("francis94c/ci-jwt", "%base64");
61 61
   }
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
    * @param  string     $key   Key of the item. e.g "alg", "typ".
76 76
    * @param  string|int $value Value of the item.
77 77
    */
78
-  public function header(string $key, $value=null) {
78
+  public function header(string $key, $value = null) {
79 79
     if ($value === null) return $this->header[$key];
80 80
     $this->header[$key] = $value;
81 81
   }
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
    * @return mixed                  Value of $key if $value == null, else
94 94
    *                                returns null.
95 95
    */
96
-  public function payload(string $key, $value=null):?mixed {
96
+  public function payload(string $key, $value = null): ?mixed {
97 97
     if ($value === null) return $this->payload[$key];
98 98
     $this->payload[$key] = $value;
99 99
   }
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
    * @return mixed      Value of the 'iss' claim, if the $iss argument wasn't
104 104
    *                    supplied. Otherwise, null.
105 105
    */
106
-  public function iss(string $iss=null):?mixed {
106
+  public function iss(string $iss = null): ?mixed {
107 107
     if ($iss === null) return $this->payload['iss'];
108 108
     $this->payload['iss'] = $iss;
109 109
   }
@@ -127,9 +127,9 @@  discard block
 block discarded – undo
127 127
    * @param  [type] $secret [description]
128 128
    * @return [type]         [description]
129 129
    */
130
-  public function sign(string $secret=null):?string {
130
+  public function sign(string $secret = null): ?string {
131 131
     // Checks.
132
-    if  (count($this->payload) == 0) return null;
132
+    if (count($this->payload) == 0) return null;
133 133
     // $key is $secret.
134 134
     $key = $secret ?? $this->secret;
135 135
     $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512);
@@ -150,9 +150,9 @@  discard block
 block discarded – undo
150 150
    * [token description]
151 151
    * @return string [description]
152 152
    */
153
-  public function token():?string {
153
+  public function token(): ?string {
154 154
     // Checks.
155
-    if  (count($this->payload) == 0) return null;
155
+    if (count($this->payload) == 0) return null;
156 156
     // Begin.
157 157
     $this->header["alg"] = self::NONE;
158 158
     if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
@@ -165,14 +165,14 @@  discard block
 block discarded – undo
165 165
    * @param  string $secret [description]
166 166
    * @return bool           [description]
167 167
    */
168
-  public function verify(string $jwt, string $secret=null):bool {
168
+  public function verify(string $jwt, string $secret = null):bool {
169 169
     if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
170 170
     $key = $secret ?? $this->secret;
171 171
     $parts = explode(".", $jwt);
172
-    $header = json_decode(base64url_decode($parts[0]) ,true);
172
+    $header = json_decode(base64url_decode($parts[0]), true);
173 173
     if ($header == null) return false;
174 174
     $alg = $header["alg"] ?? self::HS256;
175
-    $payload = json_decode(base64url_decode($parts[1]) ,true);
175
+    $payload = json_decode(base64url_decode($parts[1]), true);
176 176
     if ($payload == null) return false;
177 177
     if ($parts[2] == "") {
178 178
       return $this->allow_unsigned;
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
    * @param  string $jwt [description]
210 210
    * @return bool        [description]
211 211
    */
212
-  public function expired(string $jwt=null):bool {
212
+  public function expired(string $jwt = null):bool {
213 213
     $exp = $jwt == null ? ($this->payload["exp"] ?? time()) : $this->get_expired($jwt);
214 214
     return time() >= $exp;
215 215
   }
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
    */
232 232
   private function get_expired(string $jwt):int {
233 233
     $parts = explode(".", $jwt);
234
-    return json_decode(base64url_decode($parts[1]) ,true)["exp"] ?? time();
234
+    return json_decode(base64url_decode($parts[1]), true)["exp"] ?? time();
235 235
   }
236 236
   /**
237 237
    * [sign_token Sign JWT]
Please login to merge, or discard this patch.
Braces   +58 added lines, -19 removed lines patch added patch discarded remove patch
@@ -56,7 +56,9 @@  discard block
 block discarded – undo
56 56
   private $algorithm;
57 57
 
58 58
   function __construct($params=null) {
59
-    if ($params != null) $this->init($params);
59
+    if ($params != null) {
60
+     $this->init($params);
61
+    }
60 62
     get_instance()->load->splint("francis94c/ci-jwt", "%base64");
61 63
   }
62 64
   /**
@@ -76,7 +78,9 @@  discard block
 block discarded – undo
76 78
    * @param  string|int $value Value of the item.
77 79
    */
78 80
   public function header(string $key, $value=null) {
79
-    if ($value === null) return $this->header[$key];
81
+    if ($value === null) {
82
+     return $this->header[$key];
83
+    }
80 84
     $this->header[$key] = $value;
81 85
   }
82 86
   /**
@@ -94,7 +98,9 @@  discard block
 block discarded – undo
94 98
    *                                returns null.
95 99
    */
96 100
   public function payload(string $key, $value=null):?mixed {
97
-    if ($value === null) return $this->payload[$key];
101
+    if ($value === null) {
102
+     return $this->payload[$key];
103
+    }
98 104
     $this->payload[$key] = $value;
99 105
   }
100 106
   /**
@@ -104,7 +110,9 @@  discard block
 block discarded – undo
104 110
    *                    supplied. Otherwise, null.
105 111
    */
106 112
   public function iss(string $iss=null):?mixed {
107
-    if ($iss === null) return $this->payload['iss'];
113
+    if ($iss === null) {
114
+     return $this->payload['iss'];
115
+    }
108 116
     $this->payload['iss'] = $iss;
109 117
   }
110 118
   /**
@@ -129,21 +137,33 @@  discard block
 block discarded – undo
129 137
    */
130 138
   public function sign(string $secret=null):?string {
131 139
     // Checks.
132
-    if  (count($this->payload) == 0) return null;
140
+    if  (count($this->payload) == 0) {
141
+     return null;
142
+    }
133 143
     // $key is $secret.
134 144
     $key = $secret ?? $this->secret;
135 145
     $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512);
136 146
     $this->header["typ"] = $this->header["typ"] ?? self::JWT;
137 147
     // Generate Issued At Time.
138
-    if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
148
+    if ($this->set_iat) {
149
+     $this->payload["iat"] = $this->payload["iat"] ?? time();
150
+    }
139 151
     // Auto Expire.
140
-    if ($this->auto_expire != null && !isset($this->payload["exp"])) $this->payload["exp"] = strtotime($this->auto_expire);
152
+    if ($this->auto_expire != null && !isset($this->payload["exp"])) {
153
+     $this->payload["exp"] = strtotime($this->auto_expire);
154
+    }
141 155
     $jwt = base64url_encode(json_encode($this->header));
142
-    if ($jwt === false) return null;
143
-    if ($jwt != "") $jwt .= ".";
156
+    if ($jwt === false) {
157
+     return null;
158
+    }
159
+    if ($jwt != "") {
160
+     $jwt .= ".";
161
+    }
144 162
     $payload = base64url_encode(json_encode($this->payload));
145 163
     $jwt .= $payload;
146
-    if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]);
164
+    if ($key != "") {
165
+     return $this->sign_token($jwt, $key, $this->header["alg"]);
166
+    }
147 167
     return $jwt . ".";
148 168
   }
149 169
   /**
@@ -152,11 +172,17 @@  discard block
 block discarded – undo
152 172
    */
153 173
   public function token():?string {
154 174
     // Checks.
155
-    if  (count($this->payload) == 0) return null;
175
+    if  (count($this->payload) == 0) {
176
+     return null;
177
+    }
156 178
     // Begin.
157 179
     $this->header["alg"] = self::NONE;
158
-    if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
159
-    if ($this->auto_expire != null) $this->payload["exp"] = strtotime($this->auto_expire);
180
+    if ($this->set_iat) {
181
+     $this->payload["iat"] = $this->payload["iat"] ?? time();
182
+    }
183
+    if ($this->auto_expire != null) {
184
+     $this->payload["exp"] = strtotime($this->auto_expire);
185
+    }
160 186
     return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . ".";
161 187
   }
162 188
   /**
@@ -166,14 +192,21 @@  discard block
 block discarded – undo
166 192
    * @return bool           [description]
167 193
    */
168 194
   public function verify(string $jwt, string $secret=null):bool {
169
-    if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
195
+    if (substr_count($jwt, ".") != 2) {
196
+     return false;
197
+    }
198
+    // Invalid JWT.
170 199
     $key = $secret ?? $this->secret;
171 200
     $parts = explode(".", $jwt);
172 201
     $header = json_decode(base64url_decode($parts[0]) ,true);
173
-    if ($header == null) return false;
202
+    if ($header == null) {
203
+     return false;
204
+    }
174 205
     $alg = $header["alg"] ?? self::HS256;
175 206
     $payload = json_decode(base64url_decode($parts[1]) ,true);
176
-    if ($payload == null) return false;
207
+    if ($payload == null) {
208
+     return false;
209
+    }
177 210
     if ($parts[2] == "") {
178 211
       return $this->allow_unsigned;
179 212
     }
@@ -197,9 +230,13 @@  discard block
 block discarded – undo
197 230
   public function decode(string $jwt):bool {
198 231
     $parts = explode(".", $jwt);
199 232
     $header = json_decode(base64url_decode($parts[0]), true);
200
-    if ($header === false) return false;
233
+    if ($header === false) {
234
+     return false;
235
+    }
201 236
     $payload = json_decode(base64url_decode($parts[1]), true);
202
-    if ($payload === false) return false;
237
+    if ($payload === false) {
238
+     return false;
239
+    }
203 240
     $this->header = $header;
204 241
     $this->payload = $payload;
205 242
     return true;
@@ -241,7 +278,9 @@  discard block
 block discarded – undo
241 278
    * @return string        Complete JWT.
242 279
    */
243 280
   private function sign_token(string $token, string $key, string $alg):string {
244
-    if ($alg == self::NONE) return $token . ".";
281
+    if ($alg == self::NONE) {
282
+     return $token . ".";
283
+    }
245 284
     $token = rtrim($token, ".");
246 285
     $signature = hash_hmac(self::ALGOS[$alg], $token, $key);
247 286
     return $token . "." . $signature;
Please login to merge, or discard this patch.