MediaWiki  master
PasswordFactory Class Reference

Factory class for creating and checking Password objects. More...

Public Member Functions

 __construct (array $config=[], string $default='')
 Most of the time you'll want to use MediaWikiServices::getInstance()->getPasswordFactory instead. More...
 
 getDefaultType ()
 Get the default password type. More...
 
 getTypes ()
 Get the list of types of passwords. More...
 
 init (Config $config)
 
 needsUpdate (Password $password)
 Determine whether a password object needs updating. More...
 
 newFromCiphertext (?string $hash)
 Create a new Hash object from an existing string hash. More...
 
 newFromPlaintext (?string $password, Password $existing=null)
 Create a new Hash object from a plaintext password. More...
 
 newFromType (string $type)
 Make a new default password of the given type. More...
 
 register (string $type, array $config)
 Register a new type of password hash. More...
 
 setDefaultType (string $type)
 Set the default password type. More...
 

Static Public Member Functions

static generateRandomPasswordString (int $minLength=10)
 Generate a random string suitable for a password. More...
 
static newInvalidPassword ()
 Create an InvalidPassword. More...
 

Private Attributes

string $default = ''
 The default PasswordHash type. More...
 
array $types
 Mapping of password types to classes. More...
 

Detailed Description

Factory class for creating and checking Password objects.

Since
1.24

Definition at line 30 of file PasswordFactory.php.

Constructor & Destructor Documentation

◆ __construct()

PasswordFactory::__construct ( array  $config = [],
string  $default = '' 
)

Most of the time you'll want to use MediaWikiServices::getInstance()->getPasswordFactory instead.

Parameters
array$configMapping of password type => config
string$defaultDefault password type
See also
PasswordFactory::register
PasswordFactory::setDefaultType

Definition at line 58 of file PasswordFactory.php.

References $default, $type, and setDefaultType().

Member Function Documentation

◆ generateRandomPasswordString()

static PasswordFactory::generateRandomPasswordString ( int  $minLength = 10)
static

Generate a random string suitable for a password.

Parameters
int$minLengthMinimum length of password to generate
Returns
string

Definition at line 226 of file PasswordFactory.php.

References MWCryptRand\generateHex().

Referenced by ResetUserEmail\execute(), BotPassword\generatePassword(), and MediaWiki\Auth\TemporaryPasswordAuthenticationRequest\newRandom().

◆ getDefaultType()

PasswordFactory::getDefaultType ( )

Get the default password type.

Returns
string

Definition at line 102 of file PasswordFactory.php.

References $default.

◆ getTypes()

PasswordFactory::getTypes ( )

Get the list of types of passwords.

Returns
array

Definition at line 126 of file PasswordFactory.php.

References $types.

◆ init()

PasswordFactory::init ( Config  $config)
Deprecated:
since 1.32 Initialize settings using the constructor

Initialize the internal static variables using the global variables

Parameters
Config$configConfiguration object to load data from

Definition at line 113 of file PasswordFactory.php.

References $type, Config\get(), and setDefaultType().

◆ needsUpdate()

PasswordFactory::needsUpdate ( Password  $password)

Determine whether a password object needs updating.

Check whether the given password is of the default type. If it is, pass off further needsUpdate checks to Password::needsUpdate.

Parameters
Password$password
Returns
bool True if needs update, false otherwise

Definition at line 212 of file PasswordFactory.php.

References Password\getType(), and Password\needsUpdate().

◆ newFromCiphertext()

PasswordFactory::newFromCiphertext ( ?string  $hash)

Create a new Hash object from an existing string hash.

Parse the type of a hash and create a new hash object based on the parsed type. Pass the raw hash to the constructor of the new object. Use InvalidPassword type if a null hash is given.

Parameters
string | null$hashExisting hash or null for an invalid password
Returns
Password
Exceptions
PasswordErrorIf hash is invalid or type is not recognized

Definition at line 141 of file PasswordFactory.php.

References $type.

◆ newFromPlaintext()

PasswordFactory::newFromPlaintext ( ?string  $password,
Password  $existing = null 
)

Create a new Hash object from a plaintext password.

If no existing object is given, make a new default object. If one is given, clone that object. Then pass the plaintext to Password::crypt().

Parameters
string | null$passwordPlaintext password, or null for an invalid password
Password | null$existingOptional existing hash to get options from
Returns
Password

Definition at line 185 of file PasswordFactory.php.

References $default.

◆ newFromType()

PasswordFactory::newFromType ( string  $type)

Make a new default password of the given type.

Parameters
string$typeExisting type
Returns
Password
Exceptions
PasswordErrorIf hash is invalid or type is not recognized

Definition at line 165 of file PasswordFactory.php.

References $type.

◆ newInvalidPassword()

◆ register()

PasswordFactory::register ( string  $type,
array  $config 
)

Register a new type of password hash.

Parameters
string$typeUnique type name for the hash. Will be prefixed to the password hashes to identify what hashing method was used.
array$configArray of configuration options. 'class' is required (the Password subclass name), everything else is passed to the constructor of that class.

Definition at line 76 of file PasswordFactory.php.

References $type.

◆ setDefaultType()

PasswordFactory::setDefaultType ( string  $type)

Set the default password type.

This type will be used for creating new passwords when the type is not specified. Passwords of a different type will be considered outdated and in need of update.

Parameters
string$typePassword hash type
Exceptions
InvalidArgumentExceptionIf the type is not registered

Definition at line 90 of file PasswordFactory.php.

References $type.

Referenced by __construct(), and init().

Member Data Documentation

◆ $default

string PasswordFactory::$default = ''
private

The default PasswordHash type.

See also
PasswordFactory::setDefaultType

Definition at line 37 of file PasswordFactory.php.

Referenced by __construct(), getDefaultType(), and newFromPlaintext().

◆ $types

array PasswordFactory::$types
private
Initial value:
= [
'' => [ 'type' => '', 'class' => InvalidPassword::class ],
]

Mapping of password types to classes.

See also
PasswordFactory::register
Setup.php

Definition at line 46 of file PasswordFactory.php.

Referenced by getTypes().


The documentation for this class was generated from the following file: