Passed
Push — master ( 89ee1b...86c30b )
by Francis
01:21
created
helpers/base64_helper.php 1 patch
Braces   +3 added lines, -1 removed lines patch added patch discarded remove patch
@@ -11,7 +11,9 @@
 block discarded – undo
11 11
    */
12 12
   function base64url_encode(string $data) {
13 13
     $b64 = base64_encode($data);
14
-    if ($b64 === false) return false;
14
+    if ($b64 === false) {
15
+     return false;
16
+    }
15 17
     $url = strtr($b64, '+/', '-_');
16 18
     return rtrim($url, '=');
17 19
   }
Please login to merge, or discard this patch.
libraries/JWT.php 1 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.