diff --git a/src/JWT.php b/src/JWT.php index efa533c..6a252a1 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -143,18 +143,23 @@ public function encode(array $payload, array $header = []): string * Decode JWT token and return original payload. * * @param string $token + * @param bool $verify * * @throws JWTException * * @return array */ - public function decode(string $token): array + public function decode(string $token, bool $verify = true): array { if (\substr_count($token, '.') < 2) { throw new JWTException('Invalid token: Incomplete segments', static::ERROR_TOKEN_INVALID); } $token = \explode('.', $token, 3); + if (!$verify) { + return (array) $this->urlSafeDecode($token[1]); + } + $this->validateHeader((array) $this->urlSafeDecode($token[0])); // Validate signature. diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 9c15f28..e4a6b7f 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -116,6 +116,7 @@ public function test_kid() $token = $jwt->encode($payload = ['a' => 1, 'exp' => time() + 1000], ['kid' => 'key2']); $this->assertSame($payload, $jwt->decode($token)); + $this->assertSame($payload, $jwt->decode($token, false)); return $jwt; }