forked from hibas123/SecureFileWrapper
First release
This commit is contained in:
commit
963714d6ce
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
node_modules/
|
||||||
|
yarn.lock
|
||||||
|
*.js
|
||||||
|
private.pem
|
125
index.ts
Normal file
125
index.ts
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
import * as rsa from "node-rsa";
|
||||||
|
import fetch, { Response } from "node-fetch";
|
||||||
|
|
||||||
|
export interface File {
|
||||||
|
_id: string;
|
||||||
|
type: "binary" | "text";
|
||||||
|
name: string;
|
||||||
|
time: string;
|
||||||
|
preview: string;
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface History {
|
||||||
|
file: File;
|
||||||
|
history: File[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class SecureFile {
|
||||||
|
private Server: string;
|
||||||
|
private Username: string;
|
||||||
|
private PrivateKey: string;
|
||||||
|
constructor(server: string, username: string, private_key: string) {
|
||||||
|
this.Server = server;
|
||||||
|
this.Username = username;
|
||||||
|
this.PrivateKey = private_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getCode() {
|
||||||
|
let code_res = await fetch(this.Server + "/code?username=" + this.Username);
|
||||||
|
//ToDo check status Codes
|
||||||
|
let code = (await code_res.json()).code;
|
||||||
|
let r = new rsa(this.PrivateKey, "pkcs1-pem");
|
||||||
|
return { code: code, signature: r.sign(code).toString("base64") };
|
||||||
|
}
|
||||||
|
|
||||||
|
private async makeRequest(endpoint: string, method: "POST" | "GET" | "PUT" | "DELETE", query: any, body: Buffer) {
|
||||||
|
let code = await this.getCode();
|
||||||
|
query.code = code.code;
|
||||||
|
query.signature = code.signature;
|
||||||
|
let query_str = "?";
|
||||||
|
let first = true;
|
||||||
|
for (let key in query) {
|
||||||
|
if (!first) query_str += "&";
|
||||||
|
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
return await fetch(this.Server + endpoint + query_str, { method: method, body: body });
|
||||||
|
}
|
||||||
|
|
||||||
|
async list(): Promise<File[]> {
|
||||||
|
let res = await this.makeRequest("/files", "GET", {}, undefined)
|
||||||
|
statusParser(res);
|
||||||
|
return await res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(name: string, data: Buffer, type: "text" | "binary", preview?: Buffer): Promise<File> {
|
||||||
|
let res = await this.makeRequest("/files", "POST", { type: type, name: name, preview: preview }, data)
|
||||||
|
statusParser(res);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(id: string, version?: string): Promise<Buffer> {
|
||||||
|
let res: Response;
|
||||||
|
if (typeof version === "string") {
|
||||||
|
res = await this.makeRequest(`/files/${id}/history/${version}`, "GET", {}, undefined);
|
||||||
|
} else {
|
||||||
|
res = await this.makeRequest("/files/" + id, "GET", {}, undefined);
|
||||||
|
}
|
||||||
|
statusParser(res);
|
||||||
|
return res.buffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(id: string, data: Buffer, preview?: Buffer): Promise<File> {
|
||||||
|
let put: any = {};
|
||||||
|
if (preview) put.preview = preview;
|
||||||
|
let res = await this.makeRequest("/files/" + id, "PUT", put, data);
|
||||||
|
statusParser(res);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(id: string): Promise<boolean> {
|
||||||
|
let res = await this.makeRequest("/files/" + id, "DELETE", {}, undefined);
|
||||||
|
statusParser(res);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
async history(id: string): Promise<History> {
|
||||||
|
let res = await this.makeRequest(`/files/${id}/history`, "GET", {}, undefined);
|
||||||
|
statusParser(res);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Unauthorized extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("Not authorized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NotFound extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("Not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BadRequest extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("Bad request");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function statusParser(res: Response) {
|
||||||
|
if (res.status !== 200) {
|
||||||
|
switch (res.status) {
|
||||||
|
case 400:
|
||||||
|
throw new BadRequest();
|
||||||
|
case 404:
|
||||||
|
throw new NotFound();
|
||||||
|
case 403:
|
||||||
|
throw new Unauthorized();
|
||||||
|
default:
|
||||||
|
throw new Error(res.statusText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
package.json
Normal file
22
package.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "SecureFileWrapper",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||||
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"test": "nodeunit test.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"node-fetch": "^1.7.3",
|
||||||
|
"node-rsa": "^0.4.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^9.4.0",
|
||||||
|
"@types/node-fetch": "^1.6.7",
|
||||||
|
"@types/node-rsa": "^0.4.1",
|
||||||
|
"nodeunit": "^0.11.2",
|
||||||
|
"typescript": "^2.6.2"
|
||||||
|
}
|
||||||
|
}
|
100
test.ts
Normal file
100
test.ts
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
import SecureFile from "./index";
|
||||||
|
import { readFileSync } from "fs";
|
||||||
|
import { equal, deepEqual } from "assert";
|
||||||
|
import { Buffer } from "buffer";
|
||||||
|
|
||||||
|
let sf: SecureFile;
|
||||||
|
const testname = "ouiavgbsop687463743"
|
||||||
|
const testdata = new Buffer("Ich bin ein Test");
|
||||||
|
const newTestData = new Buffer("neue test daten");
|
||||||
|
const testprev = new Buffer("Ich bin...");
|
||||||
|
|
||||||
|
let testid: string;
|
||||||
|
let testver;
|
||||||
|
let testver2;
|
||||||
|
|
||||||
|
interface TestType {
|
||||||
|
expect(cnt: number): void;
|
||||||
|
ok(value, message?: string): void;
|
||||||
|
equal(actual, expected, message?: string): void;
|
||||||
|
notEqual(actual, expected, message?: string): void;
|
||||||
|
deepEqual(actual, expected, message?: string): void;
|
||||||
|
notDeepEqual(actual, expected, message?: string): void;
|
||||||
|
strictEqual(actual, expected, message?: string): void;
|
||||||
|
notStrictEqual(actual, expected, message?: string): void;
|
||||||
|
throws(block: any, error?, message?: string): void;
|
||||||
|
doesNotThrow(block: any, error?, message?: string): void;
|
||||||
|
ifError(value): void;
|
||||||
|
done(): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setUp: function (finished) {
|
||||||
|
let pk = readFileSync("./private.pem");
|
||||||
|
sf = new SecureFile("http://localhost:3005", "test", pk.toString("utf8"));
|
||||||
|
finished();
|
||||||
|
},
|
||||||
|
create: async function (test: TestType) {
|
||||||
|
test.expect(2);
|
||||||
|
let res = await sf.create(testname, testdata, "text", testprev)
|
||||||
|
test.ok(res);
|
||||||
|
test.ok(res._id);
|
||||||
|
testid = res._id;
|
||||||
|
testver = res.version;
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
get: async function (test: TestType) {
|
||||||
|
test.expect(2);
|
||||||
|
let res = await sf.get(testid);
|
||||||
|
test.ok(res);
|
||||||
|
test.equal(res.toString(), testdata.toString());
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
list: async function (test: TestType) {
|
||||||
|
test.expect(4);
|
||||||
|
let res = await sf.list();
|
||||||
|
console.log(res);
|
||||||
|
test.ok(res);
|
||||||
|
test.ok(Array.isArray(res), "Is from type Array");
|
||||||
|
test.ok(res.length > 0, "Do elements exist?")
|
||||||
|
res.forEach(e => {
|
||||||
|
if (e._id === testid) {
|
||||||
|
test.ok(true, "Element is in List")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
test.done()
|
||||||
|
},
|
||||||
|
update: async function (test: TestType) {
|
||||||
|
test.expect(5);
|
||||||
|
let res = await sf.update(testid, newTestData);
|
||||||
|
test.ok(res);
|
||||||
|
test.ok(res._id);
|
||||||
|
test.notEqual(res.version, testver, "Is new version_id generated?")
|
||||||
|
testver2 = res.version;
|
||||||
|
let res2 = await sf.get(testid);
|
||||||
|
test.ok(res2)
|
||||||
|
test.equal(res2.toString(), newTestData.toString(), "Is fetched data the updated?")
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
history: async function (test: TestType) {
|
||||||
|
test.expect(7);
|
||||||
|
let his = await sf.history(testid);
|
||||||
|
test.ok(his)
|
||||||
|
test.ok(his.file)
|
||||||
|
test.ok(his.history);
|
||||||
|
test.equal(his.history.length, 1)
|
||||||
|
|
||||||
|
test.equal(his.history[0].version, testver);
|
||||||
|
test.equal(his.file.version, testver2, "Is correct version");
|
||||||
|
|
||||||
|
let arch = await sf.get(testid, testver);
|
||||||
|
test.equal(arch.toString(), testdata.toString(), "Is old version data correct");
|
||||||
|
test.done();
|
||||||
|
},
|
||||||
|
delete: async function (test: TestType) {
|
||||||
|
test.expect(1);
|
||||||
|
let res = await sf.delete(testid);
|
||||||
|
test.ok(res);
|
||||||
|
test.done();
|
||||||
|
}
|
||||||
|
}
|
51
tsconfig.json
Normal file
51
tsconfig.json
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
/* Basic Options */
|
||||||
|
"watch": true,
|
||||||
|
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
|
||||||
|
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||||
|
// "lib": [], /* Specify library files to be included in the compilation: */
|
||||||
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
|
// "checkJs": true, /* Report errors in .js files. */
|
||||||
|
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||||
|
// "declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||||
|
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
||||||
|
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||||
|
// "outDir": "./", /* Redirect output structure to the directory. */
|
||||||
|
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||||
|
// "removeComments": true, /* Do not emit comments to output. */
|
||||||
|
// "noEmit": true, /* Do not emit outputs. */
|
||||||
|
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||||
|
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||||
|
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||||
|
/* Strict Type-Checking Options */
|
||||||
|
//" strict": true /* Enable all strict type-checking options. */
|
||||||
|
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||||
|
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||||
|
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||||
|
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||||
|
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||||
|
/* Additional Checks */
|
||||||
|
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
||||||
|
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||||
|
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||||
|
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||||
|
/* Module Resolution Options */
|
||||||
|
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||||
|
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||||
|
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||||
|
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||||
|
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||||
|
// "types": [], /* Type declaration files to be included in compilation. */
|
||||||
|
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||||
|
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||||
|
/* Source Map Options */
|
||||||
|
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||||
|
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||||
|
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||||
|
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||||
|
/* Experimental Options */
|
||||||
|
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||||
|
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user