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 makeRequest(endpoint, method, query, body);
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>;
update(id: string, data: Buffer, preview?: Buffer): Promise<File>;
delete(id: string): Promise<boolean>;

View File

@ -36,8 +36,18 @@ class SecureFile {
statusParser(res);
return await res.json();
}
async create(name, data, type, folder, preview) {
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview, folder: folder }, data);
async create(name, data, type, folder, encrypt = true, preview) {
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);
return res.json();
}

View File

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

36
test.js
View File

@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rsa = require("node-rsa");
const index_1 = require("./index");
const fs_1 = require("fs");
const buffer_1 = require("buffer");
@ -13,15 +14,18 @@ let testver;
let testver2;
const testfolder = "iabos";
let ftestid;
let e_testid;
let private_key;
module.exports = {
setUp: function (finished) {
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();
},
create: async function (test) {
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._id);
testid = res._id;
@ -83,7 +87,7 @@ module.exports = {
folder: {
create: async function (test) {
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._id);
ftestid = res._id;
@ -109,5 +113,31 @@ module.exports = {
test.ok(res);
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 { readFileSync } from "fs";
import { equal, deepEqual } from "assert";
@ -15,6 +16,10 @@ let testver2;
const testfolder = "iabos";
let ftestid;
let e_testid;
let private_key;
interface TestType {
expect(cnt: number): void;
ok(value, message?: string): void;
@ -33,12 +38,13 @@ interface TestType {
module.exports = {
setUp: function (finished) {
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();
},
create: async function (test: TestType) {
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._id);
testid = res._id;
@ -102,7 +108,7 @@ module.exports = {
folder: {
create: async function (test: TestType) {
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._id);
ftestid = res._id;
@ -128,5 +134,31 @@ module.exports = {
test.ok(res);
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();
},
}
}