]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AuthCrypt/AuthCryptPlugin.php
new plugin to check, store and migrate password hashes to crypt()
[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   StatusNet
24  * @author    Mikael Nordfeldth <mmn@hethane.se>
25  * @copyright 2012 StatusNet, Inc.
26  * @copyright 2012 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://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Plugin to use crypt() for user password hashes
37  *
38  * @category Plugin
39  * @package  StatusNet
40  * @author   Mikael Nordfeldth <mmn@hethane.se>
41  * @copyright 2012 StatusNet, Inc.
42  * @copyright 2012 Free Software Foundation, Inc http://www.fsf.org
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46 class AuthCryptPlugin extends AuthenticationPlugin
47 {
48     public $hash;       // defaults to SHA512, i.e. '$6$', in onInitializePlugin()
49     public $hostile;    // if true, password login means change password to crypt() hash
50     public $overwrite;  // if true, password change means always store crypt() hash
51
52     /*
53      * FUNCTIONALITY
54      */
55
56     function checkPassword($username, $password) {
57         $user = User::staticGet('nickname', common_canonical_nickname($username));
58
59         // crypt cuts the second parameter to its appropriate length based on hash scheme
60         if (!empty($user) && $user->password === crypt($password, $user->password)) {
61             return $user;
62         }
63         // if we're hostile, check the password against old-style hashing
64         if (!empty($user) && $this->hostile && $user->password === md5($password . $user->id)) {
65             // update password hash entry to crypt() compatible
66             $this->changePassword($username, $password, $password);
67             return $user;
68         }
69
70         return false;
71     }
72
73     // $oldpassword is already verified when calling this function... shouldn't this be private?!
74     function changePassword($username, $oldpassword, $newpassword) {
75         if (!$this->password_changeable) {
76             return false;
77         }
78
79         $user = User::staticGet('nickname', $username);
80         if (empty($user)) {
81             return false;
82         }
83         $original = clone($user);
84
85         // a new, unique salt per new record stored... but common_good_rand should be more diverse than hexdec
86         $user->password = crypt($newpassword, $this->hash . common_good_rand(CRYPT_SALT_LENGTH));
87
88         return (true === $user->validate() && $user->update($original));
89     }
90
91     /*
92      * EVENTS
93      */
94
95     function onInitializePlugin() {
96         $this->provider_name = 'crypt'; // we don't actually use the provider_name
97
98         if (!isset($this->hash)) {
99             $this->hash = '$6$';        // SHA512 supported on all systems since PHP 5.3.2
100         }
101         if (!isset($this->hostile)) {
102             $this->hostile = false;     // overwrite old style password hashes?
103         }
104         if (!isset($this->overwrite)) {
105             $this->overwrite = true;    // overwrite old style password hashes?
106         }
107     }
108
109     function onStartChangePassword($user, $oldpassword, $newpassword) {
110                 if (!$this->checkPassword($user->nickname, $oldpassword)) {
111             // if we ARE in overwrite mode, test password with common_check_user
112             if (!$this->overwrite || !common_check_user($user->nickname, $oldpassword)) {
113                 // either we're not in overwrite mode, or the password was incorrect
114                 return !$this->authoritative;
115             }
116             // oldpassword was apparently ok
117         }
118         $changed = $this->changePassword($user->nickname, $oldpassword, $newpassword);
119
120                 return (!$changed && empty($this->authoritative));
121     }
122
123     function onStartCheckPassword($nickname, $password, &$authenticatedUser) {
124         $authenticatedUser = $this->checkPassword($nickname, $password);
125                 return (empty($authenticatedUser) && empty($this->authoritative));
126     }
127
128     function onCheckSchema() {
129         // we only use the User database, so default AuthenticationPlugin stuff can be ignored
130         return true;
131     }
132
133     function onUserDeleteRelated($user, &$tables) {
134         // not using User_username table, so no need to add it here.
135         return true;
136     }
137
138     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 }