X Tutup
Skip to content

Commit 8dbbb07

Browse files
committed
Enforce 2fa with exclusion groups
1 parent add4323 commit 8dbbb07

File tree

6 files changed

+141
-2
lines changed

6 files changed

+141
-2
lines changed

lib/private/Authentication/TwoFactorAuth/Manager.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OCP\ILogger;
3434
use OCP\ISession;
3535
use OCP\IUser;
36+
use OCP\IGroupManager;
3637

3738
class Manager {
3839
public const SESSION_UID_KEY = 'two_factor_auth_uid';
@@ -43,6 +44,9 @@ class Manager {
4344
/** @var ISession */
4445
private $session;
4546

47+
/** @var IGroupManager */
48+
private $groupManager;
49+
4650
/** @var IConfig */
4751
private $config;
4852

@@ -55,13 +59,15 @@ class Manager {
5559
/**
5660
* @param AppManager $appManager
5761
* @param ISession $session
62+
* @param IGroupManager $groupManager
5863
* @param IConfig $config
5964
* @param IRequest $request
6065
* @param ILogger $logger
6166
*/
62-
public function __construct(AppManager $appManager, ISession $session, IConfig $config, IRequest $request, ILogger $logger) {
67+
public function __construct(AppManager $appManager, ISession $session, IGroupManager $groupManager, IConfig $config, IRequest $request, ILogger $logger) {
6368
$this->appManager = $appManager;
6469
$this->session = $session;
70+
$this->groupManager = $groupManager;
6571
$this->config = $config;
6672
$this->request = $request;
6773
$this->logger = $logger;
@@ -74,10 +80,29 @@ public function __construct(AppManager $appManager, ISession $session, IConfig $
7480
* @return boolean
7581
*/
7682
public function isTwoFactorAuthenticated(IUser $user) {
83+
if ($this->isTwoFactorEnforcedForUser($user)) {
84+
return \count($this->getProviders($user)) > 0;
85+
}
7786
$twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0;
7887
return $twoFactorEnabled && \count($this->getProviders($user)) > 0;
7988
}
8089

90+
public function isTwoFactorEnforcedForUser(IUser $user) {
91+
if ($this->config->getAppValue('core', 'enforce_2fa', 'no') !== 'yes') {
92+
return false;
93+
}
94+
95+
$enforce2faExcludedGroups = \json_decode($this->config->getAppValue('core', 'enforce_2fa_excluded_groups', '[]'), true);
96+
if (!empty($enforce2faExcludedGroups)) {
97+
foreach ($enforce2faExcludedGroups as $group) {
98+
if ($this->groupManager->isInGroup($user->getUID(), $group)) {
99+
return false;
100+
}
101+
}
102+
}
103+
return true;
104+
}
105+
81106
/**
82107
* Disable 2FA checks for the given user
83108
*
@@ -135,6 +160,10 @@ public function getProviders(IUser $user) {
135160
}
136161
}
137162

163+
if ($this->isTwoFactorEnforcedForUser($user)) {
164+
return $providers;
165+
}
166+
138167
return \array_filter($providers, function ($provider) use ($user) {
139168
/* @var $provider IProvider */
140169
return $provider->isTwoFactorAuthEnabledForUser($user);

lib/private/Server.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,14 @@ public function __construct($webRoot, \OC\Config $config) {
402402
});
403403

404404
$this->registerService('\OC\Authentication\TwoFactorAuth\Manager', function (Server $c) {
405-
return new \OC\Authentication\TwoFactorAuth\Manager($c->getAppManager(), $c->getSession(), $c->getConfig(), $c->getRequest(), $c->getLogger());
405+
return new \OC\Authentication\TwoFactorAuth\Manager(
406+
$c->getAppManager(),
407+
$c->getSession(),
408+
$c->getGroupManager(),
409+
$c->getConfig(),
410+
$c->getRequest(),
411+
$c->getLogger()
412+
);
406413
});
407414

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

lib/private/Settings/SettingsManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use OC\Settings\Panels\Personal\Tokens;
5050
use OC\Settings\Panels\Personal\Cors;
5151
use OC\Settings\Panels\Personal\Quota;
52+
use OC\Settings\Panels\Admin\Enforce2fa;
5253
use OC\Settings\Panels\Admin\BackgroundJobs;
5354
use OC\Settings\Panels\Admin\Certificates;
5455
use OC\Settings\Panels\Admin\Encryption;
@@ -236,6 +237,7 @@ private function getBuiltInSections($type) {
236237
private function getBuiltInPanels($type) {
237238
if ($type === 'admin') {
238239
return [
240+
Enforce2fa::class,
239241
LegacyAdmin::class,
240242
BackgroundJobs::class,
241243
Logging::class,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2023, ownCloud GmbH
4+
* @license AGPL-3.0
5+
*
6+
* This code is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License, version 3,
8+
* as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License, version 3,
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>
17+
*
18+
*/
19+
20+
namespace OC\Settings\Panels\Admin;
21+
22+
use OCP\Settings\ISettings;
23+
use OCP\Template;
24+
use OCP\IConfig;
25+
26+
class Enforce2fa implements ISettings {
27+
/** @var IConfig */
28+
protected $config;
29+
30+
public function __construct(IConfig $config) {
31+
$this->config = $config;
32+
}
33+
34+
public function getPriority() {
35+
return 0;
36+
}
37+
38+
public function getPanel() {
39+
$enforce2faExcludedGroups = \json_decode($this->config->getAppValue('core', 'enforce_2fa_excluded_groups', '[]'), true);
40+
$tmpl = new Template('settings', 'panels/admin/enforce2fa');
41+
$tmpl->assign('enforce2fa', $this->config->getAppValue('core', 'enforce_2fa', 'no') === 'yes');
42+
$tmpl->assign('enforce2faExcludedGroups', \implode('|', $enforce2faExcludedGroups));
43+
return $tmpl;
44+
}
45+
46+
public function getSectionID() {
47+
return 'security';
48+
}
49+
}

settings/js/panels/enforce2fa.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
$(document).ready(function() {
2+
3+
var enforce2faGroupsList = $('#enforce_2fa_excluded_groups');
4+
OC.Settings.setupGroupsSelect(enforce2faGroupsList);
5+
enforce2faGroupsList.change(function(ev) {
6+
OC.AppConfig.setValue('core', 'enforce_2fa_excluded_groups', JSON.stringify(ev.val || []));
7+
});
8+
9+
$('#enforce_2fa').change(function() {
10+
var name = $(this).attr('name');
11+
var value;
12+
if (this.checked) {
13+
value = 'yes';
14+
} else {
15+
value = 'no';
16+
}
17+
OC.AppConfig.setValue('core', name, value);
18+
});
19+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
script('settings', 'panels/enforce2fa');
3+
?>
4+
<div class="section" id="2fa">
5+
<h2 class="app-name"><?php p($l->t('Two-factor Authentication'));?></h2>
6+
<p>
7+
<em><?php p($l->t('This section requires a two-factor authentication app to be installed in ownCloud')); ?></em>
8+
</p>
9+
<h3><?php p($l->t('Enforce two-factor authentication')); ?></h3>
10+
<p><?php p($l->t('Before enforcing the two-factor authentication, check the following requirements:')); ?></p>
11+
<ul>
12+
<li><?php p($l->t('At least a two-factor authentication app is installed and enabled in ownCloud.')); ?></li>
13+
<li><?php p($l->t('The users can setup at least a two-factor app from the challenge page. Some apps might not be prepared for this')); ?></li>
14+
</ul>
15+
<p><?php p($l->t('The "twofactor_totp" app fulfills those requirements, and might be used as a fallback so the users can enter their accounts in order to configure other two-factor authentication apps')); ?></p>
16+
<br/>
17+
<p>
18+
<input type="checkbox" id="enforce_2fa" name="enforce_2fa" value="1" <?php if ($_['enforce2fa']) {
19+
print_unescaped('checked="checked"');
20+
}?> />
21+
<label for="enforce_2fa"><?php p($l->t('Enforce two-factor authentication to all the users')); ?></label>
22+
</p>
23+
<br/>
24+
<p>
25+
<?php p($l->t('Exclude the following groups of enforcing the two-factor authentication')); ?>
26+
<br />
27+
<input name="enforce_2fa_excluded_groups" type="hidden" id="enforce_2fa_excluded_groups" value="<?php p($_['enforce2faExcludedGroups']) ?>" style="width: 400px">
28+
<em>
29+
<br />
30+
<?php p($l->t('Users in these groups can use two-factor authentication on their own')); ?>
31+
</em>
32+
</p>
33+
</div>

0 commit comments

Comments
 (0)
X Tutup