Skip to content

Commit

Permalink
Update Repository to use getRefreshExpireInterval from config when st…
Browse files Browse the repository at this point in the history
…oring refresh token to redis
  • Loading branch information
Horat1us committed Nov 29, 2023
1 parent 53ba337 commit 45215ed
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,21 @@ public function create(int $userId): Token
(string)$this->factory->uuid4(),
(string)$this->factory->uuid4()
);
$expire = Carbon::now()->add($this->config->getExpireInterval($userId))
$expireAccess = Carbon::now()->add($this->config->getExpireInterval($userId))
->diffInSeconds();
$expireRefresh = Carbon::now()->add($this->config->getRefreshExpireInterval($userId))
->diffInSeconds();

$this->redis->multi();

$this->redis->setex(
$accessKey = $this->getAccessKey($token->getAccess()),
$expire,
$expireAccess,
$userId
);
$this->redis->setex(
$refreshKey = $this->getRefreshKey($token->getRefresh()),
$expire,
$expireRefresh,
$token->getAccess()
);

Expand Down
45 changes: 42 additions & 3 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,31 @@ public function testTtlWithNoValueInRedis(): void
);
}

public function testTtlWithInvalidValue(): void
{
$repository = new Authorization\Repository([
'redis' => $redis = $this->createMock(redis\Connection::class),
'config' => $this->createMock(Authorization\Config::class),
'factory' => new UuidFactory(),
]);

/** @noinspection PhpUnhandledExceptionInspection */
$token = "NOT_UUID";

$redis
->expects($this->never())
->method('__call')
->with(
$this->equalTo('ttl'),
$this->equalTo(["access-{$token}"])
);


$this->assertNull(
$repository->ttl((string)$token)
);
}

public function testDeleteWithInvalidToken(): void
{
$repository = new Authorization\Repository([
Expand Down Expand Up @@ -268,13 +293,18 @@ public function testCreatingValidUuidTokenPaid(): void

$userId = 13371;
$expireInterval = 30;
$refreshExpireInterval = 90;

/** @noinspection PhpUnhandledExceptionInspection */
$config
->expects($this->once())
->method('getExpireInterval')
->with($userId)
->willReturn(new \DateInterval('PT' . $expireInterval . 'S'));
$config
->expects($this->once())
->method('getRefreshExpireInterval')
->with($userId)
->willReturn(new \DateInterval('PT' . $refreshExpireInterval . 'S'));

$factory
->expects($this->exactly(2))
Expand All @@ -284,7 +314,16 @@ public function testCreatingValidUuidTokenPaid(): void
$redis->expects($this->exactly(4))
->method('__call')
->willReturnCallback(
function (string $method, array $keys) use ($access, $refresh, $expireInterval, $userId) {
function (
string $method,
array $keys
) use (
$access,
$refresh,
$expireInterval,
$refreshExpireInterval,
$userId
) {
switch ($method) {
case "multi":
case "exec":
Expand All @@ -295,7 +334,7 @@ function (string $method, array $keys) use ($access, $refresh, $expireInterval,
[$key, $expire, $value] = $keys;
switch ($key) {
case "refresh-{$refresh}":
$this->assertEquals($expireInterval, $expire);
$this->assertEquals($refreshExpireInterval, $expire);
$this->assertEquals($access, $value);
return null;
case "access-{$access}":
Expand Down

0 comments on commit 45215ed

Please sign in to comment.