From c1f9b1f7b1b77776192048005dcc66dcf3df2bfb Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sat, 27 Dec 2014 15:41:37 +0100 Subject: Update to MediaWiki 1.24.1 --- includes/password/PasswordFactory.php | 178 ++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 includes/password/PasswordFactory.php (limited to 'includes/password/PasswordFactory.php') diff --git a/includes/password/PasswordFactory.php b/includes/password/PasswordFactory.php new file mode 100644 index 00000000..3b4ebb1a --- /dev/null +++ b/includes/password/PasswordFactory.php @@ -0,0 +1,178 @@ + array( 'type' => '', 'class' => 'InvalidPassword' ), + ); + + /** + * Register a new type of password hash + * + * @param string $type Unique type name for the hash + * @param array $config Array of configuration options + */ + public function register( $type, array $config ) { + $config['type'] = $type; + $this->types[$type] = $config; + } + + /** + * Set the default password type + * + * @throws InvalidArgumentException If the type is not registered + * @param string $type Password hash type + */ + public function setDefaultType( $type ) { + if ( !isset( $this->types[$type] ) ) { + throw new InvalidArgumentException( "Invalid password type $type." ); + } + $this->default = $type; + } + + /** + * Initialize the internal static variables using the global variables + * + * @param Config $config Configuration object to load data from + */ + public function init( Config $config ) { + foreach ( $config->get( 'PasswordConfig' ) as $type => $options ) { + $this->register( $type, $options ); + } + + $this->setDefaultType( $config->get( 'PasswordDefault' ) ); + } + + /** + * Get the list of types of passwords + * + * @return array + */ + public function getTypes() { + return $this->types; + } + + /** + * 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. + * + * @param string|null $hash Existing hash or null for an invalid password + * @return Password + * @throws PasswordError If hash is invalid or type is not recognized + */ + public function newFromCiphertext( $hash ) { + if ( $hash === null || $hash === false || $hash === '' ) { + return new InvalidPassword( $this, array( 'type' => '' ), null ); + } elseif ( $hash[0] !== ':' ) { + throw new PasswordError( 'Invalid hash given' ); + } + + $type = substr( $hash, 1, strpos( $hash, ':', 1 ) - 1 ); + if ( !isset( $this->types[$type] ) ) { + throw new PasswordError( "Unrecognized password hash type $type." ); + } + + $config = $this->types[$type]; + + return new $config['class']( $this, $config, $hash ); + } + + /** + * Make a new default password of the given type. + * + * @param string $type Existing type + * @return Password + * @throws PasswordError If hash is invalid or type is not recognized + */ + public function newFromType( $type ) { + if ( !isset( $this->types[$type] ) ) { + throw new PasswordError( "Unrecognized password hash type $type." ); + } + + $config = $this->types[$type]; + + return new $config['class']( $this, $config ); + } + + /** + * 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(). + * + * @param string $password Plaintext password + * @param Password|null $existing Optional existing hash to get options from + * @return Password + */ + public function newFromPlaintext( $password, Password $existing = null ) { + if ( $existing === null ) { + $config = $this->types[$this->default]; + $obj = new $config['class']( $this, $config ); + } else { + $obj = clone $existing; + } + + $obj->crypt( $password ); + + return $obj; + } + + /** + * 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. + * + * @param Password $password + * + * @return bool True if needs update, false otherwise + */ + public function needsUpdate( Password $password ) { + if ( $password->getType() !== $this->default ) { + return true; + } else { + return $password->needsUpdate(); + } + } +} -- cgit v1.2.2