Skip to content

Commit

Permalink
add ability to set interval
Browse files Browse the repository at this point in the history
  • Loading branch information
Sephster committed Mar 5, 2024
1 parent 4e9de6e commit 0636d25
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 25 deletions.
4 changes: 0 additions & 4 deletions src/Entities/DeviceCodeEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public function getInterval(): int;

public function setInterval(int $interval): void;

public function getIntervalInAuthResponse(): bool;

public function setIntervalInAuthResponse(bool $intervalInAuthResponse): void;

public function getUserApproved(): bool;

public function setUserApproved(bool $userApproved): void;
Expand Down
11 changes: 0 additions & 11 deletions src/Entities/Traits/DeviceCodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
trait DeviceCodeTrait
{
private bool $userApproved = false;
private bool $intervalInAuthResponse = false;
private bool $includeVerificationUriComplete = false;
private int $interval = 5;
private string $userCode;
Expand Down Expand Up @@ -82,16 +81,6 @@ public function setInterval(int $interval): void
$this->interval = $interval;
}

public function getIntervalInAuthResponse(): bool
{
return $this->intervalInAuthResponse;
}

public function setIntervalInAuthResponse(bool $intervalInAuthResponse): void
{
$this->intervalInAuthResponse = $intervalInAuthResponse;
}

public function getUserApproved(): bool
{
return $this->userApproved;
Expand Down
3 changes: 2 additions & 1 deletion src/Grant/DeviceCodeGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function respondToDeviceAuthorizationRequest(ServerRequestInterface $requ
'expire_time' => $deviceCode->getExpiryDateTime()->getTimestamp(),
'client_id' => $deviceCode->getClient()->getIdentifier(),
'scopes' => $deviceCode->getScopes(),
'interval' => $deviceCode->getInterval(),
];

$response = new DeviceCodeResponse();
Expand Down Expand Up @@ -355,7 +356,7 @@ protected function generateUniqueUserCode(int $length = 8): string
// @codeCoverageIgnoreEnd
}

// TODO: Check interface
// TODO: Check interface
public function setIntervalVisibility(bool $intervalVisibility): void
{
$this->intervalVisibility = $intervalVisibility;
Expand Down
5 changes: 3 additions & 2 deletions src/ResponseTypes/DeviceCodeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DeviceCodeResponse extends AbstractResponseType
protected DeviceCodeEntityInterface $deviceCode;
protected string $payload;
private bool $includeVerificationUriComplete = false;
private const DEFAULT_INTERVAL = 5;

/**
* {@inheritdoc}
Expand All @@ -40,12 +41,12 @@ public function generateHttpResponse(ResponseInterface $response): ResponseInter
'verification_uri' => $this->deviceCode->getVerificationUri(),
'expires_in' => $expireDateTime - time(),
];

if ($this->includeVerificationUriComplete === true) {
$responseParams['verification_uri_complete'] = $this->deviceCode->getVerificationUriComplete();
}

if ($this->deviceCode->getIntervalInAuthResponse() === true) {
if ($this->deviceCode->getInterval() !== self::DEFAULT_INTERVAL) {
$responseParams['interval'] = $this->deviceCode->getInterval();
}

Expand Down
13 changes: 6 additions & 7 deletions tests/Grant/DeviceCodeGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
class DeviceCodeGrantTest extends TestCase
{
private const DEFAULT_SCOPE = 'basic';
private const INTERVAL_RATE = 10;

protected CryptTraitStub $cryptStub;

Expand Down Expand Up @@ -335,7 +336,6 @@ public function testDeviceAuthorizationResponse(): void
$this::assertObjectHasProperty('user_code', $responseObject);
$this::assertObjectHasProperty('verification_uri', $responseObject);
$this::assertObjectHasProperty('expires_in', $responseObject);
// TODO: $this->assertObjectHasAttribute('interval', $responseObject);
}

public function testRespondToAccessTokenRequest(): void
Expand Down Expand Up @@ -674,7 +674,7 @@ public function testIssueExpiredTokenError(): void
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));
}

public function testIntervalVisibility(): void
public function testSettingDeviceCodeIntervalRate(): void
{
$client = new ClientEntity();
$client->setIdentifier('foo');
Expand All @@ -684,8 +684,6 @@ public function testIntervalVisibility(): void

$deviceCode = new DeviceCodeEntity();

$deviceCode->setIntervalInAuthResponse(true);

$deviceCodeRepository = $this->getMockBuilder(DeviceCodeRepositoryInterface::class)->getMock();
$deviceCodeRepository->method('getNewDeviceCode')->willReturn($deviceCode);

Expand All @@ -698,13 +696,15 @@ public function testIntervalVisibility(): void
$deviceCodeRepository,
$this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(),
new DateInterval('PT10M'),
"http://foo/bar"
"http://foo/bar",
self::INTERVAL_RATE
);

$grant->setClientRepository($clientRepositoryMock);
$grant->setDefaultScope(self::DEFAULT_SCOPE);
$grant->setEncryptionKey($this->cryptStub->getKey());
$grant->setScopeRepository($scopeRepositoryMock);
$grant->setIntervalVisibility(true);

$request = (new ServerRequest())->withParsedBody([
'client_id' => 'foo',
Expand All @@ -718,9 +718,8 @@ public function testIntervalVisibility(): void
$deviceCode = json_decode((string) $deviceCodeResponse->getBody());

$this::assertObjectHasProperty('interval', $deviceCode);
$this::assertEquals(5, $deviceCode->interval);
$this::assertEquals(self::INTERVAL_RATE, $deviceCode->interval);
}

public function testIssueAccessDeniedError(): void
{
$client = new ClientEntity();
Expand Down

0 comments on commit 0636d25

Please sign in to comment.