@@ -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 | } |
@@ -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; |