Skip to content

Commit 047b46f

Browse files
committed
Add Device Code tests
1 parent 1dfe5f9 commit 047b46f

16 files changed

+98
-25
lines changed

config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
service(AccessTokenManagerInterface::class),
303303
service(RefreshTokenManagerInterface::class),
304304
service(AuthorizationCodeManagerInterface::class),
305+
service(DeviceCodeManagerInterface::class),
305306
])
306307
->tag('console.command', ['command' => 'league:oauth2-server:clear-expired-tokens'])
307308
->alias(ClearExpiredTokensCommand::class, 'league.oauth2_server.command.clear_expired_tokens')

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ For implementation into Symfony projects, please see [bundle documentation](basi
66

77
## Features
88

9-
* API endpoint for client authorization and token issuing
9+
* API endpoint for client authorization, device code and token issuing
1010
* Configurable client and token persistance (includes [Doctrine](https://www.doctrine-project.org/) support)
1111
* Integration with Symfony's [Security](https://symfony.com/doc/current/security.html) layer
1212

src/Command/ClearExpiredTokensCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function configure(): void
7676
)
7777
->addOption(
7878
'device-codes',
79-
'c',
79+
'dc',
8080
InputOption::VALUE_NONE,
8181
'Clear expired device codes.'
8282
)

src/Manager/InMemory/DeviceCodeManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class DeviceCodeManager implements DeviceCodeManagerInterface
1616

1717
public function find(string $identifier): ?DeviceCodeInterface
1818
{
19-
return $this->accessTokens[$identifier] ?? null;
19+
return $this->deviceCodes[$identifier] ?? null;
2020
}
2121

2222
public function findByUserCode(string $code): ?DeviceCodeInterface

src/Service/CredentialsRevoker/DoctrineCredentialsRevoker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public function revokeCredentialsForClient(AbstractClient $client): void
122122

123123
$this->entityManager->createQueryBuilder()
124124
->update(DeviceCode::class, 'dc')
125-
->set('ac.revoked', ':revoked')
126-
->where('ad.client = :client')
125+
->set('dc.revoked', ':revoked')
126+
->where('dc.client = :client')
127127
->setParameter('client', $doctrineClient->getIdentifier(), 'string')
128128
->setParameter('revoked', true)
129129
->getQuery()

tests/Acceptance/AuthorizationEndpointTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface;
99
use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface;
1010
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
11+
use League\Bundle\OAuth2ServerBundle\Manager\DeviceCodeManagerInterface;
1112
use League\Bundle\OAuth2ServerBundle\Manager\RefreshTokenManagerInterface;
1213
use League\Bundle\OAuth2ServerBundle\Manager\ScopeManagerInterface;
1314
use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode;
@@ -27,7 +28,8 @@ protected function setUp(): void
2728
$this->client->getContainer()->get(ClientManagerInterface::class),
2829
$this->client->getContainer()->get(AccessTokenManagerInterface::class),
2930
$this->client->getContainer()->get(RefreshTokenManagerInterface::class),
30-
$this->client->getContainer()->get(AuthorizationCodeManagerInterface::class)
31+
$this->client->getContainer()->get(AuthorizationCodeManagerInterface::class),
32+
$this->client->getContainer()->get(DeviceCodeManagerInterface::class)
3133
);
3234
}
3335

tests/Acceptance/CustomPersistenceManagerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface;
99
use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface;
1010
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
11+
use League\Bundle\OAuth2ServerBundle\Manager\DeviceCodeManagerInterface;
1112
use League\Bundle\OAuth2ServerBundle\Manager\RefreshTokenManagerInterface;
1213
use League\Bundle\OAuth2ServerBundle\Model\AccessToken;
1314
use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode;
@@ -19,6 +20,7 @@
1920
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeAuthorizationCodeManager;
2021
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeClientManager;
2122
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeCredentialsRevoker;
23+
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeDeviceCodeManager;
2224
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeRefreshTokenManager;
2325
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FixtureFactory;
2426
use League\Bundle\OAuth2ServerBundle\Tests\TestHelper;
@@ -34,6 +36,7 @@ class CustomPersistenceManagerTest extends AbstractAcceptanceTest
3436
private ClientManagerInterface&MockObject $clientManager;
3537
private RefreshTokenManagerInterface&MockObject $refreshTokenManager;
3638
private AuthorizationCodeManagerInterface&MockObject $authCodeManager;
39+
private DeviceCodeManagerInterface&MockObject $deviceCodeManager;
3740

3841
protected function setUp(): void
3942
{
@@ -42,6 +45,7 @@ protected function setUp(): void
4245
$this->clientManager = $this->createMock(ClientManagerInterface::class);
4346
$this->refreshTokenManager = $this->createMock(RefreshTokenManagerInterface::class);
4447
$this->authCodeManager = $this->createMock(AuthorizationCodeManagerInterface::class);
48+
$this->deviceCodeManager = $this->createMock(DeviceCodeManagerInterface::class);
4549
$this->application = new Application($this->client->getKernel());
4650
}
4751

@@ -52,6 +56,7 @@ public function testRegisteredServices(): void
5256
static::assertInstanceOf(FakeClientManager::class, $this->client->getContainer()->get(ClientManagerInterface::class));
5357
static::assertInstanceOf(FakeRefreshTokenManager::class, $this->client->getContainer()->get(RefreshTokenManagerInterface::class));
5458
static::assertInstanceOf(FakeCredentialsRevoker::class, $this->client->getContainer()->get(CredentialsRevokerInterface::class));
59+
static::assertInstanceOf(FakeDeviceCodeManager::class, $this->client->getContainer()->get(DeviceCodeManagerInterface::class));
5560
}
5661

5762
public function testSuccessfulClientCredentialsRequest(): void
@@ -154,6 +159,25 @@ public function testSuccessfulAuthorizationCodeRequest(): void
154159
static::assertResponseIsSuccessful();
155160
}
156161

162+
public function testSuccessfullDeviceCodeRequest(): void
163+
{
164+
$client = new Client('name', 'foo', 'secret');
165+
166+
$this->deviceCodeManager->expects(self::atLeastOnce())->method('find')->willReturn(null);
167+
$this->deviceCodeManager->expects(self::atLeastOnce())->method('save');
168+
$this->client->getContainer()->set('test.device_code_manager', $this->deviceCodeManager);
169+
170+
$this->clientManager->expects(self::atLeastOnce())->method('find')->with('foo')->willReturn($client);
171+
$this->client->getContainer()->set('test.client_manager', $this->clientManager);
172+
173+
$this->client->request('POST', '/device-code', [
174+
'client_id' => $client->getIdentifier(),
175+
]);
176+
177+
$this->client->getResponse();
178+
static::assertResponseIsSuccessful();
179+
}
180+
157181
protected static function createKernel(array $options = []): KernelInterface
158182
{
159183
return new TestKernel(
@@ -167,6 +191,7 @@ protected static function createKernel(array $options = []): KernelInterface
167191
'client_manager' => 'test.client_manager',
168192
'refresh_token_manager' => 'test.refresh_token_manager',
169193
'credentials_revoker' => 'test.credentials_revoker',
194+
'device_code_manager' => 'test.device_code_manager',
170195
],
171196
]
172197
);

tests/Acceptance/DeviceCodeEndpointTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@ public function testFailedWithUnkownClientRequest(): void
5656
$response = $this->client->getResponse();
5757

5858
$this->assertSame(401, $response->getStatusCode());
59-
$this->assertSame('application/json; charset=UTF-8', $response->headers->get('Content-Type'));
59+
$this->assertSame('application/json', $response->headers->get('Content-Type'));
6060

6161
$jsonResponse = json_decode($response->getContent(), true);
6262

6363
$this->assertNotEmpty($jsonResponse['error']);
6464
$this->assertNotEmpty($jsonResponse['error_description']);
6565
$this->assertSame('invalid_client', $jsonResponse['error']);
66-
$this->assertLessThanOrEqual(3600, $jsonResponse['expires_in']);
6766
}
6867

6968
}

tests/Acceptance/DoctrineDeviceCodeManagerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testClearExpired(): void
3838
$this->assertSame(3, $doctrineDeviceCodeManager->clearExpired());
3939

4040
$this->assertSame(
41-
$testData['output'],
41+
array_values($testData['output']),
4242
$em->getRepository(DeviceCode::class)->findBy([], ['identifier' => 'ASC'])
4343
);
4444
}
@@ -58,8 +58,8 @@ private function buildClearExpiredTestData($client): array
5858
];
5959

6060
return [
61-
'input' => $validDeviceCodes + $expiredDeviceCodes,
6261
'output' => $validDeviceCodes,
62+
'input' => $validDeviceCodes + $expiredDeviceCodes,
6363
];
6464
}
6565

tests/Acceptance/SecurityLayerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface;
88
use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface;
99
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
10+
use League\Bundle\OAuth2ServerBundle\Manager\DeviceCodeManagerInterface;
1011
use League\Bundle\OAuth2ServerBundle\Manager\RefreshTokenManagerInterface;
1112
use League\Bundle\OAuth2ServerBundle\Manager\ScopeManagerInterface;
1213
use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FixtureFactory;
@@ -23,7 +24,8 @@ protected function setUp(): void
2324
$this->client->getContainer()->get(ClientManagerInterface::class),
2425
$this->client->getContainer()->get(AccessTokenManagerInterface::class),
2526
$this->client->getContainer()->get(RefreshTokenManagerInterface::class),
26-
$this->client->getContainer()->get(AuthorizationCodeManagerInterface::class)
27+
$this->client->getContainer()->get(AuthorizationCodeManagerInterface::class),
28+
$this->client->getContainer()->get(DeviceCodeManagerInterface::class)
2729
);
2830
}
2931

0 commit comments

Comments
 (0)