123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- declare(strict_types = 1);
- namespace Plugin\OAuth2\Util;
- use App\Core\Entity;
- use DateTimeImmutable;
- use DateTimeInterface;
- use Functional as F;
- use League\OAuth2\Server\Entities\ClientEntityInterface;
- use League\OAuth2\Server\Entities\ScopeEntityInterface;
- use League\OAuth2\Server\Entities\TokenInterface;
- use Plugin\OAuth2\Repository;
- abstract class Token extends Entity implements TokenInterface
- {
- abstract public function getId(): string;
-
- abstract public function getExpiry(): DateTimeInterface;
-
- abstract public function getUserId(): ?int;
-
- abstract public function getClientId(): string;
-
- abstract public function getTokenScopes(): string;
-
- public function getIdentifier(): string
- {
- return $this->getId();
- }
- public function setIdentifier($identifier)
- {
- $this->setId($identifier);
- }
-
- public function getExpiryDateTime(): DateTimeImmutable
- {
- return DateTimeImmutable::createFromInterface($this->getExpiry());
- }
-
- public function setExpiryDateTime(DateTimeImmutable $dateTime)
- {
- $this->setExpiry($dateTime);
- }
-
- public function setUserIdentifier($identifier)
- {
- $this->setUserId($identifier);
- }
-
- public function getUserIdentifier(): int|string|null
- {
- return $this->getUserId();
- }
-
- public function getClient(): ClientEntityInterface
- {
- return (new Repository\Client)->getClientEntity($this->getClientId());
- }
-
- public function setClient(ClientEntityInterface $client)
- {
- $this->setClientId($client->getIdentifier());
- }
-
- public function addScope(ScopeEntityInterface $scope)
- {
- $scope = $this->hasTokenScopes() ? $this->getTokenScopes() . ' ' . $scope->getIdentifier() : $scope->getIdentifier();
- $this->setTokenScopes($scope);
- }
-
- public function getScopes(): array
- {
- return F\map(
- explode(' ', $this->getTokenScopes()),
- fn (string $scope) => (new Repository\Scope)->getScopeEntityByIdentifier($scope),
- );
- }
- public static function tokenSchema(string $table_name): array
- {
- return [
- 'name' => $table_name,
- 'fields' => [
- 'id' => ['type' => 'char', 'length' => 64, 'not null' => true, 'description' => 'identifier for this token'],
- 'expiry' => ['type' => 'datetime', 'not null' => true, 'description' => 'when this token expires'],
- 'user_id' => ['type' => 'int', 'foreign key' => true, 'description' => 'Actor foreign key'],
- 'client_id' => ['type' => 'char', 'length' => 64, 'not null' => true, 'foreign key' => true, 'description' => 'OAuth client foreign key'],
- 'token_scopes' => ['type' => 'text', 'not null' => true, 'description' => 'Space separated scopes'],
- 'revoked' => ['type' => 'bool', 'not null' => true, 'foreign key' => true, 'description' => 'Whether this token is revoked'],
- 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
- ],
- 'primary key' => ['id'],
- ];
- }
- }
|