Get ConfigFactory & MainConfig from MediaWikiServices
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * This represents the intention to set a temporary password for the user.
28 * @ingroup Auth
29 * @since 1.27
30 */
31 class TemporaryPasswordAuthenticationRequest extends AuthenticationRequest {
32 /** @var string|null Temporary password */
33 public $password;
34
35 /** @var bool Email password to the user. */
36 public $mailpassword = false;
37
38 /**
39 * @var bool Do not fail certain operations if the password cannot be mailed, there is a
40 * backchannel present.
41 */
42 public $hasBackchannel = false;
43
44 /** @var string Username or IP address of the caller */
45 public $caller;
46
47 public function getFieldInfo() {
48 return [
49 'mailpassword' => [
50 'type' => 'checkbox',
51 'label' => wfMessage( 'createaccountmail' ),
52 'help' => wfMessage( 'createaccountmail-help' ),
53 ],
54 ];
55 }
56
57 /**
58 * @param string|null $password
59 */
60 public function __construct( $password = null ) {
61 $this->password = $password;
62 if ( $password ) {
63 $this->mailpassword = true;
64 }
65 }
66
67 /**
68 * Return an instance with a new, random password
69 * @return TemporaryPasswordAuthenticationRequest
70 */
71 public static function newRandom() {
72 $config = MediaWikiServices::getInstance()->getMainConfig();
73
74 // get the min password length
75 $minLength = $config->get( 'MinimalPasswordLength' );
76 $policy = $config->get( 'PasswordPolicy' );
77 foreach ( $policy['policies'] as $p ) {
78 if ( isset( $p['MinimalPasswordLength'] ) ) {
79 $minLength = max( $minLength, $p['MinimalPasswordLength'] );
80 }
81 if ( isset( $p['MinimalPasswordLengthToLogin'] ) ) {
82 $minLength = max( $minLength, $p['MinimalPasswordLengthToLogin'] );
83 }
84 }
85
86 $password = \PasswordFactory::generateRandomPasswordString( $minLength );
87
88 return new self( $password );
89 }
90
91 /**
92 * Return an instance with an invalid password
93 * @return TemporaryPasswordAuthenticationRequest
94 */
95 public static function newInvalid() {
96 $request = new self( null );
97 return $request;
98 }
99
100 public function describeCredentials() {
101 return [
102 'provider' => wfMessage( 'authmanager-provider-temporarypassword' ),
103 'account' => new \RawMessage( '$1', [ $this->username ] ),
104 ] + parent::describeCredentials();
105 }
106
107 }