2020-08-19 02:24:17 +08:00
|
|
|
import { pInt } from 'Common/Utils';
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import PgpStore from 'Stores/User/Pgp';
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2021-01-24 17:25:23 +08:00
|
|
|
import { command } from 'Knoin/Knoin';
|
|
|
|
import { AbstractViewPopup } from 'Knoin/AbstractViews';
|
|
|
|
|
|
|
|
class NewOpenPgpKeyPopupView extends AbstractViewPopup {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
2021-01-24 17:25:23 +08:00
|
|
|
super('NewOpenPgpKey');
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
this.addObservables({
|
|
|
|
email: '',
|
|
|
|
emailFocus: '',
|
|
|
|
emailError: false,
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
name: '',
|
|
|
|
password: '',
|
|
|
|
keyBitLength: 2048,
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
submitRequest: false,
|
|
|
|
submitError: ''
|
2016-08-17 06:01:20 +08:00
|
|
|
});
|
2020-10-26 19:54:03 +08:00
|
|
|
|
|
|
|
this.email.subscribe(() => this.emailError(false));
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
@command()
|
|
|
|
generateOpenPgpKeyCommand() {
|
2019-07-05 03:19:24 +08:00
|
|
|
const userId = {},
|
2016-09-10 06:38:16 +08:00
|
|
|
openpgpKeyring = PgpStore.openpgpKeyring;
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
this.emailError(!this.email().trim());
|
|
|
|
if (!openpgpKeyring || this.emailError()) {
|
2016-09-10 06:38:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
userId.email = this.email();
|
2020-07-28 23:20:14 +08:00
|
|
|
if (this.name()) {
|
2016-09-10 06:38:16 +08:00
|
|
|
userId.name = this.name();
|
|
|
|
}
|
2015-04-07 03:32:19 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
this.submitRequest(true);
|
|
|
|
this.submitError('');
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2020-07-23 02:28:25 +08:00
|
|
|
setTimeout(() => {
|
2016-09-10 06:38:16 +08:00
|
|
|
try {
|
2019-07-05 03:19:24 +08:00
|
|
|
PgpStore.openpgp
|
|
|
|
.generateKey({
|
|
|
|
userIds: [userId],
|
|
|
|
numBits: pInt(this.keyBitLength()),
|
2020-08-07 00:24:46 +08:00
|
|
|
passphrase: this.password().trim()
|
2019-07-05 03:19:24 +08:00
|
|
|
})
|
|
|
|
.then((keyPair) => {
|
|
|
|
this.submitRequest(false);
|
|
|
|
|
|
|
|
if (keyPair && keyPair.privateKeyArmored) {
|
|
|
|
openpgpKeyring.privateKeys.importKey(keyPair.privateKeyArmored);
|
|
|
|
openpgpKeyring.publicKeys.importKey(keyPair.publicKeyArmored);
|
|
|
|
|
|
|
|
openpgpKeyring.store();
|
|
|
|
|
2020-09-15 15:29:25 +08:00
|
|
|
rl.app.reloadOpenPgpKeys();
|
2020-08-19 02:24:17 +08:00
|
|
|
this.cancelCommand && this.cancelCommand();
|
2019-07-05 03:19:24 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.submitRequest(false);
|
|
|
|
this.showError(e);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2016-09-10 06:38:16 +08:00
|
|
|
this.submitRequest(false);
|
|
|
|
this.showError(e);
|
|
|
|
}
|
2020-08-14 04:58:41 +08:00
|
|
|
}, 100);
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
return true;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
showError(e) {
|
2020-08-12 06:25:36 +08:00
|
|
|
console.log(e);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (e && e.message) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitError(e.message);
|
|
|
|
}
|
2016-07-16 05:29:42 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
clearPopup() {
|
|
|
|
this.name('');
|
|
|
|
this.password('');
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.email('');
|
2020-10-26 19:54:03 +08:00
|
|
|
this.emailError(false);
|
2020-08-14 04:58:41 +08:00
|
|
|
this.keyBitLength(2048);
|
2016-07-16 05:29:42 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.submitError('');
|
|
|
|
}
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShow() {
|
|
|
|
this.clearPopup();
|
|
|
|
}
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
onShowWithDelay() {
|
2020-10-26 19:54:03 +08:00
|
|
|
this.emailFocus(true);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { NewOpenPgpKeyPopupView, NewOpenPgpKeyPopupView as default };
|