2022-01-30 09:35:53 +08:00
|
|
|
import { GnuPGUserStore } from 'Stores/User/GnuPG';
|
|
|
|
import { OpenPGPUserStore } from 'Stores/User/OpenPGP';
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2021-01-24 17:25:23 +08:00
|
|
|
import { AbstractViewPopup } from 'Knoin/AbstractViews';
|
|
|
|
|
2022-01-27 23:00:52 +08:00
|
|
|
export class OpenPgpImportPopupView extends AbstractViewPopup {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
2022-01-27 23:00:52 +08:00
|
|
|
super('OpenPgpImport');
|
2014-08-21 23:08:34 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
this.addObservables({
|
|
|
|
key: '',
|
|
|
|
keyError: false,
|
2022-01-20 23:38:27 +08:00
|
|
|
keyErrorMessage: '',
|
|
|
|
|
|
|
|
saveGnuPG: true,
|
2022-02-09 22:43:14 +08:00
|
|
|
saveServer: false
|
2020-10-26 19:54:03 +08:00
|
|
|
});
|
2014-08-25 15:10:51 +08:00
|
|
|
|
2022-01-30 09:35:53 +08:00
|
|
|
this.canGnuPG = GnuPGUserStore.isSupported();
|
2022-01-20 23:38:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.key.subscribe(() => {
|
2020-10-26 19:54:03 +08:00
|
|
|
this.keyError(false);
|
|
|
|
this.keyErrorMessage('');
|
2016-08-17 06:01:20 +08:00
|
|
|
});
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2022-01-28 22:11:15 +08:00
|
|
|
submitForm() {
|
2020-08-07 00:24:46 +08:00
|
|
|
let keyTrimmed = this.key().trim();
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2022-01-20 23:38:27 +08:00
|
|
|
if (/\n/.test(keyTrimmed)) {
|
|
|
|
keyTrimmed = keyTrimmed.replace(/\r+/g, '').replace(/\n{2,}/g, '\n\n');
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
this.keyError(!keyTrimmed);
|
|
|
|
this.keyErrorMessage('');
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2022-01-20 23:38:27 +08:00
|
|
|
if (!keyTrimmed) {
|
2022-01-29 00:56:44 +08:00
|
|
|
return;
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
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;
|
2022-01-20 23:38:27 +08:00
|
|
|
// eslint-disable-next-line max-len
|
2022-01-27 23:00:52 +08:00
|
|
|
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-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]) {
|
2022-04-16 08:02:38 +08:00
|
|
|
this.saveGnuPG() && GnuPGUserStore.isSupported() && GnuPGUserStore.importKey(this.key(), (iError, oData) => {
|
|
|
|
iError && alert(oData.ErrorMessage);
|
|
|
|
});
|
2022-01-30 09:35:53 +08:00
|
|
|
OpenPGPUserStore.isSupported() && OpenPGPUserStore.importKey(this.key());
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-09-10 06:38:16 +08:00
|
|
|
|
2020-09-20 17:29:31 +08:00
|
|
|
--count;
|
2016-09-10 06:38:16 +08:00
|
|
|
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
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
if (this.keyError()) {
|
2022-01-29 00:56:44 +08:00
|
|
|
return;
|
2016-09-10 06:38:16 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2022-03-04 16:21:24 +08:00
|
|
|
this.close();
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2014-03-13 06:29:33 +08:00
|
|
|
|
2021-11-03 21:58:34 +08:00
|
|
|
onShow() {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.key('');
|
2020-10-26 19:54:03 +08:00
|
|
|
this.keyError(false);
|
|
|
|
this.keyErrorMessage('');
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
}
|