@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | */ |
42 | 42 | private $set_iat = true; |
43 | 43 | |
44 | - function __construct($params=null) { |
|
44 | + function __construct($params = null) { |
|
45 | 45 | if ($params != null) $this->init($params); |
46 | 46 | get_instance()->load->splint("francis94c/ci-jwt", "%base64"); |
47 | 47 | } |
@@ -96,9 +96,9 @@ discard block |
||
96 | 96 | * @param [type] $secret [description] |
97 | 97 | * @return [type] [description] |
98 | 98 | */ |
99 | - public function sign(string $secret=null):?string { |
|
99 | + public function sign(string $secret = null): ?string { |
|
100 | 100 | // Checks. |
101 | - if (count($this->payload) == 0) return null; |
|
101 | + if (count($this->payload) == 0) return null; |
|
102 | 102 | // $key is $secret. |
103 | 103 | $key = $secret ?? $this->secret; |
104 | 104 | $this->header["alg"] = $this->header["alg"] ?? self::HS256; |
@@ -115,9 +115,9 @@ discard block |
||
115 | 115 | * [token description] |
116 | 116 | * @return string [description] |
117 | 117 | */ |
118 | - public function token():?string { |
|
118 | + public function token(): ?string { |
|
119 | 119 | // Checks. |
120 | - if (count($this->payload) == 0) return null; |
|
120 | + if (count($this->payload) == 0) return null; |
|
121 | 121 | // Begin. |
122 | 122 | $this->header["alg"] = self::NONE; |
123 | 123 | if ($this->set_iat) $this->payload["iat"] = $this->payload["iat"] ?? time(); |
@@ -129,14 +129,14 @@ discard block |
||
129 | 129 | * @param string $secret [description] |
130 | 130 | * @return bool [description] |
131 | 131 | */ |
132 | - public function verify(string $jwt, string $secret=null):bool { |
|
132 | + public function verify(string $jwt, string $secret = null):bool { |
|
133 | 133 | if (substr_count($jwt, ".") != 2) return false; // Invalid JWT. |
134 | 134 | $key = $secret ?? $this->secret; |
135 | 135 | $parts = explode(".", $jwt); |
136 | - $header = json_decode(base64url_decode($parts[0]) ,true); |
|
136 | + $header = json_decode(base64url_decode($parts[0]), true); |
|
137 | 137 | if ($header == null) return false; |
138 | 138 | $alg = $header["alg"] ?? self::HS256; |
139 | - $payload = json_decode(base64url_decode($parts[1]) ,true); |
|
139 | + $payload = json_decode(base64url_decode($parts[1]), true); |
|
140 | 140 | if ($payload == null) return false; |
141 | 141 | if ($parts[2] == "") { |
142 | 142 | return $this->allow_unsigned; |
@@ -42,7 +42,9 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 | } |
@@ -162,7 +183,9 @@ discard block |
||
162 | 183 | * @return string Complete JWT. |
163 | 184 | */ |
164 | 185 | private function sign_token(string $token, string $key, string $alg):string { |
165 | - if ($alg == self::NONE) return $token . "."; |
|
186 | + if ($alg == self::NONE) { |
|
187 | + return $token . "."; |
|
188 | + } |
|
166 | 189 | $token = rtrim($token, "."); |
167 | 190 | $signature = hash_hmac(self::ALGOS[$alg], $token, $key); |
168 | 191 | return $token . "." . $signature; |