2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { trim, delegateRun, log } from 'Common/Utils';
|
2014-08-25 23:49:01 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import PgpStore from 'Stores/User/Pgp';
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { getApp } from 'Helper/Apps/User';
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { popup, command } from 'Knoin/Knoin';
|
|
|
|
import { AbstractViewNext } from 'Knoin/AbstractViewNext';
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
@popup({
|
2016-08-17 06:01:20 +08:00
|
|
|
name: 'View/Popup/AddOpenPgpKey',
|
|
|
|
templateID: 'PopupsAddOpenPgpKey'
|
|
|
|
})
|
2019-07-05 03:19:24 +08:00
|
|
|
class AddOpenPgpKeyPopupView extends AbstractViewNext {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.key = ko.observable('');
|
|
|
|
this.key.focus = ko.observable(false);
|
|
|
|
this.key.error = ko.observable(false);
|
|
|
|
this.key.errorMessage = ko.observable('');
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.key.subscribe(() => {
|
|
|
|
this.key.error(false);
|
|
|
|
this.key.errorMessage('');
|
|
|
|
});
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
@command()
|
|
|
|
addOpenPgpKeyCommand() {
|
2019-07-05 03:19:24 +08:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
const reg = /[-]{3,6}BEGIN[\s]PGP[\s](PRIVATE|PUBLIC)[\s]KEY[\s]BLOCK[-]{3,6}[\s\S]+?[-]{3,6}END[\s]PGP[\s](PRIVATE|PUBLIC)[\s]KEY[\s]BLOCK[-]{3,6}/gi,
|
2016-09-10 06:38:16 +08:00
|
|
|
openpgpKeyring = PgpStore.openpgpKeyring;
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
let keyTrimmed = trim(this.key());
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (/[\n]/.test(keyTrimmed)) {
|
2016-09-10 06:38:16 +08:00
|
|
|
keyTrimmed = keyTrimmed.replace(/[\r]+/g, '').replace(/[\n]{2,}/g, '\n\n');
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
this.key.error('' === keyTrimmed);
|
|
|
|
this.key.errorMessage('');
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (!openpgpKeyring || this.key.error()) {
|
2016-09-10 06:38:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
let match = null,
|
2016-09-10 06:38:16 +08:00
|
|
|
count = 30,
|
|
|
|
done = false;
|
2016-08-10 03:52:30 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
do {
|
2016-09-10 06:38:16 +08:00
|
|
|
match = reg.exec(keyTrimmed);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (match && 0 < count) {
|
|
|
|
if (match[0] && match[1] && match[2] && match[1] === match[2]) {
|
2016-09-10 06:38:16 +08:00
|
|
|
let err = null;
|
2019-07-05 03:19:24 +08:00
|
|
|
if ('PRIVATE' === match[1]) {
|
2016-09-10 06:38:16 +08:00
|
|
|
err = openpgpKeyring.privateKeys.importKey(match[0]);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else if ('PUBLIC' === match[1]) {
|
2016-09-10 06:38:16 +08:00
|
|
|
err = openpgpKeyring.publicKeys.importKey(match[0]);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (err) {
|
2016-09-10 06:38:16 +08:00
|
|
|
this.key.error(true);
|
|
|
|
this.key.errorMessage(err && err[0] ? '' + err[0] : '');
|
|
|
|
log(err);
|
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-09-10 06:38:16 +08:00
|
|
|
|
|
|
|
count -= 1;
|
|
|
|
done = false;
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-09-10 06:38:16 +08:00
|
|
|
done = true;
|
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} while (!done);
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
openpgpKeyring.store();
|
2014-07-12 02:57:22 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
getApp().reloadOpenPgpKeys();
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.key.error()) {
|
2016-09-10 06:38:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-09-10 06:38:16 +08:00
|
|
|
delegateRun(this, 'cancelCommand');
|
|
|
|
return true;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
clearPopup() {
|
|
|
|
this.key('');
|
|
|
|
this.key.error(false);
|
|
|
|
this.key.errorMessage('');
|
|
|
|
}
|
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() {
|
|
|
|
this.key.focus(true);
|
|
|
|
}
|
|
|
|
}
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { AddOpenPgpKeyPopupView, AddOpenPgpKeyPopupView as default };
|