MediaWiki  1.34.0
AbstractPasswordPrimaryAuthenticationProvider.php
Go to the documentation of this file.
1 <?php
22 namespace MediaWiki\Auth;
23 
24 use Password;
25 use PasswordFactory;
26 use Status;
27 
35 {
37  protected $authoritative;
38 
39  private $passwordFactory = null;
40 
46  public function __construct( array $params = [] ) {
47  $this->authoritative = !isset( $params['authoritative'] ) || (bool)$params['authoritative'];
48  }
49 
54  protected function getPasswordFactory() {
55  if ( $this->passwordFactory === null ) {
56  $this->passwordFactory = new PasswordFactory(
57  $this->config->get( 'PasswordConfig' ),
58  $this->config->get( 'PasswordDefault' )
59  );
60  }
62  }
63 
69  protected function getPassword( $hash ) {
71  try {
72  return $passwordFactory->newFromCiphertext( $hash );
73  } catch ( \PasswordError $e ) {
74  $class = static::class;
75  $this->logger->debug( "Invalid password hash in {$class}::getPassword()" );
76  return $passwordFactory->newFromCiphertext( null );
77  }
78  }
79 
85  protected function failResponse( PasswordAuthenticationRequest $req ) {
86  if ( $this->authoritative ) {
88  wfMessage( $req->password === '' ? 'wrongpasswordempty' : 'wrongpassword' )
89  );
90  } else {
92  }
93  }
94 
105  protected function checkPasswordValidity( $username, $password ) {
106  return \User::newFromName( $username )->checkPasswordValidity( $password );
107  }
108 
120  protected function setPasswordResetFlag( $username, Status $status, $data = null ) {
121  $reset = $this->getPasswordResetData( $username, $data );
122 
123  if ( !$reset && $this->config->get( 'InvalidPasswordReset' ) && !$status->isGood() ) {
124  $hard = $status->getValue()['forceChange'] ?? false;
125 
126  if ( $hard || !empty( $status->getValue()['suggestChangeOnLogin'] ) ) {
127  $reset = (object)[
128  'msg' => $status->getMessage( $hard ? 'resetpass-validity' : 'resetpass-validity-soft' ),
129  'hard' => $hard,
130  ];
131  }
132  }
133 
134  if ( $reset ) {
135  $this->manager->setAuthenticationSessionData( 'reset-pass', $reset );
136  }
137  }
138 
146  protected function getPasswordResetData( $username, $data ) {
147  return null;
148  }
149 
156  protected function getNewPasswordExpiry( $username ) {
157  $days = $this->config->get( 'PasswordExpirationDays' );
158  $expires = $days ? wfTimestamp( TS_MW, time() + $days * 86400 ) : null;
159 
160  // Give extensions a chance to force an expiration
161  \Hooks::run( 'ResetPasswordExpiration', [ \User::newFromName( $username ), &$expires ] );
162 
163  return $expires;
164  }
165 
166  public function getAuthenticationRequests( $action, array $options ) {
167  switch ( $action ) {
172  return [ new PasswordAuthenticationRequest() ];
173  default:
174  return [];
175  }
176  }
177 }
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\__construct
__construct(array $params=[])
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:46
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\$authoritative
bool $authoritative
Whether this provider should ABSTAIN (false) or FAIL (true) on password failure.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:37
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\getPassword
getPassword( $hash)
Get a Password object from the hash.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:69
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\failResponse
failResponse(PasswordAuthenticationRequest $req)
Return the appropriate response for failure.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:85
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider
Basic framework for a primary authentication provider that uses passwords.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:33
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
PasswordError
Show an error when any operation involving passwords fails to run.
Definition: PasswordError.php:26
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
MediaWiki\Auth\AuthenticationResponse\newAbstain
static newAbstain()
Definition: AuthenticationResponse.php:170
MediaWiki\Auth\PasswordAuthenticationRequest
This is a value object for authentication requests with a username and password.
Definition: PasswordAuthenticationRequest.php:29
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\getNewPasswordExpiry
getNewPasswordExpiry( $username)
Get expiration date for a new password, if any.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:156
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\$passwordFactory
$passwordFactory
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:39
MediaWiki\Auth\AuthManager\ACTION_CREATE
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:93
MediaWiki\Auth\AuthManager\ACTION_CHANGE
const ACTION_CHANGE
Change a user's credentials.
Definition: AuthManager.php:105
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\setPasswordResetFlag
setPasswordResetFlag( $username, Status $status, $data=null)
Check if the password should be reset.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:120
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\checkPasswordValidity
checkPasswordValidity( $username, $password)
Check that the password is valid.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:105
MediaWiki\Auth\AuthManager\ACTION_REMOVE
const ACTION_REMOVE
Remove a user's credentials.
Definition: AuthManager.php:107
MediaWiki\$action
string $action
Cache what action this request is.
Definition: MediaWiki.php:48
$status
return $status
Definition: SyntaxHighlight.php:347
MediaWiki\Auth\AuthenticationResponse\newFail
static newFail(Message $msg)
Definition: AuthenticationResponse.php:146
MediaWiki\Auth\AuthManager\ACTION_LOGIN
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
Definition: AuthManager.php:87
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\getPasswordFactory
getPasswordFactory()
Get the PasswordFactory.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:54
MediaWiki\Auth\AbstractPrimaryAuthenticationProvider
A base class that implements some of the boilerplate for a PrimaryAuthenticationProvider.
Definition: AbstractPrimaryAuthenticationProvider.php:32
PasswordFactory
Factory class for creating and checking Password objects.
Definition: PasswordFactory.php:28
Password
Represents a password hash for use in authentication.
Definition: Password.php:61
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\getPasswordResetData
getPasswordResetData( $username, $data)
Get password reset data, if any.
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:146
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
MediaWiki\Auth
Definition: AbstractAuthenticationProvider.php:22
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider\getAuthenticationRequests
getAuthenticationRequests( $action, array $options)
Return the applicable list of AuthenticationRequests.Possible values for $action depend on whether th...
Definition: AbstractPasswordPrimaryAuthenticationProvider.php:166