Changes for #349

This commit is contained in:
the-djmaze 2022-04-29 13:23:04 +02:00
parent 80eed213d9
commit a06b59784a
3 changed files with 27 additions and 16 deletions

View file

@ -8,8 +8,8 @@ class TwoFactorAuthPlugin extends \RainLoop\Plugins\AbstractPlugin
const
NAME = 'Two Factor Authentication',
VERSION = '2.15.0',
RELEASE = '2022-04-25',
REQUIRED = '2.15.0',
RELEASE = '2022-04-29',
REQUIRED = '2.15.1',
CATEGORY = 'Login',
DESCRIPTION = 'Provides support for TOTP 2FA';
@ -47,7 +47,13 @@ class TwoFactorAuthPlugin extends \RainLoop\Plugins\AbstractPlugin
public function FilterAppData($bAdmin, &$aResult)
{
if (!$bAdmin && \is_array($aResult)/* && isset($aResult['Auth']) && !$aResult['Auth']*/) {
$aResult['RequireTwoFactor'] = $this->Config()->Get('plugin', 'force_two_factor_auth', false);
$aResult['RequireTwoFactor'] = (bool) $this->Config()->Get('plugin', 'force_two_factor_auth', false);
$aResult['SetupTwoFactor'] = false;
if ($aResult['RequireTwoFactor'] && !empty($aResult['Auth'])) {
$aData = $this->getTwoFactorInfo($this->getMainAccountFromToken());
$aResult['SetupTwoFactor'] = empty($aResult['IsSet']) || empty($mData['Enable']);
}
}
}
@ -55,7 +61,7 @@ class TwoFactorAuthPlugin extends \RainLoop\Plugins\AbstractPlugin
{
if ($this->TwoFactorAuthProvider($oAccount)) {
$aData = $this->getTwoFactorInfo($oAccount);
if ($aData && isset($aData['IsSet'], $aData['Enable']) && !empty($aData['Secret']) && $aData['IsSet'] && $aData['Enable']) {
if (isset($aData['IsSet'], $aData['Enable']) && !empty($aData['Secret']) && $aData['IsSet'] && $aData['Enable']) {
$sCode = \trim($this->jsonParam('totp_code', ''));
if (empty($sCode)) {
$this->Logger()->Write("TFA: Code required for {$oAccount->Email()}");

View file

@ -1,8 +1,15 @@
(rl => {
const
forceTOTP = () => {
if (rl.settings.get('SetupTwoFactor')) {
setTimeout(() => document.location.hash = '#/settings/two-factor-auth', 50);
}
};
addEventListener('rl-view-model', e => {
if (e.detail && 'Login' === e.detail.viewModelTemplateID) {
if ('Login' === e.detail.viewModelTemplateID) {
const container = e.detail.viewModelDom.querySelector('#plugin-Login-BottomControlGroup'),
placeholder = 'PLUGIN_2FA/LABEL_TWO_FACTOR_CODE';
if (container) {
@ -19,13 +26,10 @@
});
// https://github.com/the-djmaze/snappymail/issues/349
let code;
addEventListener('sm-user-login', e => {
code = e.detail.get('totp_code');
});
addEventListener('sm-user-login-response', e => {
if (e.detail && !e.detail.error && rl.settings.get('RequireTwoFactor') && !code) {
document.location.hash = '#/settings/two-factor-auth';
addEventListener('sm-show-screen', e => {
if ('settings' !== e.detail && rl.settings.get('SetupTwoFactor')) {
e.preventDefault();
forceTOTP();
}
});

View file

@ -56,11 +56,12 @@ class TwoFactorAuthSettings
value = !!value;
if (value && this.twoFactorTested()) {
this.viewEnable_(value);
Remote.enableTwoFactor(fn, value);
Remote.enableTwoFactor(iError => {
fn(iError);
rl.settings.get('RequireTwoFactor') && rl.settings.set('SetupTwoFactor', !!iError);
}, value);
} else {
if (!value) {
this.viewEnable_(value);
}
value || this.viewEnable_(value);
Remote.enableTwoFactor(fn, false);
}
}