]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AuthCrypt/AuthCryptPlugin.php
AuthCrypt now tidied up and enabled by default.
[quix0rs-gnu-social.git] / plugins / AuthCrypt / AuthCryptPlugin.php
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
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 class AuthCryptPlugin extends AuthenticationPlugin
34 {
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()
38
39     public $provider_name   = 'crypt';  // not actually used
40
41     /*
42      * FUNCTIONALITY
43      */
44
45     function checkPassword($username, $password)
46     {
47         $user = User::getKV('nickname', $username);
48         if (!($user instanceof User)) {
49             return false;
50         }
51
52         // crypt cuts the second parameter to its appropriate length based on hash scheme
53         if ($user->password === crypt($password, $user->password)) {
54             return $user;
55         }
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
66         return false;
67     }
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
76         $user = User::getKV('nickname', $username);
77         if (empty($user)) {
78             return false;
79         }
80         $original = clone($user);
81
82         $user->password = $this->hashPassword($newpassword, $user->getProfile());
83
84         return (true === $user->validate() && $user->update($original));
85     }
86
87     public function hashPassword($password, Profile $profile=null)
88     {
89         // A new, unique salt per new record stored...
90         // TODO: common_good_rand should be more diverse than hexdec
91         return crypt($password, $this->hash . common_good_rand(CRYPT_SALT_LENGTH));
92     }
93
94     /*
95      * EVENTS
96      */
97
98     public function onStartChangePassword($user, $oldpassword, $newpassword)
99     {
100         if (!$this->checkPassword($user->nickname, $oldpassword)) {
101             // if we ARE in overwrite mode, test password with common_check_user
102             if (!$this->overwrite || !common_check_user($user->nickname, $oldpassword)) {
103                 // either we're not in overwrite mode, or the password was incorrect
104                 return !$this->authoritative;
105             }
106             // oldpassword was apparently ok
107         }
108         $changed = $this->changePassword($user->nickname, $oldpassword, $newpassword);
109
110         return (!$changed && empty($this->authoritative));
111     }
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
120     public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
121     {
122         $hashed = $this->hashPassword($password, $profile);
123         return false;
124     }
125
126     public function onCheckSchema()
127     {
128         // we only use the User database, so default AuthenticationPlugin stuff can be ignored
129         return true;
130     }
131
132     public function onUserDeleteRelated($user, &$tables)
133     {
134         // not using User_username table, so no need to add it here.
135         return true;
136     }
137
138     public function onPluginVersion(&$versions)
139     {
140         $versions[] = array('name' => 'AuthCrypt',
141                             'version' => STATUSNET_VERSION,
142                             'author' => 'Mikael Nordfeldth',
143                             'homepage' => 'http://status.net/wiki/Plugin:AuthCrypt',
144                             'rawdescription' =>
145                             // TRANS: Plugin description.
146                             _m('Authentication and password hashing with crypt()'));
147         return true;
148     }
149 }