AuthCryptPlugin.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin 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 Plugin
  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 AuthCryptPlugin extends AuthenticationPlugin
  32. {
  33. protected $hash = '$6$'; // defaults to SHA512, i.e. '$6$', in onInitializePlugin()
  34. protected $statusnet = true; // if true, also check StatusNet style password hash
  35. protected $overwrite = true; // if true, password change means overwrite with crypt()
  36. public $provider_name = 'crypt'; // not actually used
  37. /*
  38. * FUNCTIONALITY
  39. */
  40. function checkPassword($username, $password)
  41. {
  42. $user = User::getKV('nickname', $username);
  43. if (!($user instanceof User)) {
  44. return false;
  45. }
  46. // crypt understands what the salt part of $user->password is
  47. if ($user->password === crypt($password, $user->password)) {
  48. return $user;
  49. }
  50. // If we check StatusNet hash, for backwards compatibility and migration
  51. if ($this->statusnet && $user->password === md5($password . $user->id)) {
  52. // and update password hash entry to crypt() compatible
  53. if ($this->overwrite) {
  54. $this->changePassword($user->nickname, null, $password);
  55. }
  56. return $user;
  57. }
  58. return false;
  59. }
  60. protected function cryptSalt($len=CRYPT_SALT_LENGTH)
  61. {
  62. $chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  63. $salt = '';
  64. for ($i=0; $i<$len; $i++) {
  65. $salt .= $chars{mt_rand(0, strlen($chars)-1)};
  66. }
  67. return $salt;
  68. }
  69. // $oldpassword is already verified when calling this function... shouldn't this be private?!
  70. function changePassword($username, $oldpassword, $newpassword)
  71. {
  72. if (!$this->password_changeable) {
  73. return false;
  74. }
  75. $user = User::getKV('nickname', $username);
  76. if (empty($user)) {
  77. return false;
  78. }
  79. $original = clone($user);
  80. $user->password = $this->hashPassword($newpassword, $user->getProfile());
  81. return (true === $user->validate() && $user->update($original));
  82. }
  83. public function hashPassword($password, Profile $profile=null)
  84. {
  85. // A new, unique salt per new record stored...
  86. return crypt($password, $this->hash . self::cryptSalt());
  87. }
  88. /*
  89. * EVENTS
  90. */
  91. public function onStartChangePassword($user, $oldpassword, $newpassword)
  92. {
  93. if (!$this->checkPassword($user->nickname, $oldpassword)) {
  94. // if we ARE in overwrite mode, test password with common_check_user
  95. if (!$this->overwrite || !common_check_user($user->nickname, $oldpassword)) {
  96. // either we're not in overwrite mode, or the password was incorrect
  97. return !$this->authoritative;
  98. }
  99. // oldpassword was apparently ok
  100. }
  101. $changed = $this->changePassword($user->nickname, $oldpassword, $newpassword);
  102. return (!$changed && empty($this->authoritative));
  103. }
  104. public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
  105. {
  106. $authenticatedUser = $this->checkPassword($nickname, $password);
  107. // if we failed, only return false to stop plugin execution if we're authoritative
  108. return (!($authenticatedUser instanceof User) && empty($this->authoritative));
  109. }
  110. public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
  111. {
  112. $hashed = $this->hashPassword($password, $profile);
  113. return false;
  114. }
  115. public function onCheckSchema()
  116. {
  117. // we only use the User database, so default AuthenticationPlugin stuff can be ignored
  118. return true;
  119. }
  120. public function onUserDeleteRelated($user, &$tables)
  121. {
  122. // not using User_username table, so no need to add it here.
  123. return true;
  124. }
  125. public function onPluginVersion(&$versions)
  126. {
  127. $versions[] = array('name' => 'AuthCrypt',
  128. 'version' => GNUSOCIAL_VERSION,
  129. 'author' => 'Mikael Nordfeldth',
  130. 'homepage' => 'http://status.net/wiki/Plugin:AuthCrypt',
  131. 'rawdescription' =>
  132. // TRANS: Plugin description.
  133. _m('Authentication and password hashing with crypt()'));
  134. return true;
  135. }
  136. }