Skip to content

Commit 9b7c45b

Browse files
authored
Merge pull request #454 from catalyst/453-grace-mode-unknown-factors-fix
[#453] Ignore unknown factors when calculating passing score in grace…
2 parents 29963a6 + 5ec4b88 commit 9b7c45b

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

factor/grace/classes/factor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function get_state($redirectable = true) {
135135
}
136136

137137
// We should never redirect if we have already passed.
138-
if ($redirectable && \tool_mfa\manager::get_cumulative_weight() >= 100) {
138+
if ($redirectable && \tool_mfa\manager::get_total_weight() >= 100) {
139139
$redirectable = false;
140140
}
141141

factor/grace/tests/factor_test.php

+56
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @author Peter Burnett <[email protected]>
2424
* @copyright Catalyst IT
2525
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
* @covers \factor_grace
2627
*/
2728
class factor_test extends \advanced_testcase {
2829

@@ -125,4 +126,59 @@ public function test_gracemode_expires_noredirect() {
125126

126127
$this->assertFalse($redirected, 'The function cause a redirect, where none was expected.');
127128
}
129+
130+
/**
131+
* Tests that gracemode correctly calculates passing weight even when factors can return UNKNOWN
132+
*/
133+
public function test_gracemode_unknown_factor_handling(): void {
134+
$this->resetAfterTest(true);
135+
$user1 = $this->getDataGenerator()->create_user();
136+
$user2 = $this->getDataGenerator()->create_user();
137+
$grace = \tool_mfa\plugininfo\factor::get_factor('grace');
138+
139+
set_config('enabled', 1, 'factor_totp');
140+
set_config('enabled', 1, 'factor_exemption');
141+
set_config('enabled', 1, 'factor_grace');
142+
set_config('enabled', 1, 'factor_loginbanner');
143+
set_config('forcesetup', 1, 'factor_grace');
144+
set_config('graceperiod', -1, 'factor_grace'); // Grace period expired.
145+
146+
// Setup the order so that the loginbanner is at the top.
147+
set_config('factor_order', 'loginbanner,exemption,totp,grace');
148+
149+
// Login banner should give no points.
150+
set_config('weight', 0, 'factor_loginbanner');
151+
152+
// Add exemption for user 1, but NOT user 2.
153+
\factor_exemption\factor::add_exemption($user1);
154+
155+
// Get state for user 1. This should return a neutral and not redirect.
156+
$this->setUser($user1);
157+
$this->assertEquals(\tool_mfa\plugininfo\factor::STATE_NEUTRAL, $grace->get_state(true));
158+
$redirected = null;
159+
try {
160+
$grace->get_state(true);
161+
$redirected = false;
162+
} catch (\Throwable $e) {
163+
$expected = get_string('redirecterrordetected', 'error');
164+
if ($expected === $e->getMessage()) {
165+
$redirected = true;
166+
}
167+
}
168+
$this->assertFalse($redirected);
169+
170+
// Get state for user 2. This should redirect them to the totp setup since they do not pass the exemption.
171+
$this->setUser($user2);
172+
$redirected = null;
173+
try {
174+
$grace->get_state(true);
175+
$redirected = false;
176+
} catch (\Throwable $e) {
177+
$expected = get_string('redirecterrordetected', 'error');
178+
if ($expected === $e->getMessage()) {
179+
$redirected = true;
180+
}
181+
}
182+
$this->assertTrue($redirected);
183+
}
128184
}

0 commit comments

Comments
 (0)