AuthCryptModule.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Module to use password_hash() for user password hashes
  18. *
  19. * @category Module
  20. * @package GNUsocial
  21. * @author Mikael Nordfeldth <mmn@hethane.se>
  22. * @copyright 2012 StatusNet, Inc.
  23. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die;
  27. class AuthCryptModule extends AuthenticationModule
  28. {
  29. const MODULE_VERSION = '2.0.0';
  30. protected $algorithm = PASSWORD_DEFAULT;
  31. protected $algorithm_options = [];
  32. protected $overwrite = true; // if true, password change means overwrite with crypt()
  33. protected $statusnet = true; // if true, also check StatusNet-style password hash
  34. public $provider_name = 'crypt'; // not actually used
  35. // FUNCTIONALITY
  36. public function onInitializePlugin()
  37. {
  38. if (!in_array($this->algorithm, password_algos())) {
  39. common_log(
  40. LOG_ERR,
  41. "Unsupported password hashing algorithm: {$this->algorithm}"
  42. );
  43. $this->algorithm = PASSWORD_DEFAULT;
  44. }
  45. // Make "'cost' = 12" the default option, but only if bcrypt
  46. if ($this->algorithm === PASSWORD_BCRYPT
  47. && !array_key_exists('cost', $this->algorithm_options)) {
  48. $this->algorithm_options['cost'] = 12;
  49. }
  50. }
  51. public function checkPassword($username, $password)
  52. {
  53. $username = Nickname::normalize($username);
  54. $user = User::getKV('nickname', $username);
  55. if (!($user instanceof User)) {
  56. return false;
  57. }
  58. $match = false;
  59. if (password_verify($password, $user->password)) {
  60. $match = true;
  61. } elseif ($this->statusnet) {
  62. // Check StatusNet hash, for backwards compatibility and migration
  63. // Check size outside regex to take out entries of a differing size faster
  64. if (strlen($user->password) === 32
  65. && preg_match('/^[a-f0-9]$/D', $user->password)) {
  66. $match = hash_equals(
  67. $user->password,
  68. hash('md5', $password . $user->id)
  69. );
  70. }
  71. }
  72. // Update password hash entry if it doesn't match current settings
  73. if ($this->overwrite
  74. && $match
  75. && password_needs_rehash($user->password, $this->algorithm, $this->algorithm_options)) {
  76. $this->changePassword($user->nickname, null, $password);
  77. }
  78. return $match ? $user : false;
  79. }
  80. // $oldpassword is already verified when calling this function... shouldn't this be private?!
  81. public function changePassword($username, $oldpassword, $newpassword)
  82. {
  83. $username = Nickname::normalize($username);
  84. if (!$this->overwrite) {
  85. return false;
  86. }
  87. $user = User::getKV('nickname', $username);
  88. if (empty($user)) {
  89. return false;
  90. }
  91. $original = clone $user;
  92. $user->password = $this->hashPassword($newpassword, $user->getProfile());
  93. return $user->validate() === true && $user->update($original);
  94. }
  95. public function hashPassword($password, ?Profile $profile = null)
  96. {
  97. return password_hash($password, $this->algorithm, $this->algorithm_options);
  98. }
  99. // EVENTS
  100. public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
  101. {
  102. if (!$this->checkPassword($target->getNickname(), $oldpassword)) {
  103. // if we ARE in overwrite mode, test password with common_check_user
  104. if (!$this->overwrite || !common_check_user($target->getNickname(), $oldpassword)) {
  105. // either we're not in overwrite mode, or the password was incorrect
  106. return !$this->authoritative;
  107. }
  108. // oldpassword was apparently ok
  109. }
  110. $changed = $this->changePassword($target->getNickname(), $oldpassword, $newpassword);
  111. return !$changed && empty($this->authoritative);
  112. }
  113. public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
  114. {
  115. $authenticatedUser = $this->checkPassword($nickname, $password);
  116. // if we failed, only return false to stop plugin execution if we're authoritative
  117. return !($authenticatedUser instanceof User) && empty($this->authoritative);
  118. }
  119. public function onStartHashPassword(&$hashed, $password, ?Profile $profile = null)
  120. {
  121. $hashed = $this->hashPassword($password, $profile);
  122. return false;
  123. }
  124. public function onCheckSchema()
  125. {
  126. // we only use the User database, so default AuthenticationModule stuff can be ignored
  127. return true;
  128. }
  129. public function onUserDeleteRelated($user, &$tables)
  130. {
  131. // not using User_username table, so no need to add it here.
  132. return true;
  133. }
  134. public function onModuleVersion(array &$versions): bool
  135. {
  136. $versions[] = [
  137. 'name' => 'AuthCrypt',
  138. 'version' => self::MODULE_VERSION,
  139. 'author' => 'Mikael Nordfeldth',
  140. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/AuthCrypt',
  141. 'rawdescription' => // TRANS: Module description.
  142. _m('Authentication and password hashing with crypt()')
  143. ];
  144. return true;
  145. }
  146. }