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
|
<?php
/**
* Piwik - 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\Login\tests\Integration;
use Piwik\Access;
use Piwik\Auth;
use Piwik\Container\StaticContainer;
use Piwik\Mail;
use Piwik\Option;
use Piwik\Plugin\Manager;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Plugins\Login\PasswordResetter;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Tests\Framework\Fixture;
class PasswordResetterTest extends IntegrationTestCase
{
const NEWPASSWORD = 'newpassword';
/**
* @var Model
*/
private $userModel;
/**
* @var string
*/
private $capturedToken;
/**
* @var PasswordResetter
*/
private $passwordResetter;
public function setUp(): void
{
parent::setUp();
$this->passwordResetter = new PasswordResetter();
$this->userModel = new Model();
$this->capturedToken = null;
Manager::getInstance()->loadPluginTranslations();
}
public function test_passwordReset_processWorksAsExpected()
{
$user = $this->userModel->getUser('superUserLogin');
$password = $user['password'];
$passwordModified = $user['ts_password_modified'];
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEmpty($this->capturedToken);
$user = $this->userModel->getUser('superUserLogin');
$this->assertEquals($password, $user['password']);
$this->assertEquals($passwordModified, $user['ts_password_modified']);
$this->passwordResetter->confirmNewPassword('superUserLogin', $this->capturedToken);
$this->checkPasswordIs(self::NEWPASSWORD);
}
public function tests_passwordReset_worksUpToThreeTimesInAnHour()
{
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEmpty($this->capturedToken);
$token = $this->capturedToken;
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEquals($token, $this->capturedToken);
$token = $this->capturedToken;
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEquals($token, $this->capturedToken);
}
public function test_passwordReset_notAllowedMoreThanThreeTimesInAnHour()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('You have requested too many password resets recently. A new request can be made in one hour. If you have problems resetting your password, please contact your administrator for help.');
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEmpty($this->capturedToken);
$token = $this->capturedToken;
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEquals($token, $this->capturedToken);
$token = $this->capturedToken;
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEquals($token, $this->capturedToken);
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
}
public function test_passwordReset_newRequestAllowedAfterAnHour()
{
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$optionName = $this->passwordResetter->getPasswordResetInfoOptionName('superUserLogin');
$data = json_decode(Option::get($optionName), true);
$data['timestamp'] = time()-3601;
$data['requests'] = 3;
Option::set($optionName, json_encode($data));
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$optionName = $this->passwordResetter->getPasswordResetInfoOptionName('superUserLogin');
$data = json_decode(Option::get($optionName), true);
$this->assertEquals(1, $data['requests']);
}
public function test_passwordReset_shouldNotAllowTokenToBeUsedMoreThanOnce()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Token is invalid or has expired');
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEmpty($this->capturedToken);
$this->passwordResetter->confirmNewPassword('superUserLogin', $this->capturedToken);
$this->checkPasswordIs(self::NEWPASSWORD);
sleep(1);
$oldCapturedToken = $this->capturedToken;
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', 'anotherpassword');
$this->assertNotEquals($oldCapturedToken, $this->capturedToken);
$this->passwordResetter->confirmNewPassword('superUserLogin', $oldCapturedToken);
}
public function test_passwordReset_shouldNeverGenerateTheSameToken()
{
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEmpty($this->capturedToken);
sleep(1);
$oldCapturedToken = $this->capturedToken;
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEquals($oldCapturedToken, $this->capturedToken);
}
public function test_passwordReset_shouldNotAllowOldTokenToBeUsedAfterAnotherResetRequest()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Token is invalid or has expired');
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEmpty($this->capturedToken);
sleep(1);
$oldCapturedToken = $this->capturedToken;
$this->passwordResetter->initiatePasswordResetProcess('superUserLogin', self::NEWPASSWORD);
$this->assertNotEquals($oldCapturedToken, $this->capturedToken);
$this->passwordResetter->confirmNewPassword('superUserLogin', $oldCapturedToken);
}
/**
* @param Fixture $fixture
*/
protected static function configureFixture($fixture)
{
parent::configureFixture($fixture);
$fixture->createSuperUser = true;
$fixture->extraTestEnvVars['loadRealTranslations'] = true;
}
private function checkPasswordIs($pwd)
{
$auth = StaticContainer::get(Auth::class);
$auth->setLogin('superUserLogin');
$auth->setPassword($pwd);
$auth->setTokenAuth(null);
$auth->setPasswordHash(null);
$result = Access::getInstance()->reloadAccess($auth);
$this->assertTrue($result);
}
public function provideContainerConfig()
{
return [
'observers.global' => [
['Mail.send', function (Mail $mail) {
$body = $mail->getBodyHtml(true);
$body = preg_replace('/=\n/', '', $body);
preg_match('/resetToken[=\s]*3D([a-zA-Z0-9=\s]+)<\/p>/', $body, $matches);
if (!empty($matches[1])) {
$capturedToken = $matches[1];
$capturedToken = preg_replace('/=\s*/', '', $capturedToken);
$this->capturedToken = $capturedToken;
}
}],
],
];
}
}
|