SetTokenExpiration.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\OAuth2\Migration;
  8. use OC\Authentication\Token\IProvider as TokenProvider;
  9. use OCA\OAuth2\Db\AccessToken;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\Authentication\Exceptions\InvalidTokenException;
  12. use OCP\IDBConnection;
  13. use OCP\Migration\IOutput;
  14. use OCP\Migration\IRepairStep;
  15. class SetTokenExpiration implements IRepairStep {
  16. /** @var IDBConnection */
  17. private $connection;
  18. /** @var ITimeFactory */
  19. private $time;
  20. /** @var TokenProvider */
  21. private $tokenProvider;
  22. public function __construct(IDBConnection $connection,
  23. ITimeFactory $timeFactory,
  24. TokenProvider $tokenProvider) {
  25. $this->connection = $connection;
  26. $this->time = $timeFactory;
  27. $this->tokenProvider = $tokenProvider;
  28. }
  29. public function getName(): string {
  30. return 'Update OAuth token expiration times';
  31. }
  32. public function run(IOutput $output) {
  33. $qb = $this->connection->getQueryBuilder();
  34. $qb->select('*')
  35. ->from('oauth2_access_tokens');
  36. $cursor = $qb->execute();
  37. while ($row = $cursor->fetch()) {
  38. $token = AccessToken::fromRow($row);
  39. try {
  40. $appToken = $this->tokenProvider->getTokenById($token->getTokenId());
  41. $appToken->setExpires($this->time->getTime() + 3600);
  42. $this->tokenProvider->updateToken($appToken);
  43. } catch (InvalidTokenException $e) {
  44. //Skip this token
  45. }
  46. }
  47. $cursor->closeCursor();
  48. }
  49. }