local resource signature validation

This commit is contained in:
antelle 2017-06-06 19:55:46 +02:00
parent ac23f70bb5
commit c86042b885
1 changed files with 28 additions and 38 deletions

View File

@ -176,55 +176,45 @@ const Plugin = Backbone.Model.extend(_.extend({}, PluginStatus, {
},
loadResource(type, local) {
const ts = this.logger.ts();
let res;
if (local) {
res = this.loadLocalResource(type);
res = new Promise((resolve, reject) => {
const storageKey = this.getStorageResourcePath(type);
io.load(storageKey, (err, data) => err ? reject(err) : resolve(data));
});
} else {
const url = this.get('url');
res = this.loadResourceFromUrl(type, url + this.getResourcePath(type));
res = httpGet(url + this.getResourcePath(type), true);
}
return res.then(data => {
this.resources[type] = data;
});
},
loadResourceFromUrl(type, url) {
let ts = this.logger.ts();
const manifest = this.get('manifest');
return httpGet(url, true).then(data => {
this.logger.debug('Resource data loaded', type, this.logger.ts(ts));
ts = this.logger.ts();
const signature = manifest.resources[type];
return SignatureVerifier.verify(data, signature, manifest.publicKey)
.then(valid => {
if (valid) {
this.logger.debug('Resource signature valid', type, this.logger.ts(ts));
return data;
} else {
this.logger.error('Resource signature invalid', type);
throw `Signature invalid: ${type}`;
}
})
.catch(() => {
this.logger.error('Error validating resource signature', type);
throw `Error validating resource signature for ${type}`;
});
});
},
loadLocalResource(type) {
return new Promise((resolve, reject) => {
const storageKey = this.getStorageResourcePath(type);
io.load(storageKey, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
return this.verifyResource(data, type).then(data => {
this.resources[type] = data;
});
});
},
verifyResource(data, type) {
const ts = this.logger.ts();
const manifest = this.get('manifest');
const signature = manifest.resources[type];
return SignatureVerifier.verify(data, signature, manifest.publicKey)
.then(valid => {
if (valid) {
this.logger.debug('Resource signature validated', type, this.logger.ts(ts));
return data;
} else {
this.logger.error('Resource signature invalid', type);
throw `Signature invalid: ${type}`;
}
})
.catch(() => {
this.logger.error('Error validating resource signature', type);
throw `Error validating resource signature for ${type}`;
});
},
installWithResources() {
this.logger.info('Installing plugin resources');
const manifest = this.get('manifest');