Source of file AccessTokenRepository.php
Size: 2,706 Bytes - Last Modified: 2022-02-21T10:00:46+00:00
/var/www/docs.ssmods.com/process/src/src/Repositories/AccessTokenRepository.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 | <?php namespace AdvancedLearning\Oauth2Server\Repositories; use AdvancedLearning\Oauth2Server\Entities\AccessTokenEntity as AccessTokenEntity; use AdvancedLearning\Oauth2Server\Models\AccessToken; use Carbon\Carbon; use League\OAuth2\Server\Entities\AccessTokenEntityInterface; use League\OAuth2\Server\Entities\ClientEntityInterface; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; use SilverStripe\ORM\DB; class AccessTokenRepository implements AccessTokenRepositoryInterface { /** * {@inheritdoc} */ public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) { $newToken = AccessToken::create(); $newToken->Identifier = $accessTokenEntity->getIdentifier(); $newToken->Name = $accessTokenEntity->getClient()->getName(); $newToken->User = $accessTokenEntity->getUserIdentifier(); $newToken->ExpiryDateTime = $accessTokenEntity->getExpiryDateTime()->format('Y-m-d H:i'); // turn scopes into space separated string $newToken->Scopes = ''; $separator = ''; foreach ($accessTokenEntity->getScopes() as $scope) { $newToken->Scopes .= $separator . $scope->getIdentifier(); $separator = ' '; } $newToken->write(); return $newToken; } /** * {@inheritdoc} */ public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) { return new AccessTokenEntity($userIdentifier, $scopes); } /** * {@inheritdoc} */ public function revokeAccessToken($tokenId) { if ($token = $this->findToken($tokenId)) { $token->Revoked = true; $token->write(); } } /** * {@inheritdoc} */ public function isAccessTokenRevoked($tokenId): bool { $token = $this->findToken($tokenId); // return true if there is no matching token return empty($token) || $token->Revoked; } /** * Find the Token for passed id. * * @param string $tokenId The id of the token. * * @return AccessToken|null */ public function findToken(string $tokenId): ?AccessToken { return AccessToken::get()->filter(['Identifier' => $tokenId])->first(); } /** * Delete tokens which have expired. * * @param integer $days */ public function deleteExpiredTokens($days = 1) { $expiryDate = Carbon::now()->subDays($days); DB::query(sprintf( 'DELETE FROM "OauthAccessToken" WHERE "ExpiryDateTime" < \'%s\'', $expiryDate->toDateTimeString() )); } } |