diff --git a/lib/classes/API/OAuth2/Token.php b/lib/classes/API/OAuth2/Token.php deleted file mode 100644 index d85b5db509..0000000000 --- a/lib/classes/API/OAuth2/Token.php +++ /dev/null @@ -1,319 +0,0 @@ -appbox = $appbox; - $this->account = $account; - $this->generator = $generator; - - $sql = 'SELECT oauth_token, session_id, UNIX_TIMESTAMP(expires) as expires, scope - FROM api_oauth_tokens - WHERE api_account_id = :account_id'; - $stmt = $this->appbox->get_connection()->prepare($sql); - $stmt->execute([':account_id' => $this->account->get_id()]); - $row = $stmt->fetch(PDO::FETCH_ASSOC); - - if ( ! $row) - throw new NotFoundHttpException('Account not found'); - - $stmt->closeCursor(); - - $this->token = $row['oauth_token']; - $this->session_id = is_null($row['session_id']) ? null : (int) $row['session_id']; - $this->expires = $row['expires']; - $this->scope = $row['scope']; - - return $this; - } - - /** - * - * @return string - */ - public function get_value() - { - return $this->token; - } - - /** - * - * @param string $oauth_token - * @return API_OAuth2_Token - */ - public function set_value($oauth_token) - { - $sql = 'UPDATE api_oauth_tokens SET oauth_token = :oauth_token - WHERE oauth_token = :current_token'; - - $params = [ - ':oauth_token' => $oauth_token - , ':current_token' => $this->token - ]; - - $stmt = $this->appbox->get_connection()->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - $this->token = $oauth_token; - - return $this; - } - - /** - * - * @return int - */ - public function get_session_id() - { - return $this->session_id; - } - - /** - * - * @param int $session_id - * @return API_OAuth2_Token - */ - public function set_session_id($session_id) - { - $sql = 'UPDATE api_oauth_tokens SET session_id = :session_id - WHERE oauth_token = :current_token'; - - $params = [ - ':session_id' => $session_id - , ':current_token' => $this->token - ]; - - $stmt = $this->appbox->get_connection()->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - $this->session_id = $session_id !== null ? (int) $session_id : $session_id; - - return $this; - } - - /** - * - * @return int - */ - public function get_expires() - { - return $this->expires; - } - - /** - * - * @param int $expires - * @return API_OAuth2_Token - */ - public function set_expires($expires) - { - $sql = 'UPDATE api_oauth_tokens SET expires = FROM_UNIXTIME(:expires) - WHERE oauth_token = :oauth_token'; - - $params = [ - ':expires' => $expires - , ':oauth_token' => $this->get_value() - ]; - - $stmt = $this->appbox->get_connection()->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - $this->expires = $expires; - - return $this; - } - - /** - * - * @return string - */ - public function get_scope() - { - return $this->scope; - } - - public function set_scope($scope) - { - $sql = 'UPDATE api_oauth_tokens SET scope = :scope - WHERE oauth_token = :oauth_token'; - - $params = [ - ':scope' => $scope - , ':oauth_token' => $this->get_value() - ]; - - $stmt = $this->appbox->get_connection()->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - $this->scope = $scope; - - return $this; - } - - /** - * - * @return API_OAuth2_Account - */ - public function get_account() - { - return $this->account; - } - - /** - * - * @return API_OAuth2_Token - */ - public function renew() - { - $sql = 'UPDATE api_oauth_tokens SET oauth_token = :new_token - WHERE oauth_token = :old_token'; - - $new_token = $this->generator->generateString(32, TokenManipulator::LETTERS_AND_NUMBERS); - - $params = [ - ':new_token' => $new_token - , ':old_token' => $this->get_value() - ]; - - $stmt = $this->appbox->get_connection()->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - $this->token = $new_token; - - return $this; - } - - /** - * - * @return void - */ - public function delete() - { - $sql = 'DELETE FROM api_oauth_tokens WHERE oauth_token = :oauth_token'; - - $stmt = $this->appbox->get_connection()->prepare($sql); - $stmt->execute([':oauth_token' => $this->get_value()]); - $stmt->closeCursor(); - - return; - } - - /** - * - * @param Application $app - * @param string $oauth_token - * @return API_OAuth2_Token - */ - public static function load_by_oauth_token(Application $app, $oauth_token) - { - $sql = 'SELECT a.api_account_id - FROM api_oauth_tokens a, api_accounts b - WHERE a.oauth_token = :oauth_token - AND a.api_account_id = b.api_account_id'; - - $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql); - $params = [":oauth_token" => $oauth_token]; - $stmt->execute($params); - $row = $stmt->fetch(PDO::FETCH_ASSOC); - $stmt->closeCursor(); - - if (!$row) { - throw new NotFoundHttpException('Account not found'); - } - - return new self($app['phraseanet.appbox'], new API_OAuth2_Account($app, $row['api_account_id']), $app['random.medium']); - } - - /** - * - * @param appbox $appbox - * @param API_OAuth2_Account $account - * @param string $scope - * @return API_OAuth2_Token - */ - public static function create(appbox $appbox, API_OAuth2_Account $account, Generator $generator, $scope = null) - { - $sql = 'INSERT INTO api_oauth_tokens - (oauth_token, session_id, api_account_id, expires, scope) - VALUES (:token, null, :account_id, :expire, :scope)'; - - $expires = new \DateTime('+1 hour'); - - $params = [ - ':token' => $generator->generateString(32, TokenManipulator::LETTERS_AND_NUMBERS) - , ':account_id' => $account->get_id() - , ':expire' => $expires->format(DATE_ISO8601) - , ':scope' => $scope - ]; - - $stmt = $appbox->get_connection()->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - return new API_OAuth2_Token($appbox, $account, $generator); - } -} diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php b/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php index f6701ca56f..dda387dacf 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Api/ApiTestCase.php @@ -19,12 +19,12 @@ use Symfony\Component\HttpFoundation\Response; abstract class ApiTestCase extends \PhraseanetWebTestCase { /** - * @var \API_OAuth2_Token + * @var ApiOauthToken */ private static $token; /** - * @var \API_OAuth2_Account + * @var ApiAccount */ private static $account; /** @@ -32,15 +32,15 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase */ private static $oauthApplication; /** - * @var \API_OAuth2_Token + * @var ApiOauthToken */ private static $adminToken; /** - * @var \API_OAuth2_Account + * @var ApiAccount */ private static $adminAccount; /** - * @var \ApiApplication + * @var ApiApplication */ private static $adminApplication; private static $apiInitialized = false;