summaryrefslogtreecommitdiff
path: root/includes/specials/SpecialChangePassword.php
blob: 24664edb7c4bfa64bb3eb01bed68c07e3cbaea6a (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
 * Implements Special:ChangePassword
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup SpecialPage
 */

/**
 * Let users recover their password.
 *
 * @ingroup SpecialPage
 */
class SpecialChangePassword extends FormSpecialPage {
	protected $mUserName;
	protected $mDomain;

	// Optional Wikitext Message to show above the password change form
	protected $mPreTextMessage = null;

	// label for old password input
	protected $mOldPassMsg = null;

	public function __construct() {
		parent::__construct( 'ChangePassword', 'editmyprivateinfo' );
		$this->listed( false );
	}

	/**
	 * Main execution point
	 * @param string|null $par
	 */
	function execute( $par ) {
		$this->getOutput()->disallowUserJs();

		parent::execute( $par );
	}

	protected function checkExecutePermissions( User $user ) {
		parent::checkExecutePermissions( $user );

		if ( !$this->getRequest()->wasPosted() ) {
			$this->requireLogin( 'resetpass-no-info' );
		}
	}

	/**
	 * Set a message at the top of the Change Password form
	 * @since 1.23
	 * @param Message $msg Message to parse and add to the form header
	 */
	public function setChangeMessage( Message $msg ) {
		$this->mPreTextMessage = $msg;
	}

	/**
	 * Set a message at the top of the Change Password form
	 * @since 1.23
	 * @param string $msg Message label for old/temp password field
	 */
	public function setOldPasswordMessage( $msg ) {
		$this->mOldPassMsg = $msg;
	}

	protected function getFormFields() {
		$user = $this->getUser();
		$request = $this->getRequest();

		$oldpassMsg = $this->mOldPassMsg;
		if ( $oldpassMsg === null ) {
			$oldpassMsg = $user->isLoggedIn() ? 'oldpassword' : 'resetpass-temp-password';
		}

		$fields = array(
			'Name' => array(
				'type' => 'info',
				'label-message' => 'username',
				'default' => $request->getVal( 'wpName', $user->getName() ),
			),
			'Password' => array(
				'type' => 'password',
				'label-message' => $oldpassMsg,
			),
			'NewPassword' => array(
				'type' => 'password',
				'label-message' => 'newpassword',
			),
			'Retype' => array(
				'type' => 'password',
				'label-message' => 'retypenew',
			),
		);

		if ( !$this->getUser()->isLoggedIn() ) {
			if ( !LoginForm::getLoginToken() ) {
				LoginForm::setLoginToken();
			}
			$fields['LoginOnChangeToken'] = array(
				'type' => 'hidden',
				'label' => 'Change Password Token',
				'default' => LoginForm::getLoginToken(),
			);
		}

		$extraFields = array();
		wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
		foreach ( $extraFields as $extra ) {
			list( $name, $label, $type, $default ) = $extra;
			$fields[$name] = array(
				'type' => $type,
				'name' => $name,
				'label-message' => $label,
				'default' => $default,
			);
		}

		if ( !$user->isLoggedIn() ) {
			$fields['Remember'] = array(
				'type' => 'check',
				'label' => $this->msg( 'remembermypassword' )
						->numParams(
							ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
						)->text(),
				'default' => $request->getVal( 'wpRemember' ),
			);
		}

		return $fields;
	}

	protected function alterForm( HTMLForm $form ) {
		$form->setId( 'mw-resetpass-form' );
		$form->setTableId( 'mw-resetpass-table' );
		$form->setWrapperLegendMsg( 'resetpass_header' );
		$form->setSubmitTextMsg(
			$this->getUser()->isLoggedIn()
				? 'resetpass-submit-loggedin'
				: 'resetpass_submit'
		);
		$form->addButton( 'wpCancel', $this->msg( 'resetpass-submit-cancel' )->text() );
		$form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
		if ( $this->mPreTextMessage instanceof Message ) {
			$form->addPreText( $this->mPreTextMessage->parseAsBlock() );
		}
		$form->addHiddenFields(
			$this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
	}

	public function onSubmit( array $data ) {
		global $wgAuth;

		$request = $this->getRequest();

		if ( $request->getCheck( 'wpLoginToken' ) ) {
			// This comes from Special:Userlogin when logging in with a temporary password
			return false;
		}

		if ( !$this->getUser()->isLoggedIn()
			&& $request->getVal( 'wpLoginOnChangeToken' ) !== LoginForm::getLoginToken()
		) {
			// Potential CSRF (bug 62497)
			return false;
		}

		if ( $request->getCheck( 'wpCancel' ) ) {
			$titleObj = Title::newFromText( $request->getVal( 'returnto' ) );
			if ( !$titleObj instanceof Title ) {
				$titleObj = Title::newMainPage();
			}
			$query = $request->getVal( 'returntoquery' );
			$this->getOutput()->redirect( $titleObj->getFullURL( $query ) );

			return true;
		}

		try {
			$this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
			$this->mDomain = $wgAuth->getDomain();

			if ( !$wgAuth->allowPasswordChange() ) {
				throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
			}

			$this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );

			return true;
		} catch ( PasswordError $e ) {
			return $e->getMessage();
		}
	}

	public function onSuccess() {
		if ( $this->getUser()->isLoggedIn() ) {
			$this->getOutput()->wrapWikiMsg(
				"<div class=\"successbox\">\n$1\n</div>",
				'changepassword-success'
			);
			$this->getOutput()->returnToMain();
		} else {
			$request = $this->getRequest();
			LoginForm::setLoginToken();
			$token = LoginForm::getLoginToken();
			$data = array(
				'action' => 'submitlogin',
				'wpName' => $this->mUserName,
				'wpDomain' => $this->mDomain,
				'wpLoginToken' => $token,
				'wpPassword' => $request->getVal( 'wpNewPassword' ),
			) + $request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
			$login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
			$login->setContext( $this->getContext() );
			$login->execute( null );
		}
	}

	/**
	 * @param string $oldpass
	 * @param string $newpass
	 * @param string $retype
	 * @throws PasswordError When cannot set the new password because requirements not met.
	 */
	protected function attemptReset( $oldpass, $newpass, $retype ) {
		$isSelf = ( $this->mUserName === $this->getUser()->getName() );
		if ( $isSelf ) {
			$user = $this->getUser();
		} else {
			$user = User::newFromName( $this->mUserName );
		}

		if ( !$user || $user->isAnon() ) {
			throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
		}

		if ( $newpass !== $retype ) {
			wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
			throw new PasswordError( $this->msg( 'badretype' )->text() );
		}

		$throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
		if ( $throttleCount === true ) {
			$lang = $this->getLanguage();
			$throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
			throw new PasswordError( $this->msg( 'changepassword-throttled' )
				->params( $lang->formatDuration( $throttleInfo['seconds'] ) )
				->text()
			);
		}

		// @todo Make these separate messages, since the message is written for both cases
		if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
			wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
			throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
		}

		// User is resetting their password to their old password
		if ( $oldpass === $newpass ) {
			throw new PasswordError( $this->msg( 'resetpass-recycled' )->text() );
		}

		// Do AbortChangePassword after checking mOldpass, so we don't leak information
		// by possibly aborting a new password before verifying the old password.
		$abortMsg = 'resetpass-abort-generic';
		if ( !wfRunHooks( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
			wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
			throw new PasswordError( $this->msg( $abortMsg )->text() );
		}

		// Please reset throttle for successful logins, thanks!
		if ( $throttleCount ) {
			LoginForm::clearLoginThrottle( $this->mUserName );
		}

		try {
			$user->setPassword( $newpass );
			wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
		} catch ( PasswordError $e ) {
			wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
			throw new PasswordError( $e->getMessage() );
		}

		if ( $isSelf ) {
			// This is needed to keep the user connected since
			// changing the password also modifies the user's token.
			$remember = $this->getRequest()->getCookie( 'Token' ) !== null;
			$user->setCookies( null, null, $remember );
		}
		$user->resetPasswordExpiration();
		$user->saveSettings();
	}

	public function requiresUnblock() {
		return false;
	}

	protected function getGroupName() {
		return 'users';
	}
}