Set access tokens TTL depending on the requested scopes

This commit is contained in:
ErickSkrauch
2019-12-06 19:07:08 +03:00
parent f0a73f2b7a
commit efb97a2006
4 changed files with 41 additions and 5 deletions

View File

@@ -5,6 +5,8 @@ namespace api\tests\unit\components\OAuth2\Entities;
use api\components\OAuth2\Entities\AccessTokenEntity;
use api\tests\unit\TestCase;
use DateInterval;
use DateTimeImmutable;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
@@ -17,7 +19,7 @@ class AccessTokenEntityTest extends TestCase {
$entity = new AccessTokenEntity();
$entity->setClient($client);
$entity->setExpiryDateTime(new \DateTimeImmutable());
$entity->setExpiryDateTime(new DateTimeImmutable());
$entity->addScope($this->createScopeEntity('first'));
$entity->addScope($this->createScopeEntity('second'));
$entity->addScope($this->createScopeEntity('offline_access'));
@@ -33,6 +35,24 @@ class AccessTokenEntityTest extends TestCase {
$this->assertSame('offline_access', $scopes[2]->getIdentifier());
}
public function testGetExpiryDateTime() {
$initialExpiry = (new DateTimeImmutable())->add(new DateInterval('P1D'));
$entity = new AccessTokenEntity();
$entity->setExpiryDateTime($initialExpiry);
$this->assertSame($initialExpiry, $entity->getExpiryDateTime());
$entity = new AccessTokenEntity();
$entity->setExpiryDateTime($initialExpiry);
$entity->addScope($this->createScopeEntity('change_skin'));
$this->assertEqualsWithDelta(time() + 60 * 60, $entity->getExpiryDateTime()->getTimestamp(), 5);
$entity = new AccessTokenEntity();
$entity->setExpiryDateTime($initialExpiry);
$entity->addScope($this->createScopeEntity('obtain_account_email'));
$this->assertEqualsWithDelta(time() + 60 * 60, $entity->getExpiryDateTime()->getTimestamp(), 5);
}
private function createScopeEntity(string $id): ScopeEntityInterface {
/** @var ScopeEntityInterface|\PHPUnit\Framework\MockObject\MockObject $entity */
$entity = $this->createMock(ScopeEntityInterface::class);