2021-03-01 07:52:46 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use \RainLoop\Exceptions\ClientException;
|
|
|
|
|
|
|
|
class ChangePasswordPlugin extends \RainLoop\Plugins\AbstractPlugin
|
|
|
|
{
|
|
|
|
const
|
|
|
|
NAME = 'Change Password',
|
2021-03-03 18:53:11 +08:00
|
|
|
VERSION = '2.0',
|
|
|
|
RELEASE = '2021-03-03',
|
|
|
|
REQUIRED = '2.3.4',
|
2021-03-01 07:52:46 +08:00
|
|
|
CATEGORY = 'Security',
|
2021-03-03 18:53:11 +08:00
|
|
|
DESCRIPTION = 'This plugin allows you to change passwords of email accounts';
|
2021-03-01 07:52:46 +08:00
|
|
|
|
|
|
|
// \RainLoop\Notifications\
|
|
|
|
const CouldNotSaveNewPassword = 130;
|
|
|
|
const CurrentPasswordIncorrect = 131;
|
|
|
|
const NewPasswordShort = 132;
|
|
|
|
const NewPasswordWeak = 133;
|
|
|
|
|
|
|
|
public function Init() : void
|
|
|
|
{
|
|
|
|
$this->UseLangs(true); // start use langs folder
|
|
|
|
|
|
|
|
$this->addJs('js/ChangePasswordUserSettings.js'); // add js file
|
|
|
|
$this->addJsonHook('ChangePassword', 'ChangePassword');
|
|
|
|
$this->addTemplate('templates/SettingsChangePassword.html');
|
2021-03-02 19:23:50 +08:00
|
|
|
}
|
2021-03-01 07:52:46 +08:00
|
|
|
|
2021-03-02 21:23:45 +08:00
|
|
|
protected function getSupportedDrivers(bool $all = false) : iterable
|
2021-03-02 19:35:41 +08:00
|
|
|
{
|
2021-03-04 18:56:26 +08:00
|
|
|
if ($phar_file = \Phar::running()) {
|
|
|
|
$phar = new \Phar($phar_file, \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::KEY_AS_FILENAME);
|
|
|
|
foreach (new \RecursiveIteratorIterator($phar) as $file) {
|
|
|
|
if (\preg_match('#/drivers/([a-z]+)\\.php$#Di', $file, $m)) {
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ($all || $this->Config()->Get('plugin', "driver_{$m[1]}_enabled", false)) {
|
|
|
|
require_once $file;
|
|
|
|
$class = 'ChangePasswordDriver' . $m[1];
|
|
|
|
if ($class::isSupported()) {
|
|
|
|
yield $m[1] => $class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Throwable $oException)
|
|
|
|
{
|
2021-03-02 19:35:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 18:56:26 +08:00
|
|
|
} else {
|
|
|
|
// foreach (\glob(__DIR__ . '/../change-password-*', GLOB_ONLYDIR) as $file) {
|
|
|
|
foreach (\glob(__DIR__ . '/drivers/*.php') as $file) {
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$name = \basename($file, '.php');
|
|
|
|
if ($all || $this->Config()->Get('plugin', "driver_{$name}_enabled", false)) {
|
|
|
|
require_once $file;
|
|
|
|
$class = 'ChangePasswordDriver' . $name;
|
|
|
|
if ($class::isSupported()) {
|
|
|
|
yield $name => $class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Throwable $oException)
|
|
|
|
{
|
|
|
|
}
|
2021-03-02 21:23:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function Supported() : string
|
|
|
|
{
|
|
|
|
foreach ($this->getSupportedDrivers() as $class) {
|
|
|
|
return '';
|
2021-03-02 19:35:41 +08:00
|
|
|
}
|
|
|
|
return 'There are no change-password drivers enabled';
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:23:50 +08:00
|
|
|
public function configMapping() : array
|
|
|
|
{
|
2021-03-04 20:51:21 +08:00
|
|
|
$result = [
|
|
|
|
\RainLoop\Plugins\Property::NewInstance("pass_min_length")
|
|
|
|
->SetLabel('Password minimum length')
|
|
|
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::INT)
|
|
|
|
->SetDescription('Minimum length of the password')
|
|
|
|
->SetDefaultValue(10),
|
|
|
|
];
|
2021-03-02 21:23:45 +08:00
|
|
|
foreach ($this->getSupportedDrivers(true) as $name => $class) {
|
|
|
|
$result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_enabled")
|
|
|
|
->SetLabel('Enable ' . $class::NAME)
|
|
|
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
|
|
|
|
->SetDescription($class::DESCRIPTION);
|
|
|
|
$result[] = \RainLoop\Plugins\Property::NewInstance("driver_{$name}_allowed_emails")
|
|
|
|
->SetLabel('Allowed emails')
|
|
|
|
->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT)
|
|
|
|
->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@example.net user2@example1.net *@example2.net')
|
|
|
|
->SetDefaultValue('*');
|
|
|
|
$result = \array_merge($result, \call_user_func("{$class}::configMapping"));
|
2021-03-02 19:23:50 +08:00
|
|
|
}
|
|
|
|
return $result;
|
2021-03-01 07:52:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function ChangePassword()
|
|
|
|
{
|
2021-03-04 19:20:06 +08:00
|
|
|
$oActions = $this->Manager()->Actions();
|
|
|
|
$oAccount = $oActions->GetAccount();
|
|
|
|
|
2021-03-02 19:23:50 +08:00
|
|
|
if (!$oAccount->Email()) {
|
|
|
|
throw new ClientException(static::CouldNotSaveNewPassword);
|
|
|
|
}
|
|
|
|
|
2021-03-01 07:52:46 +08:00
|
|
|
$sPrevPassword = $this->jsonParam('PrevPassword');
|
|
|
|
$sNewPassword = $this->jsonParam('NewPassword');
|
|
|
|
|
|
|
|
if ($sPrevPassword !== $oAccount->Password()) {
|
|
|
|
throw new ClientException(static::CurrentPasswordIncorrect, null, $oActions->StaticI18N('NOTIFICATIONS/CURRENT_PASSWORD_INCORRECT'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$sPasswordForCheck = \trim($sNewPassword);
|
2021-03-04 20:51:21 +08:00
|
|
|
if ($this->Config()->Get('plugin', 'pass_min_length', 10) > \strlen($sPasswordForCheck)) {
|
2021-03-01 07:52:46 +08:00
|
|
|
throw new ClientException(static::NewPasswordShort, null, $oActions->StaticI18N('NOTIFICATIONS/NEW_PASSWORD_SHORT'));
|
|
|
|
}
|
|
|
|
|
2021-03-04 20:51:21 +08:00
|
|
|
if (!static::PasswordWeaknessCheck($sPasswordForCheck)) {
|
2021-03-01 07:52:46 +08:00
|
|
|
throw new ClientException(static::NewPasswordWeak, null, $oActions->StaticI18N('NOTIFICATIONS/NEW_PASSWORD_WEAK'));
|
|
|
|
}
|
2021-03-01 19:42:34 +08:00
|
|
|
|
2021-03-02 19:23:50 +08:00
|
|
|
$bResult = false;
|
|
|
|
$oConfig = $this->Config();
|
2021-03-02 21:23:45 +08:00
|
|
|
foreach ($this->getSupportedDrivers() as $name => $class) {
|
2021-03-04 20:51:21 +08:00
|
|
|
$sFoundedValue = '';
|
|
|
|
if (\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $oConfig->Get('plugin', "driver_{$name}_allowed_emails"), $sFoundedValue)) {
|
2021-03-02 19:23:50 +08:00
|
|
|
$name = $class::NAME;
|
|
|
|
try
|
|
|
|
{
|
2021-03-02 21:23:45 +08:00
|
|
|
$oDriver = new $class(
|
|
|
|
$oConfig(),
|
|
|
|
$oActions->Logger()
|
|
|
|
);
|
|
|
|
if (!$oDriver->ChangePassword($oAccount, $sPrevPassword, $sNewPassword)) {
|
|
|
|
throw new ClientException(static::CouldNotSaveNewPassword);
|
|
|
|
}
|
|
|
|
$bResult = true;
|
|
|
|
if ($this->oLogger) {
|
|
|
|
$this->oLogger->Write("{$name} password changed for {$oAccount->Email()}");
|
2021-03-02 19:23:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Throwable $oException)
|
|
|
|
{
|
|
|
|
if ($this->oLogger) {
|
|
|
|
$this->oLogger->Write("ERROR: {$name} password change for {$oAccount->Email()} failed");
|
|
|
|
$this->oLogger->WriteException($oException);
|
|
|
|
// $this->oLogger->WriteException($oException, \MailSo\Log\Enumerations\Type::WARNING, $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($bResult) {
|
|
|
|
$oAccount->SetPassword($sNewPassword);
|
|
|
|
$oActions->SetAuthToken($oAccount);
|
2021-03-01 07:52:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $oActions->GetSpecAuthToken();
|
|
|
|
// return $this->jsonResponse(__FUNCTION__, $oActions->GetSpecAuthToken());
|
|
|
|
}
|
|
|
|
|
2021-03-02 19:23:50 +08:00
|
|
|
public static function encrypt(string $algo, string $password)
|
2021-03-01 07:52:46 +08:00
|
|
|
{
|
2021-03-02 19:23:50 +08:00
|
|
|
switch (\strtolower($algo))
|
|
|
|
{
|
|
|
|
case 'argon2i':
|
|
|
|
return \password_hash($password, PASSWORD_ARGON2I);
|
|
|
|
|
|
|
|
case 'argon2id':
|
|
|
|
return \password_hash($password, PASSWORD_ARGON2ID);
|
|
|
|
|
|
|
|
case 'bcrypt':
|
|
|
|
return \password_hash($password, PASSWORD_BCRYPT);
|
|
|
|
|
|
|
|
case 'sha256-crypt':
|
|
|
|
return \crypt($password,'$5$'.\substr(\base64_encode(\random_bytes(32)), 0, 16));
|
|
|
|
|
|
|
|
case 'sha512-crypt':
|
|
|
|
return \crypt($password,'$6$'.\substr(\base64_encode(\random_bytes(32)), 0, 16));
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sPassword;
|
2021-03-01 07:52:46 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 20:51:21 +08:00
|
|
|
private static function PasswordWeaknessCheck(string $sPassword) : bool
|
|
|
|
{
|
2021-03-04 21:18:23 +08:00
|
|
|
return !\preg_match('/111|1234|password|abc|qwerty|monkey|letmein|dragon|baseball|iloveyou|trustno1|sunshine|master|welcome|shadow|ashley|football|jesus|michael|ninja|mustang|vkontakte/i', $sPassword);
|
2021-03-04 20:51:21 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 07:52:46 +08:00
|
|
|
}
|