snappymail/dev/View/Popup/NewOpenPgpKey.js

108 lines
2 KiB
JavaScript
Raw Normal View History

import { pInt } from 'Common/Utils';
2014-08-21 23:08:34 +08:00
import PgpStore from 'Stores/User/Pgp';
2014-08-25 15:10:51 +08:00
import { command } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
class NewOpenPgpKeyPopupView extends AbstractViewPopup {
constructor() {
super('NewOpenPgpKey');
2014-08-25 15:10:51 +08:00
this.addObservables({
email: '',
emailFocus: '',
emailError: false,
2014-08-21 23:08:34 +08:00
name: '',
password: '',
keyBitLength: 2048,
2014-08-21 23:08:34 +08:00
submitRequest: false,
submitError: ''
});
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;
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();
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('');
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()),
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();
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;
}
2014-08-21 23:08:34 +08:00
showError(e) {
console.log(e);
2019-07-05 03:19:24 +08:00
if (e && e.message) {
this.submitError(e.message);
}
2016-07-16 05:29:42 +08:00
}
clearPopup() {
this.name('');
this.password('');
this.email('');
this.emailError(false);
2020-08-14 04:58:41 +08:00
this.keyBitLength(2048);
2016-07-16 05:29:42 +08:00
this.submitError('');
}
onShow() {
this.clearPopup();
}
onShowWithDelay() {
this.emailFocus(true);
}
}
2019-07-05 03:19:24 +08:00
export { NewOpenPgpKeyPopupView, NewOpenPgpKeyPopupView as default };