Merge "Remove JSON polyfill, deprecate 'json' module"
[lhc/web/wiklou.git] / includes / auth / TemporaryPasswordAuthenticationRequest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Auth
20 */
21
22 namespace MediaWiki\Auth;
23
24 /**
25 * This represents the intention to set a temporary password for the user.
26 * @ingroup Auth
27 * @since 1.27
28 */
29 class TemporaryPasswordAuthenticationRequest extends AuthenticationRequest {
30 /** @var string|null Temporary password */
31 public $password;
32
33 /** @var bool Email password to the user. */
34 public $mailpassword = false;
35
36 /** @var string Username or IP address of the caller */
37 public $caller;
38
39 public function getFieldInfo() {
40 return [
41 'mailpassword' => [
42 'type' => 'checkbox',
43 'label' => wfMessage( 'createaccountmail' ),
44 'help' => wfMessage( 'createaccountmail-help' ),
45 ],
46 ];
47 }
48
49 /**
50 * @param string|null $password
51 */
52 public function __construct( $password = null ) {
53 $this->password = $password;
54 if ( $password ) {
55 $this->mailpassword = true;
56 }
57 }
58
59 /**
60 * Return an instance with a new, random password
61 * @return TemporaryPasswordAuthenticationRequest
62 */
63 public static function newRandom() {
64 $config = \ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
65
66 // get the min password length
67 $minLength = $config->get( 'MinimalPasswordLength' );
68 $policy = $config->get( 'PasswordPolicy' );
69 foreach ( $policy['policies'] as $p ) {
70 if ( isset( $p['MinimalPasswordLength'] ) ) {
71 $minLength = max( $minLength, $p['MinimalPasswordLength'] );
72 }
73 if ( isset( $p['MinimalPasswordLengthToLogin'] ) ) {
74 $minLength = max( $minLength, $p['MinimalPasswordLengthToLogin'] );
75 }
76 }
77
78 $password = \PasswordFactory::generateRandomPasswordString( $minLength );
79
80 return new self( $password );
81 }
82
83 /**
84 * Return an instance with an invalid password
85 * @return TemporaryPasswordAuthenticationRequest
86 */
87 public static function newInvalid() {
88 $request = new self( null );
89 return $request;
90 }
91
92 public function describeCredentials() {
93 return [
94 'provider' => wfMessage( 'authmanager-provider-temporarypassword' ),
95 'account' => new \RawMessage( '$1', [ $this->username ] ),
96 ] + parent::describeCredentials();
97 }
98
99 }