Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
TemporaryPasswordAuthenticationRequest
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 getFieldInfo
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 newRandom
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 newInvalid
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 describeCredentials
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
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
22namespace MediaWiki\Auth;
23
24use MediaWiki\Language\RawMessage;
25use MediaWiki\MainConfigNames;
26use MediaWiki\MediaWikiServices;
27use PasswordFactory;
28
29/**
30 * This represents the intention to set a temporary password for the user.
31 * @stable to extend
32 * @ingroup Auth
33 * @since 1.27
34 */
35class TemporaryPasswordAuthenticationRequest extends AuthenticationRequest {
36    /** @var string|null Temporary password */
37    public $password;
38
39    /** @var bool Email password to the user. */
40    public $mailpassword = false;
41
42    /** @var string Username or IP address of the caller */
43    public $caller;
44
45    /**
46     * @inheritDoc
47     * @stable to override
48     */
49    public function getFieldInfo() {
50        return [
51            'mailpassword' => [
52                'type' => 'checkbox',
53                'label' => wfMessage( 'createaccountmail' ),
54                'help' => wfMessage( 'createaccountmail-help' ),
55            ],
56        ];
57    }
58
59    /**
60     * @stable to call
61     * @param string|null $password
62     */
63    public function __construct( $password = null ) {
64        $this->password = $password;
65        if ( $password ) {
66            $this->mailpassword = true;
67        }
68    }
69
70    /**
71     * Return an instance with a new, random password
72     * @return TemporaryPasswordAuthenticationRequest
73     */
74    public static function newRandom() {
75        $config = MediaWikiServices::getInstance()->getMainConfig();
76
77        // get the min password length
78        $minLength = 0;
79        $policy = $config->get( MainConfigNames::PasswordPolicy );
80        foreach ( $policy['policies'] as $p ) {
81            foreach ( [ 'MinimalPasswordLength', 'MinimumPasswordLengthToLogin' ] as $check ) {
82                $minLength = max( $minLength, $p[$check]['value'] ?? $p[$check] ?? 0 );
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    /**
101     * @inheritDoc
102     * @stable to override
103     */
104    public function describeCredentials() {
105        return [
106            'provider' => wfMessage( 'authmanager-provider-temporarypassword' ),
107            'account' => new RawMessage( '$1', [ $this->username ] ),
108        ] + parent::describeCredentials();
109    }
110
111}