ParameterizedPassword.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Implements the ParameterizedPassword 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. * Helper class for password hash types that have a delimited set of parameters
  24. * inside of the hash.
  25. *
  26. * All passwords are in the form of :<TYPE>:... as explained in the main Password
  27. * class. This class is for hashes in the form of :<TYPE>:<PARAM1>:<PARAM2>:... where
  28. * <PARAM1>, <PARAM2>, etc. are parameters that determine how the password was hashed.
  29. * Of course, the internal delimiter (which is : by convention and default), can be
  30. * changed by overriding the ParameterizedPassword::getDelimiter() function.
  31. *
  32. * This class requires overriding an additional function: ParameterizedPassword::getDefaultParams().
  33. * See the function description for more details on the implementation.
  34. *
  35. * @since 1.24
  36. */
  37. abstract class ParameterizedPassword extends Password {
  38. /**
  39. * Named parameters that have default values for this password type
  40. * @var array
  41. */
  42. protected $params = [];
  43. /**
  44. * Extra arguments that were found in the hash. This may or may not make
  45. * the hash invalid.
  46. * @var array
  47. */
  48. protected $args = [];
  49. protected function parseHash( $hash ) {
  50. parent::parseHash( $hash );
  51. if ( $hash === null ) {
  52. $this->params = $this->getDefaultParams();
  53. return;
  54. }
  55. $parts = explode( $this->getDelimiter(), $hash );
  56. $paramKeys = array_keys( $this->getDefaultParams() );
  57. if ( count( $parts ) < count( $paramKeys ) ) {
  58. throw new PasswordError( 'Hash is missing required parameters.' );
  59. }
  60. if ( $paramKeys ) {
  61. $this->args = array_splice( $parts, count( $paramKeys ) );
  62. $this->params = array_combine( $paramKeys, $parts );
  63. } else {
  64. $this->args = $parts;
  65. }
  66. if ( $this->args ) {
  67. $this->hash = array_pop( $this->args );
  68. } else {
  69. $this->hash = null;
  70. }
  71. }
  72. public function needsUpdate() {
  73. return $this->params !== $this->getDefaultParams();
  74. }
  75. public function toString() {
  76. $str = ':' . $this->config['type'] . ':';
  77. if ( count( $this->params ) || count( $this->args ) ) {
  78. $str .= implode( $this->getDelimiter(), array_merge( $this->params, $this->args ) );
  79. $str .= $this->getDelimiter();
  80. }
  81. $res = $str . $this->hash;
  82. $this->assertIsSafeSize( $res );
  83. return $res;
  84. }
  85. /**
  86. * Returns the delimiter for the parameters inside the hash
  87. *
  88. * @return string
  89. */
  90. abstract protected function getDelimiter();
  91. /**
  92. * Return an ordered array of default parameters for this password hash
  93. *
  94. * The keys should be the parameter names and the values should be the default
  95. * values. Additionally, the order of the array should be the order in which they
  96. * appear in the hash.
  97. *
  98. * When parsing a password hash, the constructor will split the hash based on
  99. * the delimiter, and consume as many parts as it can, matching each to a parameter
  100. * in this list. Once all the parameters have been filled, all remaining parts will
  101. * be considered extra arguments, except, of course, for the very last part, which
  102. * is the hash itself.
  103. *
  104. * @return array
  105. */
  106. abstract protected function getDefaultParams();
  107. }