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