Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use custom Redirect URI validation logic in RefreshTokenGrant #42

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions common/components/OAuth2/Grants/AuthCodeGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,24 @@
namespace common\components\OAuth2\Grants;

use common\components\OAuth2\CryptTrait;
use common\components\OAuth2\Events\RequestedRefreshToken;
use common\components\OAuth2\Repositories\PublicScopeRepository;
use DateInterval;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\AuthCodeGrant as BaseAuthCodeGrant;
use League\OAuth2\Server\RequestEvent;
use Psr\Http\Message\ServerRequestInterface;
use yii\helpers\StringHelper;

final class AuthCodeGrant extends BaseAuthCodeGrant {
use CryptTrait;
use CheckOfflineAccessScopeTrait;
use ValidateRedirectUriTrait;

protected function issueAccessToken(
DateInterval $accessTokenTTL,
ClientEntityInterface $client,
?string $userIdentifier,
array $scopes = [],
): AccessTokenEntityInterface {
foreach ($scopes as $i => $scope) {
if ($scope->getIdentifier() === PublicScopeRepository::OFFLINE_ACCESS) {
unset($scopes[$i]);
$this->getEmitter()->emit(new RequestedRefreshToken('refresh_token_requested'));
}
}

$this->checkOfflineAccessScope($scopes);
return parent::issueAccessToken($accessTokenTTL, $client, $userIdentifier, $scopes);
}

protected function validateRedirectUri(
string $redirectUri,
ClientEntityInterface $client,
ServerRequestInterface $request,
): void {
$allowedRedirectUris = (array)$client->getRedirectUri();
foreach ($allowedRedirectUris as $allowedRedirectUri) {
if (StringHelper::startsWith($redirectUri, $allowedRedirectUri)) {
return;
}
}

$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
throw OAuthServerException::invalidClient($request);
}

}
26 changes: 26 additions & 0 deletions common/components/OAuth2/Grants/CheckOfflineAccessScopeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace common\components\OAuth2\Grants;

use common\components\OAuth2\Events\RequestedRefreshToken;
use common\components\OAuth2\Repositories\PublicScopeRepository;
use League\OAuth2\Server\EventEmitting\EventEmitter;

trait CheckOfflineAccessScopeTrait {

abstract public function getEmitter(): EventEmitter;

/**
* @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes
*/
protected function checkOfflineAccessScope(array $scopes = []): void {
foreach ($scopes as $i => $scope) {
if ($scope->getIdentifier() === PublicScopeRepository::OFFLINE_ACCESS) {
unset($scopes[$i]);
$this->getEmitter()->emit(new RequestedRefreshToken('refresh_token_requested'));
}
}
}

}
10 changes: 2 additions & 8 deletions common/components/OAuth2/Grants/DeviceCodeGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

namespace common\components\OAuth2\Grants;

use common\components\OAuth2\Events\RequestedRefreshToken;
use common\components\OAuth2\Repositories\ExtendedDeviceCodeRepositoryInterface;
use common\components\OAuth2\Repositories\PublicScopeRepository;
use common\components\OAuth2\ResponseTypes\EmptyResponse;
use DateInterval;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
Expand All @@ -22,6 +20,7 @@
* @property ExtendedDeviceCodeRepositoryInterface $deviceCodeRepository
*/
final class DeviceCodeGrant extends BaseDeviceCodeGrant {
use CheckOfflineAccessScopeTrait;

public function __construct(
ExtendedDeviceCodeRepositoryInterface $deviceCodeRepository,
Expand Down Expand Up @@ -95,12 +94,7 @@ protected function issueAccessToken(
?string $userIdentifier,
array $scopes = [],
): AccessTokenEntityInterface {
foreach ($scopes as $i => $scope) {
if ($scope->getIdentifier() === PublicScopeRepository::OFFLINE_ACCESS) {
unset($scopes[$i]);
$this->getEmitter()->emit(new RequestedRefreshToken('refresh_token_requested'));
}
}
$this->checkOfflineAccessScope($scopes);

return parent::issueAccessToken($accessTokenTTL, $client, $userIdentifier, $scopes);
}
Expand Down
1 change: 1 addition & 0 deletions common/components/OAuth2/Grants/RefreshTokenGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

final class RefreshTokenGrant extends BaseRefreshTokenGrant {
use CryptTrait;
use ValidateRedirectUriTrait;

/**
* Previously, refresh tokens were stored in Redis.
Expand Down
34 changes: 34 additions & 0 deletions common/components/OAuth2/Grants/ValidateRedirectUriTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace common\components\OAuth2\Grants;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\EventEmitting\EventEmitter;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\RequestEvent;
use Psr\Http\Message\ServerRequestInterface;
use yii\helpers\StringHelper;

trait ValidateRedirectUriTrait {

abstract public function getEmitter(): EventEmitter;

protected function validateRedirectUri(
string $redirectUri,
ClientEntityInterface $client,
ServerRequestInterface $request,
): void {
$allowedRedirectUris = (array)$client->getRedirectUri();
foreach ($allowedRedirectUris as $allowedRedirectUri) {
if (StringHelper::startsWith($redirectUri, $allowedRedirectUri)) {
return;
}
}

$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));

throw OAuthServerException::invalidClient($request);
}

}