Disable jwt request collecting

This commit is contained in:
Fabian Stamm 2019-01-18 15:58:47 +01:00
parent 8de05e6b7f
commit 9b57728892
15 changed files with 3 additions and 514 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules/ node_modules/
yarn.lock yarn.lock
private.pem private.pem
lib/

62
lib/index.d.ts vendored
View File

@ -1,62 +0,0 @@
import Lock from "./lock";
export interface IFileVersion {
version: string;
time: Date;
preview: string;
deleted: boolean;
}
export interface IFile {
_id: string;
type: string;
name: string;
folder: string;
deleted: boolean;
active: IFileVersion;
versions: IFileVersion[];
user: string;
application: string;
}
export interface IHistory {
file: IFile;
history: IFileVersion[];
}
export declare class NoConnection extends Error {
type: string;
constructor();
}
export declare class Unauthorized extends Error {
type: string;
constructor();
}
export declare class NoPermission extends Error {
type: string;
constructor();
}
export declare class NotFound extends Error {
type: string;
constructor();
}
export declare class BadRequest extends Error {
type: string;
constructor();
}
export default class SecureFileWrapper {
private server;
private _jwtObservableServer;
jwtObservable: {
subscribe: (callback: import("./observable").ObserverCallback<((jwt: string) => void)[]>) => void;
unsubscribe: (callback: import("./observable").ObserverCallback<((jwt: string) => void)[]>) => void;
};
jwt: string;
auth_lock: Lock;
constructor(server: string);
getJWT(): Promise<void>;
makeRequest(endpoint: string, method: "POST" | "GET" | "PUT" | "DELETE", query: any, body?: ArrayBuffer | ArrayBufferView, second?: boolean): any;
list(folder?: string): Promise<IFile[]>;
create(name: string, data: ArrayBuffer | ArrayBufferView, type: "text" | "binary", folder?: string, preview?: string): Promise<IFile>;
get(id: string, version?: string): Promise<ArrayBuffer>;
update(id: string, data: ArrayBuffer | ArrayBufferView, preview?: string): Promise<IFile>;
delete(id: string): Promise<boolean>;
history(id: string): Promise<IHistory>;
restore(id: string, version: string): Promise<void>;
}

View File

@ -1,208 +0,0 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const observable_1 = require("./observable");
const lock_1 = require("./lock");
class NoConnection extends Error {
constructor() {
super("No connection");
this.type = "noconnection";
}
}
exports.NoConnection = NoConnection;
class Unauthorized extends Error {
constructor() {
super("Not authorized");
this.type = "unauthorized";
}
}
exports.Unauthorized = Unauthorized;
class NoPermission extends Error {
constructor() {
super("No permission");
this.type = "nopermission";
}
}
exports.NoPermission = NoPermission;
class NotFound extends Error {
constructor() {
super("Not found");
this.type = "notfound";
}
}
exports.NotFound = NotFound;
class BadRequest extends Error {
constructor() {
super("Bad request");
this.type = "badrequest";
}
}
exports.BadRequest = BadRequest;
const fetch = require("isomorphic-fetch");
function statusParser(res) {
if (res.status !== 200) {
switch (res.status) {
case 400:
throw new BadRequest();
case 404:
throw new NotFound();
case 403:
throw new NoPermission();
case 401:
throw new Unauthorized();
default:
throw new Error(res.statusText);
}
}
}
class SecureFileWrapper {
constructor(server) {
this.server = server;
this._jwtObservableServer = new observable_1.default();
this.jwtObservable = this._jwtObservableServer.getPublicApi();
this.auth_lock = new lock_1.default();
if (this.server.endsWith("/")) {
this.server += "api/v1";
}
else {
this.server += "/api/v1";
}
}
getJWT() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.auth_lock.locked) {
let lock = yield this.auth_lock.getLock();
this._jwtObservableServer.send((jwt) => {
this.jwt = jwt;
lock.release();
});
}
yield this.auth_lock.getLock().then(lock => lock.release());
});
}
makeRequest(endpoint, method, query, body, second = false) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.jwt || this.jwt === undefined) {
yield this.getJWT();
}
query.jwt = this.jwt;
let query_str = "?";
let first = true;
for (let key in query) {
if (!first)
query_str += "&";
query_str += encodeURIComponent(key) + "=" + encodeURIComponent(query[key]);
first = false;
}
var headers = {
"pragme": "no-cache",
"cache-control": "no-cache"
};
let body_n;
if (body) {
headers["Content-Type"] = "application/octet-stream";
body_n = Buffer ? Buffer.from(body instanceof ArrayBuffer ? body : body.buffer) : body;
}
try {
let res = yield fetch(this.server + endpoint + query_str, { method, body: body_n, headers });
if (res.status === 401 && !second) {
yield this.getJWT();
return this.makeRequest(endpoint, method, query, body, true);
}
else {
statusParser(res);
return res;
}
}
catch (err) {
if (err instanceof TypeError || err.errno === "ECONNREFUSED")
throw new NoConnection();
throw err;
}
});
}
// async test(jwt): Promise<{ user: string, test: true }> {
// let res = await this.makeRequest("/test", "GET", {}, undefined, this.jwt_enabled);
// statusParser(res);
// return await res.json();
// }
list(folder) {
return __awaiter(this, void 0, void 0, function* () {
let query = {};
if (folder)
query.folder = folder;
let res = yield this.makeRequest("/files", "GET", query);
let d = yield res.json();
return d.files;
});
}
create(name, data, type, folder, preview) {
return __awaiter(this, void 0, void 0, function* () {
let params = { type: type, name: name };
if (preview) {
params.preview = preview;
}
if (folder) {
params.folder = folder;
}
let res = yield this.makeRequest("/files", "POST", params, data);
return (yield res.json()).file;
});
}
get(id, version) {
return __awaiter(this, void 0, void 0, function* () {
let res;
if (typeof version === "string") {
res = yield this.makeRequest(`/files/${id}/history/${version}`, "GET", {});
}
else {
res = yield this.makeRequest("/files/" + id, "GET", {});
}
if (res.arrayBuffer) {
return res.arrayBuffer();
}
else {
let blob = yield res.buffer();
// console.log(blob.length);
return Uint8Array.from(blob).buffer;
}
});
}
update(id, data, preview) {
return __awaiter(this, void 0, void 0, function* () {
let put = {};
if (preview)
put.preview = preview;
let res = yield this.makeRequest("/files/" + id, "PUT", put, data);
let json = yield res.json();
return json.file;
});
}
delete(id) {
return __awaiter(this, void 0, void 0, function* () {
let res = yield this.makeRequest("/files/" + id, "DELETE", {});
return res.json();
});
}
history(id) {
return __awaiter(this, void 0, void 0, function* () {
let res = yield this.makeRequest(`/files/${id}/history`, "GET", {});
statusParser(res);
return res.json();
});
}
restore(id, version) {
return __awaiter(this, void 0, void 0, function* () {
yield this.makeRequest(`/files/${id}/history/${version}/restore`, "PUT", {});
});
}
}
exports.default = SecureFileWrapper;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

12
lib/lock.d.ts vendored
View File

@ -1,12 +0,0 @@
export declare type Release = {
release: () => void;
};
export default class Lock {
private _locked;
readonly locked: boolean;
private toCome;
constructor();
getLock(): Promise<Release>;
private lock;
private release;
}

View File

@ -1,49 +0,0 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
class Lock {
constructor() {
this._locked = false;
this.toCome = [];
this.release = this.release.bind(this);
}
get locked() {
return this._locked;
}
getLock() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._locked)
return { release: this.lock() };
else {
return new Promise((resolve) => {
this.toCome.push(() => {
resolve({ release: this.lock() });
});
});
}
});
}
lock() {
this._locked = true;
return this.release;
}
release() {
return __awaiter(this, void 0, void 0, function* () {
if (this.toCome.length > 0) {
this.toCome.shift()();
}
else {
this._locked = false;
}
});
}
}
exports.default = Lock;
//# sourceMappingURL=lock.js.map

View File

@ -1 +0,0 @@
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":";;;;;;;;;;AACA,MAAqB,IAAI;IAOtB;QANQ,YAAO,GAAY,KAAK,CAAC;QAIzB,WAAM,GAAmB,EAAE,CAAC;QAGjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAPD,IAAI,MAAM;QACP,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAOK,OAAO;;YACV,IAAI,CAAC,IAAI,CAAC,OAAO;gBAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;iBAC9C;gBACF,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;oBACrC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;wBACnB,OAAO,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;oBACrC,CAAC,CAAC,CAAA;gBACL,CAAC,CAAC,CAAA;aACJ;QACJ,CAAC;KAAA;IAEO,IAAI;QACT,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAEa,OAAO;;YAClB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;gBACzB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC;aACxB;iBAAM;gBACJ,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;aACvB;QACJ,CAAC;KAAA;CACH;AAlCD,uBAkCC"}

14
lib/observable.d.ts vendored
View File

@ -1,14 +0,0 @@
export declare type ObserverCallback<T> = (data: T) => void;
export default class Observable<T = any> {
private collect;
private collect_intervall;
private subscriber;
private events;
private timeout;
constructor(collect?: boolean, collect_intervall?: number);
getPublicApi(): {
subscribe: (callback: ObserverCallback<T[]>) => void;
unsubscribe: (callback: ObserverCallback<T[]>) => void;
};
send(data: T): void;
}

View File

@ -1,40 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Observable {
constructor(collect = true, collect_intervall = 100) {
this.collect = collect;
this.collect_intervall = collect_intervall;
this.subscriber = [];
this.events = [];
this.timeout = undefined;
}
getPublicApi() {
return {
subscribe: (callback) => {
if (this.subscriber.indexOf(callback) < 0)
this.subscriber.push(callback);
},
unsubscribe: (callback) => {
let idx = this.subscriber.indexOf(callback);
if (idx >= 0) {
this.subscriber.splice(idx, 1);
}
}
};
}
send(data) {
if (!this.collect)
this.subscriber.forEach(e => e([data]));
else {
this.events.push(data);
if (!this.timeout) {
this.timeout = setTimeout(() => {
this.subscriber.forEach(e => e(this.events));
this.timeout = 0;
}, this.collect_intervall);
}
}
}
}
exports.default = Observable;
//# sourceMappingURL=observable.js.map

View File

@ -1 +0,0 @@
{"version":3,"file":"observable.js","sourceRoot":"","sources":["../src/observable.ts"],"names":[],"mappings":";;AAEA,MAAqB,UAAU;IAK5B,YAAoB,UAAmB,IAAI,EAAU,oBAA4B,GAAG;QAAhE,YAAO,GAAP,OAAO,CAAgB;QAAU,sBAAiB,GAAjB,iBAAiB,CAAc;QAJ5E,eAAU,GAA4B,EAAE,CAAC;QACzC,WAAM,GAAQ,EAAE,CAAC;QACjB,YAAO,GAAG,SAAS,CAAC;IAE4D,CAAC;IAEzF,YAAY;QACT,OAAO;YACJ,SAAS,EAAE,CAAC,QAA+B,EAAE,EAAE;gBAC5C,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC;oBACtC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;YACpC,CAAC;YACD,WAAW,EAAE,CAAC,QAA+B,EAAE,EAAE;gBAC9C,IAAI,GAAG,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;gBAC5C,IAAI,GAAG,IAAI,CAAC,EAAE;oBACX,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;iBACjC;YACJ,CAAC;SACH,CAAA;IACJ,CAAC;IAED,IAAI,CAAC,IAAO;QACT,IAAI,CAAC,IAAI,CAAC,OAAO;YACd,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACtC;YACF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACvB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;gBAChB,IAAI,CAAC,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE;oBAC5B,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;oBAC7C,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;gBACpB,CAAC,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC;aAC7B;SACH;IACJ,CAAC;CACH;AAnCD,6BAmCC"}

1
lib/test.d.ts vendored
View File

@ -1 +0,0 @@
export {};

View File

@ -1,122 +0,0 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const util_1 = require("util");
const testname = "ouiavgbsop687463743";
const encoder = new util_1.TextEncoder();
const decoder = new util_1.TextDecoder();
const testdata = encoder.encode("Ich bin ein Test");
const newTestData = encoder.encode("neue test daten");
const testprev = "Ich bin...";
const testfolder = "iabos";
let ftestid;
const chai_1 = require("chai");
function test(sf) {
let testid;
let testver;
let testver2;
it("create", () => __awaiter(this, void 0, void 0, function* () {
let res = yield sf.create(testname, testdata, "text", undefined, testprev);
chai_1.expect(res, "Res isnnot set").to.exist;
chai_1.expect(res._id, "Res has no _id").to.exist;
testid = res._id;
testver = res.active.version;
}));
it("get", () => __awaiter(this, void 0, void 0, function* () {
let res = yield sf.get(testid);
chai_1.expect(res, "No data returned").to.exist;
chai_1.expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
}));
it("get - fail", () => __awaiter(this, void 0, void 0, function* () {
const inverr = new Error("Should have failed!");
try {
yield sf.get(testid + "asod");
throw inverr;
}
catch (err) {
if (err !== inverr) {
chai_1.expect(err).to.be.instanceOf(index_1.NotFound);
}
}
}));
it("list", () => __awaiter(this, void 0, void 0, function* () {
let res = yield sf.list();
chai_1.expect(Array.isArray(res), "Is not from type Array").to.be.true;
chai_1.expect(res.length, "No elements returned").to.greaterThan(0);
let found = !!res.find(e => e._id === testid);
chai_1.expect(found, "Element not in List").to.be.true;
}));
it("update", () => __awaiter(this, void 0, void 0, function* () {
let res = yield sf.update(testid, newTestData, undefined);
chai_1.expect(res, "No data returned").to.exist;
chai_1.expect(res._id, "_id missing").to.exist;
chai_1.expect(res.active.version, "No new version was created").to.not.equal(testver);
testver2 = res.active.version;
let res2 = yield sf.get(testid);
chai_1.expect(decoder.decode(res2), "Fetched data not updated").to.be.equal(decoder.decode(newTestData));
}));
it("history", () => __awaiter(this, void 0, void 0, function* () {
let his = yield sf.history(testid);
chai_1.expect(his, "no data returned").to.exist;
chai_1.expect(his.file, "file not set").to.exist;
chai_1.expect(his.history, "history not set").to.exist;
chai_1.expect(his.history.length, `Not expected history length. Expected 1 got ${his.history.length}`).to.be.equal(1);
chai_1.expect(his.history[0].version, "Wrong version on history").to.be.equal(testver);
chai_1.expect(his.file.active.version, "Wrong version on file").to.be.equal(testver2);
}));
it("history get old", () => __awaiter(this, void 0, void 0, function* () {
let arch = yield sf.get(testid, testver);
chai_1.expect(decoder.decode(arch), "Old version has wrong data").to.be.equal(decoder.decode(testdata));
}));
it("history restore", () => __awaiter(this, void 0, void 0, function* () {
yield sf.restore(testid, testver);
let res = yield sf.get(testid);
chai_1.expect(res, "No data returned").to.exist;
chai_1.expect(decoder.decode(res), "Returned data not equal to stored").to.be.equal(decoder.decode(testdata));
}));
it("delete", () => __awaiter(this, void 0, void 0, function* () {
let res = yield sf.delete(testid);
chai_1.expect(res, "Res not set").to.exist;
}));
describe("folder", () => {
it("create", () => __awaiter(this, void 0, void 0, function* () {
let res = yield sf.create(testname, testdata, "text", testfolder, testprev);
chai_1.expect(res, "Res not set").to.exist;
chai_1.expect(res._id, "No _id field").to.exist;
ftestid = res._id;
testver = res.active.version;
}));
it("list", () => __awaiter(this, void 0, void 0, function* () {
let res = yield sf.list(testfolder);
chai_1.expect(Array.isArray(res), "Is from type Array").to.be.true;
chai_1.expect(res.length, "Do elements exist?").to.be.greaterThan(0);
let found = false;
res.forEach(e => {
if (e._id === ftestid) {
found = true;
}
});
chai_1.expect(found, "Element is not in List").to.exist;
}));
it("delete", () => __awaiter(this, void 0, void 0, function* () {
let res = yield sf.delete(ftestid);
chai_1.expect(res, "Res not set").to.exist;
}));
});
}
describe("SecureFile Tests", function () {
let sf = new index_1.default("http://localhost:3004");
sf.jwtObservable.subscribe((callback) => {
callback[0]("TESTJWT");
});
test(sf);
});
//# sourceMappingURL=test.js.map

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/secure-file-wrapper", "name": "@hibas123/secure-file-wrapper",
"version": "2.0.1", "version": "2.0.2",
"main": "lib/index.js", "main": "lib/index.js",
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",
"license": "MIT", "license": "MIT",

View File

@ -86,7 +86,7 @@ function statusParser(res: Response) {
} }
export default class SecureFileWrapper { export default class SecureFileWrapper {
private _jwtObservableServer: Observable<(jwt: string) => void> = new Observable(); private _jwtObservableServer: Observable<(jwt: string) => void> = new Observable(false);
jwtObservable = this._jwtObservableServer.getPublicApi(); jwtObservable = this._jwtObservableServer.getPublicApi();
private jwt: string; private jwt: string;