Skip to content

Commit

Permalink
Merge pull request #26 from lcobucci/fix-token-parsing
Browse files Browse the repository at this point in the history
Delegating payload generation to Builder and Parser
  • Loading branch information
lcobucci committed Aug 3, 2015
2 parents f524169 + 299c453 commit 9874909
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 189 deletions.
12 changes: 9 additions & 3 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,15 @@ public function unsign()
*/
public function getToken()
{
$token = new Token($this->headers, $this->claims, $this->signature);
$token->setEncoder($this->encoder);
$payload = [
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->headers)),
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->claims))
];

return $token;
if ($this->signature !== null) {
$payload[] = $this->encoder->base64UrlEncode($this->signature);
}

return new Token($this->headers, $this->claims, $this->signature, $payload);
}
}
35 changes: 7 additions & 28 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use InvalidArgumentException;
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
use Lcobucci\JWT\Parsing\Decoder;
use Lcobucci\JWT\Parsing\Encoder;

/**
* This class parses the JWT strings and convert them into tokens
Expand All @@ -20,13 +19,6 @@
*/
class Parser
{
/**
* The data encoder
*
* @var Encoder
*/
private $encoder;

/**
* The data decoder
*
Expand All @@ -44,16 +36,13 @@ class Parser
/**
* Initializes the object
*
* @param Encoder $encoder
* @param Decoder $decoder
* @param ClaimFactory $claimFactory
*/
public function __construct(
Encoder $encoder = null,
Decoder $decoder = null,
ClaimFactory $claimFactory = null
) {
$this->encoder = $encoder ?: new Encoder();
$this->decoder = $decoder ?: new Decoder();
$this->claimFactory = $claimFactory ?: new ClaimFactory();
}
Expand All @@ -67,21 +56,7 @@ public function __construct(
*/
public function parse($jwt)
{
$token = $this->createToken($this->splitJwt($jwt));
$token->setEncoder($this->encoder);

return $token;
}

/**
* Creates the token from given data
*
* @param array $data
*
* @return Token
*/
private function createToken(array $data)
{
$data = $this->splitJwt($jwt);
$header = $this->parseHeader($data[0]);
$claims = $this->parseClaims($data[1]);
$signature = $this->parseSignature($header, $data[2]);
Expand All @@ -92,11 +67,15 @@ private function createToken(array $data)
}
}

return new Token($header, $claims, $signature);
if ($signature === null) {
unset($data[2]);
}

return new Token($header, $claims, $signature, $data);
}

/**
* Slipts the JWT string into an array
* Splits the JWT string into an array
*
* @param string $jwt
*
Expand Down
48 changes: 13 additions & 35 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use BadMethodCallException;
use Generator;
use Lcobucci\JWT\Parsing\Encoder;
use Lcobucci\JWT\Claim\Validatable;
use OutOfBoundsException;

Expand Down Expand Up @@ -43,37 +42,30 @@ class Token
private $signature;

/**
* The data encoder
* The encoded data
*
* @var Encoder
* @var array
*/
private $encoder;
private $payload;

/**
* Initializes the object
*
* @param array $headers
* @param array $claims
* @param array $payload
* @param Signature $signature
*/
public function __construct(
array $headers = ['alg' => 'none'],
array $claims = [],
Signature $signature = null
Signature $signature = null,
array $payload = ['', '']
) {
$this->headers = $headers;
$this->claims = $claims;
$this->signature = $signature;
}

/**
* Configures the data encoder
*
* @param Encoder $encoder
*/
public function setEncoder(Encoder $encoder)
{
$this->encoder = $encoder;
$this->payload = $payload;
}

/**
Expand Down Expand Up @@ -197,20 +189,10 @@ private function getValidatableClaims()
* Returns the token payload
*
* @return string
*
* @throws BadMethodCallException When $this->encoder is not configured
*/
public function getPayload()
{
if ($this->encoder === null) {
throw new BadMethodCallException('Encoder must be configured');
}

return sprintf(
'%s.%s',
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->headers)),
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->claims))
);
return $this->payload[0] . '.' . $this->payload[1];
}

/**
Expand All @@ -220,16 +202,12 @@ public function getPayload()
*/
public function __toString()
{
try {
$data = $this->getPayload() . '.';
$data = implode('.', $this->payload);

if ($this->signature) {
$data .= $this->encoder->base64UrlEncode($this->signature);
}

return $data;
} catch (BadMethodCallException $e) {
return '';
if ($this->signature === null) {
$data .= '.';
}

return $data;
}
}
4 changes: 0 additions & 4 deletions test/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ public function setMustKeepAFluentInterface()
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::setEncoder
* @uses Lcobucci\JWT\Token::getHeaders
* @uses Lcobucci\JWT\Token::getClaims
*
Expand All @@ -528,7 +527,6 @@ public function getTokenMustReturnANewTokenWithCurrentConfiguration()
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::getToken
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::setEncoder
* @uses Lcobucci\JWT\Token::getPayload
*
* @covers Lcobucci\JWT\Builder::sign
Expand All @@ -554,7 +552,6 @@ public function signMustChangeTheSignature()
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::getToken
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::setEncoder
* @uses Lcobucci\JWT\Token::getPayload
*
* @covers Lcobucci\JWT\Builder::sign
Expand Down Expand Up @@ -608,7 +605,6 @@ public function unsignMustKeepAFluentInterface(Builder $builder)
* @uses Lcobucci\JWT\Builder::sign
* @uses Lcobucci\JWT\Builder::getToken
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::setEncoder
* @uses Lcobucci\JWT\Token::getPayload
*
* @covers Lcobucci\JWT\Builder::set
Expand Down
1 change: 1 addition & 0 deletions test/FunctionalTests/UnsignedTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class UnsignedTokenTest extends \PHPUnit_Framework_TestCase
* @covers Lcobucci\JWT\Token
* @covers Lcobucci\JWT\Claim\Factory
* @covers Lcobucci\JWT\Claim\Basic
* @covers Lcobucci\JWT\Parsing\Encoder
*/
public function builderCanGenerateAToken()
{
Expand Down
25 changes: 1 addition & 24 deletions test/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Lcobucci\JWT\Claim\Factory as ClaimFactory;
use Lcobucci\JWT\Parsing\Decoder;
use Lcobucci\JWT\Parsing\Encoder;
use RuntimeException;

/**
Expand All @@ -18,11 +17,6 @@
*/
class ParserTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Encoder|\PHPUnit_Framework_MockObject_MockObject
*/
protected $encoder;

/**
* @var Decoder|\PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -43,7 +37,6 @@ class ParserTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->encoder = $this->getMock(Encoder::class);
$this->decoder = $this->getMock(Decoder::class);
$this->claimFactory = $this->getMock(ClaimFactory::class, [], [], '', false);
$this->defaultClaim = $this->getMock(Claim::class);
Expand All @@ -58,11 +51,7 @@ protected function setUp()
*/
private function createParser()
{
return new Parser(
$this->encoder,
$this->decoder,
$this->claimFactory
);
return new Parser($this->decoder, $this->claimFactory);
}

/**
Expand All @@ -74,7 +63,6 @@ public function constructMustConfigureTheAttributes()
{
$parser = $this->createParser();

$this->assertAttributeSame($this->encoder, 'encoder', $parser);
$this->assertAttributeSame($this->decoder, 'decoder', $parser);
$this->assertAttributeSame($this->claimFactory, 'claimFactory', $parser);
}
Expand Down Expand Up @@ -118,7 +106,6 @@ public function parseMustRaiseExceptionWhenJWSDontHaveThreeParts()
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::createToken
* @covers Lcobucci\JWT\Parser::parseHeader
*
* @expectedException RuntimeException
Expand All @@ -140,7 +127,6 @@ public function parseMustRaiseExceptionWhenHeaderCannotBeDecoded()
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::createToken
* @covers Lcobucci\JWT\Parser::parseHeader
*
* @expectedException InvalidArgumentException
Expand All @@ -160,11 +146,9 @@ public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken()
*
* @uses Lcobucci\JWT\Parser::__construct
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::setEncoder
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::createToken
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
Expand All @@ -186,19 +170,16 @@ public function parseMustReturnANonSignedTokenWhenSignatureIsNotInformed()
$this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'none'], 'headers', $token);
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
$this->assertAttributeEquals(null, 'signature', $token);
$this->assertAttributeSame($this->encoder, 'encoder', $token);
}

/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::setEncoder
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::createToken
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
Expand All @@ -224,20 +205,17 @@ public function parseShouldReplicateClaimValueOnHeaderWhenNeeded()

$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
$this->assertAttributeEquals(null, 'signature', $token);
$this->assertAttributeSame($this->encoder, 'encoder', $token);
}

/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::setEncoder
* @uses Lcobucci\JWT\Signature::__construct
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::createToken
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
Expand All @@ -262,6 +240,5 @@ public function parseMustReturnASignedTokenWhenSignatureIsInformed()
$this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'HS256'], 'headers', $token);
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
$this->assertAttributeEquals(new Signature('aaa'), 'signature', $token);
$this->assertAttributeSame($this->encoder, 'encoder', $token);
}
}
Loading

0 comments on commit 9874909

Please sign in to comment.