2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2019-09-22 02:47:21 +05:30
|
|
|
declare(strict_types=1);
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
namespace api\components\OAuth2\Entities;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2019-11-07 03:42:18 +05:30
|
|
|
use api\components\OAuth2\Repositories\PublicScopeRepository;
|
|
|
|
use DateTimeImmutable;
|
2019-09-22 02:47:21 +05:30
|
|
|
use League\OAuth2\Server\CryptKeyInterface;
|
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
|
2019-11-07 03:42:18 +05:30
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface;
|
2019-09-22 02:47:21 +05:30
|
|
|
use League\OAuth2\Server\Entities\Traits\EntityTrait;
|
|
|
|
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;
|
2019-12-04 23:40:15 +05:30
|
|
|
use Yii;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
class AccessTokenEntity implements AccessTokenEntityInterface {
|
|
|
|
use EntityTrait;
|
|
|
|
use TokenEntityTrait {
|
|
|
|
getExpiryDateTime as parentGetExpiryDateTime;
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2019-11-07 03:42:18 +05:30
|
|
|
/**
|
|
|
|
* There is no need to store offline_access scope in the resulting access_token.
|
|
|
|
* We cannot remove it from the token because otherwise we won't be able to form a refresh_token.
|
|
|
|
* That's why we delete offline_access before creating the token and then return it back.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-09-22 02:47:21 +05:30
|
|
|
public function __toString(): string {
|
2019-11-07 03:42:18 +05:30
|
|
|
$scopes = $this->scopes;
|
|
|
|
$this->scopes = array_filter($this->scopes, function(ScopeEntityInterface $scope): bool {
|
|
|
|
return $scope->getIdentifier() !== PublicScopeRepository::OFFLINE_ACCESS;
|
|
|
|
});
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
$token = Yii::$app->tokensFactory->createForOAuthClient($this);
|
2019-11-07 03:42:18 +05:30
|
|
|
|
|
|
|
$this->scopes = $scopes;
|
|
|
|
|
|
|
|
return (string)$token;
|
2016-11-30 04:49:14 +05:30
|
|
|
}
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
public function setPrivateKey(CryptKeyInterface $privateKey): void {
|
|
|
|
// We use a general-purpose component to build JWT tokens, so there is no need to keep the key
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2019-11-07 03:42:18 +05:30
|
|
|
public function getExpiryDateTime(): DateTimeImmutable {
|
2019-09-22 02:47:21 +05:30
|
|
|
// TODO: extend token life depending on scopes list
|
|
|
|
return $this->parentGetExpiryDateTime();
|
2016-11-30 04:49:14 +05:30
|
|
|
}
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|