Skip to content

Commit eecd262

Browse files
add OCC command to enable/disable 2FA for a user
1 parent 6f85fe9 commit eecd262

File tree

6 files changed

+170
-3
lines changed

6 files changed

+170
-3
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* @author Christoph Wurst <christoph@owncloud.com>
5+
*
6+
* @copyright Copyright (c) 2016, ownCloud, Inc.
7+
* @license AGPL-3.0
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
23+
namespace OC\Core\Command\TwoFactorAuth;
24+
25+
use OC\Authentication\TwoFactorAuth\Manager;
26+
use OC\User\Manager as UserManager;
27+
use OC\Core\Command\Base;
28+
use Symfony\Component\Console\Input\InputArgument;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Output\OutputInterface;
31+
32+
class Disable extends Base {
33+
34+
/** @var Manager */
35+
private $manager;
36+
37+
/** @var UserManager */
38+
private $userManager;
39+
40+
public function __construct(Manager $manager, UserManager $userManager) {
41+
parent::__construct('twofactorauth:disable');
42+
$this->manager = $manager;
43+
$this->userManager = $userManager;
44+
}
45+
46+
protected function configure() {
47+
parent::configure();
48+
49+
$this->setName('twofactorauth:disable');
50+
$this->setDescription('Disable 2FA for a user');
51+
$this->addArgument('uid', InputArgument::REQUIRED);
52+
}
53+
54+
protected function execute(InputInterface $input, OutputInterface $output) {
55+
$uid = $input->getArgument('uid');
56+
$user = $this->userManager->get($uid);
57+
if (is_null($user)) {
58+
$output->writeln("<error>Invalid UID</error>");
59+
return;
60+
}
61+
$this->manager->disableTwoFactorAuthentication($user);
62+
$output->writeln("2FA disabled for user $uid");
63+
}
64+
65+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* @author Christoph Wurst <christoph@owncloud.com>
5+
*
6+
* @copyright Copyright (c) 2016, ownCloud, Inc.
7+
* @license AGPL-3.0
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
23+
namespace OC\Core\Command\TwoFactorAuth;
24+
25+
use OC\Authentication\TwoFactorAuth\Manager;
26+
use OC\User\Manager as UserManager;
27+
use OC\Core\Command\Base;
28+
use Symfony\Component\Console\Input\InputArgument;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Output\OutputInterface;
31+
32+
class Enable extends Base {
33+
34+
/** @var Manager */
35+
private $manager;
36+
37+
/** @var UserManager */
38+
private $userManager;
39+
40+
public function __construct(Manager $manager, UserManager $userManager) {
41+
parent::__construct('twofactorauth:enable');
42+
$this->manager = $manager;
43+
$this->userManager = $userManager;
44+
}
45+
46+
protected function configure() {
47+
parent::configure();
48+
49+
$this->setName('twofactorauth:enable');
50+
$this->setDescription('Enable 2FA for a user');
51+
$this->addArgument('uid', InputArgument::REQUIRED);
52+
}
53+
54+
protected function execute(InputInterface $input, OutputInterface $output) {
55+
$uid = $input->getArgument('uid');
56+
$user = $this->userManager->get($uid);
57+
if (is_null($user)) {
58+
$output->writeln("<error>Invalid UID</error>");
59+
return;
60+
}
61+
$this->manager->enableTwoFactorAuthentication($user);
62+
$output->writeln("2FA enabled for user $uid");
63+
}
64+
65+
}

core/Middleware/TwoFactorMiddleware.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function beforeController($controller, $methodName) {
7272

7373
if ($this->twoFactorManager->isTwoFactorAuthenticated($user)) {
7474
$this->checkTwoFactor($controller, $methodName);
75+
} else if ($controller instanceof TwoFactorChallengeController) {
76+
// Allow access to the two-factor controllers only if two-factor authentication
77+
// is in progress.
78+
throw new UserAlreadyLoggedInException();
7579
}
7680
}
7781
// TODO: force login if controller != LoginController

core/register_command.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
$application->add(new \OC\Core\Command\Integrity\CheckCore(
5151
\OC::$server->getIntegrityCodeChecker()
5252
));
53+
$application->add(new OC\Core\Command\TwoFactorAuth\Enable(
54+
\OC::$server->getTwoFactorAuthManager(),
55+
\OC::$server->getUserManager()
56+
));
57+
$application->add(new OC\Core\Command\TwoFactorAuth\Disable(
58+
\OC::$server->getTwoFactorAuthManager(),
59+
\OC::$server->getUserManager()
60+
));
5361

5462

5563
if (\OC::$server->getConfig()->getSystemValue('installed', false)) {

lib/private/Authentication/TwoFactorAuth/Manager.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OC\App\AppManager;
2727
use OCP\AppFramework\QueryException;
2828
use OCP\Authentication\TwoFactorAuth\IProvider;
29+
use OCP\IConfig;
2930
use OCP\ISession;
3031
use OCP\IUser;
3132

@@ -39,13 +40,18 @@ class Manager {
3940
/** @var ISession */
4041
private $session;
4142

43+
/** @var IConfig */
44+
private $config;
45+
4246
/**
4347
* @param AppManager $appManager
4448
* @param ISession $session
49+
* @param IConfig $config
4550
*/
46-
public function __construct(AppManager $appManager, ISession $session) {
51+
public function __construct(AppManager $appManager, ISession $session, IConfig $config) {
4752
$this->appManager = $appManager;
4853
$this->session = $session;
54+
$this->config = $config;
4955
}
5056

5157
/**
@@ -55,7 +61,26 @@ public function __construct(AppManager $appManager, ISession $session) {
5561
* @return boolean
5662
*/
5763
public function isTwoFactorAuthenticated(IUser $user) {
58-
return count($this->getProviders($user)) > 0;
64+
$twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0;
65+
return $twoFactorEnabled && count($this->getProviders($user)) > 0;
66+
}
67+
68+
/**
69+
* Disable 2FA checks for the given user
70+
*
71+
* @param IUser $user
72+
*/
73+
public function disableTwoFactorAuthentication(IUser $user) {
74+
$this->config->setUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 1);
75+
}
76+
77+
/**
78+
* Enable all 2FA checks for the given user
79+
*
80+
* @param IUser $user
81+
*/
82+
public function enableTwoFactorAuthentication(IUser $user) {
83+
$this->config->deleteUserValue($user->getUID(), 'core', 'two_factor_auth_disabled');
5984
}
6085

6186
/**

lib/private/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function __construct($webRoot, \OC\Config $config) {
279279
});
280280

281281
$this->registerService('\OC\Authentication\TwoFactorAuth\Manager', function (Server $c) {
282-
return new \OC\Authentication\TwoFactorAuth\Manager($c->getAppManager(), $c->getSession());
282+
return new \OC\Authentication\TwoFactorAuth\Manager($c->getAppManager(), $c->getSession(), $c->getConfig());
283283
});
284284

285285
$this->registerService('NavigationManager', function ($c) {

0 commit comments

Comments
 (0)