Skip to content

Commit

Permalink
Decoding json into objects instead of arrays (closes #23).
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Apr 1, 2015
1 parent 6585b40 commit 56ff23d
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions src/Parsing/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion test/FunctionalTests/EcdsaTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function createSigner()
*/
public function builderCanGenerateAToken()
{
$user = ['name' => 'testing', 'email' => '[email protected]'];
$user = (object) ['name' => 'testing', 'email' => '[email protected]'];

$token = (new Builder())->setId(1)
->setAudience('http://client.abc.com')
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/FunctionalTests/HmacTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function createSigner()
*/
public function builderCanGenerateAToken()
{
$user = ['name' => 'testing', 'email' => '[email protected]'];
$user = (object) ['name' => 'testing', 'email' => '[email protected]'];

$token = (new Builder())->setId(1)
->setAudience('http://client.abc.com')
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/FunctionalTests/RsaTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function createSigner()
*/
public function builderCanGenerateAToken()
{
$user = ['name' => 'testing', 'email' => '[email protected]'];
$user = (object) ['name' => 'testing', 'email' => '[email protected]'];

$token = (new Builder())->setId(1)
->setAudience('http://client.abc.com')
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/FunctionalTests/UnsignedTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class UnsignedTokenTest extends \PHPUnit_Framework_TestCase
*/
public function builderCanGenerateAToken()
{
$user = ['name' => 'testing', 'email' => '[email protected]'];
$user = (object) ['name' => 'testing', 'email' => '[email protected]'];

$token = (new Builder())->setId(1)
->setAudience('http://client.abc.com')
Expand Down Expand Up @@ -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);
}

/**
Expand Down
19 changes: 4 additions & 15 deletions test/Parsing/DecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"}')
);
}

/**
Expand Down

0 comments on commit 56ff23d

Please sign in to comment.