@@ -56,7 +56,9 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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; |