Skip to content

Commit

Permalink
revert getting interval from device code entity
Browse files Browse the repository at this point in the history
  • Loading branch information
hafezdivandari committed Nov 14, 2024
1 parent ca53def commit 3f09114
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions examples/src/Repositories/DeviceCodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public function getDeviceCodeEntityByDeviceCode($deviceCode): ?DeviceCodeEntityI
$deviceCodeEntity->setIdentifier($deviceCode);
$deviceCodeEntity->setExpiryDateTime(new DateTimeImmutable('now +1 hour'));
$deviceCodeEntity->setClient($clientEntity);
$deviceCodeEntity->setLastPolledAt(new DateTimeImmutable('now -5 second'));
$deviceCodeEntity->setInterval(5);
$deviceCodeEntity->setLastPolledAt(new DateTimeImmutable());

$scopes = [];
foreach ($scopes as $scope) {
Expand Down
10 changes: 5 additions & 5 deletions src/Grant/DeviceCodeGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ public function respondToAccessTokenRequest(

// If device code has no user associated, respond with pending or slow down
if (is_null($deviceCodeEntity->getUserIdentifier())) {
$shouldSlowDown = $this->deviceCodePolledTooSoon($deviceCodeEntity->getLastPolledAt());

$deviceCodeEntity->setLastPolledAt(new DateTimeImmutable());
$this->deviceCodeRepository->persistDeviceCode($deviceCodeEntity);

if ($this->deviceCodePolledTooSoon($deviceCodeEntity) === true) {
if ($shouldSlowDown) {
throw OAuthServerException::slowDown();
}

Expand Down Expand Up @@ -216,11 +218,9 @@ protected function validateDeviceCode(ServerRequestInterface $request, ClientEnt
return $deviceCodeEntity;
}

private function deviceCodePolledTooSoon(DeviceCodeEntityInterface $deviceCodeEntity): bool
private function deviceCodePolledTooSoon(?DateTimeImmutable $lastPoll): bool
{
$lastPoll = $deviceCodeEntity->getLastPolledAt();

return $lastPoll !== null && $lastPoll->getTimestamp() + $deviceCodeEntity->getInterval() > time();
return $lastPoll !== null && $lastPoll->getTimestamp() + $this->defaultInterval > time();
}

/**
Expand Down

0 comments on commit 3f09114

Please sign in to comment.