Skip to content

Commit c70e3c5

Browse files
authored
Merge pull request #11549 from nextcloud/feature/background_job_to_check
Add job to check users for backup code reminders
2 parents 98bac99 + 36bf522 commit c70e3c5

File tree

7 files changed

+323
-1
lines changed

7 files changed

+323
-1
lines changed

apps/twofactor_backupcodes/appinfo/info.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<name>Two factor backup codes</name>
66
<summary>A two-factor auth backup codes provider</summary>
77
<description>A two-factor auth backup codes provider</description>
8-
<version>1.4.0</version>
8+
<version>1.4.1</version>
99
<licence>agpl</licence>
1010
<author>Christoph Wurst</author>
1111
<namespace>TwoFactorBackupCodes</namespace>
@@ -16,6 +16,12 @@
1616
<nextcloud min-version="15" max-version="15" />
1717
</dependencies>
1818

19+
<repair-steps>
20+
<post-migration>
21+
<step>OCA\TwoFactorBackupCodes\Migration\CheckBackupCodes</step>
22+
</post-migration>
23+
</repair-steps>
24+
1925
<two-factor-providers>
2026
<provider>OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider</provider>
2127
</two-factor-providers>

apps/twofactor_backupcodes/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
return array(
99
'OCA\\TwoFactorBackupCodes\\Activity\\Provider' => $baseDir . '/../lib/Activity/Provider.php',
1010
'OCA\\TwoFactorBackupCodes\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
11+
'OCA\\TwoFactorBackupCodes\\BackgroundJob\\CheckBackupCodes' => $baseDir . '/../lib/BackgroundJob/CheckBackupCodes.php',
1112
'OCA\\TwoFactorBackupCodes\\BackgroundJob\\RememberBackupCodesJob' => $baseDir . '/../lib/BackgroundJob/RememberBackupCodesJob.php',
1213
'OCA\\TwoFactorBackupCodes\\Controller\\SettingsController' => $baseDir . '/../lib/Controller/SettingsController.php',
1314
'OCA\\TwoFactorBackupCodes\\Db\\BackupCode' => $baseDir . '/../lib/Db/BackupCode.php',
@@ -18,6 +19,7 @@
1819
'OCA\\TwoFactorBackupCodes\\Listener\\IListener' => $baseDir . '/../lib/Listener/IListener.php',
1920
'OCA\\TwoFactorBackupCodes\\Listener\\ProviderEnabled' => $baseDir . '/../lib/Listener/ProviderEnabled.php',
2021
'OCA\\TwoFactorBackupCodes\\Listener\\RegistryUpdater' => $baseDir . '/../lib/Listener/RegistryUpdater.php',
22+
'OCA\\TwoFactorBackupCodes\\Migration\\CheckBackupCodes' => $baseDir . '/../lib/Migration/CheckBackupCodes.php',
2123
'OCA\\TwoFactorBackupCodes\\Migration\\Version1002Date20170607104347' => $baseDir . '/../lib/Migration/Version1002Date20170607104347.php',
2224
'OCA\\TwoFactorBackupCodes\\Migration\\Version1002Date20170607113030' => $baseDir . '/../lib/Migration/Version1002Date20170607113030.php',
2325
'OCA\\TwoFactorBackupCodes\\Migration\\Version1002Date20170919123342' => $baseDir . '/../lib/Migration/Version1002Date20170919123342.php',

apps/twofactor_backupcodes/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ComposerStaticInitTwoFactorBackupCodes
2323
public static $classMap = array (
2424
'OCA\\TwoFactorBackupCodes\\Activity\\Provider' => __DIR__ . '/..' . '/../lib/Activity/Provider.php',
2525
'OCA\\TwoFactorBackupCodes\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
26+
'OCA\\TwoFactorBackupCodes\\BackgroundJob\\CheckBackupCodes' => __DIR__ . '/..' . '/../lib/BackgroundJob/CheckBackupCodes.php',
2627
'OCA\\TwoFactorBackupCodes\\BackgroundJob\\RememberBackupCodesJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/RememberBackupCodesJob.php',
2728
'OCA\\TwoFactorBackupCodes\\Controller\\SettingsController' => __DIR__ . '/..' . '/../lib/Controller/SettingsController.php',
2829
'OCA\\TwoFactorBackupCodes\\Db\\BackupCode' => __DIR__ . '/..' . '/../lib/Db/BackupCode.php',
@@ -33,6 +34,7 @@ class ComposerStaticInitTwoFactorBackupCodes
3334
'OCA\\TwoFactorBackupCodes\\Listener\\IListener' => __DIR__ . '/..' . '/../lib/Listener/IListener.php',
3435
'OCA\\TwoFactorBackupCodes\\Listener\\ProviderEnabled' => __DIR__ . '/..' . '/../lib/Listener/ProviderEnabled.php',
3536
'OCA\\TwoFactorBackupCodes\\Listener\\RegistryUpdater' => __DIR__ . '/..' . '/../lib/Listener/RegistryUpdater.php',
37+
'OCA\\TwoFactorBackupCodes\\Migration\\CheckBackupCodes' => __DIR__ . '/..' . '/../lib/Migration/CheckBackupCodes.php',
3638
'OCA\\TwoFactorBackupCodes\\Migration\\Version1002Date20170607104347' => __DIR__ . '/..' . '/../lib/Migration/Version1002Date20170607104347.php',
3739
'OCA\\TwoFactorBackupCodes\\Migration\\Version1002Date20170607113030' => __DIR__ . '/..' . '/../lib/Migration/Version1002Date20170607113030.php',
3840
'OCA\\TwoFactorBackupCodes\\Migration\\Version1002Date20170919123342' => __DIR__ . '/..' . '/../lib/Migration/Version1002Date20170919123342.php',
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
5+
*
6+
* @author Roeland Jago Douma <roeland@famdouma.nl>
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 OC\Authentication\TwoFactorAuth\Manager;
28+
use OC\BackgroundJob\QueuedJob;
29+
use OCP\Authentication\TwoFactorAuth\IRegistry;
30+
use OCP\BackgroundJob\IJobList;
31+
use OCP\IUser;
32+
use OCP\IUserManager;
33+
34+
class CheckBackupCodes extends QueuedJob {
35+
36+
/** @var IUserManager */
37+
private $userManager;
38+
39+
/** @var IJobList */
40+
private $jobList;
41+
42+
/** @var Manager */
43+
private $registry;
44+
45+
/** @var Manager */
46+
private $twofactorManager;
47+
48+
public function __construct(IUserManager $userManager, IJobList $jobList, Manager $twofactorManager, IRegistry $registry) {
49+
$this->userManager = $userManager;
50+
$this->jobList = $jobList;
51+
$this->twofactorManager = $twofactorManager;
52+
$this->registry = $registry;
53+
}
54+
55+
protected function run($argument) {
56+
$this->userManager->callForSeenUsers(function(IUser $user) {
57+
$providers = $this->registry->getProviderStates($user);
58+
$isTwoFactorAuthenticated = $this->twofactorManager->isTwoFactorAuthenticated($user);
59+
60+
if ($isTwoFactorAuthenticated && isset($providers['backup_codes']) && $providers['backup_codes'] === false) {
61+
$this->jobList->add(RememberBackupCodesJob::class, ['uid' => $user->getUID()]);
62+
}
63+
});
64+
}
65+
66+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
5+
*
6+
* @author Roeland Jago Douma <roeland@famdouma.nl>
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\Migration;
26+
27+
use OCP\BackgroundJob\IJobList;
28+
use OCP\Migration\IOutput;
29+
use OCP\Migration\IRepairStep;
30+
31+
class CheckBackupCodes implements IRepairStep {
32+
33+
/** @var IJobList */
34+
private $jobList;
35+
36+
public function __construct(IJobList $jobList) {
37+
$this->jobList = $jobList;
38+
}
39+
40+
public function getName(): string {
41+
return 'Add background job to check for backup codes';
42+
}
43+
44+
public function run(IOutput $output) {
45+
$this->jobList->add(\OCA\TwoFactorBackupCodes\BackgroundJob\CheckBackupCodes::class);
46+
}
47+
48+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
5+
*
6+
* @author Roeland Jago Douma <roeland@famdouma.nl>
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\Tests\Unit\BackgroundJob;
26+
27+
use OC\Authentication\TwoFactorAuth\Manager;
28+
use OCA\TwoFactorBackupCodes\BackgroundJob\CheckBackupCodes;
29+
use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
30+
use OCP\Authentication\TwoFactorAuth\IRegistry;
31+
use OCP\BackgroundJob\IJobList;
32+
use OCP\IUser;
33+
use OCP\IUserManager;
34+
use PHPUnit\Framework\MockObject\MockObject;
35+
use Test\TestCase;
36+
37+
class CheckBackupCodeTest extends TestCase {
38+
39+
/** @var IUserManager|MockObject */
40+
private $userManager;
41+
42+
/** @var IJobList|MockObject */
43+
private $jobList;
44+
45+
/** @var IRegistry|MockObject */
46+
private $registry;
47+
48+
/** @var Manager|MockObject */
49+
private $manager;
50+
51+
/** @var IUser|MockObject */
52+
private $user;
53+
54+
/** @var CheckBackupCodes */
55+
private $checkBackupCodes;
56+
57+
public function setUp() {
58+
parent::setUp();
59+
60+
$this->userManager = $this->createMock(IUserManager::class);
61+
$this->jobList = $this->createMock(IJobList::class);
62+
$this->registry = $this->createMock(IRegistry::class);
63+
$this->manager = $this->createMock(Manager::class);
64+
65+
$this->user = $this->createMock(IUser::class);
66+
67+
$this->userManager->method('callForSeenUsers')
68+
->will($this->returnCallback(function(\Closure $e) {
69+
$e($this->user);
70+
}));
71+
72+
$this->checkBackupCodes = new CheckBackupCodes(
73+
$this->userManager,
74+
$this->jobList,
75+
$this->manager,
76+
$this->registry
77+
);
78+
}
79+
80+
public function testRunAlreadyGenerated() {
81+
$this->registry->method('getProviderStates')
82+
->with($this->user)
83+
->willReturn(['backup_codes' => true]);
84+
$this->manager->method('isTwoFactorAuthenticated')
85+
->with($this->user)
86+
->willReturn(true);
87+
$this->jobList->expects($this->never())
88+
->method($this->anything());
89+
90+
$this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
91+
}
92+
93+
public function testRun() {
94+
$this->user->method('getUID')
95+
->willReturn('myUID');
96+
97+
$this->registry->expects($this->once())
98+
->method('getProviderStates')
99+
->with($this->user)
100+
->willReturn([
101+
'backup_codes' => false,
102+
]);
103+
$this->jobList->expects($this->once())
104+
->method('add')
105+
->with(
106+
$this->equalTo(RememberBackupCodesJob::class),
107+
['uid' => 'myUID']
108+
);
109+
$this->manager->method('isTwoFactorAuthenticated')
110+
->with($this->user)
111+
->willReturn(true);
112+
113+
$this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
114+
}
115+
116+
public function testRunNoProviders() {
117+
$this->registry->expects($this->once())
118+
->method('getProviderStates')
119+
->with($this->user)
120+
->willReturn([
121+
'backup_codes' => false,
122+
]);
123+
$this->jobList->expects($this->never())
124+
->method($this->anything());
125+
$this->manager->method('isTwoFactorAuthenticated')
126+
->with($this->user)
127+
->willReturn(false);
128+
129+
$this->invokePrivate($this->checkBackupCodes, 'run', [[]]);
130+
}
131+
132+
133+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\TwoFactorBackupCodes\Tests\Unit\Migration;
25+
26+
use OCA\TwoFactorBackupCodes\Event\CodesGenerated;
27+
use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater;
28+
use OCA\TwoFactorBackupCodes\Migration\CheckBackupCodes;
29+
use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
30+
use OCP\Authentication\TwoFactorAuth\IRegistry;
31+
use OCP\BackgroundJob\IJobList;
32+
use OCP\IUser;
33+
use OCP\Migration\IOutput;
34+
use Symfony\Component\EventDispatcher\Event;
35+
use Test\TestCase;
36+
37+
class CheckBackupCodeTest extends TestCase {
38+
39+
/** @var IJobList|\PHPunit\Framework\MockObject\MockObject */
40+
private $jobList;
41+
42+
/** @var CheckBackupCodes */
43+
private $checkBackupsCodes;
44+
45+
protected function setUp() {
46+
parent::setUp();
47+
48+
$this->jobList = $this->createMock(IJobList::class);
49+
$this->checkBackupsCodes = new CheckBackupCodes($this->jobList);
50+
}
51+
52+
public function testGetName() {
53+
$this->assertSame('Add background job to check for backup codes', $this->checkBackupsCodes->getName());
54+
}
55+
56+
public function testRun() {
57+
$this->jobList->expects($this->once())
58+
->method('add')
59+
->with(
60+
$this->equalTo(\OCA\TwoFactorBackupCodes\BackgroundJob\CheckBackupCodes::class)
61+
);
62+
63+
$this->checkBackupsCodes->run($this->createMock(IOutput::class));
64+
}
65+
}

0 commit comments

Comments
 (0)