| 1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace tool_mfa;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Tests for MFA manager class.
|
|
|
21 |
*
|
|
|
22 |
* @package tool_mfa
|
|
|
23 |
* @author Peter Burnett <peterburnett@catalyst-au.net>
|
|
|
24 |
* @copyright Catalyst IT
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
| 1441 |
ariadna |
27 |
final class manager_test extends \advanced_testcase {
|
| 1 |
efrain |
28 |
|
| 1441 |
ariadna |
29 |
use \tool_mfa\tests\mfa_settings_trait;
|
| 1 |
efrain |
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Tests getting the factor total weight
|
|
|
33 |
*
|
|
|
34 |
* @covers ::get_total_weight
|
|
|
35 |
* @covers ::setup_user_factor
|
|
|
36 |
*/
|
| 11 |
efrain |
37 |
public function test_get_total_weight(): void {
|
| 1 |
efrain |
38 |
$this->resetAfterTest(true);
|
|
|
39 |
|
|
|
40 |
// Create and login a user.
|
|
|
41 |
$user = $this->getDataGenerator()->create_user();
|
|
|
42 |
$this->setUser($user);
|
|
|
43 |
|
|
|
44 |
// First get weight with no active factors.
|
|
|
45 |
$this->assertEquals(0, \tool_mfa\manager::get_total_weight());
|
|
|
46 |
|
|
|
47 |
// Now setup a couple of input based factors.
|
|
|
48 |
$this->set_factor_state('totp', 1, 100);
|
|
|
49 |
|
|
|
50 |
$this->set_factor_state('email', 1, 100);
|
|
|
51 |
|
|
|
52 |
// Check weight is still 0 with no passes.
|
|
|
53 |
$this->assertEquals(0, \tool_mfa\manager::get_total_weight());
|
|
|
54 |
|
|
|
55 |
// Manually pass 1 .
|
|
|
56 |
$factor = \tool_mfa\plugininfo\factor::get_factor('totp');
|
|
|
57 |
$totpdata = [
|
|
|
58 |
'secret' => 'fakekey',
|
|
|
59 |
'devicename' => 'fakedevice',
|
|
|
60 |
];
|
|
|
61 |
$this->assertNotEmpty($factor->setup_user_factor((object) $totpdata));
|
|
|
62 |
$factor->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
|
|
|
63 |
$this->assertEquals(100, \tool_mfa\manager::get_total_weight());
|
|
|
64 |
|
|
|
65 |
// Now both.
|
|
|
66 |
$factor2 = \tool_mfa\plugininfo\factor::get_factor('email');
|
|
|
67 |
$factor2->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
|
|
|
68 |
$this->assertEquals(200, \tool_mfa\manager::get_total_weight());
|
|
|
69 |
|
|
|
70 |
// Now setup a no input factor, and check that weight is automatically added without input.
|
|
|
71 |
$this->set_factor_state('auth', 1, 100);
|
|
|
72 |
set_config('goodauth', 'manual', 'factor_auth');
|
|
|
73 |
|
|
|
74 |
$this->assertEquals(300, \tool_mfa\manager::get_total_weight());
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Tests getting the factor status
|
|
|
79 |
*
|
|
|
80 |
* @covers ::get_status
|
|
|
81 |
*/
|
| 11 |
efrain |
82 |
public function test_get_status(): void {
|
| 1 |
efrain |
83 |
$this->resetAfterTest(true);
|
|
|
84 |
|
|
|
85 |
// Create and login a user.
|
|
|
86 |
$user = $this->getDataGenerator()->create_user();
|
|
|
87 |
$this->setUser($user);
|
|
|
88 |
|
| 1441 |
ariadna |
89 |
// Disable the email factor (enabled by default).
|
|
|
90 |
set_config('enabled', 0, 'factor_email');
|
|
|
91 |
|
| 1 |
efrain |
92 |
// Check for fail status with no factors.
|
|
|
93 |
$this->assertEquals(\tool_mfa\plugininfo\factor::STATE_FAIL, \tool_mfa\manager::get_status());
|
|
|
94 |
|
|
|
95 |
// Now add a no input factor.
|
|
|
96 |
$this->set_factor_state('auth', 1, 100);
|
|
|
97 |
set_config('goodauth', 'manual', 'factor_auth');
|
|
|
98 |
|
|
|
99 |
// Check state is now passing.
|
|
|
100 |
$this->assertEquals(\tool_mfa\plugininfo\factor::STATE_PASS, \tool_mfa\manager::get_status());
|
|
|
101 |
|
|
|
102 |
// Now add a failure state factor, and ensure that fail takes precedent.
|
|
|
103 |
$this->set_factor_state('email', 1, 100);
|
|
|
104 |
$factoremail = \tool_mfa\plugininfo\factor::get_factor('email');
|
|
|
105 |
$factoremail->set_state(\tool_mfa\plugininfo\factor::STATE_FAIL);
|
|
|
106 |
|
|
|
107 |
$this->assertEquals(\tool_mfa\plugininfo\factor::STATE_FAIL, \tool_mfa\manager::get_status());
|
|
|
108 |
|
|
|
109 |
// Remove no input factor, and remove fail state by logging in/out. Simulates no data entered yet.
|
|
|
110 |
$this->setUser(null);
|
|
|
111 |
$this->setUser($user);
|
|
|
112 |
$this->set_factor_state('auth', 0, 100);
|
|
|
113 |
$factoremail->set_state(\tool_mfa\plugininfo\factor::STATE_UNKNOWN);
|
|
|
114 |
|
|
|
115 |
$this->assertEquals(\tool_mfa\plugininfo\factor::STATE_NEUTRAL, \tool_mfa\manager::get_status());
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Tests checking if passed enough factors
|
|
|
120 |
*
|
|
|
121 |
* @covers ::passed_enough_factors
|
|
|
122 |
*/
|
| 11 |
efrain |
123 |
public function test_passed_enough_factors(): void {
|
| 1 |
efrain |
124 |
$this->resetAfterTest(true);
|
|
|
125 |
|
|
|
126 |
// Create and login a user.
|
|
|
127 |
$user = $this->getDataGenerator()->create_user();
|
|
|
128 |
$this->setUser($user);
|
|
|
129 |
|
|
|
130 |
// Check when no factors are setup.
|
|
|
131 |
$this->assertEquals(false, \tool_mfa\manager::passed_enough_factors());
|
|
|
132 |
|
|
|
133 |
// Setup a no input factor.
|
|
|
134 |
$this->set_factor_state('auth', 1, 100);
|
|
|
135 |
set_config('goodauth', 'manual', 'factor_auth');
|
|
|
136 |
|
|
|
137 |
// Check that is enough to pass.
|
|
|
138 |
$this->assertEquals(true, \tool_mfa\manager::passed_enough_factors());
|
|
|
139 |
|
|
|
140 |
// Lower the weight of the factor.
|
|
|
141 |
$this->set_factor_state('auth', 1, 75);
|
|
|
142 |
$this->assertEquals(false, \tool_mfa\manager::passed_enough_factors());
|
|
|
143 |
|
|
|
144 |
// Add another factor to get enough weight to pass, but dont set pass state yet.
|
|
|
145 |
$this->set_factor_state('email', 1, 100);
|
|
|
146 |
$factoremail = \tool_mfa\plugininfo\factor::get_factor('email');
|
|
|
147 |
$this->assertEquals(false, \tool_mfa\manager::passed_enough_factors());
|
|
|
148 |
|
|
|
149 |
// Now pass the factor and check weight.
|
|
|
150 |
$factoremail->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
|
|
|
151 |
$this->assertEquals(true, \tool_mfa\manager::passed_enough_factors());
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* The data provider for whether urls should be redirected or not
|
|
|
156 |
*
|
|
|
157 |
* @return array
|
|
|
158 |
*/
|
| 1441 |
ariadna |
159 |
public static function should_redirect_urls_provider(): array {
|
| 1 |
efrain |
160 |
$badurl1 = new \moodle_url('/');
|
|
|
161 |
$badparam1 = $badurl1->out();
|
|
|
162 |
$badurl2 = new \moodle_url('admin/tool/mfa/auth.php');
|
|
|
163 |
$badparam2 = $badurl2->out();
|
|
|
164 |
return [
|
|
|
165 |
['/', 'http://test.server', true],
|
|
|
166 |
['/admin/tool/mfa/action.php', 'http://test.server', true],
|
|
|
167 |
['/admin/tool/mfa/factor/totp/settings.php', 'http://test.server', true],
|
|
|
168 |
['/', 'http://test.server', true, ['url' => $badparam1]],
|
|
|
169 |
['/', 'http://test.server', true, ['url' => $badparam2]],
|
|
|
170 |
['/admin/tool/mfa/auth.php', 'http://test.server', false],
|
|
|
171 |
['/admin/tool/mfa/auth.php', 'http://test.server/parent/directory', false],
|
|
|
172 |
['/admin/tool/mfa/action.php', 'http://test.server/parent/directory', true],
|
|
|
173 |
['/', 'http://test.server/parent/directory', true, ['url' => $badparam1]],
|
|
|
174 |
['/', 'http://test.server/parent/directory', true, ['url' => $badparam2]],
|
|
|
175 |
];
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Tests whether it should require mfa
|
|
|
180 |
*
|
|
|
181 |
* @covers ::should_require_mfa
|
|
|
182 |
* @param string $urlstring
|
|
|
183 |
* @param string $webroot
|
|
|
184 |
* @param bool $status
|
|
|
185 |
* @param array|null $params
|
|
|
186 |
* @dataProvider should_redirect_urls_provider
|
|
|
187 |
*/
|
| 11 |
efrain |
188 |
public function test_should_require_mfa_urls($urlstring, $webroot, $status, $params = null): void {
|
| 1 |
efrain |
189 |
$this->resetAfterTest(true);
|
|
|
190 |
global $CFG;
|
|
|
191 |
$user = $this->getDataGenerator()->create_user();
|
|
|
192 |
$this->setUser($user);
|
|
|
193 |
$CFG->wwwroot = $webroot;
|
|
|
194 |
$url = new \moodle_url($urlstring, $params);
|
|
|
195 |
$this->assertEquals($status, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Tests whether it should require the mfa checks
|
|
|
200 |
*
|
|
|
201 |
* @covers ::should_require_mfa
|
|
|
202 |
*/
|
| 11 |
efrain |
203 |
public function test_should_require_mfa_checks(): void {
|
| 1 |
efrain |
204 |
// Setup test and user.
|
|
|
205 |
global $CFG;
|
|
|
206 |
$this->resetAfterTest(true);
|
|
|
207 |
$user = $this->getDataGenerator()->create_user();
|
|
|
208 |
|
|
|
209 |
$badurl = new \moodle_url('/');
|
|
|
210 |
|
|
|
211 |
// Upgrade checks.
|
|
|
212 |
$this->setAdminUser();
|
|
|
213 |
// Mark the site as upgraded so it will not fail when running the unittest as a whole.
|
|
|
214 |
$CFG->allversionshash = \core_component::get_all_versions_hash();
|
|
|
215 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
216 |
$oldhash = $CFG->allversionshash;
|
|
|
217 |
$CFG->allversionshash = 'abc';
|
|
|
218 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
219 |
$CFG->allversionshash = $oldhash;
|
|
|
220 |
$upgradesettings = new \moodle_url('/admin/upgradesettings.php');
|
|
|
221 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($upgradesettings, false));
|
|
|
222 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
223 |
|
|
|
224 |
// Admin not setup.
|
|
|
225 |
$this->setUser($user);
|
|
|
226 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
227 |
$CFG->adminsetuppending = 1;
|
|
|
228 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
229 |
$CFG->adminsetuppending = 0;
|
|
|
230 |
|
|
|
231 |
// Check prevent_redirect.
|
|
|
232 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
233 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, true));
|
|
|
234 |
|
|
|
235 |
// User not setup properly.
|
|
|
236 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
237 |
$notsetup = clone($user);
|
|
|
238 |
unset($notsetup->firstname);
|
|
|
239 |
$this->setUser($notsetup);
|
|
|
240 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
241 |
$this->setUser($user);
|
|
|
242 |
|
|
|
243 |
// Guest User.
|
|
|
244 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
245 |
$this->setGuestUser();
|
|
|
246 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
247 |
$this->setUser($user);
|
|
|
248 |
|
|
|
249 |
// Forced password changes.
|
|
|
250 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
251 |
set_user_preference('auth_forcepasswordchange', true);
|
|
|
252 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
253 |
set_user_preference('auth_forcepasswordchange', false);
|
|
|
254 |
|
|
|
255 |
// Login as check.
|
|
|
256 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
257 |
$syscontext = \context_system::instance();
|
|
|
258 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
259 |
$this->setAdminUser();
|
|
|
260 |
\core\session\manager::loginas($user2->id, $syscontext, false);
|
|
|
261 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($badurl, false));
|
|
|
262 |
$this->setUser($user);
|
| 1441 |
ariadna |
263 |
|
|
|
264 |
// Access logocompact via pluginfile.
|
|
|
265 |
$logourl = new \moodle_url('/pluginfile.php/1/core_admin/logocompact/');
|
|
|
266 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($logourl, false));
|
|
|
267 |
|
|
|
268 |
// Access logo via pluginfile.
|
|
|
269 |
$logourl = new \moodle_url('/pluginfile.php/1/core_admin/logo/');
|
|
|
270 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($logourl, false));
|
|
|
271 |
|
|
|
272 |
// Access favicon via pluginfile.
|
|
|
273 |
$logourl = new \moodle_url('/pluginfile.php/1/core_admin/favicon/');
|
|
|
274 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($logourl, false));
|
|
|
275 |
|
|
|
276 |
// Access guidance files.
|
|
|
277 |
$guideurl = new \moodle_url('/pluginfile.php/1/tool_mfa/guidance/0/capybara.png');
|
|
|
278 |
$this->assertEquals(\tool_mfa\manager::NO_REDIRECT, \tool_mfa\manager::should_require_mfa($guideurl, false));
|
|
|
279 |
|
|
|
280 |
// Access private area.
|
|
|
281 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
282 |
$privateurl = new \moodle_url("/pluginfile.php/{$user3->id}/user/private/privatefile.png");
|
|
|
283 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($privateurl, false));
|
| 1 |
efrain |
284 |
}
|
|
|
285 |
|
|
|
286 |
/**
|
|
|
287 |
* Tests should require the mfa redirection loop
|
|
|
288 |
*
|
|
|
289 |
* @covers ::should_require_mfa
|
|
|
290 |
*/
|
| 11 |
efrain |
291 |
public function test_should_require_mfa_redirection_loop(): void {
|
| 1 |
efrain |
292 |
// Setup test and user.
|
|
|
293 |
global $CFG, $SESSION;
|
|
|
294 |
$CFG->wwwroot = 'http://phpunit.test';
|
|
|
295 |
$this->resetAfterTest(true);
|
|
|
296 |
$user = $this->getDataGenerator()->create_user();
|
|
|
297 |
$this->setUser($user);
|
|
|
298 |
|
|
|
299 |
// Set first referer url.
|
|
|
300 |
$_SERVER['HTTP_REFERER'] = 'http://phpunit.test';
|
|
|
301 |
$url = new \moodle_url('/');
|
|
|
302 |
|
|
|
303 |
// Test you get three redirs then exception.
|
|
|
304 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
305 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
306 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
307 |
// Set count to threshold.
|
|
|
308 |
$SESSION->mfa_redir_count = 5;
|
|
|
309 |
$this->assertEquals(\tool_mfa\manager::REDIRECT_EXCEPTION, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
310 |
// Reset session vars.
|
|
|
311 |
unset($SESSION->mfa_redir_referer);
|
|
|
312 |
unset($SESSION->mfa_redir_count);
|
|
|
313 |
|
|
|
314 |
// Test 4 different redir urls.
|
|
|
315 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
316 |
$_SERVER['HTTP_REFERER'] = 'http://phpunit.test/2';
|
|
|
317 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
318 |
$_SERVER['HTTP_REFERER'] = 'http://phpunit3.test/3';
|
|
|
319 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
320 |
$_SERVER['HTTP_REFERER'] = 'http://phpunit4.test/4';
|
|
|
321 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
322 |
// Reset session vars.
|
|
|
323 |
unset($SESSION->mfa_redir_referer);
|
|
|
324 |
unset($SESSION->mfa_redir_count);
|
|
|
325 |
|
|
|
326 |
// Test 6 then jump to new referer (5 + 1 to set the first time).
|
|
|
327 |
$_SERVER['HTTP_REFERER'] = 'http://phpunit.test';
|
|
|
328 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
329 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
330 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
331 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
332 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
333 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
334 |
|
|
|
335 |
$_SERVER['HTTP_REFERER'] = 'http://phpunit.test/2';
|
|
|
336 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
337 |
// Now test that going back to original URL doesnt cause exception.
|
|
|
338 |
$_SERVER['HTTP_REFERER'] = 'http://phpunit.test';
|
|
|
339 |
$this->assertEquals(\tool_mfa\manager::REDIRECT, \tool_mfa\manager::should_require_mfa($url, false));
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
/**
|
|
|
343 |
* Tests checking for possible setup factor
|
|
|
344 |
*
|
|
|
345 |
* @covers ::possible_factor_setup
|
|
|
346 |
* @covers ::setup_user_factor
|
|
|
347 |
*/
|
| 11 |
efrain |
348 |
public function test_possible_factor_setup(): void {
|
| 1 |
efrain |
349 |
// Setup test and user.
|
|
|
350 |
$this->resetAfterTest(true);
|
|
|
351 |
$user = $this->getDataGenerator()->create_user();
|
|
|
352 |
$this->setUser($user);
|
|
|
353 |
|
|
|
354 |
// Test for totp is able to be setup.
|
|
|
355 |
set_config('enabled', 1, 'factor_totp');
|
|
|
356 |
$this->assertTrue(\tool_mfa\manager::possible_factor_setup());
|
|
|
357 |
set_config('enabled', 0, 'factor_totp');
|
|
|
358 |
|
|
|
359 |
// Test TOTP is already setup and can be managed.
|
|
|
360 |
$totp = \tool_mfa\plugininfo\factor::get_factor('totp');
|
|
|
361 |
set_config('enabled', 1, 'factor_totp');
|
|
|
362 |
$totpdata = [
|
|
|
363 |
'secret' => 'fakekey',
|
|
|
364 |
'devicename' => 'fakedevice',
|
|
|
365 |
];
|
|
|
366 |
$this->assertNotEmpty($totp->setup_user_factor((object) $totpdata));
|
|
|
367 |
$this->assertTrue(\tool_mfa\manager::possible_factor_setup());
|
|
|
368 |
set_config('enabled', 0, 'factor_totp');
|
|
|
369 |
|
|
|
370 |
// Test no factors can be setup.
|
|
|
371 |
set_config('enabled', 1, 'factor_email');
|
|
|
372 |
set_config('enabled', 1, 'factor_admin');
|
|
|
373 |
$this->assertFalse(\tool_mfa\manager::possible_factor_setup());
|
|
|
374 |
set_config('enabled', 0, 'factor_email');
|
|
|
375 |
set_config('enabled', 0, 'factor_admin');
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
/**
|
|
|
379 |
* Tests checking if a factor is ready
|
|
|
380 |
*
|
|
|
381 |
* @covers ::is_ready
|
|
|
382 |
*/
|
| 11 |
efrain |
383 |
public function test_is_ready(): void {
|
| 1 |
efrain |
384 |
// Setup test and user.
|
|
|
385 |
global $CFG;
|
|
|
386 |
$this->resetAfterTest(true);
|
|
|
387 |
$user = $this->getDataGenerator()->create_user();
|
|
|
388 |
$this->setUser($user);
|
|
|
389 |
set_config('enabled', 1, 'factor_nosetup');
|
|
|
390 |
set_config('enabled', 1, 'tool_mfa');
|
| 1441 |
ariadna |
391 |
// Disable the email factor (enabled by default).
|
|
|
392 |
set_config('enabled', 0, 'factor_email');
|
| 1 |
efrain |
393 |
|
|
|
394 |
// Capability Check.
|
|
|
395 |
$this->assertTrue(\tool_mfa\manager::is_ready());
|
|
|
396 |
// Swap to role without capability.
|
|
|
397 |
$this->setGuestUser();
|
|
|
398 |
$this->assertFalse(\tool_mfa\manager::is_ready());
|
|
|
399 |
$this->setUser($user);
|
|
|
400 |
|
|
|
401 |
// Enabled check.
|
|
|
402 |
$this->assertTrue(\tool_mfa\manager::is_ready());
|
|
|
403 |
set_config('enabled', 0, 'tool_mfa');
|
|
|
404 |
$this->assertFalse(\tool_mfa\manager::is_ready());
|
|
|
405 |
set_config('enabled', 1, 'tool_mfa');
|
|
|
406 |
|
|
|
407 |
// Upgrade check.
|
|
|
408 |
$this->assertTrue(\tool_mfa\manager::is_ready());
|
|
|
409 |
$CFG->upgraderunning = true;
|
|
|
410 |
$this->assertFalse(\tool_mfa\manager::is_ready());
|
|
|
411 |
unset($CFG->upgraderunning);
|
|
|
412 |
|
|
|
413 |
// No factors check.
|
|
|
414 |
$this->assertTrue(\tool_mfa\manager::is_ready());
|
|
|
415 |
set_config('enabled', 0, 'factor_nosetup');
|
|
|
416 |
$this->assertFalse(\tool_mfa\manager::is_ready());
|
|
|
417 |
set_config('enabled', 1, 'factor_nosetup');
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
/**
|
|
|
421 |
* Tests core hooks
|
|
|
422 |
*
|
|
|
423 |
* @covers ::mfa_config_hook_test
|
|
|
424 |
* @covers ::mfa_login_hook_test
|
|
|
425 |
*/
|
| 11 |
efrain |
426 |
public function test_core_hooks(): void {
|
| 1 |
efrain |
427 |
// Setup test and user.
|
|
|
428 |
global $CFG, $SESSION;
|
|
|
429 |
$this->resetAfterTest(true);
|
|
|
430 |
$user = $this->getDataGenerator()->create_user();
|
|
|
431 |
$this->setUser($user);
|
|
|
432 |
|
|
|
433 |
// Require login to fire hooks. Config we get for free.
|
|
|
434 |
require_login();
|
|
|
435 |
|
|
|
436 |
$this->assertTrue($CFG->mfa_config_hook_test);
|
|
|
437 |
$this->assertTrue($SESSION->mfa_login_hook_test);
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
/**
|
|
|
441 |
* Tests circular redirect auth
|
|
|
442 |
*
|
|
|
443 |
* @covers ::should_require_mfa
|
|
|
444 |
*/
|
| 11 |
efrain |
445 |
public function test_circular_redirect_auth(): void {
|
| 1 |
efrain |
446 |
// Setup test and user.
|
|
|
447 |
$this->resetAfterTest(true);
|
|
|
448 |
$user = $this->getDataGenerator()->create_user();
|
|
|
449 |
$this->setUser($user);
|
|
|
450 |
|
|
|
451 |
// Spoof the referrer for the redirect check.
|
|
|
452 |
$_SERVER['HTTP_REFERER'] = '/admin/tool/mfa/auth.php';
|
|
|
453 |
$baseurl = new \moodle_url('/my/naughty/page.php');
|
|
|
454 |
|
|
|
455 |
// After a single check, we should redirect.
|
|
|
456 |
$this->assertEquals(\tool_mfa\manager::REDIRECT,
|
|
|
457 |
\tool_mfa\manager::should_require_mfa($baseurl, false));
|
|
|
458 |
|
|
|
459 |
// Now hammer it up to the threshold to emulate a repeated force browse from auth.php.
|
|
|
460 |
for ($i = 0; $i < \tool_mfa\manager::REDIR_LOOP_THRESHOLD; $i++) {
|
|
|
461 |
\tool_mfa\manager::should_require_mfa($baseurl, false);
|
|
|
462 |
}
|
|
|
463 |
|
|
|
464 |
// Now finally confirm that a 6th access attempt (after loop safety trigger) still redirects.
|
|
|
465 |
$this->assertEquals(\tool_mfa\manager::REDIRECT,
|
|
|
466 |
\tool_mfa\manager::should_require_mfa($baseurl, false));
|
|
|
467 |
}
|
|
|
468 |
}
|