SetTokenExpiration.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. public function __construct(
  17. private IDBConnection $connection,
  18. private ITimeFactory $time,
  19. private TokenProvider $tokenProvider,
  20. ) {
  21. }
  22. public function getName(): string {
  23. return 'Update OAuth token expiration times';
  24. }
  25. public function run(IOutput $output) {
  26. $qb = $this->connection->getQueryBuilder();
  27. $qb->select('*')
  28. ->from('oauth2_access_tokens');
  29. $cursor = $qb->executeQuery();
  30. while ($row = $cursor->fetch()) {
  31. $token = AccessToken::fromRow($row);
  32. try {
  33. $appToken = $this->tokenProvider->getTokenById($token->getTokenId());
  34. $appToken->setExpires($this->time->getTime() + 3600);
  35. $this->tokenProvider->updateToken($appToken);
  36. } catch (InvalidTokenException $e) {
  37. //Skip this token
  38. }
  39. }
  40. $cursor->closeCursor();
  41. }
  42. }