[AuthCrypt] Update README and fix formatting

This commit is contained in:
Diogo Cordeiro
2020-07-25 17:42:46 +01:00
parent 2222d6d173
commit 0b947ce2c7
4 changed files with 184 additions and 213 deletions

View File

@@ -1,50 +1,44 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social 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.
//
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* StatusNet, the distributed open-source microblogging tool
*
* Module 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 Module
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2012 StatusNet, Inc.
* @copyright 2013 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://www.gnu.org/software/social/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
if (!defined('GNUSOCIAL')) { exit(1); }
defined('GNUSOCIAL') || die;
class AuthCryptModule extends AuthenticationModule
{
const MODULE_VERSION = '2.0.0';
protected $hash = '$6$'; // defaults to SHA512, i.e. '$6$', in onInitializeModule()
protected $statusnet = true; // if true, also check StatusNet style password hash
protected $overwrite = true; // if true, password change means overwrite with crypt()
protected $argon = false; // Use Argon if supported.
protected $statusnet = true; // if true, also check StatusNet style password hash
protected $overwrite = true; // if true, password change means overwrite with crypt()
protected $argon = false; // Use Argon if supported.
public $provider_name = 'password_hash'; // not actually used
public $provider_name = 'crypt'; // not actually used
/*
* FUNCTIONALITY
*/
// FUNCTIONALITY
function checkPassword($username, $password)
public function checkPassword($username, $password)
{
$username = Nickname::normalize($username);
@@ -79,24 +73,12 @@ class AuthCryptModule extends AuthenticationModule
return false;
}
protected function cryptSalt($len=CRYPT_SALT_LENGTH)
{
$chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$salt = '';
for ($i=0; $i<$len; $i++) {
$salt .= $chars[mt_rand(0, strlen($chars)-1)];
}
return $salt;
}
// $oldpassword is already verified when calling this function... shouldn't this be private?!
function changePassword($username, $oldpassword, $newpassword)
public function changePassword($username, $oldpassword, $newpassword)
{
$username = Nickname::normalize($username);
if($this->overwrite == false) {
if (!$this->overwrite) {
return false;
}
@@ -104,24 +86,24 @@ class AuthCryptModule extends AuthenticationModule
if (empty($user)) {
return false;
}
$original = clone($user);
$original = clone $user;
$user->password = $this->hashPassword($newpassword, $user->getProfile());
return (true === $user->validate() && $user->update($original));
return $user->validate() === true && $user->update($original);
}
public function hashPassword($password, Profile $profile=null)
public function hashPassword($password, ?Profile $profile = null)
{
$algorithm = PASSWORD_DEFAULT;
$options = ['cost' => 12];
if($this->argon == true && version_compare(PHP_VERSION, '7.2.0') == 1) {
if ($this->argon) {
$algorithm = PASSWORD_ARGON2I;
$options = [
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST,
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS,
];
}
// Use the modern password hashing algorithm
@@ -130,9 +112,7 @@ class AuthCryptModule extends AuthenticationModule
return password_hash($password, $algorithm, $options);
}
/*
* EVENTS
*/
// EVENTS
public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
{
@@ -146,17 +126,17 @@ class AuthCryptModule extends AuthenticationModule
}
$changed = $this->changePassword($target->getNickname(), $oldpassword, $newpassword);
return (!$changed && empty($this->authoritative));
return !$changed && empty($this->authoritative);
}
public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
{
$authenticatedUser = $this->checkPassword($nickname, $password);
// if we failed, only return false to stop plugin execution if we're authoritative
return (!($authenticatedUser instanceof User) && empty($this->authoritative));
return !($authenticatedUser instanceof User) && empty($this->authoritative);
}
public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
public function onStartHashPassword(&$hashed, $password, ?Profile $profile = null)
{
$hashed = $this->hashPassword($password, $profile);
return false;
@@ -176,13 +156,14 @@ class AuthCryptModule extends AuthenticationModule
public function onModuleVersion(array &$versions): bool
{
$versions[] = array('name' => 'AuthCrypt',
'version' => self::MODULE_VERSION,
'author' => 'Mikael Nordfeldth',
'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/AuthCrypt',
'rawdescription' =>
// TRANS: Module description.
_m('Authentication and password hashing with crypt()'));
$versions[] = [
'name' => 'AuthCrypt',
'version' => self::MODULE_VERSION,
'author' => 'Mikael Nordfeldth',
'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/AuthCrypt',
'rawdescription' => // TRANS: Module description.
_m('Authentication and password hashing with crypt()')
];
return true;
}
}