2021-03-16 16:46:23 +08:00
|
|
|
import { Capa } from 'Common/Enums';
|
2021-03-10 18:44:48 +08:00
|
|
|
import { Settings } from 'Common/Globals';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { pString } from 'Common/Utils';
|
|
|
|
import { i18n, trigger as translatorTrigger } from 'Common/Translator';
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2020-09-15 01:40:56 +08:00
|
|
|
import Remote from 'Remote/User/Fetch';
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2021-01-24 17:25:23 +08:00
|
|
|
import { showScreenPopup } from 'Knoin/Knoin';
|
|
|
|
import { AbstractViewPopup } from 'Knoin/AbstractViews';
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2021-01-26 05:00:13 +08:00
|
|
|
import { TwoFactorTestPopupView } from 'View/Popup/TwoFactorTest';
|
|
|
|
|
2021-01-24 17:25:23 +08:00
|
|
|
class TwoFactorConfigurationPopupView extends AbstractViewPopup {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
2021-01-24 17:25:23 +08:00
|
|
|
super('TwoFactorConfiguration');
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
this.addObservables({
|
|
|
|
lock: false,
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
processing: false,
|
|
|
|
clearing: false,
|
|
|
|
secreting: false,
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
viewUser: '',
|
|
|
|
twoFactorStatus: false,
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
twoFactorTested: false,
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
viewSecret: '',
|
|
|
|
viewBackupCodes: '',
|
|
|
|
viewUrlTitle: '',
|
|
|
|
viewUrl: '',
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
viewEnable_: false
|
|
|
|
});
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2021-03-10 18:44:48 +08:00
|
|
|
this.capaTwoFactor = Settings.capa(Capa.TwoFactor);
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2021-03-18 21:48:21 +08:00
|
|
|
const fn = iError => iError && this.viewEnable_(false);
|
2020-10-26 19:54:03 +08:00
|
|
|
this.addComputables({
|
|
|
|
viewEnable: {
|
|
|
|
read: this.viewEnable_,
|
|
|
|
write: (value) => {
|
|
|
|
value = !!value;
|
|
|
|
if (value && this.twoFactorTested()) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.viewEnable_(value);
|
2021-03-18 21:48:21 +08:00
|
|
|
Remote.enableTwoFactor(fn, value);
|
2020-10-26 19:54:03 +08:00
|
|
|
} else {
|
|
|
|
if (!value) {
|
|
|
|
this.viewEnable_(value);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2021-03-18 21:48:21 +08:00
|
|
|
Remote.enableTwoFactor(fn, false);
|
2020-10-26 19:54:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
viewTwoFactorEnableTooltip: () => {
|
|
|
|
translatorTrigger();
|
|
|
|
return this.twoFactorTested() || this.viewEnable_()
|
|
|
|
? ''
|
|
|
|
: i18n('POPUPS_TWO_FACTOR_CFG/TWO_FACTOR_SECRET_TEST_BEFORE_DESC');
|
|
|
|
},
|
|
|
|
|
|
|
|
viewTwoFactorStatus: () => {
|
|
|
|
translatorTrigger();
|
|
|
|
return i18n(
|
|
|
|
this.twoFactorStatus()
|
|
|
|
? 'POPUPS_TWO_FACTOR_CFG/TWO_FACTOR_SECRET_CONFIGURED_DESC'
|
|
|
|
: 'POPUPS_TWO_FACTOR_CFG/TWO_FACTOR_SECRET_NOT_CONFIGURED_DESC'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
twoFactorAllowedEnable: () => this.viewEnable() || this.twoFactorTested()
|
2016-08-17 06:01:20 +08:00
|
|
|
});
|
|
|
|
|
2020-07-20 21:47:33 +08:00
|
|
|
this.onResult = this.onResult.bind(this);
|
|
|
|
this.onShowSecretResult = this.onShowSecretResult.bind(this);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
showSecret() {
|
|
|
|
this.secreting(true);
|
|
|
|
Remote.showTwoFactorSecret(this.onShowSecretResult);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
hideSecret() {
|
|
|
|
this.viewSecret('');
|
|
|
|
this.viewBackupCodes('');
|
|
|
|
this.viewUrlTitle('');
|
|
|
|
this.viewUrl('');
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
createTwoFactor() {
|
|
|
|
this.processing(true);
|
|
|
|
Remote.createTwoFactor(this.onResult);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
logout() {
|
2020-09-15 15:29:25 +08:00
|
|
|
rl.app.logout();
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
testTwoFactor() {
|
2021-01-26 05:00:13 +08:00
|
|
|
showScreenPopup(TwoFactorTestPopupView, [this.twoFactorTested]);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
clearTwoFactor() {
|
|
|
|
this.viewSecret('');
|
|
|
|
this.viewBackupCodes('');
|
|
|
|
this.viewUrlTitle('');
|
|
|
|
this.viewUrl('');
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.twoFactorTested(false);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.clearing(true);
|
|
|
|
Remote.clearTwoFactor(this.onResult);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShow(bLock) {
|
|
|
|
this.lock(!!bLock);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.viewSecret('');
|
|
|
|
this.viewBackupCodes('');
|
|
|
|
this.viewUrlTitle('');
|
|
|
|
this.viewUrl('');
|
2015-04-08 00:39:43 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onHide() {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.lock()) {
|
2020-08-12 06:25:36 +08:00
|
|
|
location.reload();
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
getQr() {
|
2019-07-05 03:19:24 +08:00
|
|
|
return (
|
|
|
|
'otpauth://totp/' +
|
2020-08-12 06:25:36 +08:00
|
|
|
encodeURIComponent(this.viewUser()) +
|
2019-07-05 03:19:24 +08:00
|
|
|
'?secret=' +
|
2020-08-12 06:25:36 +08:00
|
|
|
encodeURIComponent(this.viewSecret()) +
|
2019-07-05 03:19:24 +08:00
|
|
|
'&issuer=' +
|
2020-08-12 06:25:36 +08:00
|
|
|
encodeURIComponent('')
|
2019-07-05 03:19:24 +08:00
|
|
|
);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2021-03-16 16:46:23 +08:00
|
|
|
onResult(iError, oData) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.processing(false);
|
|
|
|
this.clearing(false);
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2021-03-18 21:48:21 +08:00
|
|
|
if (iError) {
|
|
|
|
this.viewUser('');
|
|
|
|
this.viewEnable_(false);
|
|
|
|
this.twoFactorStatus(false);
|
|
|
|
this.twoFactorTested(false);
|
|
|
|
|
|
|
|
this.viewSecret('');
|
|
|
|
this.viewBackupCodes('');
|
|
|
|
this.viewUrlTitle('');
|
|
|
|
this.viewUrl('');
|
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.viewUser(pString(oData.Result.User));
|
|
|
|
this.viewEnable_(!!oData.Result.Enable);
|
|
|
|
this.twoFactorStatus(!!oData.Result.IsSet);
|
|
|
|
this.twoFactorTested(!!oData.Result.Tested);
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.viewSecret(pString(oData.Result.Secret));
|
|
|
|
this.viewBackupCodes(pString(oData.Result.BackupCodes).replace(/[\s]+/g, ' '));
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.viewUrlTitle(pString(oData.Result.UrlTitle));
|
2020-08-12 06:25:36 +08:00
|
|
|
this.viewUrl(qr.toDataURL({ level: 'M', size: 8, value: this.getQr() }));
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2021-03-16 16:46:23 +08:00
|
|
|
onShowSecretResult(iError, data) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.secreting(false);
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2021-03-18 21:48:21 +08:00
|
|
|
if (iError) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.viewSecret('');
|
|
|
|
this.viewUrlTitle('');
|
|
|
|
this.viewUrl('');
|
2021-03-18 21:48:21 +08:00
|
|
|
} else {
|
|
|
|
this.viewSecret(pString(data.Result.Secret));
|
|
|
|
this.viewUrlTitle(pString(data.Result.UrlTitle));
|
|
|
|
this.viewUrl(qr.toDataURL({ level: 'M', size: 6, value: this.getQr() }));
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onBuild() {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.capaTwoFactor) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.processing(true);
|
|
|
|
Remote.getTwoFactor(this.onResult);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-04-08 00:39:43 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { TwoFactorConfigurationPopupView, TwoFactorConfigurationPopupView as default };
|