AuthCryptModule.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Module to use crypt() for user password hashes
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Module
  23. * @package GNUsocial
  24. * @author Mikael Nordfeldth <mmn@hethane.se>
  25. * @copyright 2012 StatusNet, Inc.
  26. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://www.gnu.org/software/social/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. class AuthCryptModule extends AuthenticationModule
  32. {
  33. const PLUGIN_VERSION = '2.0.0';
  34. protected $hash = '$6$'; // defaults to SHA512, i.e. '$6$', in onInitializeModule()
  35. protected $statusnet = true; // if true, also check StatusNet style password hash
  36. protected $overwrite = true; // if true, password change means overwrite with crypt()
  37. protected $argon = false; // Use Argon if supported.
  38. public $provider_name = 'password_hash'; // not actually used
  39. /*
  40. * FUNCTIONALITY
  41. */
  42. function checkPassword($username, $password)
  43. {
  44. $username = Nickname::normalize($username);
  45. $user = User::getKV('nickname', $username);
  46. if (!($user instanceof User)) {
  47. return false;
  48. }
  49. // crypt understands what the salt part of $user->password is
  50. if ($user->password === crypt($password, $user->password)) {
  51. // and update password hash entry to password_hash() compatible
  52. if ($this->overwrite) {
  53. $this->changePassword($user->nickname, null, $password);
  54. }
  55. return $user;
  56. }
  57. // If we check StatusNet hash, for backwards compatibility and migration
  58. if ($this->statusnet && $user->password === md5($password . $user->id)) {
  59. // and update password hash entry to crypt() compatible
  60. if ($this->overwrite) {
  61. $this->changePassword($user->nickname, null, $password);
  62. }
  63. return $user;
  64. }
  65. // Timing safe password verification on supported PHP versions
  66. if (password_verify($password, $user->password)) {
  67. return $user;
  68. }
  69. return false;
  70. }
  71. protected function cryptSalt($len=CRYPT_SALT_LENGTH)
  72. {
  73. $chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  74. $salt = '';
  75. for ($i=0; $i<$len; $i++) {
  76. $salt .= $chars{mt_rand(0, strlen($chars)-1)};
  77. }
  78. return $salt;
  79. }
  80. // $oldpassword is already verified when calling this function... shouldn't this be private?!
  81. function changePassword($username, $oldpassword, $newpassword)
  82. {
  83. $username = Nickname::normalize($username);
  84. if($this->overwrite == false) {
  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 (true === $user->validate() && $user->update($original));
  94. }
  95. public function hashPassword($password, Profile $profile=null)
  96. {
  97. $algorithm = PASSWORD_DEFAULT;
  98. $options = ['cost' => 12];
  99. if($this->argon == true && version_compare(PHP_VERSION, '7.2.0') == 1) {
  100. $algorithm = PASSWORD_ARGON2I;
  101. $options = [
  102. 'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
  103. 'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST,
  104. 'threads' => PASSWORD_ARGON2_DEFAULT_THREADS
  105. ];
  106. }
  107. // Use the modern password hashing algorithm
  108. // http://php.net/manual/en/function.password-hash.php
  109. // Uses PASSWORD_BCRYPT by default, with PASSWORD_ARGON2I being the next possible default in future versions
  110. return password_hash($password, $algorithm, $options);
  111. }
  112. /*
  113. * EVENTS
  114. */
  115. public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
  116. {
  117. if (!$this->checkPassword($target->getNickname(), $oldpassword)) {
  118. // if we ARE in overwrite mode, test password with common_check_user
  119. if (!$this->overwrite || !common_check_user($target->getNickname(), $oldpassword)) {
  120. // either we're not in overwrite mode, or the password was incorrect
  121. return !$this->authoritative;
  122. }
  123. // oldpassword was apparently ok
  124. }
  125. $changed = $this->changePassword($target->getNickname(), $oldpassword, $newpassword);
  126. return (!$changed && empty($this->authoritative));
  127. }
  128. public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
  129. {
  130. $authenticatedUser = $this->checkPassword($nickname, $password);
  131. // if we failed, only return false to stop plugin execution if we're authoritative
  132. return (!($authenticatedUser instanceof User) && empty($this->authoritative));
  133. }
  134. public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
  135. {
  136. $hashed = $this->hashPassword($password, $profile);
  137. return false;
  138. }
  139. public function onCheckSchema()
  140. {
  141. // we only use the User database, so default AuthenticationModule stuff can be ignored
  142. return true;
  143. }
  144. public function onUserDeleteRelated($user, &$tables)
  145. {
  146. // not using User_username table, so no need to add it here.
  147. return true;
  148. }
  149. public function onModuleVersion(array &$versions): bool
  150. {
  151. $versions[] = array('name' => 'AuthCrypt',
  152. 'version' => self::PLUGIN_VERSION,
  153. 'author' => 'Mikael Nordfeldth',
  154. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/AuthCrypt',
  155. 'rawdescription' =>
  156. // TRANS: Module description.
  157. _m('Authentication and password hashing with crypt()'));
  158. return true;
  159. }
  160. }