1
0

SetTokenExpiration.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\OAuth2\Migration;
  26. use OC\Authentication\Exceptions\InvalidTokenException;
  27. use OC\Authentication\Token\IProvider as TokenProvider;
  28. use OCA\OAuth2\Db\AccessToken;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\IDBConnection;
  31. use OCP\Migration\IOutput;
  32. use OCP\Migration\IRepairStep;
  33. class SetTokenExpiration implements IRepairStep {
  34. /** @var IDBConnection */
  35. private $connection;
  36. /** @var ITimeFactory */
  37. private $time;
  38. /** @var TokenProvider */
  39. private $tokenProvider;
  40. public function __construct(IDBConnection $connection,
  41. ITimeFactory $timeFactory,
  42. TokenProvider $tokenProvider) {
  43. $this->connection = $connection;
  44. $this->time = $timeFactory;
  45. $this->tokenProvider = $tokenProvider;
  46. }
  47. public function getName(): string {
  48. return 'Update OAuth token expiration times';
  49. }
  50. public function run(IOutput $output) {
  51. $qb = $this->connection->getQueryBuilder();
  52. $qb->select('*')
  53. ->from('oauth2_access_tokens');
  54. $cursor = $qb->execute();
  55. while ($row = $cursor->fetch()) {
  56. $token = AccessToken::fromRow($row);
  57. try {
  58. $appToken = $this->tokenProvider->getTokenById($token->getTokenId());
  59. $appToken->setExpires($this->time->getTime() + 3600);
  60. $this->tokenProvider->updateToken($appToken);
  61. } catch (InvalidTokenException $e) {
  62. //Skip this token
  63. }
  64. }
  65. $cursor->closeCursor();
  66. }
  67. }