Skip to content

Commit 3ff445b

Browse files
Revert "encrypt / decrypt the device code"
This reverts commit c9639be.
1 parent c9639be commit 3ff445b

File tree

6 files changed

+57
-179
lines changed

6 files changed

+57
-179
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99
- Support for PHP 8.4 (PR #1454)
1010

1111
### Fixed
12-
- Fixed device code encryption / decryption and bug where scopes were not set on access token when using device authorization grant (PR #1412)
12+
- Fixed bug where scopes were not set on access token when using device authorization grant (PR #1412)
1313

1414
## [9.0.1] - released 2024-10-14
1515
### Fixed

examples/src/Repositories/DeviceCodeRepository.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface;
1818
use OAuth2ServerExamples\Entities\ClientEntity;
1919
use OAuth2ServerExamples\Entities\DeviceCodeEntity;
20+
use OAuth2ServerExamples\Entities\ScopeEntity;
2021

2122
class DeviceCodeRepository implements DeviceCodeRepositoryInterface
2223
{
@@ -49,6 +50,14 @@ public function getDeviceCodeEntityByDeviceCode($deviceCode): ?DeviceCodeEntityI
4950
$deviceCodeEntity->setIdentifier($deviceCode);
5051
$deviceCodeEntity->setExpiryDateTime(new DateTimeImmutable('now +1 hour'));
5152
$deviceCodeEntity->setClient($clientEntity);
53+
$deviceCodeEntity->setLastPolledAt(new DateTimeImmutable());
54+
55+
$scopes = [];
56+
foreach ($scopes as $scope) {
57+
$scopeEntity = new ScopeEntity();
58+
$scopeEntity->setIdentifier($scope);
59+
$deviceCodeEntity->addScope($scopeEntity);
60+
}
5261

5362
// The user identifier should be set when the user authenticates on the
5463
// OAuth server, along with whether they approved the request

src/Grant/DeviceCodeGrant.php

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use League\OAuth2\Server\RequestEvent;
2929
use League\OAuth2\Server\ResponseTypes\DeviceCodeResponse;
3030
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
31-
use LogicException;
3231
use Psr\Http\Message\ServerRequestInterface;
3332
use TypeError;
3433

@@ -97,7 +96,6 @@ public function respondToDeviceAuthorizationRequest(ServerRequestInterface $requ
9796
);
9897

9998
$response = new DeviceCodeResponse();
100-
$response->setEncryptionKey($this->encryptionKey);
10199

102100
if ($this->includeVerificationUriComplete === true) {
103101
$response->includeVerificationUriComplete();
@@ -179,58 +177,38 @@ public function respondToAccessTokenRequest(
179177
*/
180178
protected function validateDeviceCode(ServerRequestInterface $request, ClientEntityInterface $client): DeviceCodeEntityInterface
181179
{
182-
$encryptedDeviceCode = $this->getRequestParameter('device_code', $request);
180+
$deviceCode = $this->getRequestParameter('device_code', $request);
183181

184-
if (is_null($encryptedDeviceCode)) {
182+
if (is_null($deviceCode)) {
185183
throw OAuthServerException::invalidRequest('device_code');
186184
}
187185

188-
try {
189-
$deviceCodePayload = json_decode($this->decrypt($encryptedDeviceCode));
190-
} catch (LogicException $e) {
191-
throw OAuthServerException::invalidRequest('code', 'Cannot decrypt the device code', $e);
192-
}
186+
$deviceCodeEntity = $this->deviceCodeRepository->getDeviceCodeEntityByDeviceCode(
187+
$deviceCode
188+
);
193189

194-
if (!property_exists($deviceCodePayload, 'device_code_id')) {
195-
throw OAuthServerException::invalidRequest('device_code', 'Device code malformed');
190+
if ($deviceCodeEntity instanceof DeviceCodeEntityInterface === false) {
191+
$this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));
192+
193+
throw OAuthServerException::invalidGrant();
196194
}
197195

198-
if (time() > $deviceCodePayload->expire_time) {
196+
if (time() > $deviceCodeEntity->getExpiryDateTime()->getTimestamp()) {
199197
throw OAuthServerException::expiredToken('device_code');
200198
}
201199

202-
if ($this->deviceCodeRepository->isDeviceCodeRevoked($deviceCodePayload->device_code_id) === true) {
200+
if ($this->deviceCodeRepository->isDeviceCodeRevoked($deviceCode) === true) {
203201
throw OAuthServerException::invalidRequest('device_code', 'Device code has been revoked');
204202
}
205203

206-
if ($deviceCodePayload->client_id !== $client->getIdentifier()) {
204+
if ($deviceCodeEntity->getClient()->getIdentifier() !== $client->getIdentifier()) {
207205
throw OAuthServerException::invalidRequest('device_code', 'Device code was not issued to this client');
208206
}
209207

210-
$deviceCodeEntity = $this->deviceCodeRepository->getDeviceCodeEntityByDeviceCode(
211-
$deviceCodePayload->device_code_id
212-
);
213-
214-
if ($deviceCodeEntity instanceof DeviceCodeEntityInterface === false) {
215-
$this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));
216-
217-
throw OAuthServerException::invalidGrant();
218-
}
219-
220208
if ($this->deviceCodePolledTooSoon($deviceCodeEntity->getLastPolledAt()) === true) {
221209
throw OAuthServerException::slowDown();
222210
}
223211

224-
$deviceCodeEntity->setIdentifier($deviceCodePayload->device_code_id);
225-
$deviceCodeEntity->setClient($client);
226-
$deviceCodeEntity->setExpiryDateTime((new DateTimeImmutable())->setTimestamp($deviceCodePayload->expire_time));
227-
228-
$scopes = $this->validateScopes($deviceCodePayload->scopes);
229-
230-
foreach ($scopes as $scope) {
231-
$deviceCodeEntity->addScope($scope);
232-
}
233-
234212
return $deviceCodeEntity;
235213
}
236214

src/ResponseTypes/DeviceCodeResponse.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,8 @@ public function generateHttpResponse(ResponseInterface $response): ResponseInter
3434
{
3535
$expireDateTime = $this->deviceCodeEntity->getExpiryDateTime()->getTimestamp();
3636

37-
$payload = [
38-
'client_id' => $this->deviceCodeEntity->getClient()->getIdentifier(),
39-
'device_code_id' => $this->deviceCodeEntity->getIdentifier(),
40-
'scopes' => $this->deviceCodeEntity->getScopes(),
41-
'expire_time' => $expireDateTime,
42-
];
43-
44-
$jsonPayload = json_encode($payload);
45-
46-
if ($jsonPayload === false) {
47-
throw new LogicException('An error was encountered when JSON encoding the device code request response');
48-
}
49-
5037
$responseParams = [
51-
'device_code' => $this->encrypt($jsonPayload),
38+
'device_code' => $this->deviceCodeEntity->getIdentifier(),
5239
'user_code' => $this->deviceCodeEntity->getUserCode(),
5340
'verification_uri' => $this->deviceCodeEntity->getVerificationUri(),
5441
'expires_in' => $expireDateTime - time(),

0 commit comments

Comments
 (0)