2022-01-27 23:00:52 +08:00
|
|
|
//import { pInt } from 'Common/Utils';
|
|
|
|
|
2022-01-31 15:30:46 +08:00
|
|
|
import { GnuPGUserStore } from 'Stores/User/GnuPG';
|
|
|
|
import { OpenPGPUserStore } from 'Stores/User/OpenPGP';
|
|
|
|
|
2022-01-27 23:00:52 +08:00
|
|
|
import { IdentityUserStore } from 'Stores/User/Identity';
|
|
|
|
|
|
|
|
import { AbstractViewPopup } from 'Knoin/AbstractViews';
|
|
|
|
|
2022-02-24 19:22:27 +08:00
|
|
|
import { SettingsCapa } from 'Common/Globals';
|
2022-01-27 23:00:52 +08:00
|
|
|
|
|
|
|
export class OpenPgpGeneratePopupView extends AbstractViewPopup {
|
|
|
|
constructor() {
|
|
|
|
super('OpenPgpGenerate');
|
|
|
|
|
|
|
|
this.identities = IdentityUserStore;
|
|
|
|
|
|
|
|
this.addObservables({
|
|
|
|
email: '',
|
|
|
|
emailError: false,
|
|
|
|
|
|
|
|
name: '',
|
|
|
|
password: '',
|
|
|
|
keyType: 'ECC',
|
|
|
|
|
|
|
|
submitRequest: false,
|
|
|
|
submitError: '',
|
|
|
|
|
2022-01-31 15:30:46 +08:00
|
|
|
backupPublicKey: true,
|
|
|
|
backupPrivateKey: false,
|
|
|
|
|
|
|
|
saveGnuPGPublic: true,
|
|
|
|
saveGnuPGPrivate: false
|
2022-01-27 23:00:52 +08:00
|
|
|
});
|
|
|
|
|
2022-03-06 05:25:32 +08:00
|
|
|
this.canGnuPG = SettingsCapa('GnuPG');
|
2022-01-27 23:00:52 +08:00
|
|
|
|
|
|
|
this.email.subscribe(() => this.emailError(false));
|
|
|
|
}
|
|
|
|
|
2022-01-28 07:22:54 +08:00
|
|
|
submitForm() {
|
2022-01-27 23:00:52 +08:00
|
|
|
const type = this.keyType().toLowerCase(),
|
|
|
|
userId = {
|
|
|
|
name: this.name(),
|
|
|
|
email: this.email()
|
|
|
|
},
|
|
|
|
cfg = {
|
|
|
|
type: type,
|
|
|
|
userIDs: [userId],
|
|
|
|
passphrase: this.password().trim()
|
|
|
|
// format: 'armored' // output key format, defaults to 'armored' (other options: 'binary' or 'object')
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if ('ecc' === type) {
|
|
|
|
cfg.curve = 'curve25519';
|
|
|
|
} else {
|
|
|
|
cfg.rsaBits = pInt(this.keyBitLength());
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
this.emailError(!this.email().trim());
|
|
|
|
if (this.emailError()) {
|
2022-01-28 07:22:54 +08:00
|
|
|
return;
|
2022-01-27 23:00:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.submitRequest(true);
|
|
|
|
this.submitError('');
|
|
|
|
|
|
|
|
openpgp.generateKey(cfg).then(keyPair => {
|
|
|
|
if (keyPair) {
|
2022-01-31 15:30:46 +08:00
|
|
|
const fn = () => {
|
2022-01-27 23:00:52 +08:00
|
|
|
this.submitRequest(false);
|
2022-03-04 16:21:24 +08:00
|
|
|
this.close();
|
2022-01-31 15:30:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
OpenPGPUserStore.storeKeyPair(keyPair);
|
|
|
|
|
|
|
|
keyPair.onServer = (this.backupPublicKey() ? 1 : 0) + (this.backupPrivateKey() ? 2 : 0);
|
|
|
|
keyPair.inGnuPG = (this.saveGnuPGPublic() ? 1 : 0) + (this.saveGnuPGPrivate() ? 2 : 0);
|
|
|
|
if (keyPair.onServer || keyPair.inGnuPG) {
|
|
|
|
if (!this.backupPrivateKey() && !this.saveGnuPGPrivate()) {
|
|
|
|
delete keyPair.privateKey;
|
|
|
|
}
|
|
|
|
GnuPGUserStore.storeKeyPair(keyPair, fn);
|
|
|
|
} else {
|
|
|
|
fn();
|
|
|
|
}
|
2022-01-27 23:00:52 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.submitRequest(false);
|
|
|
|
this.showError(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
showError(e) {
|
|
|
|
console.log(e);
|
|
|
|
if (e && e.message) {
|
|
|
|
this.submitError(e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onShow() {
|
|
|
|
this.name(''/*IdentityUserStore()[0].name()*/);
|
|
|
|
this.password('');
|
|
|
|
this.email(''/*IdentityUserStore()[0].email()*/);
|
|
|
|
this.emailError(false);
|
|
|
|
this.submitError('');
|
|
|
|
}
|
|
|
|
}
|