Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / password / ParameterizedPassword.php
blob532fccd00d5f79a475070247812ba10feccfbb62
1 <?php
2 /**
3 * Implements the ParameterizedPassword 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
20 * @file
23 declare( strict_types = 1 );
25 namespace MediaWiki\Password;
27 /**
28 * Helper class for password hash types that have a delimited set of parameters
29 * inside the hash.
31 * All passwords are in the form of :<TYPE>:... as explained in the main Password
32 * class. This class is for hashes in the form of :<TYPE>:<PARAM1>:<PARAM2>:... where
33 * <PARAM1>, <PARAM2>, etc. are parameters that determine how the password was hashed.
34 * Of course, the internal delimiter (which is : by convention and default), can be
35 * changed by overriding the ParameterizedPassword::getDelimiter() function.
37 * This class requires overriding an additional function: ParameterizedPassword::getDefaultParams().
38 * See the function description for more details on the implementation.
40 * @since 1.24
42 abstract class ParameterizedPassword extends Password {
43 /**
44 * Named parameters that have default values for this password type
45 * @var array
47 protected $params = [];
49 /**
50 * Extra arguments that were found in the hash. This may or may not make
51 * the hash invalid.
52 * @var string[]
54 protected $args = [];
56 /**
57 * @inheritDoc
59 protected function parseHash( ?string $hash ): void {
60 parent::parseHash( $hash );
62 if ( $hash === null ) {
63 $this->params = $this->getDefaultParams();
64 return;
67 $parts = explode( $this->getDelimiter(), $hash );
68 $paramKeys = array_keys( $this->getDefaultParams() );
70 if ( count( $parts ) < count( $paramKeys ) ) {
71 throw new PasswordError( 'Hash is missing required parameters.' );
74 if ( $paramKeys ) {
75 $this->args = array_splice( $parts, count( $paramKeys ) );
76 $this->params = array_combine( $paramKeys, $parts );
77 } else {
78 $this->args = $parts;
81 if ( $this->args ) {
82 $this->hash = array_pop( $this->args );
83 } else {
84 $this->hash = null;
88 public function needsUpdate(): bool {
89 return $this->params !== $this->getDefaultParams();
92 public function toString(): string {
93 $str = ':' . $this->config['type'] . ':';
95 if ( count( $this->params ) || count( $this->args ) ) {
96 $str .= implode( $this->getDelimiter(), array_merge( $this->params, $this->args ) );
97 $str .= $this->getDelimiter();
100 $res = $str . $this->hash;
101 $this->assertIsSafeSize( $res );
102 return $res;
106 * Returns the delimiter for the parameters inside the hash
108 * @return string
110 abstract protected function getDelimiter(): string;
113 * Return an ordered array of default parameters for this password hash
115 * The keys should be the parameter names and the values should be the default
116 * values. Additionally, the order of the array should be the order in which they
117 * appear in the hash.
119 * When parsing a password hash, the constructor will split the hash based on
120 * the delimiter, and consume as many parts as it can, matching each to a parameter
121 * in this list. Once all the parameters have been filled, all remaining parts will
122 * be considered extra arguments, except, of course, for the very last part, which
123 * is the hash itself.
125 * @return array
127 abstract protected function getDefaultParams(): array;
130 /** @deprecated since 1.43 use MediaWiki\\Password\\ParameterizedPassword */
131 class_alias( ParameterizedPassword::class, 'ParameterizedPassword' );