Initial commit

This commit is contained in:
distinctm
2019-01-22 17:58:20 -05:00
commit d6ced07199
6519 changed files with 752220 additions and 0 deletions

View File

@@ -0,0 +1,699 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT;
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
use Lcobucci\JWT\Parsing\Encoder;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class BuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Encoder|\PHPUnit_Framework_MockObject_MockObject
*/
protected $encoder;
/**
* @var ClaimFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $claimFactory;
/**
* @var Claim|\PHPUnit_Framework_MockObject_MockObject
*/
protected $defaultClaim;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->encoder = $this->getMock(Encoder::class);
$this->claimFactory = $this->getMock(ClaimFactory::class, [], [], '', false);
$this->defaultClaim = $this->getMock(Claim::class);
$this->claimFactory->expects($this->any())
->method('create')
->willReturn($this->defaultClaim);
}
/**
* @return Builder
*/
private function createBuilder()
{
return new Builder($this->encoder, $this->claimFactory);
}
/**
* @test
*
* @covers Lcobucci\JWT\Builder::__construct
*/
public function constructMustInitializeTheAttributes()
{
$builder = $this->createBuilder();
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
$this->assertAttributeEquals([], 'claims', $builder);
$this->assertAttributeEquals(null, 'signature', $builder);
$this->assertAttributeSame($this->encoder, 'encoder', $builder);
$this->assertAttributeSame($this->claimFactory, 'claimFactory', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setAudience
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setAudienceMustChangeTheAudClaim()
{
$builder = $this->createBuilder();
$builder->setAudience('test');
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setAudience
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setAudienceCanReplicateItemOnHeader()
{
$builder = $this->createBuilder();
$builder->setAudience('test', true);
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $builder);
$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'aud' => $this->defaultClaim],
'headers',
$builder
);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setAudience
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setAudienceMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->setAudience('test'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setExpiration
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setExpirationMustChangeTheExpClaim()
{
$builder = $this->createBuilder();
$builder->setExpiration('2');
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
$this->assertAttributeEquals(['exp' => $this->defaultClaim], 'claims', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setExpiration
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setExpirationCanReplicateItemOnHeader()
{
$builder = $this->createBuilder();
$builder->setExpiration('2', true);
$this->assertAttributeEquals(['exp' => $this->defaultClaim], 'claims', $builder);
$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'exp' => $this->defaultClaim],
'headers',
$builder
);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setExpiration
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setExpirationMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->setExpiration('2'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setId
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIdMustChangeTheJtiClaim()
{
$builder = $this->createBuilder();
$builder->setId('2');
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
$this->assertAttributeEquals(['jti' => $this->defaultClaim], 'claims', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setId
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIdCanReplicateItemOnHeader()
{
$builder = $this->createBuilder();
$builder->setId('2', true);
$this->assertAttributeEquals(['jti' => $this->defaultClaim], 'claims', $builder);
$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'jti' => $this->defaultClaim],
'headers',
$builder
);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setId
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIdMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->setId('2'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setIssuedAt
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIssuedAtMustChangeTheIatClaim()
{
$builder = $this->createBuilder();
$builder->setIssuedAt('2');
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
$this->assertAttributeEquals(['iat' => $this->defaultClaim], 'claims', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setIssuedAt
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIssuedAtCanReplicateItemOnHeader()
{
$builder = $this->createBuilder();
$builder->setIssuedAt('2', true);
$this->assertAttributeEquals(['iat' => $this->defaultClaim], 'claims', $builder);
$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'iat' => $this->defaultClaim],
'headers',
$builder
);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setIssuedAt
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIssuedAtMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->setIssuedAt('2'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setIssuer
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIssuerMustChangeTheIssClaim()
{
$builder = $this->createBuilder();
$builder->setIssuer('2');
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
$this->assertAttributeEquals(['iss' => $this->defaultClaim], 'claims', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setIssuer
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIssuerCanReplicateItemOnHeader()
{
$builder = $this->createBuilder();
$builder->setIssuer('2', true);
$this->assertAttributeEquals(['iss' => $this->defaultClaim], 'claims', $builder);
$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'iss' => $this->defaultClaim],
'headers',
$builder
);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setIssuer
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setIssuerMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->setIssuer('2'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setNotBefore
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setNotBeforeMustChangeTheNbfClaim()
{
$builder = $this->createBuilder();
$builder->setNotBefore('2');
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
$this->assertAttributeEquals(['nbf' => $this->defaultClaim], 'claims', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setNotBefore
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setNotBeforeCanReplicateItemOnHeader()
{
$builder = $this->createBuilder();
$builder->setNotBefore('2', true);
$this->assertAttributeEquals(['nbf' => $this->defaultClaim], 'claims', $builder);
$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'nbf' => $this->defaultClaim],
'headers',
$builder
);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setNotBefore
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setNotBeforeMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->setNotBefore('2'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setSubject
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setSubjectMustChangeTheSubClaim()
{
$builder = $this->createBuilder();
$builder->setSubject('2');
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
$this->assertAttributeEquals(['sub' => $this->defaultClaim], 'claims', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setSubject
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setSubjectCanReplicateItemOnHeader()
{
$builder = $this->createBuilder();
$builder->setSubject('2', true);
$this->assertAttributeEquals(['sub' => $this->defaultClaim], 'claims', $builder);
$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'sub' => $this->defaultClaim],
'headers',
$builder
);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
*
* @covers Lcobucci\JWT\Builder::setSubject
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
*/
public function setSubjectMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->setSubject('2'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
*
* @covers Lcobucci\JWT\Builder::set
*/
public function setMustConfigureTheGivenClaim()
{
$builder = $this->createBuilder();
$builder->set('userId', 2);
$this->assertAttributeEquals(['userId' => $this->defaultClaim], 'claims', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
*
* @covers Lcobucci\JWT\Builder::set
*/
public function setMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->set('userId', 2));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
*
* @covers Lcobucci\JWT\Builder::setHeader
*/
public function setHeaderMustConfigureTheGivenClaim()
{
$builder = $this->createBuilder();
$builder->setHeader('userId', 2);
$this->assertAttributeEquals(
['alg' => 'none', 'typ' => 'JWT', 'userId' => $this->defaultClaim],
'headers',
$builder
);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
*
* @covers Lcobucci\JWT\Builder::setHeader
*/
public function setHeaderMustKeepAFluentInterface()
{
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->setHeader('userId', 2));
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::getToken
* @uses Lcobucci\JWT\Token
*
* @covers Lcobucci\JWT\Builder::sign
*/
public function signMustChangeTheSignature()
{
$signer = $this->getMock(Signer::class);
$signature = $this->getMock(Signature::class, [], [], '', false);
$signer->expects($this->any())
->method('sign')
->willReturn($signature);
$builder = $this->createBuilder();
$builder->sign($signer, 'test');
$this->assertAttributeSame($signature, 'signature', $builder);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::getToken
* @uses Lcobucci\JWT\Token
*
* @covers Lcobucci\JWT\Builder::sign
*/
public function signMustKeepAFluentInterface()
{
$signer = $this->getMock(Signer::class);
$signature = $this->getMock(Signature::class, [], [], '', false);
$signer->expects($this->any())
->method('sign')
->willReturn($signature);
$builder = $this->createBuilder();
$this->assertSame($builder, $builder->sign($signer, 'test'));
return $builder;
}
/**
* @test
*
* @depends signMustKeepAFluentInterface
*
* @covers Lcobucci\JWT\Builder::unsign
*/
public function unsignMustRemoveTheSignature(Builder $builder)
{
$builder->unsign();
$this->assertAttributeSame(null, 'signature', $builder);
}
/**
* @test
*
* @depends signMustKeepAFluentInterface
*
* @covers Lcobucci\JWT\Builder::unsign
*/
public function unsignMustKeepAFluentInterface(Builder $builder)
{
$this->assertSame($builder, $builder->unsign());
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::sign
* @uses Lcobucci\JWT\Builder::getToken
* @uses Lcobucci\JWT\Token
*
* @covers Lcobucci\JWT\Builder::set
*
* @expectedException BadMethodCallException
*/
public function setMustRaiseExceptionWhenTokenHasBeenSigned()
{
$signer = $this->getMock(Signer::class);
$signature = $this->getMock(Signature::class, [], [], '', false);
$signer->expects($this->any())
->method('sign')
->willReturn($signature);
$builder = $this->createBuilder();
$builder->sign($signer, 'test');
$builder->set('test', 123);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::sign
* @uses Lcobucci\JWT\Builder::getToken
* @uses Lcobucci\JWT\Token
*
* @covers Lcobucci\JWT\Builder::setHeader
*
* @expectedException BadMethodCallException
*/
public function setHeaderMustRaiseExceptionWhenTokenHasBeenSigned()
{
$signer = $this->getMock(Signer::class);
$signature = $this->getMock(Signature::class, [], [], '', false);
$signer->expects($this->any())
->method('sign')
->willReturn($signature);
$builder = $this->createBuilder();
$builder->sign($signer, 'test');
$builder->setHeader('test', 123);
}
/**
* @test
*
* @uses Lcobucci\JWT\Builder::__construct
* @uses Lcobucci\JWT\Builder::set
* @uses Lcobucci\JWT\Token
*
* @covers Lcobucci\JWT\Builder::getToken
*/
public function getTokenMustReturnANewTokenWithCurrentConfiguration()
{
$signature = $this->getMock(Signature::class, [], [], '', false);
$this->encoder->expects($this->exactly(2))
->method('jsonEncode')
->withConsecutive([['typ'=> 'JWT', 'alg' => 'none']], [['test' => $this->defaultClaim]])
->willReturnOnConsecutiveCalls('1', '2');
$this->encoder->expects($this->exactly(3))
->method('base64UrlEncode')
->withConsecutive(['1'], ['2'], [$signature])
->willReturnOnConsecutiveCalls('1', '2', '3');
$builder = $this->createBuilder()->set('test', 123);
$builderSign = new \ReflectionProperty($builder, 'signature');
$builderSign->setAccessible(true);
$builderSign->setValue($builder, $signature);
$token = $builder->getToken();
$tokenSign = new \ReflectionProperty($token, 'signature');
$tokenSign->setAccessible(true);
$this->assertAttributeEquals(['1', '2', '3'], 'payload', $token);
$this->assertAttributeEquals($token->getHeaders(), 'headers', $builder);
$this->assertAttributeEquals($token->getClaims(), 'claims', $builder);
$this->assertAttributeSame($tokenSign->getValue($token), 'signature', $builder);
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Claim;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.0.0
*/
class BasicTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Claim\Basic::__construct
*/
public function constructorShouldConfigureTheAttributes()
{
$claim = new Basic('test', 1);
$this->assertAttributeEquals('test', 'name', $claim);
$this->assertAttributeEquals(1, 'value', $claim);
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Basic::getName
*/
public function getNameShouldReturnTheClaimName()
{
$claim = new Basic('test', 1);
$this->assertEquals('test', $claim->getName());
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Basic::getValue
*/
public function getValueShouldReturnTheClaimValue()
{
$claim = new Basic('test', 1);
$this->assertEquals(1, $claim->getValue());
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Basic::jsonSerialize
*/
public function jsonSerializeShouldReturnTheClaimValue()
{
$claim = new Basic('test', 1);
$this->assertEquals(1, $claim->jsonSerialize());
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Basic::__toString
*/
public function toStringShouldReturnTheClaimValue()
{
$claim = new Basic('test', 1);
$this->assertEquals('1', (string) $claim);
}
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Claim;
use Lcobucci\JWT\ValidationData;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.0.0
*/
class EqualsToTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::has
*
* @covers Lcobucci\JWT\Claim\EqualsTo::validate
*/
public function validateShouldReturnTrueWhenValidationDontHaveTheClaim()
{
$claim = new EqualsTo('iss', 'test');
$this->assertTrue($claim->validate(new ValidationData()));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::get
*
* @covers Lcobucci\JWT\Claim\EqualsTo::validate
*/
public function validateShouldReturnTrueWhenValueIsEqualsToValidationData()
{
$claim = new EqualsTo('iss', 'test');
$data = new ValidationData();
$data->setIssuer('test');
$this->assertTrue($claim->validate($data));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::get
*
* @covers Lcobucci\JWT\Claim\EqualsTo::validate
*/
public function validateShouldReturnFalseWhenValueIsNotEqualsToValidationData()
{
$claim = new EqualsTo('iss', 'test');
$data = new ValidationData();
$data->setIssuer('test1');
$this->assertFalse($claim->validate($data));
}
}

View File

@@ -0,0 +1,168 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Claim;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.0.0
*/
class FactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Claim\Factory::__construct
*/
public function constructMustConfigureTheCallbacks()
{
$callback = function () {
};
$factory = new Factory(['test' => $callback]);
$expected = [
'iat' => [$factory, 'createLesserOrEqualsTo'],
'nbf' => [$factory, 'createLesserOrEqualsTo'],
'exp' => [$factory, 'createGreaterOrEqualsTo'],
'iss' => [$factory, 'createEqualsTo'],
'aud' => [$factory, 'createEqualsTo'],
'sub' => [$factory, 'createEqualsTo'],
'jti' => [$factory, 'createEqualsTo'],
'test' => $callback
];
$this->assertAttributeEquals($expected, 'callbacks', $factory);
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Factory::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Factory::create
* @covers Lcobucci\JWT\Claim\Factory::createLesserOrEqualsTo
*/
public function createShouldReturnALesserOrEqualsToClaimForIssuedAt()
{
$claim = new Factory();
$this->assertInstanceOf(LesserOrEqualsTo::class, $claim->create('iat', 1));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Factory::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Factory::create
* @covers Lcobucci\JWT\Claim\Factory::createLesserOrEqualsTo
*/
public function createShouldReturnALesserOrEqualsToClaimForNotBefore()
{
$claim = new Factory();
$this->assertInstanceOf(LesserOrEqualsTo::class, $claim->create('nbf', 1));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Factory::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Factory::create
* @covers Lcobucci\JWT\Claim\Factory::createGreaterOrEqualsTo
*/
public function createShouldReturnAGreaterOrEqualsToClaimForExpiration()
{
$claim = new Factory();
$this->assertInstanceOf(GreaterOrEqualsTo::class, $claim->create('exp', 1));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Factory::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Factory::create
* @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
*/
public function createShouldReturnAnEqualsToClaimForId()
{
$claim = new Factory();
$this->assertInstanceOf(EqualsTo::class, $claim->create('jti', 1));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Factory::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Factory::create
* @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
*/
public function createShouldReturnAnEqualsToClaimForIssuer()
{
$claim = new Factory();
$this->assertInstanceOf(EqualsTo::class, $claim->create('iss', 1));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Factory::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Factory::create
* @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
*/
public function createShouldReturnAnEqualsToClaimForAudience()
{
$claim = new Factory();
$this->assertInstanceOf(EqualsTo::class, $claim->create('aud', 1));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Factory::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Factory::create
* @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
*/
public function createShouldReturnAnEqualsToClaimForSubject()
{
$claim = new Factory();
$this->assertInstanceOf(EqualsTo::class, $claim->create('sub', 1));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Factory::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Claim\Factory::create
* @covers Lcobucci\JWT\Claim\Factory::createBasic
*/
public function createShouldReturnABasiclaimForOtherClaims()
{
$claim = new Factory();
$this->assertInstanceOf(Basic::class, $claim->create('test', 1));
}
}

View File

@@ -0,0 +1,103 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Claim;
use Lcobucci\JWT\ValidationData;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.0.0
*/
class GreaterOrEqualsToTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::has
*
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
*/
public function validateShouldReturnTrueWhenValidationDontHaveTheClaim()
{
$claim = new GreaterOrEqualsTo('iss', 10);
$this->assertTrue($claim->validate(new ValidationData()));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::get
*
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
*/
public function validateShouldReturnTrueWhenValueIsGreaterThanValidationData()
{
$claim = new GreaterOrEqualsTo('iss', 11);
$data = new ValidationData();
$data->setIssuer(10);
$this->assertTrue($claim->validate($data));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::get
*
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
*/
public function validateShouldReturnTrueWhenValueIsEqualsToValidationData()
{
$claim = new GreaterOrEqualsTo('iss', 10);
$data = new ValidationData();
$data->setIssuer(10);
$this->assertTrue($claim->validate($data));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::get
*
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
*/
public function validateShouldReturnFalseWhenValueIsLesserThanValidationData()
{
$claim = new GreaterOrEqualsTo('iss', 10);
$data = new ValidationData();
$data->setIssuer(11);
$this->assertFalse($claim->validate($data));
}
}

View File

@@ -0,0 +1,103 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Claim;
use Lcobucci\JWT\ValidationData;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.0.0
*/
class LesserOrEqualsToTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::has
*
* @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
*/
public function validateShouldReturnTrueWhenValidationDontHaveTheClaim()
{
$claim = new LesserOrEqualsTo('iss', 10);
$this->assertTrue($claim->validate(new ValidationData()));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::get
*
* @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
*/
public function validateShouldReturnTrueWhenValueIsLesserThanValidationData()
{
$claim = new LesserOrEqualsTo('iss', 10);
$data = new ValidationData();
$data->setIssuer(11);
$this->assertTrue($claim->validate($data));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::get
*
* @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
*/
public function validateShouldReturnTrueWhenValueIsEqualsToValidationData()
{
$claim = new LesserOrEqualsTo('iss', 10);
$data = new ValidationData();
$data->setIssuer(10);
$this->assertTrue($claim->validate($data));
}
/**
* @test
*
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::get
*
* @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
*/
public function validateShouldReturnFalseWhenValueIsGreaterThanValidationData()
{
$claim = new LesserOrEqualsTo('iss', 11);
$data = new ValidationData();
$data->setIssuer(10);
$this->assertFalse($claim->validate($data));
}
}

View File

@@ -0,0 +1,244 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT;
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
use Lcobucci\JWT\Parsing\Decoder;
use RuntimeException;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class ParserTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Decoder|\PHPUnit_Framework_MockObject_MockObject
*/
protected $decoder;
/**
* @var ClaimFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $claimFactory;
/**
* @var Claim|\PHPUnit_Framework_MockObject_MockObject
*/
protected $defaultClaim;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->decoder = $this->getMock(Decoder::class);
$this->claimFactory = $this->getMock(ClaimFactory::class, [], [], '', false);
$this->defaultClaim = $this->getMock(Claim::class);
$this->claimFactory->expects($this->any())
->method('create')
->willReturn($this->defaultClaim);
}
/**
* @return Parser
*/
private function createParser()
{
return new Parser($this->decoder, $this->claimFactory);
}
/**
* @test
*
* @covers Lcobucci\JWT\Parser::__construct
*/
public function constructMustConfigureTheAttributes()
{
$parser = $this->createParser();
$this->assertAttributeSame($this->decoder, 'decoder', $parser);
$this->assertAttributeSame($this->claimFactory, 'claimFactory', $parser);
}
/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
*
* @expectedException InvalidArgumentException
*/
public function parseMustRaiseExceptionWhenJWSIsNotAString()
{
$parser = $this->createParser();
$parser->parse(['asdasd']);
}
/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
*
* @expectedException InvalidArgumentException
*/
public function parseMustRaiseExceptionWhenJWSDontHaveThreeParts()
{
$parser = $this->createParser();
$parser->parse('');
}
/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::parseHeader
*
* @expectedException RuntimeException
*/
public function parseMustRaiseExceptionWhenHeaderCannotBeDecoded()
{
$this->decoder->expects($this->any())
->method('jsonDecode')
->willThrowException(new RuntimeException());
$parser = $this->createParser();
$parser->parse('asdfad.asdfasdf.');
}
/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::parseHeader
*
* @expectedException InvalidArgumentException
*/
public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken()
{
$this->decoder->expects($this->any())
->method('jsonDecode')
->willReturn(['enc' => 'AAA']);
$parser = $this->createParser();
$parser->parse('a.a.');
}
/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
*
*/
public function parseMustReturnANonSignedTokenWhenSignatureIsNotInformed()
{
$this->decoder->expects($this->at(1))
->method('jsonDecode')
->willReturn(['typ' => 'JWT', 'alg' => 'none']);
$this->decoder->expects($this->at(3))
->method('jsonDecode')
->willReturn(['aud' => 'test']);
$parser = $this->createParser();
$token = $parser->parse('a.a.');
$this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'none'], 'headers', $token);
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
$this->assertAttributeEquals(null, 'signature', $token);
}
/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
*/
public function parseShouldReplicateClaimValueOnHeaderWhenNeeded()
{
$this->decoder->expects($this->at(1))
->method('jsonDecode')
->willReturn(['typ' => 'JWT', 'alg' => 'none', 'aud' => 'test']);
$this->decoder->expects($this->at(3))
->method('jsonDecode')
->willReturn(['aud' => 'test']);
$parser = $this->createParser();
$token = $parser->parse('a.a.');
$this->assertAttributeEquals(
['typ' => 'JWT', 'alg' => 'none', 'aud' => $this->defaultClaim],
'headers',
$token
);
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
$this->assertAttributeEquals(null, 'signature', $token);
}
/**
* @test
*
* @uses Lcobucci\JWT\Parser::__construct
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Signature::__construct
*
* @covers Lcobucci\JWT\Parser::parse
* @covers Lcobucci\JWT\Parser::splitJwt
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
*/
public function parseMustReturnASignedTokenWhenSignatureIsInformed()
{
$this->decoder->expects($this->at(1))
->method('jsonDecode')
->willReturn(['typ' => 'JWT', 'alg' => 'HS256']);
$this->decoder->expects($this->at(3))
->method('jsonDecode')
->willReturn(['aud' => 'test']);
$this->decoder->expects($this->at(4))
->method('base64UrlDecode')
->willReturn('aaa');
$parser = $this->createParser();
$token = $parser->parse('a.a.a');
$this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'HS256'], 'headers', $token);
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
$this->assertAttributeEquals(new Signature('aaa'), 'signature', $token);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Parsing;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class DecoderTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Parsing\Decoder::jsonDecode
*/
public function jsonDecodeMustReturnTheDecodedData()
{
$decoder = new Decoder();
$this->assertEquals(
(object) ['test' => 'test'],
$decoder->jsonDecode('{"test":"test"}')
);
}
/**
* @test
*
* @covers Lcobucci\JWT\Parsing\Decoder::jsonDecode
*
* @expectedException \RuntimeException
*/
public function jsonDecodeMustRaiseExceptionWhenAnErrorHasOccured()
{
$decoder = new Decoder();
$decoder->jsonDecode('{"test":\'test\'}');
}
/**
* @test
*
* @covers Lcobucci\JWT\Parsing\Decoder::base64UrlDecode
*/
public function base64UrlDecodeMustReturnTheRightData()
{
$data = base64_decode('0MB2wKB+L3yvIdzeggmJ+5WOSLaRLTUPXbpzqUe0yuo=');
$decoder = new Decoder();
$this->assertEquals($data, $decoder->base64UrlDecode('0MB2wKB-L3yvIdzeggmJ-5WOSLaRLTUPXbpzqUe0yuo'));
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Parsing;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class EncoderTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Parsing\Encoder::jsonEncode
*/
public function jsonEncodeMustReturnAJSONString()
{
$encoder = new Encoder();
$this->assertEquals('{"test":"test"}', $encoder->jsonEncode(['test' => 'test']));
}
/**
* @test
*
* @covers Lcobucci\JWT\Parsing\Encoder::jsonEncode
*
* @expectedException \RuntimeException
*/
public function jsonEncodeMustRaiseExceptionWhenAnErrorHasOccured()
{
$encoder = new Encoder();
$encoder->jsonEncode("\xB1\x31");
}
/**
* @test
*
* @covers Lcobucci\JWT\Parsing\Encoder::base64UrlEncode
*/
public function base64UrlEncodeMustReturnAnUrlSafeBase64()
{
$data = base64_decode('0MB2wKB+L3yvIdzeggmJ+5WOSLaRLTUPXbpzqUe0yuo=');
$encoder = new Encoder();
$this->assertEquals('0MB2wKB-L3yvIdzeggmJ-5WOSLaRLTUPXbpzqUe0yuo', $encoder->base64UrlEncode($data));
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class SignatureTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Signer|\PHPUnit_Framework_MockObject_MockObject
*/
protected $signer;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->signer = $this->getMock(Signer::class);
}
/**
* @test
*
* @covers Lcobucci\JWT\Signature::__construct
*/
public function constructorMustConfigureAttributes()
{
$signature = new Signature('test');
$this->assertAttributeEquals('test', 'hash', $signature);
}
/**
* @test
*
* @uses Lcobucci\JWT\Signature::__construct
*
* @covers Lcobucci\JWT\Signature::__toString
*/
public function toStringMustReturnTheHash()
{
$signature = new Signature('test');
$this->assertEquals('test', (string) $signature);
}
/**
* @test
*
* @uses Lcobucci\JWT\Signature::__construct
* @uses Lcobucci\JWT\Signature::__toString
*
* @covers Lcobucci\JWT\Signature::verify
*/
public function verifyMustReturnWhatSignerSays()
{
$this->signer->expects($this->any())
->method('verify')
->willReturn(true);
$signature = new Signature('test');
$this->assertTrue($signature->verify($this->signer, 'one', 'key'));
}
}

View File

@@ -0,0 +1,128 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signature;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class BaseSignerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var BaseSigner|\PHPUnit_Framework_MockObject_MockObject
*/
protected $signer;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->signer = $this->getMockForAbstractClass(BaseSigner::class);
$this->signer->method('getAlgorithmId')
->willReturn('TEST123');
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\BaseSigner::modifyHeader
*/
public function modifyHeaderShouldChangeAlgorithm()
{
$headers = ['typ' => 'JWT'];
$this->signer->modifyHeader($headers);
$this->assertEquals($headers['typ'], 'JWT');
$this->assertEquals($headers['alg'], 'TEST123');
}
/**
* @test
*
* @uses Lcobucci\JWT\Signature::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\BaseSigner::sign
* @covers Lcobucci\JWT\Signer\BaseSigner::getKey
*/
public function signMustReturnANewSignature()
{
$key = new Key('123');
$this->signer->expects($this->once())
->method('createHash')
->with('test', $key)
->willReturn('test');
$this->assertEquals(new Signature('test'), $this->signer->sign('test', $key));
}
/**
* @test
*
* @uses Lcobucci\JWT\Signature::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\BaseSigner::sign
* @covers Lcobucci\JWT\Signer\BaseSigner::getKey
*/
public function signShouldConvertKeyWhenItsNotAnObject()
{
$this->signer->expects($this->once())
->method('createHash')
->with('test', new Key('123'))
->willReturn('test');
$this->assertEquals(new Signature('test'), $this->signer->sign('test', '123'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Signature::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\BaseSigner::verify
* @covers Lcobucci\JWT\Signer\BaseSigner::getKey
*/
public function verifyShouldDelegateTheCallToAbstractMethod()
{
$key = new Key('123');
$this->signer->expects($this->once())
->method('doVerify')
->with('test', 'test', $key)
->willReturn(true);
$this->assertTrue($this->signer->verify('test', 'test', $key));
}
/**
* @test
*
* @uses Lcobucci\JWT\Signature::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\BaseSigner::verify
* @covers Lcobucci\JWT\Signer\BaseSigner::getKey
*/
public function verifyShouldConvertKeyWhenItsNotAnObject()
{
$this->signer->expects($this->once())
->method('doVerify')
->with('test', 'test', new Key('123'))
->willReturn(true);
$this->assertTrue($this->signer->verify('test', 'test', '123'));
}
}

View File

@@ -0,0 +1,178 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Ecdsa;
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
use Mdanter\Ecc\Math\MathAdapterInterface;
use Mdanter\Ecc\Serializer\PrivateKey\PrivateKeySerializerInterface;
use Mdanter\Ecc\Serializer\PublicKey\PublicKeySerializerInterface;
use Lcobucci\JWT\Signer\Key;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 3.0.4
*/
class KeyParserTest extends \PHPUnit_Framework_TestCase
{
/**
* @var MathAdapterInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $adapter;
/**
* @var PrivateKeySerializerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $privateKeySerializer;
/**
* @var PublicKeySerializerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $publicKeySerializer;
/**
* @before
*/
public function createDependencies()
{
$this->adapter = $this->getMock(MathAdapterInterface::class);
$this->privateKeySerializer = $this->getMock(PrivateKeySerializerInterface::class);
$this->publicKeySerializer = $this->getMock(PublicKeySerializerInterface::class);
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
*/
public function constructShouldConfigureDependencies()
{
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
$this->assertAttributeSame($this->privateKeySerializer, 'privateKeySerializer', $parser);
$this->assertAttributeSame($this->publicKeySerializer, 'publicKeySerializer', $parser);
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPrivateKey
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
*/
public function getPrivateKeyShouldAskSerializerToParseTheKey()
{
$privateKey = $this->getMock(PrivateKeyInterface::class);
$keyContent = 'MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGC'
. 'CqGSM49AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iF'
. 'ruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==';
$this->privateKeySerializer->expects($this->once())
->method('parse')
->with($keyContent)
->willReturn($privateKey);
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
$this->assertSame($privateKey, $parser->getPrivateKey($this->getPrivateKey()));
}
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPrivateKey
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
*/
public function getPrivateKeyShouldRaiseExceptionWhenAWrongKeyWasGiven()
{
$this->privateKeySerializer->expects($this->never())
->method('parse');
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
$parser->getPrivateKey($this->getPublicKey());
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPublicKey
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
*/
public function getPublicKeyShouldAskSerializerToParseTheKey()
{
$publicKey = $this->getMock(PublicKeyInterface::class);
$keyContent = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn'
. 'd0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==';
$this->publicKeySerializer->expects($this->once())
->method('parse')
->with($keyContent)
->willReturn($publicKey);
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
$this->assertSame($publicKey, $parser->getPublicKey($this->getPublicKey()));
}
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPublicKey
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
*/
public function getPublicKeyShouldRaiseExceptionWhenAWrongKeyWasGiven()
{
$this->publicKeySerializer->expects($this->never())
->method('parse');
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
$parser->getPublicKey($this->getPrivateKey());
}
/**
* @return Key
*/
private function getPrivateKey()
{
return new Key(
"-----BEGIN EC PRIVATE KEY-----\n"
. "MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGCCqGSM49\n"
. "AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iFruiI2tsEdGFTLTsy\n"
. "U+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==\n"
. "-----END EC PRIVATE KEY-----"
);
}
/**
* @return Key
*/
private function getPublicKey()
{
return new Key(
"-----BEGIN PUBLIC KEY-----\n"
. "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn\n"
. "d0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==\n"
. "-----END PUBLIC KEY-----"
);
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Ecdsa;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.1.0
*/
class Sha256Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha256();
$this->assertEquals('ES256', $signer->getAlgorithmId());
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha256();
$this->assertEquals('sha256', $signer->getAlgorithm());
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getSignatureLength
*/
public function getSignatureLengthMustBeCorrect()
{
$signer = new Sha256();
$this->assertEquals(64, $signer->getSignatureLength());
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Ecdsa;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.1.0
*/
class Sha384Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha384();
$this->assertEquals('ES384', $signer->getAlgorithmId());
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha384();
$this->assertEquals('sha384', $signer->getAlgorithm());
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getSignatureLength
*/
public function getSignatureLengthMustBeCorrect()
{
$signer = new Sha384();
$this->assertEquals(96, $signer->getSignatureLength());
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Ecdsa;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.1.0
*/
class Sha512Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha512();
$this->assertEquals('ES512', $signer->getAlgorithmId());
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha512();
$this->assertEquals('sha512', $signer->getAlgorithm());
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
*
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getSignatureLength
*/
public function getSignatureLengthMustBeCorrect()
{
$signer = new Sha512();
$this->assertEquals(132, $signer->getSignatureLength());
}
}

View File

@@ -0,0 +1,173 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Ecdsa\KeyParser;
use Mdanter\Ecc\Crypto\Signature\Signature;
use Mdanter\Ecc\Crypto\Signature\Signer;
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
use Mdanter\Ecc\Math\MathAdapterInterface as Adapter;
use Mdanter\Ecc\Primitives\PointInterface;
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.1.0
*/
class EcdsaTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Adapter|\PHPUnit_Framework_MockObject_MockObject
*/
private $adapter;
/**
* @var Signer|\PHPUnit_Framework_MockObject_MockObject
*/
private $signer;
/**
* @var RandomNumberGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $randomGenerator;
/**
* @var KeyParser|\PHPUnit_Framework_MockObject_MockObject
*/
private $parser;
/**
* @before
*/
public function createDependencies()
{
$this->adapter = $this->getMock(Adapter::class);
$this->signer = $this->getMock(Signer::class, [], [$this->adapter]);
$this->randomGenerator = $this->getMock(RandomNumberGeneratorInterface::class);
$this->parser = $this->getMock(KeyParser::class, [], [], '', false);
}
/**
* @return Ecdsa
*/
private function getSigner()
{
$signer = $this->getMockForAbstractClass(
Ecdsa::class,
[$this->adapter, $this->signer, $this->parser]
);
$signer->method('getSignatureLength')
->willReturn(64);
$signer->method('getAlgorithm')
->willReturn('sha256');
$signer->method('getAlgorithmId')
->willReturn('ES256');
return $signer;
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Ecdsa::__construct
*/
public function constructShouldConfigureDependencies()
{
$signer = $this->getSigner();
$this->assertAttributeSame($this->adapter, 'adapter', $signer);
$this->assertAttributeSame($this->signer, 'signer', $signer);
$this->assertAttributeSame($this->parser, 'parser', $signer);
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Ecdsa::createHash
* @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
* @covers Lcobucci\JWT\Signer\Ecdsa::createSignatureHash
*/
public function createHashShouldReturnAHashUsingPrivateKey()
{
$signer = $this->getSigner();
$key = new Key('testing');
$privateKey = $this->getMock(PrivateKeyInterface::class);
$point = $this->getMock(PointInterface::class);
$privateKey->method('getPoint')
->willReturn($point);
$point->method('getOrder')
->willReturn('1');
$this->parser->expects($this->once())
->method('getPrivateKey')
->with($key)
->willReturn($privateKey);
$this->randomGenerator->expects($this->once())
->method('generate')
->with('1')
->willReturn('123');
$this->adapter->expects($this->once())
->method('hexDec')
->willReturn('123');
$this->adapter->expects($this->exactly(2))
->method('decHex')
->willReturn('123');
$this->signer->expects($this->once())
->method('sign')
->with($privateKey, $this->isType('string'), $this->isType('string'))
->willReturn(new Signature('1234', '456'));
$this->assertInternalType('string', $signer->createHash('testing', $key, $this->randomGenerator));
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Ecdsa::__construct
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Ecdsa::doVerify
* @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
* @covers Lcobucci\JWT\Signer\Ecdsa::extractSignature
*/
public function doVerifyShouldDelegateToEcdsaSignerUsingPublicKey()
{
$signer = $this->getSigner();
$key = new Key('testing');
$publicKey = $this->getMock(PublicKeyInterface::class);
$this->parser->expects($this->once())
->method('getPublicKey')
->with($key)
->willReturn($publicKey);
$this->adapter->expects($this->exactly(3))
->method('hexDec')
->willReturn('123');
$this->signer->expects($this->once())
->method('verify')
->with($publicKey, $this->isInstanceOf(Signature::class), $this->isType('string'))
->willReturn(true);
$this->assertTrue($signer->doVerify('testing', 'testing2', $key));
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Hmac;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class Sha256Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Hmac\Sha256::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha256();
$this->assertEquals('HS256', $signer->getAlgorithmId());
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Hmac\Sha256::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha256();
$this->assertEquals('sha256', $signer->getAlgorithm());
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Hmac;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class Sha384Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Hmac\Sha384::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha384();
$this->assertEquals('HS384', $signer->getAlgorithmId());
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Hmac\Sha384::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha384();
$this->assertEquals('sha384', $signer->getAlgorithm());
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Hmac;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class Sha512Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Hmac\Sha512::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha512();
$this->assertEquals('HS512', $signer->getAlgorithmId());
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Hmac\Sha512::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha512();
$this->assertEquals('sha512', $signer->getAlgorithm());
}
}

View File

@@ -0,0 +1,134 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class HmacTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Hmac|\PHPUnit_Framework_MockObject_MockObject
*/
protected $signer;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->signer = $this->getMockForAbstractClass(Hmac::class);
$this->signer->expects($this->any())
->method('getAlgorithmId')
->willReturn('TEST123');
$this->signer->expects($this->any())
->method('getAlgorithm')
->willReturn('sha256');
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Hmac::createHash
*/
public function createHashMustReturnAHashAccordingWithTheAlgorithm()
{
$hash = hash_hmac('sha256', 'test', '123', true);
$this->assertEquals($hash, $this->signer->createHash('test', new Key('123')));
return $hash;
}
/**
* @test
*
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
*
* @uses Lcobucci\JWT\Signer\Hmac::createHash
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Hmac::doVerify
*/
public function doVerifyShouldReturnTrueWhenExpectedHashWasCreatedWithSameInformation($expected)
{
$this->assertTrue($this->signer->doVerify($expected, 'test', new Key('123')));
}
/**
* @test
*
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
*
* @uses Lcobucci\JWT\Signer\Hmac::createHash
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Hmac::doVerify
*/
public function doVerifyShouldReturnFalseWhenExpectedHashWasNotCreatedWithSameInformation($expected)
{
$this->assertFalse($this->signer->doVerify($expected, 'test', new Key('1234')));
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Hmac::doVerify
*/
public function doVerifyShouldReturnFalseWhenExpectedHashIsNotString()
{
$this->assertFalse($this->signer->doVerify(false, 'test', new Key('1234')));
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
*/
public function hashEqualsShouldReturnFalseWhenExpectedHashHasDifferentLengthThanGenerated()
{
$this->assertFalse($this->signer->hashEquals('123', '1234'));
}
/**
* @test
*
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
*
* @uses Lcobucci\JWT\Signer\Hmac::createHash
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
*/
public function hashEqualsShouldReturnFalseWhenExpectedHashIsDifferentThanGenerated($expected)
{
$this->assertFalse($this->signer->hashEquals($expected, $this->signer->createHash('test', new Key('1234'))));
}
/**
* @test
*
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
*
* @uses Lcobucci\JWT\Signer\Hmac::createHash
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
*/
public function hashEqualsShouldReturnTrueWhenExpectedHashIsEqualsThanGenerated($expected)
{
$this->assertTrue($this->signer->hashEquals($expected, $this->signer->createHash('test', new Key('123'))));
}
}

View File

@@ -0,0 +1,119 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
use org\bovigo\vfs\vfsStream;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 3.0.4
*/
class KeyTest extends \PHPUnit_Framework_TestCase
{
/**
* @before
*/
public function configureRootDir()
{
vfsStream::setup(
'root',
null,
[
'test.pem' => 'testing',
'emptyFolder' => []
]
);
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Key::__construct
* @covers Lcobucci\JWT\Signer\Key::setContent
*/
public function constructShouldConfigureContentAndPassphrase()
{
$key = new Key('testing', 'test');
$this->assertAttributeEquals('testing', 'content', $key);
$this->assertAttributeEquals('test', 'passphrase', $key);
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Key::__construct
* @covers Lcobucci\JWT\Signer\Key::setContent
* @covers Lcobucci\JWT\Signer\Key::readFile
*/
public function constructShouldBeAbleToConfigureContentFromFile()
{
$key = new Key('file://' . vfsStream::url('root/test.pem'));
$this->assertAttributeEquals('testing', 'content', $key);
$this->assertAttributeEquals(null, 'passphrase', $key);
}
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @covers Lcobucci\JWT\Signer\Key::__construct
* @covers Lcobucci\JWT\Signer\Key::setContent
* @covers Lcobucci\JWT\Signer\Key::readFile
*/
public function constructShouldRaiseExceptionWhenFileDoesNotExists()
{
new Key('file://' . vfsStream::url('root/test2.pem'));
}
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @covers Lcobucci\JWT\Signer\Key::__construct
* @covers Lcobucci\JWT\Signer\Key::setContent
* @covers Lcobucci\JWT\Signer\Key::readFile
*/
public function constructShouldRaiseExceptionWhenFileGetContentsFailed()
{
new Key('file://' . vfsStream::url('root/emptyFolder'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Key::__construct
* @uses Lcobucci\JWT\Signer\Key::setContent
*
* @covers Lcobucci\JWT\Signer\Key::getContent
*/
public function getContentShouldReturnConfiguredData()
{
$key = new Key('testing', 'test');
$this->assertEquals('testing', $key->getContent());
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Key::__construct
* @uses Lcobucci\JWT\Signer\Key::setContent
*
* @covers Lcobucci\JWT\Signer\Key::getPassphrase
*/
public function getPassphraseShouldReturnConfiguredData()
{
$key = new Key('testing', 'test');
$this->assertEquals('test', $key->getPassphrase());
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.1.0
*/
class KeychainTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Keychain::getPrivateKey
*/
public function getPrivateKeyShouldReturnAKey()
{
$keychain = new Keychain();
$key = $keychain->getPrivateKey('testing', 'test');
$this->assertInstanceOf(Key::class, $key);
$this->assertAttributeEquals('testing', 'content', $key);
$this->assertAttributeEquals('test', 'passphrase', $key);
}
/**
* @test
*
* @uses Lcobucci\JWT\Signer\Key
*
* @covers Lcobucci\JWT\Signer\Keychain::getPublicKey
*/
public function getPublicKeyShouldReturnAValidResource()
{
$keychain = new Keychain();
$key = $keychain->getPublicKey('testing');
$this->assertInstanceOf(Key::class, $key);
$this->assertAttributeEquals('testing', 'content', $key);
$this->assertAttributeEquals(null, 'passphrase', $key);
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Rsa;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.1.0
*/
class Sha256Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Rsa\Sha256::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha256();
$this->assertEquals('RS256', $signer->getAlgorithmId());
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Rsa\Sha256::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha256();
$this->assertEquals(OPENSSL_ALGO_SHA256, $signer->getAlgorithm());
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Rsa;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.1.0
*/
class Sha384Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Rsa\Sha384::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha384();
$this->assertEquals('RS384', $signer->getAlgorithmId());
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Rsa\Sha384::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha384();
$this->assertEquals(OPENSSL_ALGO_SHA384, $signer->getAlgorithm());
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT\Signer\Rsa;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.1.0
*/
class Sha512Test extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Rsa\Sha512::getAlgorithmId
*/
public function getAlgorithmIdMustBeCorrect()
{
$signer = new Sha512();
$this->assertEquals('RS512', $signer->getAlgorithmId());
}
/**
* @test
*
* @covers Lcobucci\JWT\Signer\Rsa\Sha512::getAlgorithm
*/
public function getAlgorithmMustBeCorrect()
{
$signer = new Sha512();
$this->assertEquals(OPENSSL_ALGO_SHA512, $signer->getAlgorithm());
}
}

View File

@@ -0,0 +1,502 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT;
use DateInterval;
use DateTime;
use Lcobucci\JWT\Claim\Basic;
use Lcobucci\JWT\Claim\EqualsTo;
use Lcobucci\JWT\Claim\GreaterOrEqualsTo;
use Lcobucci\JWT\Claim\LesserOrEqualsTo;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 0.1.0
*/
class TokenTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\Token::__construct
*/
public function constructMustInitializeAnEmptyPlainTextTokenWhenNoArgumentsArePassed()
{
$token = new Token();
$this->assertAttributeEquals(['alg' => 'none'], 'headers', $token);
$this->assertAttributeEquals([], 'claims', $token);
$this->assertAttributeEquals(null, 'signature', $token);
$this->assertAttributeEquals(['', ''], 'payload', $token);
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::hasHeader
*/
public function hasHeaderMustReturnTrueWhenItIsConfigured()
{
$token = new Token(['test' => 'testing']);
$this->assertTrue($token->hasHeader('test'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::hasHeader
*/
public function hasHeaderMustReturnFalseWhenItIsNotConfigured()
{
$token = new Token(['test' => 'testing']);
$this->assertFalse($token->hasHeader('testing'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasHeader
*
* @covers Lcobucci\JWT\Token::getHeader
*
* @expectedException \OutOfBoundsException
*/
public function getHeaderMustRaiseExceptionWhenHeaderIsNotConfigured()
{
$token = new Token(['test' => 'testing']);
$token->getHeader('testing');
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasHeader
*
* @covers Lcobucci\JWT\Token::getHeader
*/
public function getHeaderMustReturnTheDefaultValueWhenIsNotConfigured()
{
$token = new Token(['test' => 'testing']);
$this->assertEquals('blah', $token->getHeader('testing', 'blah'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasHeader
*
* @covers Lcobucci\JWT\Token::getHeader
* @covers Lcobucci\JWT\Token::getHeaderValue
*/
public function getHeaderMustReturnTheRequestedHeader()
{
$token = new Token(['test' => 'testing']);
$this->assertEquals('testing', $token->getHeader('test'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasHeader
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::getHeader
* @covers Lcobucci\JWT\Token::getHeaderValue
*/
public function getHeaderMustReturnValueWhenItIsAReplicatedClaim()
{
$token = new Token(['jti' => new EqualsTo('jti', 1)]);
$this->assertEquals(1, $token->getHeader('jti'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::getHeaders
*/
public function getHeadersMustReturnTheConfiguredHeader()
{
$token = new Token(['test' => 'testing']);
$this->assertEquals(['test' => 'testing'], $token->getHeaders());
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::getClaims
*/
public function getClaimsMustReturnTheConfiguredClaims()
{
$token = new Token([], ['test' => 'testing']);
$this->assertEquals(['test' => 'testing'], $token->getClaims());
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::hasClaim
*/
public function hasClaimMustReturnTrueWhenItIsConfigured()
{
$token = new Token([], ['test' => new Basic('test', 'testing')]);
$this->assertTrue($token->hasClaim('test'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::hasClaim
*/
public function hasClaimMustReturnFalseWhenItIsNotConfigured()
{
$token = new Token([], ['test' => new Basic('test', 'testing')]);
$this->assertFalse($token->hasClaim('testing'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasClaim
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::getClaim
*/
public function getClaimMustReturnTheDefaultValueWhenIsNotConfigured()
{
$token = new Token([], ['test' => new Basic('test', 'testing')]);
$this->assertEquals('blah', $token->getClaim('testing', 'blah'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasClaim
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::getClaim
*
* @expectedException \OutOfBoundsException
*/
public function getClaimShouldRaiseExceptionWhenClaimIsNotConfigured()
{
$token = new Token();
$token->getClaim('testing');
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasClaim
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::getClaim
*/
public function getClaimShouldReturnTheClaimValueWhenItExists()
{
$token = new Token([], ['testing' => new Basic('testing', 'test')]);
$this->assertEquals('test', $token->getClaim('testing'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::verify
*
* @expectedException BadMethodCallException
*/
public function verifyMustRaiseExceptionWhenTokenIsUnsigned()
{
$signer = $this->getMock(Signer::class);
$token = new Token();
$token->verify($signer, 'test');
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::verify
* @covers Lcobucci\JWT\Token::getPayload
*/
public function verifyShouldReturnFalseWhenTokenAlgorithmIsDifferent()
{
$signer = $this->getMock(Signer::class);
$signature = $this->getMock(Signature::class, [], [], '', false);
$signer->expects($this->any())
->method('getAlgorithmId')
->willReturn('HS256');
$signature->expects($this->never())
->method('verify');
$token = new Token(['alg' => 'RS256'], [], $signature);
$this->assertFalse($token->verify($signer, 'test'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::verify
* @covers Lcobucci\JWT\Token::getPayload
*/
public function verifyMustDelegateTheValidationToSignature()
{
$signer = $this->getMock(Signer::class);
$signature = $this->getMock(Signature::class, [], [], '', false);
$signer->expects($this->any())
->method('getAlgorithmId')
->willReturn('HS256');
$signature->expects($this->once())
->method('verify')
->with($signer, $this->isType('string'), 'test')
->willReturn(true);
$token = new Token(['alg' => 'HS256'], [], $signature);
$this->assertTrue($token->verify($signer, 'test'));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\Token::validate
* @covers Lcobucci\JWT\Token::getValidatableClaims
*/
public function validateShouldReturnTrueWhenClaimsAreEmpty()
{
$token = new Token();
$this->assertTrue($token->validate(new ValidationData()));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
*
* @covers Lcobucci\JWT\Token::validate
* @covers Lcobucci\JWT\Token::getValidatableClaims
*/
public function validateShouldReturnTrueWhenThereAreNoValidatableClaims()
{
$token = new Token([], ['testing' => new Basic('testing', 'test')]);
$this->assertTrue($token->validate(new ValidationData()));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\ValidationData
* @uses Lcobucci\JWT\Claim\Basic
* @uses Lcobucci\JWT\Claim\EqualsTo
*
* @covers Lcobucci\JWT\Token::validate
* @covers Lcobucci\JWT\Token::getValidatableClaims
*/
public function validateShouldReturnFalseWhenThereIsAtLeastOneFailedValidatableClaim()
{
$token = new Token(
[],
[
'iss' => new EqualsTo('iss', 'test'),
'testing' => new Basic('testing', 'test')
]
);
$data = new ValidationData();
$data->setIssuer('test1');
$this->assertFalse($token->validate($data));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\ValidationData
* @uses Lcobucci\JWT\Claim\Basic
* @uses Lcobucci\JWT\Claim\EqualsTo
* @uses Lcobucci\JWT\Claim\LesserOrEqualsTo
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
*
* @covers Lcobucci\JWT\Token::validate
* @covers Lcobucci\JWT\Token::getValidatableClaims
*/
public function validateShouldReturnTrueWhenThereAreNoFailedValidatableClaims()
{
$now = time();
$token = new Token(
[],
[
'iss' => new EqualsTo('iss', 'test'),
'iat' => new LesserOrEqualsTo('iat', $now),
'exp' => new GreaterOrEqualsTo('exp', $now + 500),
'testing' => new Basic('testing', 'test')
]
);
$data = new ValidationData($now + 10);
$data->setIssuer('test');
$this->assertTrue($token->validate($data));
}
/**
* @test
*
* @covers Lcobucci\JWT\Token::isExpired
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::getClaim
* @uses Lcobucci\JWT\Token::hasClaim
*/
public function isExpiredShouldReturnFalseWhenTokenDoesNotExpires()
{
$token = new Token(['alg' => 'none']);
$this->assertFalse($token->isExpired());
}
/**
* @test
*
* @covers Lcobucci\JWT\Token::isExpired
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::getClaim
* @uses Lcobucci\JWT\Token::hasClaim
* @uses Lcobucci\JWT\Claim\Basic
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
*/
public function isExpiredShouldReturnFalseWhenTokenIsNotExpired()
{
$token = new Token(
['alg' => 'none'],
['exp' => new GreaterOrEqualsTo('exp', time() + 500)]
);
$this->assertFalse($token->isExpired());
}
/**
* @test
*
* @covers Lcobucci\JWT\Token::isExpired
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::getClaim
* @uses Lcobucci\JWT\Token::hasClaim
* @uses Lcobucci\JWT\Claim\Basic
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
*/
public function isExpiredShouldReturnTrueAfterTokenExpires()
{
$token = new Token(
['alg' => 'none'],
['exp' => new GreaterOrEqualsTo('exp', time())]
);
$this->assertTrue($token->isExpired(new DateTime('+10 days')));
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
*
* @covers Lcobucci\JWT\Token::getPayload
*/
public function getPayloadShouldReturnAStringWithTheTwoEncodePartsThatGeneratedTheToken()
{
$token = new Token(['alg' => 'none'], [], null, ['test1', 'test2', 'test3']);
$this->assertEquals('test1.test2', $token->getPayload());
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::getPayload
*
* @covers Lcobucci\JWT\Token::__toString
*/
public function toStringMustReturnEncodedDataWithEmptySignature()
{
$token = new Token(['alg' => 'none'], [], null, ['test', 'test']);
$this->assertEquals('test.test.', (string) $token);
}
/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::getPayload
*
* @covers Lcobucci\JWT\Token::__toString
*/
public function toStringMustReturnEncodedData()
{
$signature = $this->getMock(Signature::class, [], [], '', false);
$token = new Token(['alg' => 'none'], [], $signature, ['test', 'test', 'test']);
$this->assertEquals('test.test.test', (string) $token);
}
}

View File

@@ -0,0 +1,224 @@
<?php
/**
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/
namespace Lcobucci\JWT;
/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
* @since 2.0.0
*/
class ValidationDataTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*
* @covers Lcobucci\JWT\ValidationData::__construct
*/
public function constructorShouldConfigureTheItems()
{
$expected = $this->createExpectedData();
$data = new ValidationData(1);
$this->assertAttributeSame($expected, 'items', $data);
}
/**
* @test
*
* @dataProvider claimValues
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setId
*/
public function setIdShouldChangeTheId($id)
{
$expected = $this->createExpectedData($id);
$data = new ValidationData(1);
$data->setId($id);
$this->assertAttributeSame($expected, 'items', $data);
}
/**
* @test
*
* @dataProvider claimValues
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setIssuer
*/
public function setIssuerShouldChangeTheIssuer($iss)
{
$expected = $this->createExpectedData(null, null, $iss);
$data = new ValidationData(1);
$data->setIssuer($iss);
$this->assertAttributeSame($expected, 'items', $data);
}
/**
* @test
*
* @dataProvider claimValues
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setAudience
*/
public function setAudienceShouldChangeTheAudience($aud)
{
$expected = $this->createExpectedData(null, null, null, $aud);
$data = new ValidationData(1);
$data->setAudience($aud);
$this->assertAttributeSame($expected, 'items', $data);
}
/**
* @test
*
* @dataProvider claimValues
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setSubject
*/
public function setSubjectShouldChangeTheSubject($sub)
{
$expected = $this->createExpectedData(null, $sub);
$data = new ValidationData(1);
$data->setSubject($sub);
$this->assertAttributeSame($expected, 'items', $data);
}
/**
* @test
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setCurrentTime
*/
public function setCurrentTimeShouldChangeTheTimeBasedValues()
{
$expected = $this->createExpectedData(null, null, null, null, 2);
$data = new ValidationData(1);
$data->setCurrentTime(2);
$this->assertAttributeSame($expected, 'items', $data);
}
/**
* @test
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::has
*/
public function hasShouldReturnTrueWhenItemIsNotEmpty()
{
$data = new ValidationData(1);
$this->assertTrue($data->has('iat'));
}
/**
* @test
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::has
*/
public function hasShouldReturnFalseWhenItemIsEmpty()
{
$data = new ValidationData(1);
$this->assertFalse($data->has('jti'));
}
/**
* @test
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::has
*/
public function hasShouldReturnFalseWhenItemIsNotDefined()
{
$data = new ValidationData(1);
$this->assertFalse($data->has('test'));
}
/**
* @test
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::get
*/
public function getShouldReturnTheItemValue()
{
$data = new ValidationData(1);
$this->assertEquals(1, $data->get('iat'));
}
/**
* @test
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::get
*/
public function getShouldReturnNullWhenItemIsNotDefined()
{
$data = new ValidationData(1);
$this->assertNull($data->get('test'));
}
/**
* @return array
*/
public function claimValues()
{
return [
[1],
['test']
];
}
/**
* @param string $id
* @param string $sub
* @param string $iss
* @param string $aud
* @param int $time
*
* @return array
*/
private function createExpectedData(
$id = null,
$sub = null,
$iss = null,
$aud = null,
$time = 1
) {
return [
'jti' => $id !== null ? (string) $id : null,
'iss' => $iss !== null ? (string) $iss : null,
'aud' => $aud !== null ? (string) $aud : null,
'sub' => $sub !== null ? (string) $sub : null,
'iat' => $time,
'nbf' => $time,
'exp' => $time
];
}
}