Skip to content

Commit

Permalink
Allow tokens with unlimited usages (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
yann-eugone authored Oct 12, 2018
1 parent 0d0ca9a commit faa8006
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
7 changes: 6 additions & 1 deletion Entity/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ public function isUsed()
*/
public function isConsumed()
{
return $this->getCountUsages() >= $this->getAllowedUsages();
$allowed = $this->getAllowedUsages();
if ($allowed === 0) {
return false;
}

return $this->getCountUsages() >= $allowed;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/2-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Each token can have following options :
- `generator` : a service id that implements [`Yokai\SecurityTokenBundle\Generator\TokenGeneratorInterface`](../../Generator/TokenGeneratorInterface.php)
- `duration` : a valid [`DateTime::modify`](https://php.net/manual/datetime.modify.php) argument that represent the validity duration for tokens of this type
- `usages` : an integer that represent the number of allowed usages for tokens of this type
- `usages` : an integer that represent the number of allowed usages for tokens of this type (`0` means unlimited)
- `keep` : a valid [`DateTime::modify`](https://php.net/manual/datetime.modify.php) argument that represent the keep duration for tokens of this type
- `unique` : a boolean that indicates whether or not the token must be unique per user

Expand Down
43 changes: 43 additions & 0 deletions Tests/Entity/TokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Yokai\SecurityTokenBundle\Tests\Entity;

use Yokai\SecurityTokenBundle\Entity\Token;

/**
* @author Yann Eugoné <[email protected]>
*/
class TokenTest extends \PHPUnit_Framework_TestCase
{
public function testLimitedUsagesToken()
{
$token = new Token('string', 'jdoe', 'unique-token', 'reset-password', '+1 day', '+1 month', 2);
self::assertFalse($token->isConsumed());

$token->consume([1]);
self::assertFalse($token->isConsumed());
$token->consume([2]);
self::assertTrue($token->isConsumed());

self::assertCount(2, $token->getUsages());
self::assertSame(2, $token->getCountUsages());
self::assertSame([2], $token->getLastUsage()->getInformation());
}

public function testUnlimitedUsagesToken()
{
$token = new Token('string', 'jdoe', 'unique-token', 'reset-password', '+1 day', '+1 month', 0);
self::assertFalse($token->isConsumed());

$token->consume([1]);
self::assertFalse($token->isConsumed());
$token->consume([2]);
self::assertFalse($token->isConsumed());
$token->consume([3]);
self::assertFalse($token->isConsumed());

self::assertCount(3, $token->getUsages());
self::assertSame(3, $token->getCountUsages());
self::assertSame([3], $token->getLastUsage()->getInformation());
}
}

0 comments on commit faa8006

Please sign in to comment.