From 521eb439b0223400e0ed6fefc3e5b0d8978ffdc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 5 May 2022 11:53:27 +0000 Subject: [PATCH] Code cleanup --- app/config/collections.php | 2 +- src/Appwrite/Auth/Auth.php | 90 +++++------------ src/Appwrite/Auth/Hash/PHPass.php | 158 +++++++++++------------------- tests/unit/Auth/AuthTest.php | 9 ++ 4 files changed, 93 insertions(+), 166 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index e68017a6df..bde4be0e8b 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -991,7 +991,7 @@ $collections = [ 'required' => false, 'default' => null, 'array' => false, - 'filters' => [], + 'filters' => ['encrypt'], ], [ '$id' => 'hash', // Hashing algorithm used to hash the password diff --git a/src/Appwrite/Auth/Auth.php b/src/Appwrite/Auth/Auth.php index e1e149889f..19787600dc 100644 --- a/src/Appwrite/Auth/Auth.php +++ b/src/Appwrite/Auth/Auth.php @@ -2,12 +2,6 @@ namespace Appwrite\Auth; -use Appwrite\Auth\Hash\BCrypt; -use Appwrite\Auth\Hash\MD5; -use Appwrite\Auth\Hash\PHPass; -use Appwrite\Auth\Hash\Argon2; -use Appwrite\Auth\Hash\SCrypt; -use Appwrite\Auth\Hash\SCryptModified; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; @@ -20,8 +14,8 @@ class Auth 'md5' => 'MD5', 'phpass' => 'PHPass', 'scrypt' => 'SCrypt', - 'scrypt_modified' => 'SCryptModified', - 'plaintext' => 'PlainText' + 'scrypt_mod' => 'SCryptModified', + 'plaintext' => '' // This is alias for DX purposes. It is translated to default algo ]; const DEFAULT_ALGO = 'argon2'; @@ -158,36 +152,22 @@ class Auth */ public static function passwordHash(string $string, string $algo, mixed $options = []) { - // TODO: Abstract, somehow. - switch ($algo) { - case 'bcrypt': - $hasher = new BCrypt($options); - $hash = $hasher->hash($string); - return $hash; - case 'scrypt_mod': - $hasher = new SCryptModified($options); - $hash = $hasher->hash($string); - return $hash; - case 'scrypt': - $hasher = new SCrypt($options); - $hash = $hasher->hash($string); - return $hash; - case 'md5': - $hasher = new MD5($options); - $hash = $hasher->hash($string); - return $hash; - case 'argon2': - $hasher = new Argon2($options); - $hash = $hasher->hash($string); - return $hash; - case 'phpass': - // TODO: Rework to use abstract class - $hahser = new PHPass(8, FALSE); - $hash = $hahser->hash($string); - return $hash; + // Plain text not supported, just an alias. Switch to recommended algo + if($algo === 'plaintext') { + $algo = Auth::DEFAULT_ALGO; + $options = Auth::DEFAULT_ALGO_OPTIONS; } - return null; + if(!\array_key_exists($algo, Auth::SUPPORTED_ALGOS)) { + throw new \Exception('Hashing algorithm \'' . $algo . '\' is not supported.'); + } + + $className = Auth::SUPPORTED_ALGOS[$algo]; + $classPath = '\\Appwrite\\Auth\\Hash\\'.$className; + $hasher = new $classPath($options); + $hash = $hasher->hash($string); + + return $hash; } /** @@ -202,36 +182,18 @@ class Auth */ public static function passwordVerify(string $plain, string $hash, string $algo, mixed $options = []) { - // TODO: Abstract, somehow. - switch ($algo) { - case 'bcrypt': - $hasher = new BCrypt($options); - $verify = $hasher->verify($plain, $hash); - return $verify; - case 'scrypt_mod': - $hasher = new SCryptModified($options); - $verify = $hasher->verify($plain, $hash); - return $verify; - case 'scrypt': - $hasher = new SCrypt($options ?? [ 'cost_cpu' => 8, 'cost_memory' => 14, 'cost_parallel' => 1, 'length' => 64 ]); - $verify = $hasher->verify($plain, $hash); - return $verify; - case 'md5': - $hasher = new MD5($options ?? []); - $verify = $hasher->verify($plain, $hash); - return $verify; - case 'argon2': - $hasher = new Argon2($options ?? []); - $verify = $hasher->verify($plain, $hash); - return $verify; - case 'phpass': - // TODO: Rework to use abstract class - $hahser = new PHPass(8, FALSE); - $verify = $hahser->verify($plain, $hash); - return $verify; + // Plain text not supported, just an alias. Switch to recommended algo + if($algo === 'plaintext') { + $algo = Auth::DEFAULT_ALGO; + $options = Auth::DEFAULT_ALGO_OPTIONS; } - return false; + $className = Auth::SUPPORTED_ALGOS[$algo]; + $classPath = '\\Appwrite\\Auth\\Hash\\'.$className; + $hasher = new $classPath($options); + $verify = $hasher->verify($plain, $hash); + + return $verify; } /** diff --git a/src/Appwrite/Auth/Hash/PHPass.php b/src/Appwrite/Auth/Hash/PHPass.php index 9ca2a9654e..7520a2a33f 100644 --- a/src/Appwrite/Auth/Hash/PHPass.php +++ b/src/Appwrite/Auth/Hash/PHPass.php @@ -22,12 +22,17 @@ namespace Appwrite\Auth\Hash; -/** - * PasswordHash class is a portable password hashing framework for use in PHP applications. - * - * @since 0.1 - */ -class PHPass +use Appwrite\Auth\Hash; + +/* + * PHPass accepted options: + * int iteration_count_log2; The Logarithmic cost value used when generating hash values indicating the number of rounds used to generate hashes + * string portable_hashes + * string random_state; The cached random state + * + * Refference: +*/ +class PHPass extends Hash { /** * Alphabet used in itoa64 conversions. @@ -37,77 +42,42 @@ class PHPass */ protected $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; - /** - * The Logarithmic cost value used when generating hash values indicating the number of rounds used to generate hashes - * - * @var integer - * @since 0.1.0 - */ - public $iteration_count_log2 = 12; - - /** - * The portable_hashes - * - * @var string - * @since 0.1.0 - */ - public $portable_hashes; - - /** - * The cached random state - * - * @var string - * @since 0.1.0 - */ - protected $random_state; - - /** - * Constructor - * - * @param int $iteration_count_log2 Logarithmic cost value used when generating hash values - * @param string $portable_hashes - * - * @since 0.5.0 - */ - public function __construct($iteration_count_log2, $portable_hashes) + /** + * Get default options for specific hashing algo + * + * @return mixed options named array + */ + public function getDefaultOptions(): mixed { - if ($iteration_count_log2 < 10) { - // The minimum Logarithmic cost value - $iteration_count_log2 = 10; - } else { - if ($iteration_count_log2 > 32) { - // The maximum Logarithmic cost value - $iteration_count_log2 = 32; - } + $randomState = \microtime(); + if (\function_exists('getmypid')) { + $randomState .= getmypid(); } - $this->iteration_count_log2 = $iteration_count_log2; - $this->portable_hashes = $portable_hashes; - $this->random_state = microtime(); - if (function_exists('getmypid')) { - $this->random_state .= getmypid(); - } - } + + return ['iteration_count_log2' => 8, 'portable_hashes' => false, 'random_state' => $randomState]; + } /** - * @param String $password - * - * @return String $hash - * @since 0.1.0 - */ - public function hash($password) + * @param string $password Input password to hash + * + * @return string hash + */ + public function hash(string $password): string { + $options = $this->getDefaultOptions(); + $random = ''; - if (CRYPT_BLOWFISH === 1 && !$this->portable_hashes) { - $random = $this->get_random_bytes(16); - $hash = crypt($password, $this->gensalt_blowfish($random)); + if (CRYPT_BLOWFISH === 1 && !$options['portable_hashes']) { + $random = $this->get_random_bytes(16, $options); + $hash = crypt($password, $this->gensalt_blowfish($random, $options)); if (strlen($hash) === 60) { return $hash; } } if (strlen($random) < 6) { - $random = $this->get_random_bytes(6); + $random = $this->get_random_bytes(6, $options); } - $hash = $this->crypt_private($password, $this->gensalt_private($random)); + $hash = $this->crypt_private($password, $this->gensalt_private($random, $options)); if (strlen($hash) === 34) { return $hash; } @@ -120,18 +90,17 @@ class PHPass return '*'; } - /** - * @param String $password - * @param String $stored_hash - * - * @return boolean - * @since 0.1.0 - */ - public function verify($password, $stored_hash) + /** + * @param string $password Input password to validate + * @param string $hash Hash to verify password against + * + * @return boolean true if password matches hash + */ + public function verify(string $password, string $hash): bool { - $hash = $this->crypt_private($password, $stored_hash); + $hash = $this->crypt_private($password, $hash); if ($hash[0] === '*') { - $hash = crypt($password, $stored_hash); + $hash = crypt($password, $hash); } /** @@ -140,20 +109,7 @@ class PHPass * unpredictable, which they are at least in the non-fallback * cases (that is, when we use /dev/urandom and bcrypt). */ - return $hash === $stored_hash; - } - - /** - * A backwards compatible constructor - * - * @param int $iteration_count_log2 Logarithmic cost value used when generating hash values - * @param string $portable_hashes - * - * @since 0.1.0 - */ - public function PasswordHash($iteration_count_log2, $portable_hashes) - { - self::__construct($iteration_count_log2, $portable_hashes); + return $hash === $hash; } /** @@ -161,12 +117,12 @@ class PHPass * * @return String $output * @since 0.1.0 - * @throws InvalidArgumentException Thows an InvalidArgumentException if the $count parameter is not a positive integer. + * @throws Exception Thows an Exception if the $count parameter is not a positive integer. */ - protected function get_random_bytes($count) + protected function get_random_bytes($count, $options) { if (!is_int($count) || $count < 1) { - throw new InvalidArgumentException('Argument count must be a positive integer'); + throw new \Exception('Argument count must be a positive integer'); } $output = ''; if (@is_readable('/dev/urandom') && ($fh = @fopen('/dev/urandom', 'rb'))) { @@ -178,8 +134,8 @@ class PHPass $output = ''; for ($i = 0; $i < $count; $i += 16) { - $this->random_state = md5(microtime() . $this->random_state); - $output .= md5($this->random_state, TRUE); + $options['iteration_count_log2'] = md5(microtime() . $options['iteration_count_log2']); + $output .= md5($options['iteration_count_log2'], TRUE); } $output = substr($output, 0, $count); @@ -194,12 +150,12 @@ class PHPass * * @return String $output * @since 0.1.0 - * @throws InvalidArgumentException Thows an InvalidArgumentException if the $count parameter is not a positive integer. + * @throws Exception Thows an Exception if the $count parameter is not a positive integer. */ protected function encode64($input, $count) { if (!is_int($count) || $count < 1) { - throw new InvalidArgumentException('Argument count must be a positive integer'); + throw new \Exception('Argument count must be a positive integer'); } $output = ''; $i = 0; @@ -232,10 +188,10 @@ class PHPass * @return String $output * @since 0.1.0 */ - private function gensalt_private($input) + private function gensalt_private($input, $options) { $output = '$P$'; - $output .= $this->itoa64[min($this->iteration_count_log2 + ((PHP_VERSION >= '5') ? 5 : 3), 30)]; + $output .= $this->itoa64[min($options['iteration_count_log2'] + ((PHP_VERSION >= '5') ? 5 : 3), 30)]; $output .= $this->encode64($input, 6); return $output; @@ -292,7 +248,7 @@ class PHPass * @return String $output * @since 0.1.0 */ - private function gensalt_blowfish($input) + private function gensalt_blowfish($input, $options) { /** * This one needs to use a different order of characters and a @@ -306,8 +262,8 @@ class PHPass */ $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $output = '$2a$'; - $output .= chr(ord('0') + $this->iteration_count_log2 / 10); - $output .= chr(ord('0') + $this->iteration_count_log2 % 10); + $output .= chr(ord('0') + $options['iteration_count_log2'] / 10); + $output .= chr(ord('0') + $options['iteration_count_log2'] % 10); $output .= '$'; $i = 0; do { diff --git a/tests/unit/Auth/AuthTest.php b/tests/unit/Auth/AuthTest.php index 52c9f45193..6af1cb6213 100644 --- a/tests/unit/Auth/AuthTest.php +++ b/tests/unit/Auth/AuthTest.php @@ -141,6 +141,15 @@ class AuthTest extends TestCase $this->assertEquals(true, Auth::passwordVerify($plain, $generatedHash, 'scrypt_mod', $options)); $this->assertEquals(true, Auth::passwordVerify($plain, $hash, 'scrypt_mod', $options)); } + + public function testUnknownAlgo() { + $this->expectExceptionMessage('Hashing algorithm \'md8\' is not supported.'); + + // Bcrypt - Cost 5 + $plain = 'whatIsMd8?!?'; + $generatedHash = Auth::passwordHash($plain, 'md8'); + $this->assertEquals(true, Auth::passwordVerify($plain, $generatedHash, 'md8')); + } public function testPasswordGenerator() {