3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to use crypt() for user password hashes
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.
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.
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/>.
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/
31 if (!defined('GNUSOCIAL')) { exit(1); }
33 class AuthCryptPlugin extends AuthenticationPlugin
35 protected $hash = '$6$'; // defaults to SHA512, i.e. '$6$', in onInitializePlugin()
36 protected $statusnet = true; // if true, also check StatusNet style password hash
37 protected $overwrite = true; // if true, password change means overwrite with crypt()
39 public $provider_name = 'crypt'; // not actually used
45 function checkPassword($username, $password)
47 $username = Nickname::normalize($username);
49 $user = User::getKV('nickname', $username);
50 if (!($user instanceof User)) {
54 // crypt understands what the salt part of $user->password is
55 if ($user->password === crypt($password, $user->password)) {
59 // If we check StatusNet hash, for backwards compatibility and migration
60 if ($this->statusnet && $user->password === md5($password . $user->id)) {
61 // and update password hash entry to crypt() compatible
62 if ($this->overwrite) {
63 $this->changePassword($user->nickname, null, $password);
71 protected function cryptSalt($len=CRYPT_SALT_LENGTH)
73 $chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
76 for ($i=0; $i<$len; $i++) {
77 $salt .= $chars{mt_rand(0, strlen($chars)-1)};
83 // $oldpassword is already verified when calling this function... shouldn't this be private?!
84 function changePassword($username, $oldpassword, $newpassword)
86 $username = Nickname::normalize($username);
88 if (!$this->password_changeable) {
92 $user = User::getKV('nickname', $username);
96 $original = clone($user);
98 $user->password = $this->hashPassword($newpassword, $user->getProfile());
100 return (true === $user->validate() && $user->update($original));
103 public function hashPassword($password, Profile $profile=null)
105 // A new, unique salt per new record stored...
106 return crypt($password, $this->hash . self::cryptSalt());
113 public function onStartChangePassword($user, $oldpassword, $newpassword)
115 if (!$this->checkPassword($user->nickname, $oldpassword)) {
116 // if we ARE in overwrite mode, test password with common_check_user
117 if (!$this->overwrite || !common_check_user($user->nickname, $oldpassword)) {
118 // either we're not in overwrite mode, or the password was incorrect
119 return !$this->authoritative;
121 // oldpassword was apparently ok
123 $changed = $this->changePassword($user->nickname, $oldpassword, $newpassword);
125 return (!$changed && empty($this->authoritative));
128 public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
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));
135 public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
137 $hashed = $this->hashPassword($password, $profile);
141 public function onCheckSchema()
143 // we only use the User database, so default AuthenticationPlugin stuff can be ignored
147 public function onUserDeleteRelated($user, &$tables)
149 // not using User_username table, so no need to add it here.
153 public function onPluginVersion(array &$versions)
155 $versions[] = array('name' => 'AuthCrypt',
156 'version' => GNUSOCIAL_VERSION,
157 'author' => 'Mikael Nordfeldth',
158 'homepage' => 'http://status.net/wiki/Plugin:AuthCrypt',
160 // TRANS: Plugin description.
161 _m('Authentication and password hashing with crypt()'));