X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/password/ParameterizedPassword.php diff --git a/includes/password/ParameterizedPassword.php b/includes/password/ParameterizedPassword.php new file mode 100644 index 00000000..78d624ce --- /dev/null +++ b/includes/password/ParameterizedPassword.php @@ -0,0 +1,121 @@ +:... as explained in the main Password + * class. This class is for hashes in the form of ::::... where + * , , etc. are parameters that determine how the password was hashed. + * Of course, the internal delimiter (which is : by convention and default), can be + * changed by overriding the ParameterizedPassword::getDelimiter() function. + * + * This class requires overriding an additional function: ParameterizedPassword::getDefaultParams(). + * See the function description for more details on the implementation. + * + * @since 1.24 + */ +abstract class ParameterizedPassword extends Password { + /** + * Named parameters that have default values for this password type + * @var array + */ + protected $params = []; + + /** + * Extra arguments that were found in the hash. This may or may not make + * the hash invalid. + * @var array + */ + protected $args = []; + + protected function parseHash( $hash ) { + parent::parseHash( $hash ); + + if ( $hash === null ) { + $this->params = $this->getDefaultParams(); + return; + } + + $parts = explode( $this->getDelimiter(), $hash ); + $paramKeys = array_keys( $this->getDefaultParams() ); + + if ( count( $parts ) < count( $paramKeys ) ) { + throw new PasswordError( 'Hash is missing required parameters.' ); + } + + if ( $paramKeys ) { + $this->args = array_splice( $parts, count( $paramKeys ) ); + $this->params = array_combine( $paramKeys, $parts ); + } else { + $this->args = $parts; + } + + if ( $this->args ) { + $this->hash = array_pop( $this->args ); + } else { + $this->hash = null; + } + } + + public function needsUpdate() { + return $this->params !== $this->getDefaultParams(); + } + + public function toString() { + $str = ':' . $this->config['type'] . ':'; + + if ( count( $this->params ) || count( $this->args ) ) { + $str .= implode( $this->getDelimiter(), array_merge( $this->params, $this->args ) ); + $str .= $this->getDelimiter(); + } + + $res = $str . $this->hash; + $this->assertIsSafeSize( $res ); + return $res; + } + + /** + * Returns the delimiter for the parameters inside the hash + * + * @return string + */ + abstract protected function getDelimiter(); + + /** + * Return an ordered array of default parameters for this password hash + * + * The keys should be the parameter names and the values should be the default + * values. Additionally, the order of the array should be the order in which they + * appear in the hash. + * + * When parsing a password hash, the constructor will split the hash based on + * the delimiter, and consume as many parts as it can, matching each to a parameter + * in this list. Once all the parameters have been filled, all remaining parts will + * be considered extra arguments, except, of course, for the very last part, which + * is the hash itself. + * + * @return array + */ + abstract protected function getDefaultParams(); +}