MediaWiki REL1_32
PasswordFactory.php
Go to the documentation of this file.
1<?php
28final class PasswordFactory {
35 private $default = '';
36
44 private $types = [
45 '' => [ 'type' => '', 'class' => InvalidPassword::class ],
46 ];
47
56 public function __construct( array $config = [], $default = '' ) {
57 foreach ( $config as $type => $options ) {
58 $this->register( $type, $options );
59 }
60
61 if ( $default !== '' ) {
62 $this->setDefaultType( $default );
63 }
64 }
65
74 public function register( $type, array $config ) {
75 $config['type'] = $type;
76 $this->types[$type] = $config;
77 }
78
88 public function setDefaultType( $type ) {
89 if ( !isset( $this->types[$type] ) ) {
90 throw new InvalidArgumentException( "Invalid password type $type." );
91 }
92 $this->default = $type;
93 }
94
100 public function getDefaultType() {
101 return $this->default;
102 }
103
111 public function init( Config $config ) {
112 foreach ( $config->get( 'PasswordConfig' ) as $type => $options ) {
113 $this->register( $type, $options );
114 }
115
116 $this->setDefaultType( $config->get( 'PasswordDefault' ) );
117 }
118
124 public function getTypes() {
125 return $this->types;
126 }
127
139 public function newFromCiphertext( $hash ) {
140 if ( $hash === null || $hash === false || $hash === '' ) {
141 return new InvalidPassword( $this, [ 'type' => '' ], null );
142 } elseif ( $hash[0] !== ':' ) {
143 throw new PasswordError( 'Invalid hash given' );
144 }
145
146 $type = substr( $hash, 1, strpos( $hash, ':', 1 ) - 1 );
147 if ( !isset( $this->types[$type] ) ) {
148 throw new PasswordError( "Unrecognized password hash type $type." );
149 }
150
151 $config = $this->types[$type];
152
153 return new $config['class']( $this, $config, $hash );
154 }
155
163 public function newFromType( $type ) {
164 if ( !isset( $this->types[$type] ) ) {
165 throw new PasswordError( "Unrecognized password hash type $type." );
166 }
167
168 $config = $this->types[$type];
169
170 return new $config['class']( $this, $config );
171 }
172
183 public function newFromPlaintext( $password, Password $existing = null ) {
184 if ( $password === null ) {
185 return new InvalidPassword( $this, [ 'type' => '' ], null );
186 }
187
188 if ( $existing === null ) {
189 $config = $this->types[$this->default];
190 $obj = new $config['class']( $this, $config );
191 } else {
192 $obj = clone $existing;
193 }
194
195 $obj->crypt( $password );
196
197 return $obj;
198 }
199
210 public function needsUpdate( Password $password ) {
211 if ( $password->getType() !== $this->default ) {
212 return true;
213 } else {
214 return $password->needsUpdate();
215 }
216 }
217
224 public static function generateRandomPasswordString( $minLength = 10 ) {
225 // Decide the final password length based on our min password length,
226 // stopping at a minimum of 10 chars.
227 $length = max( 10, $minLength );
228 // Multiply by 1.25 to get the number of hex characters we need
229 // Generate random hex chars
230 $hex = MWCryptRand::generateHex( ceil( $length * 1.25 ) );
231 // Convert from base 16 to base 32 to get a proper password like string
232 return substr( Wikimedia\base_convert( $hex, 16, 32, $length ), -$length );
233 }
234
240 public static function newInvalidPassword() {
241 static $password = null;
242
243 if ( $password === null ) {
244 $factory = new self();
245 $password = new InvalidPassword( $factory, [ 'type' => '' ], null );
246 }
247
248 return $password;
249 }
250}
Represents an invalid password hash.
static generateHex( $chars)
Generate a run of cryptographically random data and return it in hexadecimal string format.
Show an error when any operation involving passwords fails to run.
Factory class for creating and checking Password objects.
getDefaultType()
Get the default password type.
string $default
The default PasswordHash type.
static generateRandomPasswordString( $minLength=10)
Generate a random string suitable for a password.
newFromType( $type)
Make a new default password of the given type.
needsUpdate(Password $password)
Determine whether a password object needs updating.
setDefaultType( $type)
Set the default password type.
init(Config $config)
__construct(array $config=[], $default='')
Construct a new password factory.
getTypes()
Get the list of types of passwords.
static newInvalidPassword()
Create an InvalidPassword.
array $types
Mapping of password types to classes.
newFromCiphertext( $hash)
Create a new Hash object from an existing string hash.
newFromPlaintext( $password, Password $existing=null)
Create a new Hash object from a plaintext password.
Represents a password hash for use in authentication.
Definition Password.php:66
getType()
Get the type name of the password.
Definition Password.php:122
needsUpdate()
Determine if the hash needs to be updated.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2050
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for configuration instances.
Definition Config.php:28
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...