Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 factor_totp;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
require_once(__DIR__.'/../extlib/OTPHP/OTPInterface.php');
22
require_once(__DIR__.'/../extlib/OTPHP/TOTPInterface.php');
23
require_once(__DIR__.'/../extlib/OTPHP/ParameterTrait.php');
1441 ariadna 24
require_once(__DIR__.'/../extlib/OTPHP/InternalClock.php');
1 efrain 25
require_once(__DIR__.'/../extlib/OTPHP/OTP.php');
26
require_once(__DIR__.'/../extlib/OTPHP/TOTP.php');
27
 
28
require_once(__DIR__.'/../extlib/ParagonIE/ConstantTime/EncoderInterface.php');
29
require_once(__DIR__.'/../extlib/ParagonIE/ConstantTime/Binary.php');
30
require_once(__DIR__.'/../extlib/ParagonIE/ConstantTime/Base32.php');
31
 
32
/**
33
 * Tests for TOTP factor.
34
 *
35
 * @covers      \factor_totp\factor
36
 * @package     factor_totp
37
 * @author      Peter Burnett <peterburnett@catalyst-au.net>
38
 * @copyright   Catalyst IT
39
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
1441 ariadna 41
final class factor_test extends \advanced_testcase {
1 efrain 42
 
43
    /**
44
     * Test code validation of the TOTP factor
45
     */
11 efrain 46
    public function test_validate_code(): void {
1 efrain 47
        global $DB;
48
 
1441 ariadna 49
        $clock = $this->mock_clock_with_frozen(10000000);
50
 
1 efrain 51
        $this->resetAfterTest(true);
52
        $user = $this->getDataGenerator()->create_user();
53
        $this->setUser($user);
54
        // Setup test staples.
1441 ariadna 55
        $totp = \OTPHP\TOTP::create('fakekey', clock: $clock);
56
        $window = 29;
1 efrain 57
 
58
        set_config('enabled', 1, 'factor_totp');
59
        $totpfactor = \tool_mfa\plugininfo\factor::get_factor('totp');
60
        $totpdata = [
61
            'secret' => 'fakekey',
62
            'devicename' => 'fakedevice',
63
        ];
64
        $factorinstance = $totpfactor->setup_user_factor((object) $totpdata);
65
 
66
        // First check that a valid code is actually valid.
1441 ariadna 67
        $code = $totp->at($clock->time());
1 efrain 68
        // Manually set timeverified of factor.
1441 ariadna 69
        $DB->set_field('tool_mfa', 'lastverified', $clock->time() - WEEKSECS, ['id' => $factorinstance->id]);
1 efrain 70
        $result = $totpfactor->validate_code($code, $window, $totp, $factorinstance);
71
        $this->assertEquals($totpfactor::TOTP_VALID, $result);
72
 
1441 ariadna 73
        // Now update timeverified to 20 seconds ago, and check codes within window is blocked.
74
        $code = $totp->at($clock->time() - (20));
75
        $DB->set_field('tool_mfa', 'lastverified', $clock->time() - (20), ['id' => $factorinstance->id]);
1 efrain 76
        $result = $totpfactor->validate_code($code, $window, $totp, $factorinstance);
77
        $this->assertEquals($totpfactor::TOTP_USED, $result);
78
 
1441 ariadna 79
        // Now update timeverified to 20 seconds ago, and check code from current increment within window is blocked.
80
        $code = $totp->at($clock->time());
81
        $DB->set_field('tool_mfa', 'lastverified', $clock->time() - (20), ['id' => $factorinstance->id]);
1 efrain 82
        $result = $totpfactor->validate_code($code, $window, $totp, $factorinstance);
83
        $this->assertEquals($totpfactor::TOTP_USED, $result);
84
 
85
        // Now check future codes.
86
        $window = 1;
1441 ariadna 87
        $code = $totp->at($clock->time() + (2 * MINSECS));
88
        $DB->set_field('tool_mfa', 'lastverified', $clock->time() - WEEKSECS, ['id' => $factorinstance->id]);
1 efrain 89
        $result = $totpfactor->validate_code($code, $window, $totp, $factorinstance);
90
        $this->assertEquals($totpfactor::TOTP_FUTURE, $result);
91
 
92
        // Codes in far future are invalid.
1441 ariadna 93
        $code = $totp->at($clock->time() + (20 * MINSECS));
1 efrain 94
        $result = $totpfactor->validate_code($code, $window, $totp, $factorinstance);
95
        $this->assertEquals($totpfactor::TOTP_INVALID, $result);
96
 
97
        // Do the same for past codes.
98
        $window = 1;
1441 ariadna 99
        $code = $totp->at($clock->time() - (2 * MINSECS));
1 efrain 100
        $result = $totpfactor->validate_code($code, $window, $totp, $factorinstance);
101
        $this->assertEquals($totpfactor::TOTP_OLD, $result);
102
 
103
        // Codes in far future are invalid.
1441 ariadna 104
        $code = $totp->at($clock->time() - (20 * MINSECS));
1 efrain 105
        $result = $totpfactor->validate_code($code, $window, $totp, $factorinstance);
106
        $this->assertEquals($totpfactor::TOTP_INVALID, $result);
107
 
108
        // Check incorrect codes are invalid.
109
        // Note code has a 1 in 30,000,000 chance of failing.
110
        $code = '123456';
111
        $result = $totpfactor->validate_code($code, $window, $totp, $factorinstance);
112
        $this->assertEquals($totpfactor::TOTP_INVALID, $result);
113
    }
114
 
115
    /**
116
     * Do not store the TOTP secret + user combination more than once
117
     *
118
     * @covers ::setup_user_factor
119
     */
11 efrain 120
    public function test_wont_store_same_secret_twice(): void {
1 efrain 121
        global $DB;
122
        $this->resetAfterTest(true);
123
        $user = $this->getDataGenerator()->create_user();
124
        $this->setUser($user);
125
 
126
        set_config('enabled', 1, 'factor_totp');
127
        $totpfactor = \tool_mfa\plugininfo\factor::get_factor('totp');
128
        $totpdata = [
129
            'secret' => 'fakekey',
130
            'devicename' => 'fakedevice',
131
        ];
132
        $totpfactor->setup_user_factor((object) $totpdata);
133
 
134
        // Trying to add the same TOTP should return null.
135
        $anotherecord = $totpfactor->setup_user_factor((object) $totpdata);
136
        $this->assertNull($anotherecord);
137
 
138
        // The total count for factors added should be 1 at this point.
139
        $count = $DB->count_records('tool_mfa');
140
        $this->assertEquals(1, $count);
141
    }
142
}