1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
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 <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\TwoFactorBackupCodes\BackgroundJob; |
26
|
|
|
|
27
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
28
|
|
|
use OCP\Authentication\TwoFactorAuth\IRegistry; |
29
|
|
|
use OCP\BackgroundJob\IJobList; |
30
|
|
|
use OCP\BackgroundJob\TimedJob; |
31
|
|
|
use OCP\IUserManager; |
32
|
|
|
use OCP\Notification\IManager; |
33
|
|
|
|
34
|
|
|
class RememberBackupCodesJob extends TimedJob { |
35
|
|
|
|
36
|
|
|
/** @var IRegistry */ |
37
|
|
|
private $registry; |
38
|
|
|
|
39
|
|
|
/** @var IUserManager */ |
40
|
|
|
private $userManager; |
41
|
|
|
|
42
|
|
|
/** @var IManager */ |
43
|
|
|
private $notificationManager; |
44
|
|
|
|
45
|
|
|
/** @var IJobList */ |
46
|
|
|
private $jobList; |
47
|
|
|
|
48
|
|
|
public function __construct(IRegistry $registry, |
49
|
|
|
IUserManager $userManager, |
50
|
|
|
ITimeFactory $timeFactory, |
51
|
|
|
IManager $notificationManager, |
52
|
|
|
IJobList $jobList) { |
53
|
|
|
parent::__construct($timeFactory); |
54
|
|
|
$this->registry = $registry; |
55
|
|
|
$this->userManager = $userManager; |
56
|
|
|
$this->time = $timeFactory; |
57
|
|
|
$this->notificationManager = $notificationManager; |
58
|
|
|
$this->jobList = $jobList; |
59
|
|
|
|
60
|
|
|
$this->setInterval(60*60*24*14); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function run($argument) { |
64
|
|
|
$uid = $argument['uid']; |
65
|
|
|
$user = $this->userManager->get($uid); |
66
|
|
|
|
67
|
|
|
if ($user === null) { |
68
|
|
|
// We can't run with an invalid user |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$providers = $this->registry->getProviderStates($user); |
73
|
|
|
$state2fa = array_reduce($providers, function(bool $carry, bool $state) { |
74
|
|
|
return $carry || $state; |
75
|
|
|
}, false); |
76
|
|
|
|
77
|
|
|
/* |
78
|
|
|
* If no provider is active or if the backup codes are already generate |
79
|
|
|
* we can remove the job |
80
|
|
|
*/ |
81
|
|
|
if ($state2fa === false || (isset($providers['backup_codes']) && $providers['backup_codes'] === true)) { |
82
|
|
|
// Backup codes already generated lets remove this job |
83
|
|
|
$this->jobList->remove(self::class, $argument); |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$date = new \DateTime(); |
88
|
|
|
$date->setTimestamp($this->time->getTime()); |
89
|
|
|
|
90
|
|
|
$notification = $this->notificationManager->createNotification(); |
91
|
|
|
$notification->setApp('twofactor_backupcodes') |
92
|
|
|
->setUser($user->getUID()) |
93
|
|
|
->setDateTime($date) |
94
|
|
|
->setObject('create', 'codes') |
95
|
|
|
->setSubject('create_backupcodes'); |
96
|
|
|
$this->notificationManager->notify($notification); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|