| 29 | | final class AccessTokenRepository extends AbstractRepository implements AccessTokenRepositoryInterface |
| 30 | | { |
| 31 | | public function getNewToken( |
| 32 | | ClientEntityInterface $clientEntity, |
| 33 | | array $scopes, |
| 34 | | ?string $userIdentifier = null, |
| 35 | | ): AccessTokenEntityInterface { |
| 36 | | $token = new AccessTokenEntity(); |
| 37 | | $token->setClient($clientEntity); |
| 38 | | |
| 39 | | foreach ($scopes as $scope) { |
| 40 | | |
| 41 | | if (!$scope instanceof ScopeEntityInterface) { |
| 42 | | continue; |
| 43 | | } |
| 44 | | |
| 45 | | $token->addScope($scope); |
| 46 | | } |
| 47 | | |
| 48 | | if ($userIdentifier !== null && $userIdentifier !== '') { |
| 49 | | $token->setUserIdentifier($userIdentifier); |
| 50 | | } |
| 51 | | |
| 52 | | return $token; |
| 53 | | } |
| 54 | | |
| 55 | | public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity): void |
| 56 | | { |
| 57 | | $scopes = array_map( |
| 58 | | static fn(ScopeEntityInterface $scope): string => $scope->getIdentifier(), |
| 59 | | $accessTokenEntity->getScopes(), |
| 60 | | ); |
| 61 | | |
| 62 | | $tokenUserIdentifier = $accessTokenEntity->getUserIdentifier(); |
| 63 | | |
| 64 | | $tokenUserIdentifier = $tokenUserIdentifier === null ? null : (string) $tokenUserIdentifier; |
| 65 | | |
| 66 | | $insert = sprintf( |
| 67 | | "INSERT INTO %s (identifier, client_id, user_id, scopes, revoked, expires_at, created) |
| 68 | | VALUES ('%s', '%s', %s, '%s', 0, '%s', %s)", |
| 69 | | $this->table('faqoauth_access_tokens'), |
| 70 | | $this->db()->escape($accessTokenEntity->getIdentifier()), |
| 71 | | $this->db()->escape($accessTokenEntity->getClient()->getIdentifier()), |
| 72 | | $tokenUserIdentifier === null ? 'NULL' : "'" . $this->db()->escape($tokenUserIdentifier) . "'", |
| 73 | | $this->db()->escape((string) json_encode($scopes)), |
| 74 | | $this->db()->escape($accessTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s')), |
| 75 | | $this->db()->now(), |
| 76 | | ); |
| 77 | | |
| 78 | | if ($this->db()->query($insert) === false) { |
| 79 | | $dbError = strtolower($this->db()->error()); |
| 80 | | if (str_contains($dbError, 'duplicate') || str_contains($dbError, 'unique')) { |
| 81 | | throw UniqueTokenIdentifierConstraintViolationException::create(); |
| 82 | | } |
| 83 | | |
| 84 | | throw new \RuntimeException('Failed to persist access token: ' . $this->db()->error()); |
| 85 | | } |
| 86 | | } |
| 87 | | |
| 88 | | public function revokeAccessToken(string $tokenId): void |
| 89 | | { |
| 90 | | $this->db()->query(sprintf( |
| 91 | | "UPDATE %s SET revoked = 1 WHERE identifier = '%s'", |
| 92 | | $this->table('faqoauth_access_tokens'), |
| 93 | | $this->db()->escape($tokenId), |
| 94 | | )); |
| 95 | | } |
| 96 | | |
| 97 | | public function isAccessTokenRevoked(string $tokenId): bool |
| 98 | | { |
| 99 | | $result = $this->db()->query(sprintf( |
| 100 | | "SELECT revoked FROM %s WHERE identifier = '%s'", |
| 101 | | $this->table('faqoauth_access_tokens'), |
| 102 | | $this->db()->escape($tokenId), |
| 103 | | )); |
| 104 | | |
| 105 | | if ($result === false) { |
| 106 | | return true; |
| 107 | | } |
| 108 | | |
| 109 | | $row = $this->db()->fetchObject($result); |
| 110 | | if (!is_object($row)) { |
| 111 | | return true; |
| 112 | | } |
| 113 | | |
| 114 | | return (int) ($row->revoked ?? 1) === 1; |
| 115 | | } |
| 116 | | } |