keeweb/app/scripts/plugins/plugin-gallery.js

100 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-05-08 11:38:23 +02:00
import * as kdbxweb from 'kdbxweb';
2019-09-16 22:57:56 +02:00
import { Events } from 'framework/events';
2019-09-15 14:16:32 +02:00
import { SettingsStore } from 'comp/settings/settings-store';
import { Links } from 'const/links';
import { SignatureVerifier } from 'util/data/signature-verifier';
import { Logger } from 'util/logger';
2017-05-13 22:36:07 +02:00
const PluginGallery = {
logger: new Logger('plugin-gallery'),
gallery: null,
loading: false,
loadError: null,
loadPlugins() {
if (this.gallery) {
return Promise.resolve(this.gallery);
}
this.loading = true;
this.loadError = false;
2017-06-06 20:40:27 +02:00
const ts = this.logger.ts();
2020-06-01 16:53:51 +02:00
return new Promise((resolve) => {
2017-05-13 22:36:07 +02:00
this.logger.debug('Loading plugins...');
const xhr = new XMLHttpRequest();
xhr.open('GET', Links.Plugins + '/plugins.json');
2017-06-06 20:40:27 +02:00
xhr.responseType = 'json';
2017-05-13 22:36:07 +02:00
xhr.send();
xhr.addEventListener('load', () => {
const data = xhr.response;
2017-06-06 20:40:27 +02:00
resolve(data);
2017-05-13 22:36:07 +02:00
});
xhr.addEventListener('error', () => {
this.logger.error('Network error loading plugins');
resolve();
});
})
2020-06-01 16:53:51 +02:00
.then((data) => {
2017-06-06 20:40:27 +02:00
this.loading = false;
if (!data) {
this.loadError = true;
Events.emit('plugin-gallery-load-complete');
return;
2017-06-06 20:40:27 +02:00
}
2020-06-01 16:53:51 +02:00
return this.verifySignature(data).then((gallery) => {
this.loadError = !gallery;
if (gallery) {
this.logger.debug(
`Loaded ${gallery.plugins.length} plugins`,
this.logger.ts(ts)
);
this.gallery = gallery;
this.saveGallery(gallery);
}
Events.emit('plugin-gallery-load-complete');
return gallery;
});
})
2020-06-01 16:53:51 +02:00
.catch((e) => {
this.loadError = true;
this.logger.error('Error loading plugin gallery', e);
2019-09-16 22:57:56 +02:00
Events.emit('plugin-gallery-load-complete');
2017-06-06 20:40:27 +02:00
});
},
verifySignature(gallery) {
const dataToVerify = JSON.stringify(gallery, null, 2).replace(gallery.signature, '');
2019-08-18 08:05:38 +02:00
return SignatureVerifier.verify(
kdbxweb.ByteUtils.stringToBytes(dataToVerify),
gallery.signature
)
2020-06-01 16:53:51 +02:00
.then((isValid) => {
2019-08-16 23:05:39 +02:00
if (isValid) {
return gallery;
}
this.logger.error('JSON signature invalid');
})
2020-06-01 16:53:51 +02:00
.catch((e) => {
2019-08-16 23:05:39 +02:00
this.logger.error('Error verifying plugins signature', e);
});
2017-06-06 20:40:27 +02:00
},
getCachedGallery() {
const ts = this.logger.ts();
2020-06-01 16:53:51 +02:00
return SettingsStore.load('plugin-gallery').then((data) => {
2017-12-03 01:04:04 +01:00
if (data) {
2020-06-01 16:53:51 +02:00
return this.verifySignature(data).then((gallery) => {
2017-12-03 01:04:04 +01:00
this.logger.debug(`Loaded cached plugin gallery`, this.logger.ts(ts));
return gallery;
});
}
2017-05-13 22:36:07 +02:00
});
2017-06-05 11:48:03 +02:00
},
saveGallery(data) {
SettingsStore.save('plugin-gallery', data);
2017-05-13 22:36:07 +02:00
}
};
2019-09-15 14:16:32 +02:00
export { PluginGallery };