. // }}} /** * OAuth2 Client * * @package GNUsocial * * @author Hugo Sales * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ namespace Plugin\OAuth2\Entity; use App\Core\Entity; 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 { public function getIdentifier() { return $this->getId(); } public function setIdentifier($identifier) { $this->setId($identifier); } /** * Get the token's expiry date time. * * @return DateTimeImmutable */ public function getExpiryDateTime() { return $this->getExpiry(); } /** * Set the date time when the token expires. * * @param DateTimeImmutable $dateTime */ public function setExpiryDateTime(\DateTimeImmutable $dateTime) { $this->setExpiry($dateTime); } /** * Set the identifier of the user associated with the token. * * @param null|int|string $identifier The identifier of the user */ public function setUserIdentifier($identifier) { $this->setUserId($identifier); } /** * Get the token user's identifier. * * @return null|int|string */ public function getUserIdentifier() { return $this->getUserId(); } /** * Get the client that the token was issued to. * * @return ClientEntityInterface */ public function getClient() { return (new Repository\Client)->getClientEntity($this->getClientId()); } /** * Set the client that the token was issued to. */ public function setClient(ClientEntityInterface $client) { $this->setClientId($client->getIdentifier()); } /** * Associate a scope with the token. */ public function addScope(ScopeEntityInterface $scope) { $scope = $this->hasTokenScopes() ? $this->getTokenScopes() . ' ' . $scope->getIdentifier() : $scope->getIdentifier(); $this->setTokenScopes($scope); } /** * Return an array of scopes associated with the token. * * @return ScopeEntityInterface[] */ public function getScopes() { 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'], ]; } }