| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Auth; |
| 23 |
|
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\Password\Password; |
| 26 |
use MediaWiki\Password\PasswordError; |
| 27 |
use MediaWiki\Password\PasswordFactory; |
| 28 |
use MediaWiki\SpecialPage\SpecialPage; |
| 29 |
use MediaWiki\Status\Status; |
| 30 |
use MediaWiki\User\User; |
| 31 |
use Wikimedia\Assert\Assert; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
abstract class AbstractPasswordPrimaryAuthenticationProvider |
| 41 |
extends AbstractPrimaryAuthenticationProvider |
| 42 |
{ |
| 43 |
|
| 44 |
protected $authoritative; |
| 45 |
|
| 46 |
|
| 47 |
private $passwordFactory = null; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( array $params = [] ) { |
| 56 |
$this->authoritative = !isset( $params['authoritative'] ) || (bool)$params['authoritative']; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
protected function getPasswordFactory() { |
| 63 |
if ( $this->passwordFactory === null ) { |
| 64 |
$this->passwordFactory = new PasswordFactory( |
| 65 |
$this->config->get( MainConfigNames::PasswordConfig ), |
| 66 |
$this->config->get( MainConfigNames::PasswordDefault ) |
| 67 |
); |
| 68 |
} |
| 69 |
return $this->passwordFactory; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
protected function getPassword( $hash ) { |
| 78 |
$passwordFactory = $this->getPasswordFactory(); |
| 79 |
try { |
| 80 |
return $passwordFactory->newFromCiphertext( $hash ); |
| 81 |
} catch ( PasswordError $e ) { |
| 82 |
$class = static::class; |
| 83 |
$this->logger->debug( "Invalid password hash in {$class}::getPassword()" ); |
| 84 |
return $passwordFactory->newFromCiphertext( null ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
protected function failResponse( PasswordAuthenticationRequest $req ) { |
| 94 |
if ( $this->authoritative ) { |
| 95 |
return AuthenticationResponse::newFail( |
| 96 |
wfMessage( $req->password === '' ? 'wrongpasswordempty' : 'wrongpassword' ) |
| 97 |
); |
| 98 |
} else { |
| 99 |
return AuthenticationResponse::newAbstain(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
protected function checkPasswordValidity( $username, $password ) { |
| 114 |
return User::newFromName( $username )->checkPasswordValidity( $password ); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
protected function getFatalPasswordErrorResponse( |
| 126 |
string $username, |
| 127 |
Status $status |
| 128 |
): AuthenticationResponse { |
| 129 |
Assert::precondition( !$status->isOK(), __METHOD__ . ' expects a fatal Status' ); |
| 130 |
$resetLinkUrl = SpecialPage::getTitleFor( 'PasswordReset' ) |
| 131 |
->getFullURL( [ 'wpUsername' => $username ] ); |
| 132 |
return AuthenticationResponse::newFail( wfMessage( 'fatalpassworderror', |
| 133 |
$status->getMessage(), $resetLinkUrl ) ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected function setPasswordResetFlag( $username, Status $status, $data = null ) { |
| 148 |
$reset = $this->getPasswordResetData( $username, $data ); |
| 149 |
|
| 150 |
if ( !$reset && $this->config->get( MainConfigNames::InvalidPasswordReset ) && |
| 151 |
!$status->isGood() ) { |
| 152 |
$hard = $status->getValue()['forceChange'] ?? false; |
| 153 |
|
| 154 |
if ( $hard || !empty( $status->getValue()['suggestChangeOnLogin'] ) ) { |
| 155 |
$reset = (object)[ |
| 156 |
'msg' => $status->getMessage( $hard ? 'resetpass-validity' : 'resetpass-validity-soft' ), |
| 157 |
'hard' => $hard, |
| 158 |
]; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if ( $reset ) { |
| 163 |
$this->manager->setAuthenticationSessionData( 'reset-pass', $reset ); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
protected function getPasswordResetData( $username, $data ) { |
| 176 |
return null; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
protected function getNewPasswordExpiry( $username ) { |
| 187 |
$days = $this->config->get( MainConfigNames::PasswordExpirationDays ); |
| 188 |
$expires = $days ? wfTimestamp( TS_MW, time() + $days * 86400 ) : null; |
| 189 |
|
| 190 |
|
| 191 |
$this->getHookRunner()->onResetPasswordExpiration( |
| 192 |
User::newFromName( $username ), $expires ); |
| 193 |
|
| 194 |
return $expires; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function getAuthenticationRequests( $action, array $options ) { |
| 205 |
switch ( $action ) { |
| 206 |
case AuthManager::ACTION_LOGIN: |
| 207 |
case AuthManager::ACTION_REMOVE: |
| 208 |
case AuthManager::ACTION_CREATE: |
| 209 |
case AuthManager::ACTION_CHANGE: |
| 210 |
return [ new PasswordAuthenticationRequest() ]; |
| 211 |
default: |
| 212 |
return []; |
| 213 |
} |
| 214 |
} |
| 215 |
} |