Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Commit

Permalink
Add extra timestamp checks
Browse files Browse the repository at this point in the history
Check for INT and bigger than 0 (not negative)
  • Loading branch information
nleroy committed Sep 16, 2015
1 parent 044b757 commit 68ca77e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
Empty file removed src/.empty
Empty file.
27 changes: 27 additions & 0 deletions src/NonceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ private function noncesRedisKey(ConsumerInterface $consumer, $timestamp)
*/
public function checkNonceAndTimestampUnicity($nonce, $timestamp, ConsumerInterface $consumer)
{
// Check timestamp: The timestamp value MUST be a positive integer
// and MUST be equal or greater than the timestamp used in previous requests.
// @see http://oauth.net/core/1.0/#nonce
if (!is_integer($timestamp)) {
throw new \InvalidArgumentException(
'Timestamp should be an integer, got ' . $this->checkPlain($timestamp)
);
}

if ($timestamp < 0) {
throw new \InvalidArgumentException(
'Timestamp should be a positive number bigger than 0, got ' . $this->checkPlain($timestamp)
);
}

//$maxTimestamp = $this->client->
/*if ($timestamp < $maxTimestamp) {
throw new \InvalidArgumentException(
'Timestamp must be bigger than the last timestamp we have recorded'
);
}*/

$noncesRedisKey = $this->noncesRedisKey($consumer, $timestamp);
$exists = $this->client->sismember($noncesRedisKey, $nonce);

Expand All @@ -82,4 +104,9 @@ public function registerNonceAndTimestamp($nonce, $timestamp, ConsumerInterface

return true;
}

protected function checkPlain($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
}
Empty file removed tests/.empty
Empty file.
51 changes: 51 additions & 0 deletions tests/NonceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,55 @@ public function it_only_accepts_integers_bigger_than_zero_for_ttl($invalidTTLVal
$this->setExpectedException(\InvalidArgumentException::class);
new NonceProvider($this->client, $invalidTTLValue);
}

/**
* @test
*/
public function it_throws_an_error_when_using_a_text_timestamp()
{
$this->setExpectedException(
'InvalidArgumentException',
'Timestamp should be an integer, got abcdef'
);

$this->nonceProvider->checkNonceAndTimestampUnicity(
'foo',
'abcdef',
$this->consumer
);
}

/**
* @test
*/
public function it_throws_an_error_when_using_a_negative_timestamp()
{
$this->setExpectedException(
'InvalidArgumentException',
'Timestamp should be a positive number bigger than 0, got -123456'
);

$this->nonceProvider->checkNonceAndTimestampUnicity(
'foo',
-123456,
$this->consumer
);
}

/**
* @test
*/
public function it_throws_an_error_when_using_a_decimal_timestamp()
{
$this->setExpectedException(
'InvalidArgumentException',
'Timestamp should be an integer, got 1234.56'
);

$this->nonceProvider->checkNonceAndTimestampUnicity(
'foo',
1234.56,
$this->consumer
);
}
}

0 comments on commit 68ca77e

Please sign in to comment.