MediaWiki  master
ParameterizedPassword.php
Go to the documentation of this file.
1 <?php
23 declare( strict_types = 1 );
24 
40 abstract class ParameterizedPassword extends Password {
45  protected $params = [];
46 
52  protected $args = [];
53 
57  protected function parseHash( ?string $hash ) : void {
58  parent::parseHash( $hash );
59 
60  if ( $hash === null ) {
61  $this->params = $this->getDefaultParams();
62  return;
63  }
64 
65  $parts = explode( $this->getDelimiter(), $hash );
66  $paramKeys = array_keys( $this->getDefaultParams() );
67 
68  if ( count( $parts ) < count( $paramKeys ) ) {
69  throw new PasswordError( 'Hash is missing required parameters.' );
70  }
71 
72  if ( $paramKeys ) {
73  $this->args = array_splice( $parts, count( $paramKeys ) );
74  $this->params = array_combine( $paramKeys, $parts );
75  } else {
76  $this->args = $parts;
77  }
78 
79  if ( $this->args ) {
80  $this->hash = array_pop( $this->args );
81  } else {
82  $this->hash = null;
83  }
84  }
85 
86  public function needsUpdate() : bool {
87  return $this->params !== $this->getDefaultParams();
88  }
89 
90  public function toString() : string {
91  $str = ':' . $this->config['type'] . ':';
92 
93  if ( count( $this->params ) || count( $this->args ) ) {
94  $str .= implode( $this->getDelimiter(), array_merge( $this->params, $this->args ) );
95  $str .= $this->getDelimiter();
96  }
97 
98  $res = $str . $this->hash;
99  $this->assertIsSafeSize( $res );
100  return $res;
101  }
102 
108  abstract protected function getDelimiter() : string;
109 
125  abstract protected function getDefaultParams() : array;
126 }
ParameterizedPassword\toString
toString()
Convert this hash to a string that can be stored in the database.
Definition: ParameterizedPassword.php:90
Password\assertIsSafeSize
assertIsSafeSize(string $hash)
Assert that hash will fit in a tinyblob field.
Definition: Password.php:193
ParameterizedPassword\getDelimiter
getDelimiter()
Returns the delimiter for the parameters inside the hash.
PasswordError
Show an error when any operation involving passwords fails to run.
Definition: PasswordError.php:28
ParameterizedPassword\$params
array $params
Named parameters that have default values for this password type.
Definition: ParameterizedPassword.php:45
$res
$res
Definition: testCompression.php:57
ParameterizedPassword\getDefaultParams
getDefaultParams()
Return an ordered array of default parameters for this password hash.
ParameterizedPassword\parseHash
parseHash(?string $hash)
Perform any parsing necessary on the hash to see if the hash is valid and/or to perform logic for see...
Definition: ParameterizedPassword.php:57
ParameterizedPassword\needsUpdate
needsUpdate()
Determine if the hash needs to be updated.
Definition: ParameterizedPassword.php:86
Password\$hash
string null $hash
String representation of the hash without the type.
Definition: Password.php:71
ParameterizedPassword
Helper class for password hash types that have a delimited set of parameters inside of the hash.
Definition: ParameterizedPassword.php:40
Password
Represents a password hash for use in authentication.
Definition: Password.php:61
ParameterizedPassword\$args
array $args
Extra arguments that were found in the hash.
Definition: ParameterizedPassword.php:52