AuthCryptModule.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. if (version_compare(PHP_VERSION, '7.4.0', '<')) {
  28. function password_algos(): array
  29. {
  30. $algos = [PASSWORD_BCRYPT];
  31. defined('PASSWORD_ARGON2I') && $algos[] = PASSWORD_ARGON2I;
  32. defined('PASSWORD_ARGON2ID') && $algos[] = PASSWORD_ARGON2ID;
  33. return $algos;
  34. }
  35. }
  36. class AuthCryptModule extends AuthenticationModule
  37. {
  38. const MODULE_VERSION = '2.0.0';
  39. protected $algorithm = PASSWORD_DEFAULT;
  40. protected $algorithm_options = [];
  41. protected $overwrite = true; // if true, password change means overwrite with crypt()
  42. protected $statusnet = true; // if true, also check StatusNet-style password hash
  43. public $provider_name = 'crypt'; // not actually used
  44. // FUNCTIONALITY
  45. public function onInitializePlugin()
  46. {
  47. if (!in_array($this->algorithm, password_algos())) {
  48. common_log(
  49. LOG_ERR,
  50. "Unsupported password hashing algorithm: {$this->algorithm}"
  51. );
  52. $this->algorithm = PASSWORD_DEFAULT;
  53. }
  54. // Make "'cost' = 12" the default option, but only if bcrypt
  55. if ($this->algorithm === PASSWORD_BCRYPT
  56. && !array_key_exists('cost', $this->algorithm_options)) {
  57. $this->algorithm_options['cost'] = 12;
  58. }
  59. }
  60. public function checkPassword($username, $password)
  61. {
  62. $username = Nickname::normalize($username);
  63. $user = User::getKV('nickname', $username);
  64. if (!($user instanceof User)) {
  65. return false;
  66. }
  67. $match = false;
  68. if (password_verify($password, $user->password)) {
  69. $match = true;
  70. } elseif ($this->statusnet) {
  71. // Check StatusNet hash, for backwards compatibility and migration
  72. // Check size outside regex to take out entries of a differing size faster
  73. if (strlen($user->password) === 32
  74. && preg_match('/^[a-f0-9]$/D', $user->password)) {
  75. $match = hash_equals(
  76. $user->password,
  77. hash('md5', $password . $user->id)
  78. );
  79. }
  80. }
  81. // Update password hash entry if it doesn't match current settings
  82. if ($this->overwrite
  83. && $match
  84. && password_needs_rehash($user->password, $this->algorithm, $this->algorithm_options)) {
  85. $this->changePassword($user->nickname, null, $password);
  86. }
  87. return $match ? $user : false;
  88. }
  89. // $oldpassword is already verified when calling this function... shouldn't this be private?!
  90. public function changePassword($username, $oldpassword, $newpassword)
  91. {
  92. $username = Nickname::normalize($username);
  93. if (!$this->overwrite) {
  94. return false;
  95. }
  96. $user = User::getKV('nickname', $username);
  97. if (empty($user)) {
  98. return false;
  99. }
  100. $original = clone $user;
  101. $user->password = $this->hashPassword($newpassword, $user->getProfile());
  102. return $user->validate() === true && $user->update($original);
  103. }
  104. public function hashPassword($password, ?Profile $profile = null)
  105. {
  106. return password_hash($password, $this->algorithm, $this->algorithm_options);
  107. }
  108. // EVENTS
  109. public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
  110. {
  111. if (!$this->checkPassword($target->getNickname(), $oldpassword)) {
  112. // if we ARE in overwrite mode, test password with common_check_user
  113. if (!$this->overwrite || !common_check_user($target->getNickname(), $oldpassword)) {
  114. // either we're not in overwrite mode, or the password was incorrect
  115. return !$this->authoritative;
  116. }
  117. // oldpassword was apparently ok
  118. }
  119. $changed = $this->changePassword($target->getNickname(), $oldpassword, $newpassword);
  120. return !$changed && empty($this->authoritative);
  121. }
  122. public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
  123. {
  124. $authenticatedUser = $this->checkPassword($nickname, $password);
  125. // if we failed, only return false to stop plugin execution if we're authoritative
  126. return !($authenticatedUser instanceof User) && empty($this->authoritative);
  127. }
  128. public function onStartHashPassword(&$hashed, $password, ?Profile $profile = null)
  129. {
  130. $hashed = $this->hashPassword($password, $profile);
  131. return false;
  132. }
  133. public function onCheckSchema()
  134. {
  135. // we only use the User database, so default AuthenticationModule stuff can be ignored
  136. return true;
  137. }
  138. public function onUserDeleteRelated($user, &$tables)
  139. {
  140. // not using User_username table, so no need to add it here.
  141. return true;
  142. }
  143. public function onModuleVersion(array &$versions): bool
  144. {
  145. $versions[] = [
  146. 'name' => 'AuthCrypt',
  147. 'version' => self::MODULE_VERSION,
  148. 'author' => 'Mikael Nordfeldth',
  149. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/AuthCrypt',
  150. 'rawdescription' => // TRANS: Module description.
  151. _m('Authentication and password hashing with crypt()')
  152. ];
  153. return true;
  154. }
  155. }