MediaWiki master
AbstractPasswordPrimaryAuthenticationProvider.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Auth;
8
16use Wikimedia\Assert\Assert;
17use Wikimedia\Timestamp\TimestampFormat as TS;
18
28{
30 protected $authoritative;
31
33 private $passwordFactory = null;
34
41 public function __construct( array $params = [] ) {
42 $this->authoritative = !isset( $params['authoritative'] ) || (bool)$params['authoritative'];
43 }
44
48 protected function getPasswordFactory() {
49 if ( $this->passwordFactory === null ) {
50 $this->passwordFactory = new PasswordFactory(
51 $this->config->get( MainConfigNames::PasswordConfig ),
52 $this->config->get( MainConfigNames::PasswordDefault )
53 );
54 }
55 return $this->passwordFactory;
56 }
57
63 protected function getPassword( $hash ) {
64 $passwordFactory = $this->getPasswordFactory();
65 try {
66 return $passwordFactory->newFromCiphertext( $hash );
67 } catch ( PasswordError ) {
68 $class = static::class;
69 $this->logger->debug( "Invalid password hash in {$class}::getPassword()" );
70 return $passwordFactory->newFromCiphertext( null );
71 }
72 }
73
79 protected function failResponse( PasswordAuthenticationRequest $req ) {
80 if ( $this->authoritative ) {
82 wfMessage( $req->password === '' ? 'wrongpasswordempty' : 'wrongpassword' )
83 );
84 } else {
86 }
87 }
88
99 protected function checkPasswordValidity( $username, $password ) {
100 return User::newFromName( $username )->checkPasswordValidity( $password );
101 }
102
112 string $username,
113 Status $status
115 Assert::precondition( !$status->isOK(), __METHOD__ . ' expects a fatal Status' );
116 $resetLinkUrl = SpecialPage::getTitleFor( 'PasswordReset' )
117 ->getFullURL( [ 'wpUsername' => $username ] );
118 return AuthenticationResponse::newFail( wfMessage( 'fatalpassworderror',
119 $status->getMessage(), $resetLinkUrl ) );
120 }
121
133 protected function setPasswordResetFlag( $username, Status $status, $data = null ) {
134 $reset = $this->getPasswordResetData( $username, $data );
135
136 if ( !$reset && $this->config->get( MainConfigNames::InvalidPasswordReset ) &&
137 !$status->isGood() ) {
138 $hard = $status->getValue()['forceChange'] ?? false;
139
140 if ( $hard || !empty( $status->getValue()['suggestChangeOnLogin'] ) ) {
141 $reset = (object)[
142 'msg' => $status->getMessage( $hard ? 'resetpass-validity' : 'resetpass-validity-soft' ),
143 'hard' => $hard,
144 ];
145 }
146 }
147
148 if ( $reset ) {
149 $this->manager->setAuthenticationSessionData( 'reset-pass', $reset );
150 }
151 }
152
161 protected function getPasswordResetData( $username, $data ) {
162 return null;
163 }
164
172 protected function getNewPasswordExpiry( $username ) {
173 $days = $this->config->get( MainConfigNames::PasswordExpirationDays );
174 $expires = $days ? wfTimestamp( TS::MW, time() + $days * 86400 ) : null;
175
176 // Give extensions a chance to force an expiration
177 $this->getHookRunner()->onResetPasswordExpiration(
178 User::newFromName( $username ), $expires );
179
180 return $expires;
181 }
182
190 public function getAuthenticationRequests( $action, array $options ) {
191 switch ( $action ) {
196 return [ new PasswordAuthenticationRequest() ];
197 default:
198 return [];
199 }
200 }
201}
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.
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.
newFromCiphertext(?string $hash)
Create a new Password object from an existing string hash.
Represents a password hash for use in authentication.
Definition Password.php:52
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:44
getMessage( $shortContext=false, $longContext=false, $lang=null)
Get a bullet list of the errors as a Message object.
Definition Status.php:241
User class for the MediaWiki software.
Definition User.php:130
isGood()
Returns whether the operation completed and didn't have any error or warnings.