diff --git a/src/Parser.php b/src/Parser.php index 91775ab1..03be11e7 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -130,7 +130,7 @@ protected function splitJwt($jwt) */ protected function parseHeader($data) { - $header = $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data)); + $header = (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data)); if (isset($header['enc'])) { throw new InvalidArgumentException('Encryption is not supported yet'); @@ -148,7 +148,7 @@ protected function parseHeader($data) */ protected function parseClaims($data) { - $claims = $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data)); + $claims = (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data)); foreach ($claims as $name => &$value) { $value = $this->claimFactory->create($name, $value); diff --git a/src/Parsing/Decoder.php b/src/Parsing/Decoder.php index 854ac9ee..86336b08 100644 --- a/src/Parsing/Decoder.php +++ b/src/Parsing/Decoder.php @@ -30,16 +30,12 @@ class Decoder */ public function jsonDecode($json) { - $data = json_decode($json, true); + $data = json_decode($json); if (json_last_error() != JSON_ERROR_NONE) { throw new RuntimeException('Error while decoding to JSON: ' . json_last_error_msg()); } - if (!is_array($data)) { - throw new RuntimeException('The decoded data must be an array'); - } - return $data; } diff --git a/test/FunctionalTests/EcdsaTokenTest.php b/test/FunctionalTests/EcdsaTokenTest.php index 0e1eae50..739b9155 100644 --- a/test/FunctionalTests/EcdsaTokenTest.php +++ b/test/FunctionalTests/EcdsaTokenTest.php @@ -52,7 +52,7 @@ public function createSigner() */ public function builderCanGenerateAToken() { - $user = ['name' => 'testing', 'email' => 'testing@abc.com']; + $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com']; $token = (new Builder())->setId(1) ->setAudience('http://client.abc.com') @@ -88,6 +88,7 @@ public function parserCanReadAToken(Token $generated) $read = (new Parser())->parse((string) $generated); $this->assertEquals($generated, $read); + $this->assertEquals('testing', $read->getClaim('user')->name); } /** diff --git a/test/FunctionalTests/HmacTokenTest.php b/test/FunctionalTests/HmacTokenTest.php index 2c59f0e3..81af4d3f 100644 --- a/test/FunctionalTests/HmacTokenTest.php +++ b/test/FunctionalTests/HmacTokenTest.php @@ -48,7 +48,7 @@ public function createSigner() */ public function builderCanGenerateAToken() { - $user = ['name' => 'testing', 'email' => 'testing@abc.com']; + $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com']; $token = (new Builder())->setId(1) ->setAudience('http://client.abc.com') @@ -84,6 +84,7 @@ public function parserCanReadAToken(Token $generated) $read = (new Parser())->parse((string) $generated); $this->assertEquals($generated, $read); + $this->assertEquals('testing', $read->getClaim('user')->name); } /** diff --git a/test/FunctionalTests/RsaTokenTest.php b/test/FunctionalTests/RsaTokenTest.php index af8ff88f..b19ef53a 100644 --- a/test/FunctionalTests/RsaTokenTest.php +++ b/test/FunctionalTests/RsaTokenTest.php @@ -52,7 +52,7 @@ public function createSigner() */ public function builderCanGenerateAToken() { - $user = ['name' => 'testing', 'email' => 'testing@abc.com']; + $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com']; $token = (new Builder())->setId(1) ->setAudience('http://client.abc.com') @@ -88,6 +88,7 @@ public function parserCanReadAToken(Token $generated) $read = (new Parser())->parse((string) $generated); $this->assertEquals($generated, $read); + $this->assertEquals('testing', $read->getClaim('user')->name); } /** diff --git a/test/FunctionalTests/UnsignedTokenTest.php b/test/FunctionalTests/UnsignedTokenTest.php index dce34791..a6280b88 100644 --- a/test/FunctionalTests/UnsignedTokenTest.php +++ b/test/FunctionalTests/UnsignedTokenTest.php @@ -30,7 +30,7 @@ class UnsignedTokenTest extends \PHPUnit_Framework_TestCase */ public function builderCanGenerateAToken() { - $user = ['name' => 'testing', 'email' => 'testing@abc.com']; + $user = (object) ['name' => 'testing', 'email' => 'testing@abc.com']; $token = (new Builder())->setId(1) ->setAudience('http://client.abc.com') @@ -66,6 +66,7 @@ public function parserCanReadAToken(Token $generated) $read = (new Parser())->parse((string) $generated); $this->assertEquals($generated, $read); + $this->assertEquals('testing', $read->getClaim('user')->name); } /** diff --git a/test/Parsing/DecoderTest.php b/test/Parsing/DecoderTest.php index e737b24c..51d727bd 100644 --- a/test/Parsing/DecoderTest.php +++ b/test/Parsing/DecoderTest.php @@ -22,21 +22,10 @@ public function jsonDecodeMustReturnTheDecodedData() { $decoder = new Decoder(); - $this->assertEquals(['test' => 'test'], $decoder->jsonDecode('{"test":"test"}')); - } - - /** - * @test - * - * @covers Lcobucci\JWT\Parsing\Decoder::jsonDecode - * - * @expectedException \RuntimeException - */ - public function jsonDecodeMustRaiseExceptionWhenResultIsNotAnArray() - { - $decoder = new Decoder(); - - $decoder->jsonDecode('"test"'); + $this->assertEquals( + (object) ['test' => 'test'], + $decoder->jsonDecode('{"test":"test"}') + ); } /**