3 * Implements the Password class for the MediaWiki software.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Factory class for creating and checking Password objects
28 final class PasswordFactory
{
30 * The default PasswordHash type
33 * @see PasswordFactory::setDefaultType
35 private $default = '';
38 * Mapping of password types to classes
40 * @see PasswordFactory::register
43 private $types = array(
44 '' => array( 'type' => '', 'class' => 'InvalidPassword' ),
48 * Register a new type of password hash
50 * @param string $type Unique type name for the hash
51 * @param array $config Array of configuration options
53 public function register( $type, array $config ) {
54 $config['type'] = $type;
55 $this->types
[$type] = $config;
59 * Set the default password type
61 * @throws InvalidArgumentException If the type is not registered
62 * @param string $type Password hash type
64 public function setDefaultType( $type ) {
65 if ( !isset( $this->types
[$type] ) ) {
66 throw new InvalidArgumentException( "Invalid password type $type." );
68 $this->default = $type;
72 * Initialize the internal static variables using the global variables
74 * @param Config $config Configuration object to load data from
76 public function init( Config
$config ) {
77 foreach ( $config->get( 'PasswordConfig' ) as $type => $options ) {
78 $this->register( $type, $options );
81 $this->setDefaultType( $config->get( 'PasswordDefault' ) );
85 * Get the list of types of passwords
89 public function getTypes() {
94 * Create a new Hash object from an existing string hash
96 * Parse the type of a hash and create a new hash object based on the parsed type.
97 * Pass the raw hash to the constructor of the new object. Use InvalidPassword type
98 * if a null hash is given.
100 * @param string|null $hash Existing hash or null for an invalid password
102 * @throws PasswordError If hash is invalid or type is not recognized
104 public function newFromCiphertext( $hash ) {
105 if ( $hash === null ||
$hash === false ||
$hash === '' ) {
106 return new InvalidPassword( $this, array( 'type' => '' ), null );
107 } elseif ( $hash[0] !== ':' ) {
108 throw new PasswordError( 'Invalid hash given' );
111 $type = substr( $hash, 1, strpos( $hash, ':', 1 ) - 1 );
112 if ( !isset( $this->types
[$type] ) ) {
113 throw new PasswordError( "Unrecognized password hash type $type." );
116 $config = $this->types
[$type];
118 return new $config['class']( $this, $config, $hash );
122 * Make a new default password of the given type.
124 * @param string $type Existing type
126 * @throws PasswordError If hash is invalid or type is not recognized
128 public function newFromType( $type ) {
129 if ( !isset( $this->types
[$type] ) ) {
130 throw new PasswordError( "Unrecognized password hash type $type." );
133 $config = $this->types
[$type];
135 return new $config['class']( $this, $config );
139 * Create a new Hash object from a plaintext password
141 * If no existing object is given, make a new default object. If one is given, clone that
142 * object. Then pass the plaintext to Password::crypt().
144 * @param string $password Plaintext password
145 * @param Password|null $existing Optional existing hash to get options from
148 public function newFromPlaintext( $password, Password
$existing = null ) {
149 if ( $existing === null ) {
150 $config = $this->types
[$this->default];
151 $obj = new $config['class']( $this, $config );
153 $obj = clone $existing;
156 $obj->crypt( $password );
162 * Determine whether a password object needs updating
164 * Check whether the given password is of the default type. If it is,
165 * pass off further needsUpdate checks to Password::needsUpdate.
167 * @param Password $password
169 * @return bool True if needs update, false otherwise
171 public function needsUpdate( Password
$password ) {
172 if ( $password->getType() !== $this->default ) {
175 return $password->needsUpdate();