@@ -27,7 +27,7 @@ |
||
27 | 27 | * |
28 | 28 | * @return string|bool Decoded String. |
29 | 29 | */ |
30 | - function base64url_decode(string $data, bool $strict=false) { |
|
30 | + function base64url_decode(string $data, bool $strict = false) { |
|
31 | 31 | $b64 = strtr($data, '-_', '+/'); |
32 | 32 | return base64_decode($b64, $strict); |
33 | 33 | } |
@@ -11,7 +11,9 @@ |
||
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 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | */ |
37 | 37 | private $allow_unsigned = false; |
38 | 38 | |
39 | - function __construct($params=null) { |
|
39 | + function __construct($params = null) { |
|
40 | 40 | if ($params != null) $this->init($params); |
41 | 41 | get_instance()->load->splint("francis94c/ci-jwt", "%base64"); |
42 | 42 | } |
@@ -93,9 +93,9 @@ discard block |
||
93 | 93 | * @param [type] $secret [description] |
94 | 94 | * @return [type] [description] |
95 | 95 | */ |
96 | - public function sign(string $secret=null):?string { |
|
96 | + public function sign(string $secret = null): ?string { |
|
97 | 97 | // Checks. |
98 | - if (count($this->payload) == 0) return null; |
|
98 | + if (count($this->payload) == 0) return null; |
|
99 | 99 | // $key is $secret. |
100 | 100 | $key = $secret ?? $this->secret; |
101 | 101 | $this->header["alg"] = $this->header["alg"] ?? self::HS256; |
@@ -112,9 +112,9 @@ discard block |
||
112 | 112 | * [token description] |
113 | 113 | * @return string [description] |
114 | 114 | */ |
115 | - public function token():?string { |
|
115 | + public function token(): ?string { |
|
116 | 116 | // Checks. |
117 | - if (count($this->payload) == 0) return null; |
|
117 | + if (count($this->payload) == 0) return null; |
|
118 | 118 | // Begin. |
119 | 119 | $this->header["alg"] = self::NONE; |
120 | 120 | return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . "."; |
@@ -125,14 +125,14 @@ discard block |
||
125 | 125 | * @param string $secret [description] |
126 | 126 | * @return bool [description] |
127 | 127 | */ |
128 | - public function verify(string $jwt, string $secret=null):bool { |
|
128 | + public function verify(string $jwt, string $secret = null):bool { |
|
129 | 129 | if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
130 | 130 | $key = $secret ?? $this->secret; |
131 | 131 | $parts = explode(".", $jwt); |
132 | - $header = json_decode(base64url_decode($parts[0]) ,true); |
|
132 | + $header = json_decode(base64url_decode($parts[0]), true); |
|
133 | 133 | if ($header == null) return false; |
134 | 134 | $alg = $header["alg"] ?? self::HS256; |
135 | - $payload = json_decode(base64url_decode($parts[1]) ,true); |
|
135 | + $payload = json_decode(base64url_decode($parts[1]), true); |
|
136 | 136 | if ($payload == null) return false; |
137 | 137 | if ($parts[2] == "") { |
138 | 138 | return $this->allow_unsigned; |
@@ -37,7 +37,9 @@ discard block |
||
37 | 37 | private $allow_unsigned = false; |
38 | 38 | |
39 | 39 | function __construct($params=null) { |
40 | - if ($params != null) $this->init($params); |
|
40 | + if ($params != null) { |
|
41 | + $this->init($params); |
|
42 | + } |
|
41 | 43 | get_instance()->load->splint("francis94c/ci-jwt", "%base64"); |
42 | 44 | } |
43 | 45 | /** |
@@ -95,17 +97,25 @@ discard block |
||
95 | 97 | */ |
96 | 98 | public function sign(string $secret=null):?string { |
97 | 99 | // Checks. |
98 | - if (count($this->payload) == 0) return null; |
|
100 | + if (count($this->payload) == 0) { |
|
101 | + return null; |
|
102 | + } |
|
99 | 103 | // $key is $secret. |
100 | 104 | $key = $secret ?? $this->secret; |
101 | 105 | $this->header["alg"] = $this->header["alg"] ?? self::HS256; |
102 | 106 | $this->header["typ"] = $this->header["typ"] ?? self::JWT; |
103 | 107 | $jwt = base64url_encode(json_encode($this->header)); |
104 | - if ($jwt === false) return null; |
|
105 | - if ($jwt != "") $jwt .= "."; |
|
108 | + if ($jwt === false) { |
|
109 | + return null; |
|
110 | + } |
|
111 | + if ($jwt != "") { |
|
112 | + $jwt .= "."; |
|
113 | + } |
|
106 | 114 | $payload = base64url_encode(json_encode($this->payload)); |
107 | 115 | $jwt .= $payload; |
108 | - if ($key != "") return $this->sign_token($jwt, $key, $this->header["alg"]); |
|
116 | + if ($key != "") { |
|
117 | + return $this->sign_token($jwt, $key, $this->header["alg"]); |
|
118 | + } |
|
109 | 119 | return $jwt . "."; |
110 | 120 | } |
111 | 121 | /** |
@@ -114,7 +124,9 @@ discard block |
||
114 | 124 | */ |
115 | 125 | public function token():?string { |
116 | 126 | // Checks. |
117 | - if (count($this->payload) == 0) return null; |
|
127 | + if (count($this->payload) == 0) { |
|
128 | + return null; |
|
129 | + } |
|
118 | 130 | // Begin. |
119 | 131 | $this->header["alg"] = self::NONE; |
120 | 132 | return base64url_encode(json_encode($this->header)) . "." . base64url_encode(json_encode($this->payload)) . "."; |
@@ -126,14 +138,21 @@ discard block |
||
126 | 138 | * @return bool [description] |
127 | 139 | */ |
128 | 140 | public function verify(string $jwt, string $secret=null):bool { |
129 | - if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
|
141 | + if (substr_count($jwt, ".") != 2) { |
|
142 | + return false; |
|
143 | + } |
|
144 | + // Invalid JWT. |
|
130 | 145 | $key = $secret ?? $this->secret; |
131 | 146 | $parts = explode(".", $jwt); |
132 | 147 | $header = json_decode(base64url_decode($parts[0]) ,true); |
133 | - if ($header == null) return false; |
|
148 | + if ($header == null) { |
|
149 | + return false; |
|
150 | + } |
|
134 | 151 | $alg = $header["alg"] ?? self::HS256; |
135 | 152 | $payload = json_decode(base64url_decode($parts[1]) ,true); |
136 | - if ($payload == null) return false; |
|
153 | + if ($payload == null) { |
|
154 | + return false; |
|
155 | + } |
|
137 | 156 | if ($parts[2] == "") { |
138 | 157 | return $this->allow_unsigned; |
139 | 158 | } |
@@ -162,7 +181,9 @@ discard block |
||
162 | 181 | * @return string Complete JWT. |
163 | 182 | */ |
164 | 183 | private function sign_token(string $token, string $key, string $alg):string { |
165 | - if ($alg == self::NONE) return $token . "."; |
|
184 | + if ($alg == self::NONE) { |
|
185 | + return $token . "."; |
|
186 | + } |
|
166 | 187 | $token = rtrim($token, "."); |
167 | 188 | $signature = hash_hmac(self::ALGOS[$alg], $token, $key); |
168 | 189 | return $token . "." . $signature; |
@@ -20,7 +20,7 @@ |
||
20 | 20 | * @covers JWT::__construct |
21 | 21 | */ |
22 | 22 | public static function setUpBeforeClass(): void { |
23 | - self::$ci =& get_instance(); |
|
23 | + self::$ci = & get_instance(); |
|
24 | 24 | /** |
25 | 25 | * [$params Config Items.] |
26 | 26 | * |