Skip to content

Commit

Permalink
Fix PHPStan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sephster committed Jan 17, 2024
1 parent 93157f4 commit 76cb886
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 186 deletions.
1 change: 0 additions & 1 deletion src/AuthorizationServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
use League\OAuth2\Server\RequestTypes\DeviceAuthorizationRequest;
use League\OAuth2\Server\ResponseTypes\AbstractResponseType;
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
Expand Down
3 changes: 1 addition & 2 deletions src/Grant/AbstractGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use League\OAuth2\Server\RequestEvent;
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
use League\OAuth2\Server\RequestTypes\DeviceAuthorizationRequest;
use League\OAuth2\Server\ResponseTypes\DeviceCodeResponse;
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
use LogicException;
Expand Down Expand Up @@ -552,7 +551,7 @@ public function respondToDeviceAuthorizationRequest(ServerRequestInterface $requ
/**
* {@inheritdoc}
*/
public function completeDeviceAuthorizationRequest(string $deviceCode, string|int $userId, bool $userApproved): void
public function completeDeviceAuthorizationRequest(string $deviceCode, string $userId, bool $userApproved): void
{
throw new LogicException('This grant cannot complete a device authorization request');
}
Expand Down
28 changes: 19 additions & 9 deletions src/Grant/DeviceCodeGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\RequestEvent;
use League\OAuth2\Server\RequestTypes\DeviceAuthorizationRequest;
use League\OAuth2\Server\ResponseTypes\DeviceCodeResponse;
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
use LogicException;
Expand Down Expand Up @@ -122,10 +121,19 @@ public function respondToDeviceAuthorizationRequest(ServerRequestInterface $requ
/**
* {@inheritdoc}
*/
public function completeDeviceAuthorizationRequest(string $deviceCode, string|int $userId, bool $approved): void
// TODO: Make sure this cant be abused to try and brute force a device code
public function completeDeviceAuthorizationRequest(string $deviceCode, string $userId, bool $approved): void
{
$deviceCode = $this->deviceCodeRepository->getDeviceCodeEntityByDeviceCode($deviceCode);

if ($deviceCode instanceof DeviceCodeEntityInterface === false) {
throw OAuthServerException::invalidRequest('device_code', 'Device code does not exist');
}

if ($userId === '') {
throw OAuthServerException::invalidRequest('user_id', 'User ID is required');
}

$deviceCode->setUserIdentifier($userId);
$deviceCode->setUserApproved($approved);

Expand Down Expand Up @@ -210,21 +218,19 @@ protected function validateDeviceCode(ServerRequestInterface $request, ClientEnt
}

$deviceCode = $this->deviceCodeRepository->getDeviceCodeEntityByDeviceCode(
$deviceCodePayload->device_code_id,
$this->getIdentifier(),
$client
$deviceCodePayload->device_code_id
);

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

if ($deviceCode instanceof DeviceCodeEntityInterface === false) {
$this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));

throw OAuthServerException::invalidGrant();
}

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

return $deviceCode;
}

Expand Down Expand Up @@ -312,6 +318,10 @@ protected function issueDeviceCode(
}
}
}


// This should never be hit. It is here to work around a PHPStan false error
return $deviceCode;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Grant/GrantTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
use League\OAuth2\Server\RequestTypes\DeviceAuthorizationRequest;
use League\OAuth2\Server\ResponseTypes\DeviceCodeResponse;
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -103,7 +102,7 @@ public function respondToDeviceAuthorizationRequest(ServerRequestInterface $requ
*
* If the validation is successful a DeviceCode object is persisted.
*/
public function completeDeviceAuthorizationRequest(string $deviceCode, string|int $userId, bool $userApproved): void;
public function completeDeviceAuthorizationRequest(string $deviceCode, string $userId, bool $userApproved): void;

/**
* Set the client repository.
Expand Down
40 changes: 0 additions & 40 deletions src/Middleware/DeviceGrantMiddleware.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/Repositories/DeviceCodeRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ public function getNewDeviceCode(): DeviceCodeEntityInterface;
/**
* Persists a device code to permanent storage.
*
*
* @throws UniqueTokenIdentifierConstraintViolationException
*/
public function persistDeviceCode(DeviceCodeEntityInterface $deviceCodeEntity): void;

/**
* Get a device code entity.
*
*
*/
public function getDeviceCodeEntityByDeviceCode(
string $deviceCode
Expand Down
95 changes: 0 additions & 95 deletions src/RequestTypes/DeviceAuthorizationRequest.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/ResponseTypes/DeviceCodeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function setPayload(string $payload): void
/**
* {@inheritdoc}
*/
public function setDeviceCode(DeviceCodeEntityInterface $deviceCode)
public function setDeviceCode(DeviceCodeEntityInterface $deviceCode): void
{
$this->deviceCode = $deviceCode;
}
Expand Down
Loading

0 comments on commit 76cb886

Please sign in to comment.