Passed
Push — master ( 07000a...cffd12 )
by Francis
01:16
created
libraries/JWT.php 1 patch
Braces   +40 added lines, -13 removed lines patch added patch discarded remove patch
@@ -42,7 +42,9 @@  discard block
 block discarded – undo
42 42
   private $set_iat = true;
43 43
 
44 44
   function __construct($params=null) {
45
-    if ($params != null) $this->init($params);
45
+    if ($params != null) {
46
+     $this->init($params);
47
+    }
46 48
     get_instance()->load->splint("francis94c/ci-jwt", "%base64");
47 49
   }
48 50
   /**
@@ -98,17 +100,25 @@  discard block
 block discarded – undo
98 100
    */
99 101
   public function sign(string $secret=null):?string {
100 102
     // Checks.
101
-    if  (count($this->payload) == 0) return null;
103
+    if  (count($this->payload) == 0) {
104
+     return null;
105
+    }
102 106
     // $key is $secret.
103 107
     $key = $secret ?? $this->secret;
104 108
     $this->header["alg"] = $this->header["alg"] ?? self::HS256;
105 109
     $this->header["typ"] = $this->header["typ"] ?? self::JWT;
106 110
     $jwt = base64url_encode(json_encode($this->header));
107
-    if ($jwt === false) return null;
108
-    if ($jwt != "") $jwt .= ".";
111
+    if ($jwt === false) {
112
+     return null;
113
+    }
114
+    if ($jwt != "") {
115
+     $jwt .= ".";
116
+    }
109 117
     $payload = base64url_encode(json_encode($this->payload));
110 118
     $jwt .= $payload;
111
-    if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]);
119
+    if ($key != "") {
120
+     return $this->sign_token($jwt, $key, $this->header["alg"]);
121
+    }
112 122
     return $jwt . ".";
113 123
   }
114 124
   /**
@@ -117,10 +127,14 @@  discard block
 block discarded – undo
117 127
    */
118 128
   public function token():?string {
119 129
     // Checks.
120
-    if  (count($this->payload) == 0) return null;
130
+    if  (count($this->payload) == 0) {
131
+     return null;
132
+    }
121 133
     // Begin.
122 134
     $this->header["alg"] = self::NONE;
123
-    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
+    }
124 138
     return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . ".";
125 139
   }
126 140
   /**
@@ -130,14 +144,21 @@  discard block
 block discarded – undo
130 144
    * @return bool           [description]
131 145
    */
132 146
   public function verify(string $jwt, string $secret=null):bool {
133
-    if (substr_count($jwt, ".") != 2) return false; // Invalid JWT.
147
+    if (substr_count($jwt, ".") != 2) {
148
+     return false;
149
+    }
150
+    // Invalid JWT.
134 151
     $key = $secret ?? $this->secret;
135 152
     $parts = explode(".", $jwt);
136 153
     $header = json_decode(base64url_decode($parts[0]) ,true);
137
-    if ($header == null) return false;
154
+    if ($header == null) {
155
+     return false;
156
+    }
138 157
     $alg = $header["alg"] ?? self::HS256;
139 158
     $payload = json_decode(base64url_decode($parts[1]) ,true);
140
-    if ($payload == null) return false;
159
+    if ($payload == null) {
160
+     return false;
161
+    }
141 162
     if ($parts[2] == "") {
142 163
       return $this->allow_unsigned;
143 164
     }
@@ -151,9 +172,13 @@  discard block
 block discarded – undo
151 172
   public function decode(string $jwt):bool {
152 173
     $parts = explode(".", $jwt);
153 174
     $header = json_decode(base64url_decode($parts[0]), true);
154
-    if ($header === false) return false;
175
+    if ($header === false) {
176
+     return false;
177
+    }
155 178
     $payload = json_decode(base64url_decode($parts[1]), true);
156
-    if ($payload === false) return false;
179
+    if ($payload === false) {
180
+     return false;
181
+    }
157 182
     $this->header = $header;
158 183
     $this->payload = $payload;
159 184
     return true;
@@ -177,7 +202,9 @@  discard block
 block discarded – undo
177 202
    * @return string        Complete JWT.
178 203
    */
179 204
   private function sign_token(string $token, string $key, string $alg):string {
180
-    if ($alg == self::NONE) return $token . ".";
205
+    if ($alg == self::NONE) {
206
+     return $token . ".";
207
+    }
181 208
     $token = rtrim($token, ".");
182 209
     $signature = hash_hmac(self::ALGOS[$alg], $token, $key);
183 210
     return $token . "." . $signature;
Please login to merge, or discard this patch.