CleanUpJob.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. private IConfig $config;
  38. private IVerificationToken $verificationToken;
  39. private IUserManager $userManager;
  40. public function __construct(ITimeFactory $time, IConfig $config, IVerificationToken $verificationToken, IUserManager $userManager) {
  41. parent::__construct($time);
  42. $this->config = $config;
  43. $this->verificationToken = $verificationToken;
  44. $this->userManager = $userManager;
  45. }
  46. public function setArgument($argument) {
  47. parent::setArgument($argument);
  48. $args = \json_decode($argument, true);
  49. $this->userId = (string)$args['userId'];
  50. $this->subject = (string)$args['subject'];
  51. $this->pwdPrefix = (string)$args['pp'];
  52. $this->runNotBefore = (int)$args['notBefore'];
  53. }
  54. protected function run($argument) {
  55. try {
  56. $user = $this->userManager->get($this->userId);
  57. if ($user === null) {
  58. return;
  59. }
  60. $this->verificationToken->check('irrelevant', $user, $this->subject, $this->pwdPrefix);
  61. } catch (InvalidTokenException $e) {
  62. if ($e->getCode() === InvalidTokenException::TOKEN_EXPIRED) {
  63. // make sure to only remove expired tokens
  64. $this->config->deleteUserValue($this->userId, 'core', $this->subject);
  65. }
  66. }
  67. }
  68. public function start(IJobList $jobList): void {
  69. if ($this->time->getTime() >= $this->runNotBefore) {
  70. $jobList->remove($this, $this->argument);
  71. parent::start($jobList);
  72. }
  73. }
  74. }