CleanUpJob.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Security\VerificationToken;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\IConfig;
  27. use OCP\IUserManager;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\BackgroundJob\Job;
  30. use OCP\Security\VerificationToken\InvalidTokenException;
  31. use OCP\Security\VerificationToken\IVerificationToken;
  32. class CleanUpJob extends Job {
  33. protected ?int $runNotBefore = null;
  34. protected ?string $userId = null;
  35. protected ?string $subject = null;
  36. protected ?string $pwdPrefix = null;
  37. public function __construct(
  38. ITimeFactory $time,
  39. private IConfig $config,
  40. private IVerificationToken $verificationToken,
  41. private IUserManager $userManager,
  42. ) {
  43. parent::__construct($time);
  44. }
  45. public function setArgument($argument): void {
  46. parent::setArgument($argument);
  47. $args = \json_decode($argument, true);
  48. $this->userId = (string)$args['userId'];
  49. $this->subject = (string)$args['subject'];
  50. $this->pwdPrefix = (string)$args['pp'];
  51. $this->runNotBefore = (int)$args['notBefore'];
  52. }
  53. protected function run($argument): void {
  54. try {
  55. $user = $this->userManager->get($this->userId);
  56. if ($user === null) {
  57. return;
  58. }
  59. $this->verificationToken->check('irrelevant', $user, $this->subject, $this->pwdPrefix);
  60. } catch (InvalidTokenException $e) {
  61. if ($e->getCode() === InvalidTokenException::TOKEN_EXPIRED) {
  62. // make sure to only remove expired tokens
  63. $this->config->deleteUserValue($this->userId, 'core', $this->subject);
  64. }
  65. }
  66. }
  67. public function start(IJobList $jobList): void {
  68. if ($this->time->getTime() >= $this->runNotBefore) {
  69. $jobList->remove($this, $this->argument);
  70. parent::start($jobList);
  71. }
  72. }
  73. }