]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AuthCrypt/AuthCryptPlugin.php
Merge branch 'nightly' of git.gnu.io:gnu/gnu-social into nightly
[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         $username = Nickname::normalize($username);
48
49         $user = User::getKV('nickname', $username);
50         if (!($user instanceof User)) {
51             return false;
52         }
53
54         // crypt understands what the salt part of $user->password is
55         if ($user->password === crypt($password, $user->password)) {
56             return $user;
57         }
58
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);
64             }
65             return $user;
66         }
67
68         return false;
69     }
70
71     protected function cryptSalt($len=CRYPT_SALT_LENGTH)
72     {
73         $chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
74         $salt  = '';
75
76         for ($i=0; $i<$len; $i++) {
77             $salt .= $chars{mt_rand(0, strlen($chars)-1)};
78         }
79
80         return $salt;
81     }
82
83     // $oldpassword is already verified when calling this function... shouldn't this be private?!
84     function changePassword($username, $oldpassword, $newpassword)
85     {
86         $username = Nickname::normalize($username);
87
88         if (!$this->password_changeable) {
89             return false;
90         }
91
92         $user = User::getKV('nickname', $username);
93         if (empty($user)) {
94             return false;
95         }
96         $original = clone($user);
97
98         $user->password = $this->hashPassword($newpassword, $user->getProfile());
99
100         return (true === $user->validate() && $user->update($original));
101     }
102
103     public function hashPassword($password, Profile $profile=null)
104     {
105         // A new, unique salt per new record stored...
106         return crypt($password, $this->hash . self::cryptSalt());
107     }
108
109     /*
110      * EVENTS
111      */
112
113     public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
114     {
115         if (!$this->checkPassword($target->getNickname(), $oldpassword)) {
116             // if we ARE in overwrite mode, test password with common_check_user
117             if (!$this->overwrite || !common_check_user($target->getNickname(), $oldpassword)) {
118                 // either we're not in overwrite mode, or the password was incorrect
119                 return !$this->authoritative;
120             }
121             // oldpassword was apparently ok
122         }
123         $changed = $this->changePassword($target->getNickname(), $oldpassword, $newpassword);
124
125         return (!$changed && empty($this->authoritative));
126     }
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
135     public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
136     {
137         $hashed = $this->hashPassword($password, $profile);
138         return false;
139     }
140
141     public function onCheckSchema()
142     {
143         // we only use the User database, so default AuthenticationPlugin stuff can be ignored
144         return true;
145     }
146
147     public function onUserDeleteRelated($user, &$tables)
148     {
149         // not using User_username table, so no need to add it here.
150         return true;
151     }
152
153     public function onPluginVersion(array &$versions)
154     {
155         $versions[] = array('name' => 'AuthCrypt',
156                             'version' => GNUSOCIAL_VERSION,
157                             'author' => 'Mikael Nordfeldth',
158                             'homepage' => 'http://status.net/wiki/Plugin:AuthCrypt',
159                             'rawdescription' =>
160                             // TRANS: Plugin description.
161                             _m('Authentication and password hashing with crypt()'));
162         return true;
163     }
164 }