Passed
Push — master ( 5543a5...653c6a )
by Francis
01:13
created
libraries/JWT.php 2 patches
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
    */
49 49
   private $auto_expire;
50 50
 
51
-  function __construct($params=null) {
51
+  function __construct($params = null) {
52 52
     if ($params != null) $this->init($params);
53 53
     get_instance()->load->splint("francis94c/ci-jwt", "%base64");
54 54
   }
@@ -104,9 +104,9 @@  discard block
 block discarded – undo
104 104
    * @param  [type] $secret [description]
105 105
    * @return [type]         [description]
106 106
    */
107
-  public function sign(string $secret=null):?string {
107
+  public function sign(string $secret = null): ?string {
108 108
     // Checks.
109
-    if  (count($this->payload) == 0) return null;
109
+    if (count($this->payload) == 0) return null;
110 110
     // $key is $secret.
111 111
     $key = $secret ?? $this->secret;
112 112
     $this->header["alg"] = $this->header["alg"] ?? self::HS256;
@@ -127,9 +127,9 @@  discard block
 block discarded – undo
127 127
    * [token description]
128 128
    * @return string [description]
129 129
    */
130
-  public function token():?string {
130
+  public function token(): ?string {
131 131
     // Checks.
132
-    if  (count($this->payload) == 0) return null;
132
+    if (count($this->payload) == 0) return null;
133 133
     // Begin.
134 134
     $this->header["alg"] = self::NONE;
135 135
     if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
@@ -142,14 +142,14 @@  discard block
 block discarded – undo
142 142
    * @param  string $secret [description]
143 143
    * @return bool           [description]
144 144
    */
145
-  public function verify(string $jwt, string $secret=null):bool {
145
+  public function verify(string $jwt, string $secret = null):bool {
146 146
     if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
147 147
     $key = $secret ?? $this->secret;
148 148
     $parts = explode(".", $jwt);
149
-    $header = json_decode(base64url_decode($parts[0]) ,true);
149
+    $header = json_decode(base64url_decode($parts[0]), true);
150 150
     if ($header == null) return false;
151 151
     $alg = $header["alg"] ?? self::HS256;
152
-    $payload = json_decode(base64url_decode($parts[1]) ,true);
152
+    $payload = json_decode(base64url_decode($parts[1]), true);
153 153
     if ($payload == null) return false;
154 154
     if ($parts[2] == "") {
155 155
       return $this->allow_unsigned;
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
    * @param  string $jwt [description]
187 187
    * @return bool        [description]
188 188
    */
189
-  public function expired(string $jwt=null):bool {
189
+  public function expired(string $jwt = null):bool {
190 190
     $exp = $jwt == null ? ($this->payload["exp"] ?? time()) : $this->get_expired($jwt);
191 191
     return time() >= $exp;
192 192
   }
@@ -208,7 +208,7 @@  discard block
 block discarded – undo
208 208
    */
209 209
   private function get_expired(string $jwt):int {
210 210
     $parts = explode(".", $jwt);
211
-    return json_decode(base64url_decode($parts[1]) ,true)["exp"] ?? time();
211
+    return json_decode(base64url_decode($parts[1]), true)["exp"] ?? time();
212 212
   }
213 213
   /**
214 214
    * [sign_token Sign JWT]
Please login to merge, or discard this patch.
Braces   +49 added lines, -16 removed lines patch added patch discarded remove patch
@@ -49,7 +49,9 @@  discard block
 block discarded – undo
49 49
   private $auto_expire;
50 50
 
51 51
   function __construct($params=null) {
52
-    if ($params != null) $this->init($params);
52
+    if ($params != null) {
53
+     $this->init($params);
54
+    }
53 55
     get_instance()->load->splint("francis94c/ci-jwt", "%base64");
54 56
   }
55 57
   /**
@@ -106,21 +108,33 @@  discard block
 block discarded – undo
106 108
    */
107 109
   public function sign(string $secret=null):?string {
108 110
     // Checks.
109
-    if  (count($this->payload) == 0) return null;
111
+    if  (count($this->payload) == 0) {
112
+     return null;
113
+    }
110 114
     // $key is $secret.
111 115
     $key = $secret ?? $this->secret;
112 116
     $this->header["alg"] = $this->header["alg"] ?? self::HS256;
113 117
     $this->header["typ"] = $this->header["typ"] ?? self::JWT;
114 118
     // Generate Issued At Time.
115
-    if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
119
+    if ($this->set_iat) {
120
+     $this->payload["iat"] = $this->payload["iat"] ?? time();
121
+    }
116 122
     // Auto Expire.
117
-    if ($this->auto_expire != null) $this->payload["exp"] = strtotime($this->auto_expire);
123
+    if ($this->auto_expire != null) {
124
+     $this->payload["exp"] = strtotime($this->auto_expire);
125
+    }
118 126
     $jwt = base64url_encode(json_encode($this->header));
119
-    if ($jwt === false) return null;
120
-    if ($jwt != "") $jwt .= ".";
127
+    if ($jwt === false) {
128
+     return null;
129
+    }
130
+    if ($jwt != "") {
131
+     $jwt .= ".";
132
+    }
121 133
     $payload = base64url_encode(json_encode($this->payload));
122 134
     $jwt .= $payload;
123
-    if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]);
135
+    if ($key != "") {
136
+     return $this->sign_token($jwt, $key, $this->header["alg"]);
137
+    }
124 138
     return $jwt . ".";
125 139
   }
126 140
   /**
@@ -129,11 +143,17 @@  discard block
 block discarded – undo
129 143
    */
130 144
   public function token():?string {
131 145
     // Checks.
132
-    if  (count($this->payload) == 0) return null;
146
+    if  (count($this->payload) == 0) {
147
+     return null;
148
+    }
133 149
     // Begin.
134 150
     $this->header["alg"] = self::NONE;
135
-    if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time();
136
-    if ($this->auto_expire != null) $this->payload["exp"] = strtotime($this->auto_expire);
151
+    if ($this->set_iat) {
152
+     $this->payload["iat"] = $this->payload["iat"] ?? time();
153
+    }
154
+    if ($this->auto_expire != null) {
155
+     $this->payload["exp"] = strtotime($this->auto_expire);
156
+    }
137 157
     return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . ".";
138 158
   }
139 159
   /**
@@ -143,14 +163,21 @@  discard block
 block discarded – undo
143 163
    * @return bool           [description]
144 164
    */
145 165
   public function verify(string $jwt, string $secret=null):bool {
146
-    if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
166
+    if (substr_count($jwt, ".") != 2) {
167
+     return false;
168
+    }
169
+    // Invalid JWT.
147 170
     $key = $secret ?? $this->secret;
148 171
     $parts = explode(".", $jwt);
149 172
     $header = json_decode(base64url_decode($parts[0]) ,true);
150
-    if ($header == null) return false;
173
+    if ($header == null) {
174
+     return false;
175
+    }
151 176
     $alg = $header["alg"] ?? self::HS256;
152 177
     $payload = json_decode(base64url_decode($parts[1]) ,true);
153
-    if ($payload == null) return false;
178
+    if ($payload == null) {
179
+     return false;
180
+    }
154 181
     if ($parts[2] == "") {
155 182
       return $this->allow_unsigned;
156 183
     }
@@ -174,9 +201,13 @@  discard block
 block discarded – undo
174 201
   public function decode(string $jwt):bool {
175 202
     $parts = explode(".", $jwt);
176 203
     $header = json_decode(base64url_decode($parts[0]), true);
177
-    if ($header === false) return false;
204
+    if ($header === false) {
205
+     return false;
206
+    }
178 207
     $payload = json_decode(base64url_decode($parts[1]), true);
179
-    if ($payload === false) return false;
208
+    if ($payload === false) {
209
+     return false;
210
+    }
180 211
     $this->header = $header;
181 212
     $this->payload = $payload;
182 213
     return true;
@@ -218,7 +249,9 @@  discard block
 block discarded – undo
218 249
    * @return string        Complete JWT.
219 250
    */
220 251
   private function sign_token(string $token, string $key, string $alg):string {
221
-    if ($alg == self::NONE) return $token . ".";
252
+    if ($alg == self::NONE) {
253
+     return $token . ".";
254
+    }
222 255
     $token = rtrim($token, ".");
223 256
     $signature = hash_hmac(self::ALGOS[$alg], $token, $key);
224 257
     return $token . "." . $signature;
Please login to merge, or discard this patch.