1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright 2018 Christoph Wurst <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @author 2018 Christoph Wurst <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OC\Authentication\TwoFactorAuth; |
28
|
|
|
|
29
|
|
|
use OCP\IConfig; |
30
|
|
|
use OCP\IGroupManager; |
31
|
|
|
use OCP\IUser; |
32
|
|
|
|
33
|
|
|
class MandatoryTwoFactor { |
34
|
|
|
|
35
|
|
|
/** @var IConfig */ |
36
|
|
|
private $config; |
37
|
|
|
|
38
|
|
|
/** @var IGroupManager */ |
39
|
|
|
private $groupManager; |
40
|
|
|
|
41
|
|
|
public function __construct(IConfig $config, IGroupManager $groupManager) { |
42
|
|
|
$this->config = $config; |
43
|
|
|
$this->groupManager = $groupManager; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the state of enforced two-factor auth |
48
|
|
|
*/ |
49
|
|
|
public function getState(): EnforcementState { |
50
|
|
|
return new EnforcementState( |
51
|
|
|
$this->config->getSystemValue('twofactor_enforced', 'false') === 'true', |
52
|
|
|
$this->config->getSystemValue('twofactor_enforced_groups', []), |
53
|
|
|
$this->config->getSystemValue('twofactor_enforced_excluded_groups', []) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set the state of enforced two-factor auth |
59
|
|
|
*/ |
60
|
|
|
public function setState(EnforcementState $state) { |
61
|
|
|
$this->config->setSystemValue('twofactor_enforced', $state->isEnforced() ? 'true' : 'false'); |
62
|
|
|
$this->config->setSystemValue('twofactor_enforced_groups', $state->getEnforcedGroups()); |
63
|
|
|
$this->config->setSystemValue('twofactor_enforced_excluded_groups', $state->getExcludedGroups()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check if two-factor auth is enforced for a specific user |
68
|
|
|
* |
69
|
|
|
* The admin(s) can enforce two-factor auth system-wide, for certain groups only |
70
|
|
|
* and also have the option to exclude users of certain groups. This method will |
71
|
|
|
* check their membership of those groups. |
72
|
|
|
* |
73
|
|
|
* @param IUser $user |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function isEnforcedFor(IUser $user): bool { |
78
|
|
|
$state = $this->getState(); |
79
|
|
|
if (!$state->isEnforced()) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
$uid = $user->getUID(); |
83
|
|
|
|
84
|
|
|
/* |
85
|
|
|
* If there is a list of enforced groups, we only enforce 2FA for members of those groups. |
86
|
|
|
* For all the other users it is not enforced (overruling the excluded groups list). |
87
|
|
|
*/ |
88
|
|
|
if (!empty($state->getEnforcedGroups())) { |
89
|
|
|
foreach ($state->getEnforcedGroups() as $group) { |
90
|
|
|
if ($this->groupManager->isInGroup($uid, $group)) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
// Not a member of any of these groups -> no 2FA enforced |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* If the user is member of an excluded group, 2FA won't be enforced. |
100
|
|
|
*/ |
101
|
|
|
foreach ($state->getExcludedGroups() as $group) { |
102
|
|
|
if ($this->groupManager->isInGroup($uid, $group)) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* No enforced groups configured and user not member of an excluded groups, |
109
|
|
|
* so 2FA is enforced. |
110
|
|
|
*/ |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|