snappymail/dev/View/Popup/TwoFactorConfiguration.js

201 lines
4.8 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { Capa, StorageResultType } from 'Common/Enums';
import { pString } from 'Common/Utils';
import { i18n, trigger as translatorTrigger } from 'Common/Translator';
import Remote from 'Remote/User/Fetch';
2019-07-05 03:19:24 +08:00
import { popup, showScreenPopup } from 'Knoin/Knoin';
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/TwoFactorConfiguration',
templateID: 'PopupsTwoFactorConfiguration'
})
2019-07-05 03:19:24 +08:00
class TwoFactorConfigurationPopupView extends AbstractViewNext {
constructor() {
super();
this.lock = ko.observable(false);
this.capaTwoFactor = rl.settings.capa(Capa.TwoFactor);
this.processing = ko.observable(false);
this.clearing = ko.observable(false);
this.secreting = ko.observable(false);
this.viewUser = ko.observable('');
this.twoFactorStatus = ko.observable(false);
this.twoFactorTested = ko.observable(false);
this.viewSecret = ko.observable('');
this.viewBackupCodes = ko.observable('');
this.viewUrlTitle = ko.observable('');
this.viewUrl = ko.observable('');
this.viewEnable_ = ko.observable(false);
this.viewEnable = ko.computed({
read: this.viewEnable_,
write: (value) => {
value = !!value;
2019-07-05 03:19:24 +08:00
if (value && this.twoFactorTested()) {
this.viewEnable_(value);
Remote.enableTwoFactor((result, data) => {
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success !== result || !data || !data.Result) {
this.viewEnable_(false);
}
}, true);
2019-07-05 03:19:24 +08:00
} else {
if (!value) {
this.viewEnable_(value);
}
Remote.enableTwoFactor((result, data) => {
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success !== result || !data || !data.Result) {
this.viewEnable_(false);
}
}, false);
}
}
});
this.viewTwoFactorEnableTooltip = ko.computed(() => {
translatorTrigger();
2019-07-05 03:19:24 +08:00
return this.twoFactorTested() || this.viewEnable_()
? ''
: i18n('POPUPS_TWO_FACTOR_CFG/TWO_FACTOR_SECRET_TEST_BEFORE_DESC');
});
this.viewTwoFactorStatus = ko.computed(() => {
translatorTrigger();
2019-07-05 03:19:24 +08:00
return i18n(
this.twoFactorStatus()
? 'POPUPS_TWO_FACTOR_CFG/TWO_FACTOR_SECRET_CONFIGURED_DESC'
: 'POPUPS_TWO_FACTOR_CFG/TWO_FACTOR_SECRET_NOT_CONFIGURED_DESC'
);
});
this.twoFactorAllowedEnable = ko.computed(() => this.viewEnable() || this.twoFactorTested());
this.onResult = this.onResult.bind(this);
this.onShowSecretResult = this.onShowSecretResult.bind(this);
}
2016-06-30 08:02:45 +08:00
showSecret() {
this.secreting(true);
Remote.showTwoFactorSecret(this.onShowSecretResult);
}
2016-06-30 08:02:45 +08:00
hideSecret() {
this.viewSecret('');
this.viewBackupCodes('');
this.viewUrlTitle('');
this.viewUrl('');
}
2016-06-30 08:02:45 +08:00
createTwoFactor() {
this.processing(true);
Remote.createTwoFactor(this.onResult);
}
2016-06-30 08:02:45 +08:00
logout() {
2020-09-15 15:29:25 +08:00
rl.app.logout();
}
2016-06-30 08:02:45 +08:00
testTwoFactor() {
showScreenPopup(require('View/Popup/TwoFactorTest'), [this.twoFactorTested]);
}
2016-06-30 08:02:45 +08:00
clearTwoFactor() {
this.viewSecret('');
this.viewBackupCodes('');
this.viewUrlTitle('');
this.viewUrl('');
2016-06-30 08:02:45 +08:00
this.twoFactorTested(false);
2016-06-30 08:02:45 +08:00
this.clearing(true);
Remote.clearTwoFactor(this.onResult);
}
2016-06-30 08:02:45 +08:00
onShow(bLock) {
this.lock(!!bLock);
2016-06-30 08:02:45 +08:00
this.viewSecret('');
this.viewBackupCodes('');
this.viewUrlTitle('');
this.viewUrl('');
}
onHide() {
2019-07-05 03:19:24 +08:00
if (this.lock()) {
location.reload();
}
}
getQr() {
2019-07-05 03:19:24 +08:00
return (
'otpauth://totp/' +
encodeURIComponent(this.viewUser()) +
2019-07-05 03:19:24 +08:00
'?secret=' +
encodeURIComponent(this.viewSecret()) +
2019-07-05 03:19:24 +08:00
'&issuer=' +
encodeURIComponent('')
2019-07-05 03:19:24 +08:00
);
}
onResult(sResult, oData) {
this.processing(false);
this.clearing(false);
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === sResult && oData && oData.Result) {
this.viewUser(pString(oData.Result.User));
this.viewEnable_(!!oData.Result.Enable);
this.twoFactorStatus(!!oData.Result.IsSet);
this.twoFactorTested(!!oData.Result.Tested);
this.viewSecret(pString(oData.Result.Secret));
this.viewBackupCodes(pString(oData.Result.BackupCodes).replace(/[\s]+/g, ' '));
this.viewUrlTitle(pString(oData.Result.UrlTitle));
this.viewUrl(qr.toDataURL({ level: 'M', size: 8, value: this.getQr() }));
2019-07-05 03:19:24 +08:00
} else {
this.viewUser('');
this.viewEnable_(false);
this.twoFactorStatus(false);
this.twoFactorTested(false);
this.viewSecret('');
this.viewBackupCodes('');
this.viewUrlTitle('');
this.viewUrl('');
}
2016-06-30 08:02:45 +08:00
}
onShowSecretResult(result, data) {
this.secreting(false);
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === result && data && data.Result) {
this.viewSecret(pString(data.Result.Secret));
this.viewUrlTitle(pString(data.Result.UrlTitle));
this.viewUrl(qr.toDataURL({ level: 'M', size: 6, value: this.getQr() }));
2019-07-05 03:19:24 +08:00
} else {
this.viewSecret('');
this.viewUrlTitle('');
this.viewUrl('');
}
2016-06-30 08:02:45 +08:00
}
onBuild() {
2019-07-05 03:19:24 +08:00
if (this.capaTwoFactor) {
this.processing(true);
Remote.getTwoFactor(this.onResult);
}
2016-06-30 08:02:45 +08:00
}
}
2019-07-05 03:19:24 +08:00
export { TwoFactorConfigurationPopupView, TwoFactorConfigurationPopupView as default };