Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Mar 18, 2014
2 parents 6568cc3 + 696633b commit 28edaa4
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 15 deletions.
30 changes: 15 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
verbose="true"
strict="true">
<testsuites>
<testsuite name="JWT Test Suite">
Expand Down
40 changes: 40 additions & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,46 @@ public function verify($key)
return $this->signature->verify($this->getPayload(), $key);
}

/**
* Validates if the token is valid
*
* @param string $issuer
* @param string $audience
* @param string $subject
* @param int $currentTime
* @return boolean
*/
public function validate(
$issuer = null,
$audience = null,
$subject = null,
$currentTime = null
) {
$currentTime = $currentTime ?: time();

if (isset($this->claims['iss']) && $this->claims['iss'] != $issuer) {
return false;
}

if (isset($this->claims['aud']) && $this->claims['aud'] != $audience) {
return false;
}

if (isset($this->claims['sub']) && $this->claims['sub'] != $subject) {
return false;
}

if (isset($this->claims['nbf']) && $this->claims['nbf'] > $currentTime) {
return false;
}

if (isset($this->claims['exp']) && $this->claims['exp'] < $currentTime) {
return false;
}

return true;
}

/**
* Returns the token payload
*
Expand Down
95 changes: 95 additions & 0 deletions test/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,101 @@ public function verifyMustDelegateTheValidationToSignature()
$this->assertTrue($token->verify('test'));
}

/**
* @test
* @covers ::__construct
* @covers ::validate
*/
public function validateShouldReturnTrueWhenClaimsAreEmpty()
{
$token = new Token();

$this->assertTrue($token->validate());
}

/**
* @test
* @covers ::__construct
* @covers ::validate
*/
public function validateShouldReturnFalseWhenIssuerIsDiferentThanTheGivenOne()
{
$token = new Token([], ['iss' => 'test']);

$this->assertFalse($token->validate('test1'));
}

/**
* @test
* @covers ::__construct
* @covers ::validate
*/
public function validateShouldReturnFalseWhenAudienceIsDiferentThanTheGivenOne()
{
$token = new Token([], ['aud' => 'test']);

$this->assertFalse($token->validate(null, 'test1'));
}

/**
* @test
* @covers ::__construct
* @covers ::validate
*/
public function validateShouldReturnFalseWhenSubjectIsDiferentThanTheGivenOne()
{
$token = new Token([], ['sub' => 'test']);

$this->assertFalse($token->validate(null, null, 'test1'));
}

/**
* @test
* @covers ::__construct
* @covers ::validate
*/
public function validateShouldReturnFalseWhenTokenCannotYetBeUsed()
{
$token = new Token([], ['nbf' => strtotime('+2 hours')]);

$this->assertFalse($token->validate(null, null, null, time()));
}

/**
* @test
* @covers ::__construct
* @covers ::validate
*/
public function validateShouldReturnFalseWhenTokenIsExpired()
{
$token = new Token([], ['exp' => time()]);

$this->assertFalse($token->validate(null, null, null, strtotime('+2 hours')));
}

/**
* @test
* @covers ::__construct
* @covers ::validate
*/
public function validateShouldReturnTrueWhenAllInformationsAreRight()
{
$token = new Token(
[],
[
'iss' => 'test0',
'aud' => 'test1',
'sub' => 'test2',
'nbf' => time(),
'exp' => strtotime('+3 hours')
]
);

$this->assertTrue(
$token->validate('test0', 'test1', 'test2', strtotime('+1 hours'))
);
}

/**
* @test
* @covers ::__construct
Expand Down

0 comments on commit 28edaa4

Please sign in to comment.