Completed
Push — master ( 96c8f4...34065d )
by Francis
01:17
created
libraries/JWT.php 2 patches
Spacing   +12 added lines, -12 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
   }
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
    * @param  string|int      $value JWT Claim Value.
93 93
    * @return string|int|null Value of $key if $value == null, else returns null.
94 94
    */
95
-  public function payload(string $key, $value=null) {
95
+  public function payload(string $key, $value = null) {
96 96
     if ($value === null) return $this->payload[$key];
97 97
     $this->payload[$key] = $value;
98 98
   }
@@ -116,9 +116,9 @@  discard block
 block discarded – undo
116 116
    * @param  [type] $secret [description]
117 117
    * @return [type]         [description]
118 118
    */
119
-  public function sign(string $secret=null):?string {
119
+  public function sign(string $secret = null): ?string {
120 120
     // Checks.
121
-    if  (count($this->payload) == 0) return null;
121
+    if (count($this->payload) == 0) return null;
122 122
     // $key is $secret.
123 123
     $key = $secret ?? $this->secret;
124 124
     $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512);
@@ -139,9 +139,9 @@  discard block
 block discarded – undo
139 139
    * [token description]
140 140
    * @return string [description]
141 141
    */
142
-  public function token():?string {
142
+  public function token(): ?string {
143 143
     // Checks.
144
-    if  (count($this->payload) == 0) return null;
144
+    if (count($this->payload) == 0) return null;
145 145
     // Begin.
146 146
     $this->header["alg"] = self::NONE;
147 147
     if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
@@ -154,14 +154,14 @@  discard block
 block discarded – undo
154 154
    * @param  string $secret [description]
155 155
    * @return bool           [description]
156 156
    */
157
-  public function verify(string $jwt, string $secret=null):bool {
157
+  public function verify(string $jwt, string $secret = null):bool {
158 158
     if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
159 159
     $key = $secret ?? $this->secret;
160 160
     $parts = explode(".", $jwt);
161
-    $header = json_decode(base64url_decode($parts[0]) ,true);
161
+    $header = json_decode(base64url_decode($parts[0]), true);
162 162
     if ($header == null) return false;
163 163
     $alg = $header["alg"] ?? self::HS256;
164
-    $payload = json_decode(base64url_decode($parts[1]) ,true);
164
+    $payload = json_decode(base64url_decode($parts[1]), true);
165 165
     if ($payload == null) return false;
166 166
     if ($parts[2] == "") {
167 167
       return $this->allow_unsigned;
@@ -198,7 +198,7 @@  discard block
 block discarded – undo
198 198
    * @param  string $jwt [description]
199 199
    * @return bool        [description]
200 200
    */
201
-  public function expired(string $jwt=null):bool {
201
+  public function expired(string $jwt = null):bool {
202 202
     $exp = $jwt == null ? ($this->payload["exp"] ?? time()) : $this->get_expired($jwt);
203 203
     return time() >= $exp;
204 204
   }
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
    */
221 221
   private function get_expired(string $jwt):int {
222 222
     $parts = explode(".", $jwt);
223
-    return json_decode(base64url_decode($parts[1]) ,true)["exp"] ?? time();
223
+    return json_decode(base64url_decode($parts[1]), true)["exp"] ?? time();
224 224
   }
225 225
   /**
226 226
    * [sign_token Sign JWT]
Please login to merge, or discard this patch.
Braces   +55 added lines, -18 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
   /**
@@ -93,7 +97,9 @@  discard block
 block discarded – undo
93 97
    * @return string|int|null Value of $key if $value == null, else returns null.
94 98
    */
95 99
   public function payload(string $key, $value=null) {
96
-    if ($value === null) return $this->payload[$key];
100
+    if ($value === null) {
101
+     return $this->payload[$key];
102
+    }
97 103
     $this->payload[$key] = $value;
98 104
   }
99 105
   /**
@@ -118,21 +124,33 @@  discard block
 block discarded – undo
118 124
    */
119 125
   public function sign(string $secret=null):?string {
120 126
     // Checks.
121
-    if  (count($this->payload) == 0) return null;
127
+    if  (count($this->payload) == 0) {
128
+     return null;
129
+    }
122 130
     // $key is $secret.
123 131
     $key = $secret ?? $this->secret;
124 132
     $this->header["alg"] = $this->header["alg"] ?? ($this->algorithm ?? self::HS512);
125 133
     $this->header["typ"] = $this->header["typ"] ?? self::JWT;
126 134
     // Generate Issued At Time.
127
-    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
+    }
128 138
     // Auto Expire.
129
-    if ($this->auto_expire != null && !isset($this->payload["exp"])) $this->payload["exp"] = strtotime($this->auto_expire);
139
+    if ($this->auto_expire != null && !isset($this->payload["exp"])) {
140
+     $this->payload["exp"] = strtotime($this->auto_expire);
141
+    }
130 142
     $jwt = base64url_encode(json_encode($this->header));
131
-    if ($jwt === false) return null;
132
-    if ($jwt != "") $jwt .= ".";
143
+    if ($jwt === false) {
144
+     return null;
145
+    }
146
+    if ($jwt != "") {
147
+     $jwt .= ".";
148
+    }
133 149
     $payload = base64url_encode(json_encode($this->payload));
134 150
     $jwt .= $payload;
135
-    if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]);
151
+    if ($key != "") {
152
+     return $this->sign_token($jwt, $key, $this->header["alg"]);
153
+    }
136 154
     return $jwt . ".";
137 155
   }
138 156
   /**
@@ -141,11 +159,17 @@  discard block
 block discarded – undo
141 159
    */
142 160
   public function token():?string {
143 161
     // Checks.
144
-    if  (count($this->payload) == 0) return null;
162
+    if  (count($this->payload) == 0) {
163
+     return null;
164
+    }
145 165
     // Begin.
146 166
     $this->header["alg"] = self::NONE;
147
-    if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
148
-    if ($this->auto_expire != null) $this->payload["exp"] = strtotime($this->auto_expire);
167
+    if ($this->set_iat) {
168
+     $this->payload["iat"] = $this->payload["iat"] ?? time();
169
+    }
170
+    if ($this->auto_expire != null) {
171
+     $this->payload["exp"] = strtotime($this->auto_expire);
172
+    }
149 173
     return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . ".";
150 174
   }
151 175
   /**
@@ -155,14 +179,21 @@  discard block
 block discarded – undo
155 179
    * @return bool           [description]
156 180
    */
157 181
   public function verify(string $jwt, string $secret=null):bool {
158
-    if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
182
+    if (substr_count($jwt, ".") != 2) {
183
+     return false;
184
+    }
185
+    // Invalid JWT.
159 186
     $key = $secret ?? $this->secret;
160 187
     $parts = explode(".", $jwt);
161 188
     $header = json_decode(base64url_decode($parts[0]) ,true);
162
-    if ($header == null) return false;
189
+    if ($header == null) {
190
+     return false;
191
+    }
163 192
     $alg = $header["alg"] ?? self::HS256;
164 193
     $payload = json_decode(base64url_decode($parts[1]) ,true);
165
-    if ($payload == null) return false;
194
+    if ($payload == null) {
195
+     return false;
196
+    }
166 197
     if ($parts[2] == "") {
167 198
       return $this->allow_unsigned;
168 199
     }
@@ -186,9 +217,13 @@  discard block
 block discarded – undo
186 217
   public function decode(string $jwt):bool {
187 218
     $parts = explode(".", $jwt);
188 219
     $header = json_decode(base64url_decode($parts[0]), true);
189
-    if ($header === false) return false;
220
+    if ($header === false) {
221
+     return false;
222
+    }
190 223
     $payload = json_decode(base64url_decode($parts[1]), true);
191
-    if ($payload === false) return false;
224
+    if ($payload === false) {
225
+     return false;
226
+    }
192 227
     $this->header = $header;
193 228
     $this->payload = $payload;
194 229
     return true;
@@ -230,7 +265,9 @@  discard block
 block discarded – undo
230 265
    * @return string        Complete JWT.
231 266
    */
232 267
   private function sign_token(string $token, string $key, string $alg):string {
233
-    if ($alg == self::NONE) return $token . ".";
268
+    if ($alg == self::NONE) {
269
+     return $token . ".";
270
+    }
234 271
     $token = rtrim($token, ".");
235 272
     $signature = hash_hmac(self::ALGOS[$alg], $token, $key);
236 273
     return $token . "." . $signature;
Please login to merge, or discard this patch.