forked from GNUsocial/gnu-social
new plugin to check, store and migrate password hashes to crypt()
This commit is contained in:
parent
56cfd2bf22
commit
1095f7a935
149
plugins/AuthCrypt/AuthCryptPlugin.php
Normal file
149
plugins/AuthCrypt/AuthCryptPlugin.php
Normal file
@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* StatusNet, the distributed open-source microblogging tool
|
||||
*
|
||||
* Plugin to use crypt() for user password hashes
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* LICENCE: This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @category Plugin
|
||||
* @package StatusNet
|
||||
* @author Mikael Nordfeldth <mmn@hethane.se>
|
||||
* @copyright 2012 StatusNet, Inc.
|
||||
* @copyright 2012 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://status.net/
|
||||
*/
|
||||
|
||||
if (!defined('STATUSNET')) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin to use crypt() for user password hashes
|
||||
*
|
||||
* @category Plugin
|
||||
* @package StatusNet
|
||||
* @author Mikael Nordfeldth <mmn@hethane.se>
|
||||
* @copyright 2012 StatusNet, Inc.
|
||||
* @copyright 2012 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||
* @link http://status.net/
|
||||
*/
|
||||
class AuthCryptPlugin extends AuthenticationPlugin
|
||||
{
|
||||
public $hash; // defaults to SHA512, i.e. '$6$', in onInitializePlugin()
|
||||
public $hostile; // if true, password login means change password to crypt() hash
|
||||
public $overwrite; // if true, password change means always store crypt() hash
|
||||
|
||||
/*
|
||||
* FUNCTIONALITY
|
||||
*/
|
||||
|
||||
function checkPassword($username, $password) {
|
||||
$user = User::staticGet('nickname', common_canonical_nickname($username));
|
||||
|
||||
// crypt cuts the second parameter to its appropriate length based on hash scheme
|
||||
if (!empty($user) && $user->password === crypt($password, $user->password)) {
|
||||
return $user;
|
||||
}
|
||||
// if we're hostile, check the password against old-style hashing
|
||||
if (!empty($user) && $this->hostile && $user->password === md5($password . $user->id)) {
|
||||
// update password hash entry to crypt() compatible
|
||||
$this->changePassword($username, $password, $password);
|
||||
return $user;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// $oldpassword is already verified when calling this function... shouldn't this be private?!
|
||||
function changePassword($username, $oldpassword, $newpassword) {
|
||||
if (!$this->password_changeable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$user = User::staticGet('nickname', $username);
|
||||
if (empty($user)) {
|
||||
return false;
|
||||
}
|
||||
$original = clone($user);
|
||||
|
||||
// a new, unique salt per new record stored... but common_good_rand should be more diverse than hexdec
|
||||
$user->password = crypt($newpassword, $this->hash . common_good_rand(CRYPT_SALT_LENGTH));
|
||||
|
||||
return (true === $user->validate() && $user->update($original));
|
||||
}
|
||||
|
||||
/*
|
||||
* EVENTS
|
||||
*/
|
||||
|
||||
function onInitializePlugin() {
|
||||
$this->provider_name = 'crypt'; // we don't actually use the provider_name
|
||||
|
||||
if (!isset($this->hash)) {
|
||||
$this->hash = '$6$'; // SHA512 supported on all systems since PHP 5.3.2
|
||||
}
|
||||
if (!isset($this->hostile)) {
|
||||
$this->hostile = false; // overwrite old style password hashes?
|
||||
}
|
||||
if (!isset($this->overwrite)) {
|
||||
$this->overwrite = true; // overwrite old style password hashes?
|
||||
}
|
||||
}
|
||||
|
||||
function onStartChangePassword($user, $oldpassword, $newpassword) {
|
||||
if (!$this->checkPassword($user->nickname, $oldpassword)) {
|
||||
// if we ARE in overwrite mode, test password with common_check_user
|
||||
if (!$this->overwrite || !common_check_user($user->nickname, $oldpassword)) {
|
||||
// either we're not in overwrite mode, or the password was incorrect
|
||||
return !$this->authoritative;
|
||||
}
|
||||
// oldpassword was apparently ok
|
||||
}
|
||||
$changed = $this->changePassword($user->nickname, $oldpassword, $newpassword);
|
||||
|
||||
return (!$changed && empty($this->authoritative));
|
||||
}
|
||||
|
||||
function onStartCheckPassword($nickname, $password, &$authenticatedUser) {
|
||||
$authenticatedUser = $this->checkPassword($nickname, $password);
|
||||
return (empty($authenticatedUser) && empty($this->authoritative));
|
||||
}
|
||||
|
||||
function onCheckSchema() {
|
||||
// we only use the User database, so default AuthenticationPlugin stuff can be ignored
|
||||
return true;
|
||||
}
|
||||
|
||||
function onUserDeleteRelated($user, &$tables) {
|
||||
// not using User_username table, so no need to add it here.
|
||||
return true;
|
||||
}
|
||||
|
||||
function onPluginVersion(&$versions)
|
||||
{
|
||||
$versions[] = array('name' => 'AuthCrypt',
|
||||
'version' => STATUSNET_VERSION,
|
||||
'author' => 'Mikael Nordfeldth',
|
||||
'homepage' => 'http://status.net/wiki/Plugin:AuthCrypt',
|
||||
'rawdescription' =>
|
||||
// TRANS: Plugin description.
|
||||
_m('Authentication and password hashing with crypt()'));
|
||||
return true;
|
||||
}
|
||||
}
|
48
plugins/AuthCrypt/README
Normal file
48
plugins/AuthCrypt/README
Normal file
@ -0,0 +1,48 @@
|
||||
AuthCrypt allows for StatusNet to use crypt() hashing to store password credentials.
|
||||
|
||||
Requirements
|
||||
============
|
||||
* PHP >= 5.3.2 with php5-mcrypt extension for certain SHA512 support
|
||||
* lib/authenticationplugin.php in your StatusNet install
|
||||
|
||||
Installation
|
||||
============
|
||||
Add either of the following configurations to your config.php (see Example below for more options):
|
||||
|
||||
addPlugin('AuthCrypt');
|
||||
|
||||
The recommended use is to overwrite old hash values when logging in (hostile mode) and be the authority on password checks when logging in. If you only wish to update entries on password change the default values are enough.
|
||||
|
||||
addPlugin('AuthCrypt', array(
|
||||
'authoritative'=>true,
|
||||
'hostile'=>true,
|
||||
));
|
||||
|
||||
To disable updating to crypt() on password change, simply set the 'overwrite' setting to false:
|
||||
|
||||
addPlugin('AuthCrypt', array(
|
||||
'overwrite'=>false,
|
||||
));
|
||||
|
||||
Settings
|
||||
========
|
||||
Default values in parenthesis. Many settings are inherited from the AuthenticationPlugin class.
|
||||
|
||||
authoritative (false): Set to true when all passwords are hashed with crypt()
|
||||
(warning: this may disable all other password verification, also when changing passwords!)
|
||||
hash ('$6$'): Hash signature to use, defaults to SHA512. See all supported strings at http://php.net/crypt
|
||||
(warning: set this to something crypt() understands, or you will default to the very weak 2-char DES scheme)
|
||||
hostile (false): Do we update the password hash entries on login?
|
||||
(notice: will check password login against old-style hash and then update using crypt())
|
||||
overwrite (true): Do we overwrite old style password hashes with crypt() hashes on password change?
|
||||
(notice: to make use of stronger security or migrate to crypt() hashes, this must be true)
|
||||
password_changeable (true): Enables or disables password changing.
|
||||
(notice: if combined with authoritative, it disables changing passwords and removes option from menu.)
|
||||
autoregistration: This setting is ignored. Password can never be valid without existing User.
|
||||
provider_name: This setting defaults to 'crypt' but is never stored anywhere.
|
||||
|
||||
Todo
|
||||
====
|
||||
This does not in any way get in touch with common_munge_password, which is
|
||||
called upon User::register(...) and RecoverpasswordAction->resetPassword().
|
||||
Maybe there should be an event like StartMungePassword in the core?
|
Loading…
Reference in New Issue
Block a user