Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| TemporaryPasswordAuthenticationRequest | |
100.00% |
23 / 23 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
| getFieldInfo | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| newRandom | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| newInvalid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| describeCredentials | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Auth; |
| 8 | |
| 9 | use MediaWiki\Language\RawMessage; |
| 10 | use MediaWiki\MainConfigNames; |
| 11 | use MediaWiki\MediaWikiServices; |
| 12 | use MediaWiki\Password\PasswordFactory; |
| 13 | |
| 14 | /** |
| 15 | * This represents the intention to set a temporary password for the user. |
| 16 | * |
| 17 | * @stable to extend |
| 18 | * @ingroup Auth |
| 19 | * @since 1.27 |
| 20 | */ |
| 21 | class TemporaryPasswordAuthenticationRequest extends AuthenticationRequest { |
| 22 | /** @var string|null Temporary password */ |
| 23 | public $password; |
| 24 | |
| 25 | /** @var bool Email password to the user. */ |
| 26 | public $mailpassword = false; |
| 27 | |
| 28 | /** @var string Username or IP address of the caller */ |
| 29 | public $caller; |
| 30 | |
| 31 | /** |
| 32 | * @inheritDoc |
| 33 | * @stable to override |
| 34 | */ |
| 35 | public function getFieldInfo() { |
| 36 | return [ |
| 37 | 'mailpassword' => [ |
| 38 | 'type' => 'checkbox', |
| 39 | 'label' => wfMessage( 'createaccountmail' ), |
| 40 | 'help' => wfMessage( 'createaccountmail-help' ), |
| 41 | ], |
| 42 | ]; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @stable to call |
| 47 | * @param string|null $password |
| 48 | */ |
| 49 | public function __construct( $password = null ) { |
| 50 | $this->password = $password; |
| 51 | if ( $password ) { |
| 52 | $this->mailpassword = true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Return an instance with a new, random password |
| 58 | * @return TemporaryPasswordAuthenticationRequest |
| 59 | */ |
| 60 | public static function newRandom() { |
| 61 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
| 62 | |
| 63 | // get the min password length |
| 64 | $minLength = 0; |
| 65 | $policy = $config->get( MainConfigNames::PasswordPolicy ); |
| 66 | foreach ( $policy['policies'] as $p ) { |
| 67 | foreach ( [ 'MinimalPasswordLength', 'MinimumPasswordLengthToLogin' ] as $check ) { |
| 68 | $minLength = max( $minLength, $p[$check]['value'] ?? $p[$check] ?? 0 ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | $password = PasswordFactory::generateRandomPasswordString( $minLength ); |
| 73 | |
| 74 | return new self( $password ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Return an instance with an invalid password |
| 79 | * @return TemporaryPasswordAuthenticationRequest |
| 80 | */ |
| 81 | public static function newInvalid() { |
| 82 | return new self( null ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @inheritDoc |
| 87 | * @stable to override |
| 88 | */ |
| 89 | public function describeCredentials() { |
| 90 | return [ |
| 91 | 'provider' => wfMessage( 'authmanager-provider-temporarypassword' ), |
| 92 | 'account' => new RawMessage( '$1', [ $this->username ] ), |
| 93 | ] + parent::describeCredentials(); |
| 94 | } |
| 95 | |
| 96 | } |