This repository has been archived on 2019-08-30. You can view files and clone it, but cannot push or open issues or pull requests.
SecureFileWrapper/index.js

141 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-02-01 15:37:12 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rsa = require("node-rsa");
2018-02-25 11:20:30 +00:00
require("isomorphic-fetch");
const btb = require("blob-to-buffer");
2018-02-01 15:37:12 +00:00
class SecureFile {
constructor(server, username, private_key) {
this.Server = server;
2018-02-25 11:24:52 +00:00
if (this.Server.endsWith("/")) {
this.Server += "api";
}
else {
this.Server += "/api";
}
2018-02-01 15:37:12 +00:00
this.Username = username;
this.PrivateKey = private_key;
}
async getCode() {
2018-02-25 11:20:30 +00:00
let code_res = await fetch(this.Server + "/code?username=" + this.Username);
2018-02-01 15:37:12 +00:00
//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") };
}
async makeRequest(endpoint, method, query, body) {
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;
}
2018-02-25 11:20:30 +00:00
return await fetch(this.Server + endpoint + query_str, { method: method, body: body });
2018-02-01 15:37:12 +00:00
}
2018-02-25 10:39:01 +00:00
async test() {
let res = await this.makeRequest("/test", "GET", {}, undefined);
statusParser(res);
return await res.json();
}
2018-02-05 09:32:48 +00:00
async list(folder) {
if (!folder)
folder = "root";
let res = await this.makeRequest("/files", "GET", { folder: folder }, undefined);
2018-02-01 15:37:12 +00:00
statusParser(res);
return await res.json();
}
2018-02-18 13:03:23 +00:00
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);
2018-02-01 15:37:12 +00:00
statusParser(res);
return res.json();
}
async get(id, version) {
let res;
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);
2018-02-25 11:20:30 +00:00
if (res.buffer) {
return res.buffer();
}
else {
return new Promise(async (resolve, reject) => {
btb(await res.blob(), (err, buffer) => {
if (err)
reject(err);
else
resolve(buffer);
});
});
}
2018-02-01 15:37:12 +00:00
}
async update(id, data, preview) {
let put = {};
if (preview)
put.preview = preview;
let res = await this.makeRequest("/files/" + id, "PUT", put, data);
statusParser(res);
return res.json();
}
async delete(id) {
let res = await this.makeRequest("/files/" + id, "DELETE", {}, undefined);
statusParser(res);
return res.json();
}
async history(id) {
let res = await this.makeRequest(`/files/${id}/history`, "GET", {}, undefined);
statusParser(res);
return res.json();
}
}
exports.default = SecureFile;
class Unauthorized extends Error {
constructor() {
super("Not authorized");
}
}
exports.Unauthorized = Unauthorized;
class NotFound extends Error {
constructor() {
super("Not found");
}
}
exports.NotFound = NotFound;
class BadRequest extends Error {
constructor() {
super("Bad request");
}
}
exports.BadRequest = BadRequest;
function statusParser(res) {
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);
}
}
}