1
0

CleanUpJob.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Security\VerificationToken;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\BackgroundJob\Job;
  11. use OCP\IConfig;
  12. use OCP\IUserManager;
  13. use OCP\Security\VerificationToken\InvalidTokenException;
  14. use OCP\Security\VerificationToken\IVerificationToken;
  15. class CleanUpJob extends Job {
  16. protected ?int $runNotBefore = null;
  17. protected ?string $userId = null;
  18. protected ?string $subject = null;
  19. protected ?string $pwdPrefix = null;
  20. public function __construct(
  21. ITimeFactory $time,
  22. private IConfig $config,
  23. private IVerificationToken $verificationToken,
  24. private IUserManager $userManager,
  25. ) {
  26. parent::__construct($time);
  27. }
  28. public function setArgument($argument): void {
  29. parent::setArgument($argument);
  30. $args = \json_decode($argument, true);
  31. $this->userId = (string)$args['userId'];
  32. $this->subject = (string)$args['subject'];
  33. $this->pwdPrefix = (string)$args['pp'];
  34. $this->runNotBefore = (int)$args['notBefore'];
  35. }
  36. protected function run($argument): void {
  37. try {
  38. $user = $this->userManager->get($this->userId);
  39. if ($user === null) {
  40. return;
  41. }
  42. $this->verificationToken->check('irrelevant', $user, $this->subject, $this->pwdPrefix);
  43. } catch (InvalidTokenException $e) {
  44. if ($e->getCode() === InvalidTokenException::TOKEN_EXPIRED) {
  45. // make sure to only remove expired tokens
  46. $this->config->deleteUserValue($this->userId, 'core', $this->subject);
  47. }
  48. }
  49. }
  50. public function start(IJobList $jobList): void {
  51. if ($this->time->getTime() >= $this->runNotBefore) {
  52. $jobList->remove($this, $this->argument);
  53. parent::start($jobList);
  54. }
  55. }
  56. }