Adding server side file encryption

This commit is contained in:
hibas123 2018-02-18 14:03:23 +01:00
parent dd23d930dd
commit cce8c794ad
5 changed files with 93 additions and 11 deletions

2
index.d.ts vendored
View File

@ -19,7 +19,7 @@ export default class SecureFile {
private getCode(); private getCode();
private makeRequest(endpoint, method, query, body); private makeRequest(endpoint, method, query, body);
list(folder?: string): Promise<File[]>; list(folder?: string): Promise<File[]>;
create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer): Promise<File>; create(name: string, data: Buffer, type: "text" | "binary", folder?: string, encrypt?: boolean, preview?: Buffer): Promise<File>;
get(id: string, version?: string): Promise<Buffer>; get(id: string, version?: string): Promise<Buffer>;
update(id: string, data: Buffer, preview?: Buffer): Promise<File>; update(id: string, data: Buffer, preview?: Buffer): Promise<File>;
delete(id: string): Promise<boolean>; delete(id: string): Promise<boolean>;

View File

@ -36,8 +36,18 @@ class SecureFile {
statusParser(res); statusParser(res);
return await res.json(); return await res.json();
} }
async create(name, data, type, folder, preview) { async create(name, data, type, folder, encrypt = true, preview) {
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview, folder: folder }, data); let params = { type: type, name: name, no_encryption: !encrypt };
if (preview) {
params.preview = preview;
}
if (folder) {
params.folder = folder;
}
if (!encrypt) {
params.no_encryption = true;
}
let res = await this.makeRequest("/files", "POST", params, data);
statusParser(res); statusParser(res);
return res.json(); return res.json();
} }

View File

@ -54,8 +54,18 @@ export default class SecureFile {
return await res.json(); return await res.json();
} }
async create(name: string, data: Buffer, type: "text" | "binary", folder?: string, preview?: Buffer): Promise<File> { async create(name: string, data: Buffer, type: "text" | "binary", folder?: string, encrypt: boolean = true, preview?: Buffer): Promise<File> {
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview, folder: folder }, data) let params: any = { type: type, name: name, no_encryption: !encrypt };
if (preview) {
params.preview = preview;
}
if (folder) {
params.folder = folder;
}
if (!encrypt) {
params.no_encryption = true;
}
let res = await this.makeRequest("/files", "POST", params, data)
statusParser(res); statusParser(res);
return res.json(); return res.json();
} }

36
test.js
View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const rsa = require("node-rsa");
const index_1 = require("./index"); const index_1 = require("./index");
const fs_1 = require("fs"); const fs_1 = require("fs");
const buffer_1 = require("buffer"); const buffer_1 = require("buffer");
@ -13,15 +14,18 @@ let testver;
let testver2; let testver2;
const testfolder = "iabos"; const testfolder = "iabos";
let ftestid; let ftestid;
let e_testid;
let private_key;
module.exports = { module.exports = {
setUp: function (finished) { setUp: function (finished) {
let pk = fs_1.readFileSync("./private.pem"); let pk = fs_1.readFileSync("./private.pem");
sf = new index_1.default("http://localhost:3005/api", "test", pk.toString("utf8")); private_key = pk.toString("utf8");
sf = new index_1.default("http://localhost:3005/api", "test", private_key);
finished(); finished();
}, },
create: async function (test) { create: async function (test) {
test.expect(2); test.expect(2);
let res = await sf.create(testname, testdata, "text", undefined, testprev); let res = await sf.create(testname, testdata, "text", undefined, false, testprev);
test.ok(res); test.ok(res);
test.ok(res._id); test.ok(res._id);
testid = res._id; testid = res._id;
@ -83,7 +87,7 @@ module.exports = {
folder: { folder: {
create: async function (test) { create: async function (test) {
test.expect(2); test.expect(2);
let res = await sf.create(testname, testdata, "text", testfolder, testprev); let res = await sf.create(testname, testdata, "text", testfolder, false, testprev);
test.ok(res); test.ok(res);
test.ok(res._id); test.ok(res._id);
ftestid = res._id; ftestid = res._id;
@ -109,5 +113,31 @@ module.exports = {
test.ok(res); test.ok(res);
test.done(); test.done();
}, },
},
encryption: {
create: async function (test) {
test.expect(2);
let res = await sf.create(testname, testdata, "text", undefined, true, testprev);
test.ok(res);
test.ok(res._id);
e_testid = res._id;
testver = res.version;
test.done();
},
get: async function (test) {
let r = new rsa(private_key, "pkcs1-pem");
test.expect(2);
let res = await sf.get(e_testid);
res = r.decrypt(res);
test.ok(res);
test.equal(res.toString(), testdata.toString());
test.done();
},
delete: async function (test) {
test.expect(1);
let res = await sf.delete(e_testid);
test.ok(res);
test.done();
},
} }
}; };

38
test.ts
View File

@ -1,3 +1,4 @@
import * as rsa from "node-rsa";
import SecureFile from "./index"; import SecureFile from "./index";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { equal, deepEqual } from "assert"; import { equal, deepEqual } from "assert";
@ -15,6 +16,10 @@ let testver2;
const testfolder = "iabos"; const testfolder = "iabos";
let ftestid; let ftestid;
let e_testid;
let private_key;
interface TestType { interface TestType {
expect(cnt: number): void; expect(cnt: number): void;
ok(value, message?: string): void; ok(value, message?: string): void;
@ -33,12 +38,13 @@ interface TestType {
module.exports = { module.exports = {
setUp: function (finished) { setUp: function (finished) {
let pk = readFileSync("./private.pem"); let pk = readFileSync("./private.pem");
sf = new SecureFile("http://localhost:3005/api", "test", pk.toString("utf8")); private_key = pk.toString("utf8");
sf = new SecureFile("http://localhost:3005/api", "test", private_key);
finished(); finished();
}, },
create: async function (test: TestType) { create: async function (test: TestType) {
test.expect(2); test.expect(2);
let res = await sf.create(testname, testdata, "text", undefined, testprev) let res = await sf.create(testname, testdata, "text", undefined, false, testprev)
test.ok(res); test.ok(res);
test.ok(res._id); test.ok(res._id);
testid = res._id; testid = res._id;
@ -102,7 +108,7 @@ module.exports = {
folder: { folder: {
create: async function (test: TestType) { create: async function (test: TestType) {
test.expect(2); test.expect(2);
let res = await sf.create(testname, testdata, "text", testfolder, testprev) let res = await sf.create(testname, testdata, "text", testfolder, false, testprev)
test.ok(res); test.ok(res);
test.ok(res._id); test.ok(res._id);
ftestid = res._id; ftestid = res._id;
@ -128,5 +134,31 @@ module.exports = {
test.ok(res); test.ok(res);
test.done(); test.done();
}, },
},
encryption: {
create: async function (test: TestType) {
test.expect(2);
let res = await sf.create(testname, testdata, "text", undefined, true, testprev)
test.ok(res);
test.ok(res._id);
e_testid = res._id;
testver = res.version;
test.done();
},
get: async function (test: TestType) {
let r = new rsa(private_key, "pkcs1-pem");
test.expect(2);
let res = await sf.get(e_testid);
res = r.decrypt(res);
test.ok(res);
test.equal(res.toString(), testdata.toString());
test.done();
},
delete: async function (test: TestType) {
test.expect(1);
let res = await sf.delete(e_testid);
test.ok(res);
test.done();
},
} }
} }