mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Set access tokens TTL depending on the requested scopes
This commit is contained in:
@@ -25,7 +25,7 @@ class Component extends BaseComponent {
|
||||
$authCodesRepo = new Repositories\AuthCodeRepository();
|
||||
$refreshTokensRepo = new Repositories\RefreshTokenRepository();
|
||||
|
||||
$accessTokenTTL = CarbonInterval::day();
|
||||
$accessTokenTTL = CarbonInterval::days(2);
|
||||
|
||||
$authServer = new AuthorizationServer(
|
||||
$clientsRepo,
|
||||
|
||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
namespace api\components\OAuth2\Entities;
|
||||
|
||||
use api\components\OAuth2\Repositories\PublicScopeRepository;
|
||||
use api\rbac\Permissions;
|
||||
use Carbon\CarbonImmutable;
|
||||
use DateTimeImmutable;
|
||||
use League\OAuth2\Server\CryptKeyInterface;
|
||||
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
|
||||
@@ -43,8 +45,22 @@ class AccessTokenEntity implements AccessTokenEntityInterface {
|
||||
}
|
||||
|
||||
public function getExpiryDateTime(): DateTimeImmutable {
|
||||
// TODO: extend token life depending on scopes list
|
||||
return $this->parentGetExpiryDateTime();
|
||||
$expiryTime = $this->parentGetExpiryDateTime();
|
||||
if ($this->hasScope(PublicScopeRepository::CHANGE_SKIN) || $this->hasScope(Permissions::OBTAIN_ACCOUNT_EMAIL)) {
|
||||
$expiryTime = min($expiryTime, CarbonImmutable::now()->addHour());
|
||||
}
|
||||
|
||||
return $expiryTime;
|
||||
}
|
||||
|
||||
private function hasScope(string $scopeIdentifier): bool {
|
||||
foreach ($this->getScopes() as $scope) {
|
||||
if ($scope->getIdentifier() === $scopeIdentifier) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
||||
class PublicScopeRepository implements ScopeRepositoryInterface {
|
||||
|
||||
public const OFFLINE_ACCESS = 'offline_access';
|
||||
public const CHANGE_SKIN = 'change_skin';
|
||||
|
||||
private const CHANGE_SKIN = 'change_skin';
|
||||
private const ACCOUNT_INFO = 'account_info';
|
||||
private const ACCOUNT_EMAIL = 'account_email';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user