MediaWiki master
AbstractPasswordPrimaryAuthenticationProvider.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Auth;
23
31use Wikimedia\Assert\Assert;
32
42{
44 protected $authoritative;
45
46 private $passwordFactory = null;
47
54 public function __construct( array $params = [] ) {
55 $this->authoritative = !isset( $params['authoritative'] ) || (bool)$params['authoritative'];
56 }
57
61 protected function getPasswordFactory() {
62 if ( $this->passwordFactory === null ) {
63 $this->passwordFactory = new PasswordFactory(
64 $this->config->get( MainConfigNames::PasswordConfig ),
65 $this->config->get( MainConfigNames::PasswordDefault )
66 );
67 }
68 return $this->passwordFactory;
69 }
70
76 protected function getPassword( $hash ) {
77 $passwordFactory = $this->getPasswordFactory();
78 try {
79 return $passwordFactory->newFromCiphertext( $hash );
80 } catch ( PasswordError $e ) {
81 $class = static::class;
82 $this->logger->debug( "Invalid password hash in {$class}::getPassword()" );
83 return $passwordFactory->newFromCiphertext( null );
84 }
85 }
86
92 protected function failResponse( PasswordAuthenticationRequest $req ) {
93 if ( $this->authoritative ) {
95 wfMessage( $req->password === '' ? 'wrongpasswordempty' : 'wrongpassword' )
96 );
97 } else {
99 }
100 }
101
112 protected function checkPasswordValidity( $username, $password ) {
113 return User::newFromName( $username )->checkPasswordValidity( $password );
114 }
115
125 string $username,
126 Status $status
128 Assert::precondition( !$status->isOK(), __METHOD__ . ' expects a fatal Status' );
129 $resetLinkUrl = SpecialPage::getTitleFor( 'PasswordReset' )
130 ->getFullURL( [ 'wpUsername' => $username ] );
131 return AuthenticationResponse::newFail( wfMessage( 'fatalpassworderror',
132 $status->getMessage(), $resetLinkUrl ) );
133 }
134
146 protected function setPasswordResetFlag( $username, Status $status, $data = null ) {
147 $reset = $this->getPasswordResetData( $username, $data );
148
149 if ( !$reset && $this->config->get( MainConfigNames::InvalidPasswordReset ) &&
150 !$status->isGood() ) {
151 $hard = $status->getValue()['forceChange'] ?? false;
152
153 if ( $hard || !empty( $status->getValue()['suggestChangeOnLogin'] ) ) {
154 $reset = (object)[
155 'msg' => $status->getMessage( $hard ? 'resetpass-validity' : 'resetpass-validity-soft' ),
156 'hard' => $hard,
157 ];
158 }
159 }
160
161 if ( $reset ) {
162 $this->manager->setAuthenticationSessionData( 'reset-pass', $reset );
163 }
164 }
165
174 protected function getPasswordResetData( $username, $data ) {
175 return null;
176 }
177
185 protected function getNewPasswordExpiry( $username ) {
186 $days = $this->config->get( MainConfigNames::PasswordExpirationDays );
187 $expires = $days ? wfTimestamp( TS_MW, time() + $days * 86400 ) : null;
188
189 // Give extensions a chance to force an expiration
190 $this->getHookRunner()->onResetPasswordExpiration(
191 User::newFromName( $username ), $expires );
192
193 return $expires;
194 }
195
203 public function getAuthenticationRequests( $action, array $options ) {
204 switch ( $action ) {
209 return [ new PasswordAuthenticationRequest() ];
210 default:
211 return [];
212 }
213 }
214}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
Basic framework for a primary authentication provider that uses passwords.
failResponse(PasswordAuthenticationRequest $req)
Return the appropriate response for failure.
setPasswordResetFlag( $username, Status $status, $data=null)
Check if the password should be reset.
bool $authoritative
Whether this provider should ABSTAIN (false) or FAIL (true) on password failure.
getNewPasswordExpiry( $username)
Get expiration date for a new password, if any.
getFatalPasswordErrorResponse(string $username, Status $status)
Adds user-friendly description to a fatal password validity check error.
A base class that implements some of the boilerplate for a PrimaryAuthenticationProvider.
const ACTION_CHANGE
Change a user's credentials.
const ACTION_REMOVE
Remove a user's credentials.
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
const ACTION_CREATE
Create a new user.
This is a value object to hold authentication response data.
static newFail(Message $msg, array $failReasons=[])
This is a value object for authentication requests with a username and password.
A class containing constants representing the names of configuration variables.
const PasswordExpirationDays
Name constant for the PasswordExpirationDays setting, for use with Config::get()
const PasswordDefault
Name constant for the PasswordDefault setting, for use with Config::get()
const PasswordConfig
Name constant for the PasswordConfig setting, for use with Config::get()
const InvalidPasswordReset
Name constant for the InvalidPasswordReset setting, for use with Config::get()
Show an error when any operation involving passwords fails to run.
Factory class for creating and checking Password objects.
Represents a password hash for use in authentication.
Definition Password.php:66
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition Status.php:242
internal since 1.36
Definition User.php:93
isGood()
Returns whether the operation completed and didn't have any error or warnings.