Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TwoFactorAuthenticationTest.php « Integration « tests « TwoFactorAuth « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4ede7e6ab1a0d29a6fd8ea9b04856251bdd866c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\TwoFactorAuth\tests\Integration;

use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Option;
use Piwik\Plugins\TwoFactorAuth\Dao\RecoveryCodeDao;
use Piwik\Plugins\TwoFactorAuth\Dao\TwoFaSecretRandomGenerator;
use Piwik\Plugins\TwoFactorAuth\SystemSettings;
use Piwik\Plugins\TwoFactorAuth\TwoFactorAuthentication;
use Piwik\Plugins\UsersManager\API;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group TwoFactorAuth
 * @group TwoFactorAuthenticationTest
 * @group Plugins
 */
class TwoFactorAuthenticationTest extends IntegrationTestCase
{
    /**
     * @var RecoveryCodeDao
     */
    private $dao;

    /**
     * @var SystemSettings
     */
    private $settings;

    /**
     * @var TwoFactorAuthentication
     */
    private $twoFa;

    public function setUp(): void
    {
        parent::setUp();

        foreach (['mylogin', 'mylogin1', 'mylogin2'] as $user) {
            API::getInstance()->addUser($user, '123abcDk3_l3', $user . '@matomo.org');
        }

        $this->dao = StaticContainer::get(RecoveryCodeDao::class);
        $this->settings = new SystemSettings();
        $secretGenerator = new TwoFaSecretRandomGenerator();
        $this->twoFa = new TwoFactorAuthentication($this->settings, $this->dao, $secretGenerator);
    }

    public function test_generateSecret()
    {
        $this->assertSame(16, mb_strlen($this->twoFa->generateSecret()));
    }

    public function test_isUserRequiredToHaveTwoFactorEnabled_notByDefault()
    {
        $this->assertFalse($this->twoFa->isUserRequiredToHaveTwoFactorEnabled());
    }

    public function test_isUserRequiredToHaveTwoFactorEnabled()
    {
        $this->settings->twoFactorAuthRequired->setValue(1);
        $this->assertTrue($this->twoFa->isUserRequiredToHaveTwoFactorEnabled());
    }

    public function test_saveSecret_disable2FAforUser_isUserUsingTwoFactorAuthentication()
    {
        $this->dao->createRecoveryCodesForLogin('mylogin');

        $this->assertFalse(TwoFactorAuthentication::isUserUsingTwoFactorAuthentication('mylogin'));
        $this->twoFa->saveSecret('mylogin', '123456');

        $this->assertTrue(TwoFactorAuthentication::isUserUsingTwoFactorAuthentication('mylogin'));
        $this->assertFalse(TwoFactorAuthentication::isUserUsingTwoFactorAuthentication('mylogin2'));

        $this->twoFa->disable2FAforUser('mylogin');

        $this->assertFalse(TwoFactorAuthentication::isUserUsingTwoFactorAuthentication('mylogin'));
    }

    public function test_disable2FAforUser_removesAllRecoveryCodes()
    {
        $this->dao->createRecoveryCodesForLogin('mylogin');
        $this->assertNotEmpty($this->dao->getAllRecoveryCodesForLogin('mylogin'));
        $this->twoFa->disable2FAforUser('mylogin');
        $this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('mylogin'));
    }

    public function test_saveSecret_neverWorksForAnonymous()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Anonymous cannot use');

        $this->twoFa->saveSecret('anonymous', '123456');
    }

    public function test_saveSecret_notWorksWhenNoRecoveryCodesCreated()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('no recovery codes have been created');

        $this->twoFa->saveSecret('not', '123456');
    }

    public function test_isUserUsingTwoFactorAuthentication_neverWorksForAnonymous()
    {
        $this->assertFalse(TwoFactorAuthentication::isUserUsingTwoFactorAuthentication('anonymous'));
    }

    public function test_validateAuthCodeDuringSetup()
    {
        $secret = '789123';
        $this->assertFalse($this->twoFa->validateAuthCodeDuringSetup('123456', $secret));

        $authCode = $this->generateValidAuthCode($secret);

        $this->assertTrue($this->twoFa->validateAuthCodeDuringSetup($authCode, $secret));
    }

    public function test_validateAuthCode_userIsNotUsingTwoFa()
    {
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin', '123456'));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin', false));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin', null));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin', ''));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin', 0));
    }

    public function test_validateAuthCode_userIsUsingTwoFa_authenticatesThroughApp()
    {
        $secret1 = '123456';
        $secret2 = '654321';
        $this->dao->createRecoveryCodesForLogin('mylogin1');
        $this->dao->createRecoveryCodesForLogin('mylogin2');
        $this->twoFa->saveSecret('mylogin1', $secret1);
        $this->twoFa->saveSecret('mylogin2', $secret2);

        $authCode1 = $this->generateValidAuthCode($secret1);
        $authCode2 = $this->generateValidAuthCode($secret2);

        $this->assertTrue($this->twoFa->validateAuthCode('mylogin1', $authCode1));
        $this->assertTrue($this->twoFa->validateAuthCode('mylogin2', $authCode2));

        $this->assertFalse($this->twoFa->validateAuthCode('mylogin2', $authCode1));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin1', $authCode2));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin1', false));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin2', null));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin2', ''));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin1', 0));
    }

    public function test_validateAuthCode_userIsUsingTwoFa_sameCodeCannotBeUsedTwice()
    {
        $secret1 = '654321';
        $secret2 = '654321';
        $this->dao->createRecoveryCodesForLogin('mylogin1');
        $this->dao->createRecoveryCodesForLogin('mylogin2');
        $this->twoFa->saveSecret('mylogin1', $secret1);
        $this->twoFa->saveSecret('mylogin2', $secret2);

        $authCode1 = $this->generateValidAuthCode($secret1);
        $authCode2 = $this->generateValidAuthCode($secret2);

        $options = Option::getLike(TwoFactorAuthentication::OPTION_PREFIX_TWO_FA_CODE_USED . '%');
        $this->assertEquals(array(), $options); // no token used yet

        $this->assertTrue($this->twoFa->validateAuthCode('mylogin2', $authCode2));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin2', $authCode2));

        // can be used by different user though
        $this->assertTrue($this->twoFa->validateAuthCode('mylogin1', $authCode1));
        $this->assertFalse($this->twoFa->validateAuthCode('mylogin1', $authCode1));

        $options = Option::getLike(TwoFactorAuthentication::OPTION_PREFIX_TWO_FA_CODE_USED . '%');
        $this->assertCount(2, $options);
    }

    public function test_validateAuthCode_userIsUsingTwoFa_authenticatesThroughRecoveryCode()
    {
        $this->dao->createRecoveryCodesForLogin('mylogin1');
        $this->dao->createRecoveryCodesForLogin('mylogin2');
        $this->twoFa->saveSecret('mylogin1', '123456');
        $this->twoFa->saveSecret('mylogin2', '654321');

        $codesLogin1 = $this->dao->getAllRecoveryCodesForLogin('mylogin1');
        $codesLogin2 = $this->dao->getAllRecoveryCodesForLogin('mylogin2');
        $this->assertNotEmpty($codesLogin1);
        $this->assertNotEmpty($codesLogin2);

        foreach ($codesLogin1 as $code) {
            // doesn't work cause belong to different user
            $this->assertFalse($this->twoFa->validateAuthCode('mylogin2', $code));
        }

        foreach ($codesLogin1 as $code) {
            $this->assertTrue($this->twoFa->validateAuthCode('mylogin1', $code));
        }

        foreach ($codesLogin1 as $code) {
            // no code can be used twice
            $this->assertFalse($this->twoFa->validateAuthCode('mylogin1', $code));
        }
    }

    public function test_cleanupTwoFaCodesUsedRecently()
    {
        $this->twoFa->cleanupTwoFaCodesUsedRecently();

        Option::set(TwoFactorAuthentication::OPTION_PREFIX_TWO_FA_CODE_USED . 'test1', time());
        Option::set(TwoFactorAuthentication::OPTION_PREFIX_TWO_FA_CODE_USED . 'test2', time() - 100);

        // those two should be deleted because they were used more than 10min ago
        Option::set(TwoFactorAuthentication::OPTION_PREFIX_TWO_FA_CODE_USED . 'test3', time() - 1000);
        Option::set(TwoFactorAuthentication::OPTION_PREFIX_TWO_FA_CODE_USED . 'test4', time() - 5000);

        $options = Option::getLike(TwoFactorAuthentication::OPTION_PREFIX_TWO_FA_CODE_USED . '%');
        $this->assertCount(4, $options);

        $this->twoFa->cleanupTwoFaCodesUsedRecently();

        $options = Option::getLike(TwoFactorAuthentication::OPTION_PREFIX_TWO_FA_CODE_USED . '%');
        $this->assertEquals(['twofa_codes_used_test1', 'twofa_codes_used_test2'], array_keys($options));
    }

    private function generateValidAuthCode($secret)
    {
        $code = new \TwoFactorAuthenticator();
        return $code->getCode($secret);
    }
}