PasswordFactory.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Implements the Password class for the MediaWiki software.
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. */
  22. /**
  23. * Factory class for creating and checking Password objects
  24. *
  25. * @since 1.24
  26. */
  27. final class PasswordFactory {
  28. /**
  29. * The default PasswordHash type
  30. *
  31. * @var string
  32. * @see PasswordFactory::setDefaultType
  33. */
  34. private $default = '';
  35. /**
  36. * Mapping of password types to classes
  37. *
  38. * @var array
  39. * @see PasswordFactory::register
  40. * @see Setup.php
  41. */
  42. private $types = [
  43. '' => [ 'type' => '', 'class' => InvalidPassword::class ],
  44. ];
  45. /**
  46. * Construct a new password factory.
  47. * Most of the time you'll want to use MediaWikiServices::getInstance()->getPasswordFactory
  48. * instead.
  49. * @param array $config Mapping of password type => config
  50. * @param string $default Default password type
  51. * @see PasswordFactory::register
  52. * @see PasswordFactory::setDefaultType
  53. */
  54. public function __construct( array $config = [], $default = '' ) {
  55. foreach ( $config as $type => $options ) {
  56. $this->register( $type, $options );
  57. }
  58. if ( $default !== '' ) {
  59. $this->setDefaultType( $default );
  60. }
  61. }
  62. /**
  63. * Register a new type of password hash
  64. *
  65. * @param string $type Unique type name for the hash. Will be prefixed to the password hashes
  66. * to identify what hashing method was used.
  67. * @param array $config Array of configuration options. 'class' is required (the Password
  68. * subclass name), everything else is passed to the constructor of that class.
  69. */
  70. public function register( $type, array $config ) {
  71. $config['type'] = $type;
  72. $this->types[$type] = $config;
  73. }
  74. /**
  75. * Set the default password type
  76. *
  77. * This type will be used for creating new passwords when the type is not specified.
  78. * Passwords of a different type will be considered outdated and in need of update.
  79. *
  80. * @param string $type Password hash type
  81. * @throws InvalidArgumentException If the type is not registered
  82. */
  83. public function setDefaultType( $type ) {
  84. if ( !isset( $this->types[$type] ) ) {
  85. throw new InvalidArgumentException( "Invalid password type $type." );
  86. }
  87. $this->default = $type;
  88. }
  89. /**
  90. * Get the default password type
  91. *
  92. * @return string
  93. */
  94. public function getDefaultType() {
  95. return $this->default;
  96. }
  97. /**
  98. * @deprecated since 1.32 Initialize settings using the constructor
  99. *
  100. * Initialize the internal static variables using the global variables
  101. *
  102. * @param Config $config Configuration object to load data from
  103. */
  104. public function init( Config $config ) {
  105. foreach ( $config->get( 'PasswordConfig' ) as $type => $options ) {
  106. $this->register( $type, $options );
  107. }
  108. $this->setDefaultType( $config->get( 'PasswordDefault' ) );
  109. }
  110. /**
  111. * Get the list of types of passwords
  112. *
  113. * @return array
  114. */
  115. public function getTypes() {
  116. return $this->types;
  117. }
  118. /**
  119. * Create a new Hash object from an existing string hash
  120. *
  121. * Parse the type of a hash and create a new hash object based on the parsed type.
  122. * Pass the raw hash to the constructor of the new object. Use InvalidPassword type
  123. * if a null hash is given.
  124. *
  125. * @param string|null $hash Existing hash or null for an invalid password
  126. * @return Password
  127. * @throws PasswordError If hash is invalid or type is not recognized
  128. */
  129. public function newFromCiphertext( $hash ) {
  130. if ( $hash === null || $hash === false || $hash === '' ) {
  131. return new InvalidPassword( $this, [ 'type' => '' ], null );
  132. } elseif ( $hash[0] !== ':' ) {
  133. throw new PasswordError( 'Invalid hash given' );
  134. }
  135. $type = substr( $hash, 1, strpos( $hash, ':', 1 ) - 1 );
  136. if ( !isset( $this->types[$type] ) ) {
  137. throw new PasswordError( "Unrecognized password hash type $type." );
  138. }
  139. $config = $this->types[$type];
  140. return new $config['class']( $this, $config, $hash );
  141. }
  142. /**
  143. * Make a new default password of the given type.
  144. *
  145. * @param string $type Existing type
  146. * @return Password
  147. * @throws PasswordError If hash is invalid or type is not recognized
  148. */
  149. public function newFromType( $type ) {
  150. if ( !isset( $this->types[$type] ) ) {
  151. throw new PasswordError( "Unrecognized password hash type $type." );
  152. }
  153. $config = $this->types[$type];
  154. return new $config['class']( $this, $config );
  155. }
  156. /**
  157. * Create a new Hash object from a plaintext password
  158. *
  159. * If no existing object is given, make a new default object. If one is given, clone that
  160. * object. Then pass the plaintext to Password::crypt().
  161. *
  162. * @param string|null $password Plaintext password, or null for an invalid password
  163. * @param Password|null $existing Optional existing hash to get options from
  164. * @return Password
  165. */
  166. public function newFromPlaintext( $password, Password $existing = null ) {
  167. if ( $password === null ) {
  168. return new InvalidPassword( $this, [ 'type' => '' ], null );
  169. }
  170. if ( $existing === null ) {
  171. $config = $this->types[$this->default];
  172. $obj = new $config['class']( $this, $config );
  173. } else {
  174. $obj = clone $existing;
  175. }
  176. $obj->crypt( $password );
  177. return $obj;
  178. }
  179. /**
  180. * Determine whether a password object needs updating
  181. *
  182. * Check whether the given password is of the default type. If it is,
  183. * pass off further needsUpdate checks to Password::needsUpdate.
  184. *
  185. * @param Password $password
  186. *
  187. * @return bool True if needs update, false otherwise
  188. */
  189. public function needsUpdate( Password $password ) {
  190. if ( $password->getType() !== $this->default ) {
  191. return true;
  192. } else {
  193. return $password->needsUpdate();
  194. }
  195. }
  196. /**
  197. * Generate a random string suitable for a password
  198. *
  199. * @param int $minLength Minimum length of password to generate
  200. * @return string
  201. */
  202. public static function generateRandomPasswordString( $minLength = 10 ) {
  203. // Decide the final password length based on our min password length,
  204. // stopping at a minimum of 10 chars.
  205. $length = max( 10, $minLength );
  206. // Multiply by 1.25 to get the number of hex characters we need
  207. // Generate random hex chars
  208. $hex = MWCryptRand::generateHex( ceil( $length * 1.25 ) );
  209. // Convert from base 16 to base 32 to get a proper password like string
  210. return substr( Wikimedia\base_convert( $hex, 16, 32, $length ), -$length );
  211. }
  212. /**
  213. * Create an InvalidPassword
  214. *
  215. * @return InvalidPassword
  216. */
  217. public static function newInvalidPassword() {
  218. static $password = null;
  219. if ( $password === null ) {
  220. $factory = new self();
  221. $password = new InvalidPassword( $factory, [ 'type' => '' ], null );
  222. }
  223. return $password;
  224. }
  225. }