Merge "Move up devunt's name to Developers"
[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 /**
37 * @var bool Do not fail certain operations if the password cannot be mailed, there is a
38 * backchannel present.
39 */
40 public $hasBackchannel = false;
41
42 /** @var string Username or IP address of the caller */
43 public $caller;
44
45 public function getFieldInfo() {
46 return [
47 'mailpassword' => [
48 'type' => 'checkbox',
49 'label' => wfMessage( 'createaccountmail' ),
50 'help' => wfMessage( 'createaccountmail-help' ),
51 ],
52 ];
53 }
54
55 /**
56 * @param string|null $password
57 */
58 public function __construct( $password = null ) {
59 $this->password = $password;
60 if ( $password ) {
61 $this->mailpassword = true;
62 }
63 }
64
65 /**
66 * Return an instance with a new, random password
67 * @return TemporaryPasswordAuthenticationRequest
68 */
69 public static function newRandom() {
70 $config = \ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
71
72 // get the min password length
73 $minLength = $config->get( 'MinimalPasswordLength' );
74 $policy = $config->get( 'PasswordPolicy' );
75 foreach ( $policy['policies'] as $p ) {
76 if ( isset( $p['MinimalPasswordLength'] ) ) {
77 $minLength = max( $minLength, $p['MinimalPasswordLength'] );
78 }
79 if ( isset( $p['MinimalPasswordLengthToLogin'] ) ) {
80 $minLength = max( $minLength, $p['MinimalPasswordLengthToLogin'] );
81 }
82 }
83
84 $password = \PasswordFactory::generateRandomPasswordString( $minLength );
85
86 return new self( $password );
87 }
88
89 /**
90 * Return an instance with an invalid password
91 * @return TemporaryPasswordAuthenticationRequest
92 */
93 public static function newInvalid() {
94 $request = new self( null );
95 return $request;
96 }
97
98 public function describeCredentials() {
99 return [
100 'provider' => wfMessage( 'authmanager-provider-temporarypassword' ),
101 'account' => new \RawMessage( '$1', [ $this->username ] ),
102 ] + parent::describeCredentials();
103 }
104
105 }