MediaWiki REL1_37
AbstractPasswordPrimaryAuthenticationProvider.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Auth;
23
24use Password;
26use Status;
27
37{
39 protected $authoritative;
40
41 private $passwordFactory = null;
42
49 public function __construct( array $params = [] ) {
50 $this->authoritative = !isset( $params['authoritative'] ) || (bool)$params['authoritative'];
51 }
52
56 protected function getPasswordFactory() {
57 if ( $this->passwordFactory === null ) {
58 $this->passwordFactory = new PasswordFactory(
59 $this->config->get( 'PasswordConfig' ),
60 $this->config->get( 'PasswordDefault' )
61 );
62 }
64 }
65
71 protected function getPassword( $hash ) {
73 try {
74 return $passwordFactory->newFromCiphertext( $hash );
75 } catch ( \PasswordError $e ) {
76 $class = static::class;
77 $this->logger->debug( "Invalid password hash in {$class}::getPassword()" );
78 return $passwordFactory->newFromCiphertext( null );
79 }
80 }
81
87 protected function failResponse( PasswordAuthenticationRequest $req ) {
88 if ( $this->authoritative ) {
90 wfMessage( $req->password === '' ? 'wrongpasswordempty' : 'wrongpassword' )
91 );
92 } else {
94 }
95 }
96
107 protected function checkPasswordValidity( $username, $password ) {
108 return \User::newFromName( $username )->checkPasswordValidity( $password );
109 }
110
122 protected function setPasswordResetFlag( $username, Status $status, $data = null ) {
123 $reset = $this->getPasswordResetData( $username, $data );
124
125 if ( !$reset && $this->config->get( 'InvalidPasswordReset' ) && !$status->isGood() ) {
126 $hard = $status->getValue()['forceChange'] ?? false;
127
128 if ( $hard || !empty( $status->getValue()['suggestChangeOnLogin'] ) ) {
129 $reset = (object)[
130 'msg' => $status->getMessage( $hard ? 'resetpass-validity' : 'resetpass-validity-soft' ),
131 'hard' => $hard,
132 ];
133 }
134 }
135
136 if ( $reset ) {
137 $this->manager->setAuthenticationSessionData( 'reset-pass', $reset );
138 }
139 }
140
149 protected function getPasswordResetData( $username, $data ) {
150 return null;
151 }
152
160 protected function getNewPasswordExpiry( $username ) {
161 $days = $this->config->get( 'PasswordExpirationDays' );
162 $expires = $days ? wfTimestamp( TS_MW, time() + $days * 86400 ) : null;
163
164 // Give extensions a chance to force an expiration
165 $this->getHookRunner()->onResetPasswordExpiration(
166 \User::newFromName( $username ), $expires );
167
168 return $expires;
169 }
170
178 public function getAuthenticationRequests( $action, array $options ) {
179 switch ( $action ) {
184 return [ new PasswordAuthenticationRequest() ];
185 default:
186 return [];
187 }
188 }
189}
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.
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 for authentication requests with a username and password.
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:61
isGood()
Returns whether the operation completed and didn't have any error or warnings.
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:243
static newFromName( $name, $validate='valid')
Definition User.php:607