2009-11-06 03:23:21 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
|
|
|
use ReflectionClass;
|
2017-06-09 05:07:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2009-11-06 03:23:21 +01:00
|
|
|
/**
|
|
|
|
* Allows pluggable password encryption.
|
|
|
|
* By default, this might be PHP's integrated sha1()
|
|
|
|
* function, but could also be more sophisticated to facilitate
|
|
|
|
* password migrations from other systems.
|
|
|
|
* Use {@link register()} to add new implementations.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-11-06 03:23:21 +01:00
|
|
|
* Used in {@link Security::encrypt_password()}.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
abstract class PasswordEncryptor
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
private static $encryptors = [];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @return array Map of encryptor code to the used class.
|
|
|
|
*/
|
|
|
|
public static function get_encryptors()
|
|
|
|
{
|
2018-02-01 04:24:31 +01:00
|
|
|
return Config::inst()->get(self::class, 'encryptors');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
2020-12-21 22:23:23 +01:00
|
|
|
* @param string $algorithm
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return PasswordEncryptor
|
|
|
|
* @throws PasswordEncryptor_NotFoundException
|
|
|
|
*/
|
|
|
|
public static function create_for_algorithm($algorithm)
|
|
|
|
{
|
|
|
|
$encryptors = self::get_encryptors();
|
|
|
|
if (!isset($encryptors[$algorithm])) {
|
|
|
|
throw new PasswordEncryptor_NotFoundException(
|
|
|
|
sprintf('No implementation found for "%s"', $algorithm)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$class=key($encryptors[$algorithm] ?? []);
|
|
|
|
if (!class_exists($class ?? '')) {
|
2016-11-29 00:31:16 +01:00
|
|
|
throw new PasswordEncryptor_NotFoundException(
|
|
|
|
sprintf('No class found for "%s"', $class)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$refClass = new ReflectionClass($class);
|
|
|
|
if (!$refClass->getConstructor()) {
|
|
|
|
return new $class;
|
|
|
|
}
|
2009-11-06 03:23:21 +01:00
|
|
|
|
2020-08-30 01:54:39 +02:00
|
|
|
// Don't treat array keys as argument names - keeps PHP 7 and PHP 8 operating similarly
|
2022-04-14 03:12:59 +02:00
|
|
|
$arguments = array_values($encryptors[$algorithm] ?? []);
|
2016-11-29 00:31:16 +01:00
|
|
|
return($refClass->newInstanceArgs($arguments));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Return a string value stored in the {@link Member->Password} property.
|
|
|
|
* The password should be hashed with {@link salt()} if applicable.
|
|
|
|
*
|
2020-12-21 22:23:23 +01:00
|
|
|
* @param string $password Cleartext password to be hashed
|
|
|
|
* @param string $salt (Optional)
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param Member $member (Optional)
|
|
|
|
* @return String Maximum of 512 characters.
|
|
|
|
*/
|
|
|
|
abstract public function encrypt($password, $salt = null, $member = null);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Return a string value stored in the {@link Member->Salt} property.
|
|
|
|
*
|
|
|
|
* @uses RandomGenerator
|
|
|
|
*
|
|
|
|
* @param string $password Cleartext password
|
|
|
|
* @param Member $member (Optional)
|
|
|
|
* @return string Maximum of 50 characters
|
|
|
|
*/
|
|
|
|
public function salt($password, $member = null)
|
|
|
|
{
|
|
|
|
$generator = new RandomGenerator();
|
2022-04-14 03:12:59 +02:00
|
|
|
return substr($generator->randomToken('sha1') ?? '', 0, 50);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* This usually just returns a strict string comparison,
|
|
|
|
* but is necessary for retain compatibility with password hashed
|
|
|
|
* with flawed algorithms - see {@link PasswordEncryptor_LegacyPHPHash} and
|
|
|
|
* {@link PasswordEncryptor_Blowfish}
|
|
|
|
*
|
|
|
|
* @param string $hash
|
|
|
|
* @param string $password
|
|
|
|
* @param string $salt
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function check($hash, $password, $salt = null, $member = null)
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return hash_equals($hash ?? '', $this->encrypt($password, $salt, $member) ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2009-11-06 03:23:21 +01:00
|
|
|
}
|