@autofleet/outbreak 0.0.8-beta.5 → 0.1.0-beta-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/identity/middlewares.js +2 -2
- package/dist/src/identity/middlewares.js.map +1 -1
- package/dist/src/tracer/index.d.ts +4 -1
- package/dist/src/tracer/index.js +14 -7
- package/dist/src/tracer/index.js.map +1 -1
- package/dist/tests/http-wrapper.test.js +1 -1
- package/dist/tests/http-wrapper.test.js.map +1 -1
- package/package.json +2 -1
- package/src/http-wrapper/config.ts +1 -1
- package/src/index.ts +0 -12
- package/src/tracer/index.ts +24 -15
- package/tests/http-wrapper.test.ts +1 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -12
- package/dist/index.js.map +0 -1
- package/index.ts +0 -21
- package/src/identity/ApiUser.ts +0 -91
- package/src/identity/index.ts +0 -15
- package/src/identity/middlewares.ts +0 -76
- package/src/identity/utils.ts +0 -74
- package/src/network/index.ts +0 -13
@@ -17,7 +17,7 @@ const tracer_1 = require("../tracer");
|
|
17
17
|
const utils_1 = require("./utils");
|
18
18
|
exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
19
19
|
const userId = req.headers['x-af-user-id'];
|
20
|
-
const trace = tracer_1.
|
20
|
+
const trace = tracer_1.getCurrentTrace();
|
21
21
|
if (!userId) {
|
22
22
|
trace.context.set('userObject', {});
|
23
23
|
return next();
|
@@ -35,7 +35,7 @@ exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, voi
|
|
35
35
|
});
|
36
36
|
exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
37
37
|
var _a, _b;
|
38
|
-
const trace = tracer_1.
|
38
|
+
const trace = tracer_1.getCurrentTrace();
|
39
39
|
if (req.headers.authorization) {
|
40
40
|
const decoded = utils_1.decodeBearer(req.headers.authorization);
|
41
41
|
const userId = (_b = (_a = decoded) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b.id;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"middlewares.js","sourceRoot":"","sources":["../../../src/identity/middlewares.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,wDAAgC;AAChC,
|
1
|
+
{"version":3,"file":"middlewares.js","sourceRoot":"","sources":["../../../src/identity/middlewares.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,wDAAgC;AAChC,sCAA4C;AAC5C,mCAAuC;AAE1B,QAAA,UAAU,GAAG,CAAC,UAGvB,EAAE,EAAE,EAAE,CAAC,CAAO,GAAG,EAAE,GAAG,EAAE,IAAI,EAAiB,EAAE;IACjD,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;IAC3C,MAAM,KAAK,GAAG,wBAAe,EAAE,CAAC;IAChC,IAAI,CAAC,MAAM,EAAE;QACX,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;QACpC,OAAO,IAAI,EAAE,CAAC;KACf;IAED,MAAM,UAAU,GAAG,IAAI,iBAAO,CAAC,MAAM,CAAC,CAAC;IAEvC,IAAI,OAAO,CAAC,wBAAwB,EAAE;QACpC,MAAM,UAAU,CAAC,kBAAkB,EAAE,CAAC;KACvC;IAED,IAAI,OAAO,CAAC,8BAA8B,EAAE;QAC1C,MAAM,UAAU,CAAC,wBAAwB,EAAE,CAAC;KAC7C;IAED,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC;IACtB,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;IAE5C,OAAO,IAAI,EAAE,CAAC;AAChB,CAAC,CAAA,CAAC;AAEW,QAAA,oBAAoB,GAAG,CAAC,UAGjC,EAAE,EAAE,EAAE,CAAC,CAAO,GAAG,EAAE,GAAG,EAAE,IAAI,EAAiB,EAAE;;IACjD,MAAM,KAAK,GAA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15 @@
|
|
1
1
|
/// <reference types="node" />
|
2
2
|
import asyncHooks from 'async_hooks';
|
3
|
-
declare const executionContextMap: Map<any, any>;
|
4
3
|
declare class Trace {
|
5
4
|
id: string;
|
6
5
|
type: string;
|
7
6
|
context: Map<string, any>;
|
7
|
+
hooks: {
|
8
|
+
[key: string]: Function[];
|
9
|
+
};
|
8
10
|
constructor(type: any);
|
9
11
|
}
|
12
|
+
declare const executionContextMap: Map<number, Trace>;
|
10
13
|
export declare const newTrace: (type: any) => Trace;
|
11
14
|
export declare const enable: () => asyncHooks.AsyncHook;
|
12
15
|
export declare const getCurrentTrace: () => Trace;
|
package/dist/src/tracer/index.js
CHANGED
@@ -5,6 +5,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
const async_hooks_1 = __importDefault(require("async_hooks"));
|
7
7
|
const uuid_1 = __importDefault(require("uuid"));
|
8
|
+
class Trace {
|
9
|
+
constructor(type) {
|
10
|
+
this.id = uuid_1.default.v1();
|
11
|
+
this.type = type;
|
12
|
+
this.context = new Map();
|
13
|
+
this.hooks = {
|
14
|
+
onDestroy: [],
|
15
|
+
};
|
16
|
+
}
|
17
|
+
}
|
8
18
|
const executionContextMap = new Map();
|
9
19
|
function init(asyncId, type, triggerAsyncId) {
|
10
20
|
if (executionContextMap.has(triggerAsyncId)) {
|
@@ -12,18 +22,15 @@ function init(asyncId, type, triggerAsyncId) {
|
|
12
22
|
}
|
13
23
|
}
|
14
24
|
function destroy(asyncId) {
|
25
|
+
const ecValue = executionContextMap.get(asyncId);
|
26
|
+
if (ecValue) {
|
27
|
+
ecValue.hooks.onDestroy.map((cb) => cb(asyncId, ecValue));
|
28
|
+
}
|
15
29
|
executionContextMap.delete(asyncId);
|
16
30
|
}
|
17
31
|
const hook = async_hooks_1.default.createHook({
|
18
32
|
init, destroy,
|
19
33
|
});
|
20
|
-
class Trace {
|
21
|
-
constructor(type) {
|
22
|
-
this.id = uuid_1.default.v1();
|
23
|
-
this.type = type;
|
24
|
-
this.context = new Map();
|
25
|
-
}
|
26
|
-
}
|
27
34
|
exports.newTrace = (type) => {
|
28
35
|
const trace = new Trace(type);
|
29
36
|
executionContextMap.set(async_hooks_1.default.executionAsyncId(), trace);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/tracer/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AACrC,gDAAwB;AAExB,MAAM,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/tracer/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AACrC,gDAAwB;AAExB,MAAM,KAAK;IAST,YAAY,IAAI;QACd,IAAI,CAAC,EAAE,GAAG,cAAI,CAAC,EAAE,EAAE,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG;YACX,SAAS,EAAE,EAAE;SACd,CAAA;IACH,CAAC;CACF;AAED,MAAM,mBAAmB,GAAuB,IAAI,GAAG,EAAE,CAAC;AAE1D,SAAS,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,cAAc;IACzC,IAAI,mBAAmB,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE;QAC3C,mBAAmB,CAAC,GAAG,CAAC,OAAO,EAAE,mBAAmB,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC;KAC3E;AACH,CAAC;AAED,SAAS,OAAO,CAAC,OAAO;IACtB,MAAM,OAAO,GAAG,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACjD,IAAI,OAAO,EAAE;QACX,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;KAC3D;IACD,mBAAmB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,IAAI,GAAG,qBAAU,CAAC,UAAU,CAAC;IACjC,IAAI,EAAE,OAAO;CACd,CAAC,CAAC;AAEU,QAAA,QAAQ,GAAG,CAAC,IAAI,EAAS,EAAE;IACtC,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC;IAC9B,mBAAmB,CAAC,GAAG,CAAC,qBAAU,CAAC,gBAAgB,EAAE,EAAE,KAAK,CAAC,CAAC;IAC9D,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEW,QAAA,MAAM,GAAG,GAAyB,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;AAEnD,QAAA,eAAe,GAAG,GAAU,EAAE;IACzC,MAAM,cAAc,GAAG,mBAAmB,CAAC,GAAG,CAAC,qBAAU,CAAC,gBAAgB,EAAE,CAAC,CAAC;IAC9E,IAAI,CAAC,cAAc,EAAE;QACnB,OAAO,gBAAQ,CAAC,gBAAgB,CAAC,CAAC;KACnC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAEF,kBAAe,mBAAmB,CAAC"}
|
@@ -16,7 +16,7 @@ const express_1 = __importDefault(require("express"));
|
|
16
16
|
const axios_1 = __importDefault(require("axios"));
|
17
17
|
const winston_1 = __importDefault(require("winston"));
|
18
18
|
const http_wrapper_1 = __importDefault(require("../src/http-wrapper"));
|
19
|
-
const index_1 = require("../index");
|
19
|
+
const index_1 = require("../src/index");
|
20
20
|
console.log(index_1.headersTracer.toString(), index_1.middlewareWithDecode.toString());
|
21
21
|
const logger = winston_1.default.createLogger({
|
22
22
|
transports: [
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"http-wrapper.test.js","sourceRoot":"","sources":["../../tests/http-wrapper.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,sDAA8B;AAE9B,kDAA0B;AAC1B,sDAA8B;AAE9B,uEAAg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AO,CAAC,eAAe,CAAC,CAAA;IAC9C,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAA"}
|
package/package.json
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "@autofleet/outbreak",
|
3
|
-
"version": "0.0
|
3
|
+
"version": "0.1.0-beta-1",
|
4
4
|
"description": "",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
7
7
|
"scripts": {
|
8
8
|
"build": "rm -rf ./dist && tsc",
|
9
|
+
"prepublish": "npm run build",
|
9
10
|
"start": "ts-node src/example.ts",
|
10
11
|
"dev": "./node_modules/nodemon/bin/nodemon.js",
|
11
12
|
"lint": "./node_modules/.bin/eslint src/**/*.ts",
|
package/src/index.ts
CHANGED
@@ -1,20 +1,8 @@
|
|
1
1
|
import headersTracer from './http-wrapper';
|
2
2
|
import { newTrace, getCurrentTrace } from './tracer';
|
3
|
-
import {
|
4
|
-
ApiUser,
|
5
|
-
middleware,
|
6
|
-
middlewareWithDecode,
|
7
|
-
eagerLoadPermissionsMiddleware,
|
8
|
-
getDecodedBearer,
|
9
|
-
} from './identity';
|
10
3
|
|
11
4
|
export {
|
12
5
|
headersTracer,
|
13
6
|
getCurrentTrace,
|
14
7
|
newTrace,
|
15
|
-
ApiUser,
|
16
|
-
middleware,
|
17
|
-
middlewareWithDecode,
|
18
|
-
eagerLoadPermissionsMiddleware,
|
19
|
-
getDecodedBearer,
|
20
8
|
};
|
package/src/tracer/index.ts
CHANGED
@@ -1,7 +1,26 @@
|
|
1
1
|
import asyncHooks from 'async_hooks';
|
2
2
|
import uuid from 'uuid';
|
3
3
|
|
4
|
-
|
4
|
+
class Trace {
|
5
|
+
id: string;
|
6
|
+
|
7
|
+
type: string;
|
8
|
+
|
9
|
+
context: Map<string, any>;
|
10
|
+
|
11
|
+
hooks: { [key: string]: Function[] }
|
12
|
+
|
13
|
+
constructor(type) {
|
14
|
+
this.id = uuid.v1();
|
15
|
+
this.type = type;
|
16
|
+
this.context = new Map();
|
17
|
+
this.hooks = {
|
18
|
+
onDestroy: [],
|
19
|
+
}
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
const executionContextMap: Map<number, Trace> = new Map();
|
5
24
|
|
6
25
|
function init(asyncId, type, triggerAsyncId): void {
|
7
26
|
if (executionContextMap.has(triggerAsyncId)) {
|
@@ -10,6 +29,10 @@ function init(asyncId, type, triggerAsyncId): void {
|
|
10
29
|
}
|
11
30
|
|
12
31
|
function destroy(asyncId): void {
|
32
|
+
const ecValue = executionContextMap.get(asyncId);
|
33
|
+
if (ecValue) {
|
34
|
+
ecValue.hooks.onDestroy.map((cb) => cb(asyncId, ecValue));
|
35
|
+
}
|
13
36
|
executionContextMap.delete(asyncId);
|
14
37
|
}
|
15
38
|
|
@@ -17,20 +40,6 @@ const hook = asyncHooks.createHook({
|
|
17
40
|
init, destroy,
|
18
41
|
});
|
19
42
|
|
20
|
-
class Trace {
|
21
|
-
id: string;
|
22
|
-
|
23
|
-
type: string;
|
24
|
-
|
25
|
-
context: Map<string, any>;
|
26
|
-
|
27
|
-
constructor(type) {
|
28
|
-
this.id = uuid.v1();
|
29
|
-
this.type = type;
|
30
|
-
this.context = new Map();
|
31
|
-
}
|
32
|
-
}
|
33
|
-
|
34
43
|
export const newTrace = (type): Trace => {
|
35
44
|
const trace = new Trace(type);
|
36
45
|
executionContextMap.set(asyncHooks.executionAsyncId(), trace);
|
@@ -4,9 +4,8 @@ import axios from 'axios';
|
|
4
4
|
import winston from 'winston';
|
5
5
|
|
6
6
|
import headersTracer from '../src/http-wrapper';
|
7
|
-
import { headersTracer as a
|
7
|
+
import { headersTracer as a } from '../src/index';
|
8
8
|
|
9
|
-
console.log(a.toString(), b.toString());
|
10
9
|
|
11
10
|
const logger = winston.createLogger({
|
12
11
|
transports: [
|
package/dist/index.d.ts
DELETED
@@ -1,2 +0,0 @@
|
|
1
|
-
import { headersTracer, getCurrentTrace, newTrace, ApiUser, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, getDecodedBearer } from './src';
|
2
|
-
export { headersTracer, getCurrentTrace, newTrace, ApiUser, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, getDecodedBearer, };
|
package/dist/index.js
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
const src_1 = require("./src");
|
4
|
-
exports.headersTracer = src_1.headersTracer;
|
5
|
-
exports.getCurrentTrace = src_1.getCurrentTrace;
|
6
|
-
exports.newTrace = src_1.newTrace;
|
7
|
-
exports.ApiUser = src_1.ApiUser;
|
8
|
-
exports.middleware = src_1.middleware;
|
9
|
-
exports.middlewareWithDecode = src_1.middlewareWithDecode;
|
10
|
-
exports.eagerLoadPermissionsMiddleware = src_1.eagerLoadPermissionsMiddleware;
|
11
|
-
exports.getDecodedBearer = src_1.getDecodedBearer;
|
12
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;AAAA,+BASe;AAGb,wBAXA,mBAAa,CAWA;AACb,0BAXA,qBAAe,CAWA;AACf,mBAXA,cAAQ,CAWA;AACR,kBAXA,aAAO,CAWA;AACP,qBAXA,gBAAU,CAWA;AACV,+BAXA,0BAAoB,CAWA;AACpB,yCAXA,oCAA8B,CAWA;AAC9B,2BAXA,sBAAgB,CAWA"}
|
package/index.ts
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
headersTracer,
|
3
|
-
getCurrentTrace,
|
4
|
-
newTrace,
|
5
|
-
ApiUser,
|
6
|
-
middleware,
|
7
|
-
middlewareWithDecode,
|
8
|
-
eagerLoadPermissionsMiddleware,
|
9
|
-
getDecodedBearer,
|
10
|
-
} from './src';
|
11
|
-
|
12
|
-
export {
|
13
|
-
headersTracer,
|
14
|
-
getCurrentTrace,
|
15
|
-
newTrace,
|
16
|
-
ApiUser,
|
17
|
-
middleware,
|
18
|
-
middlewareWithDecode,
|
19
|
-
eagerLoadPermissionsMiddleware,
|
20
|
-
getDecodedBearer,
|
21
|
-
};
|
package/src/identity/ApiUser.ts
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
import { IdentityNetwork } from '../network';
|
2
|
-
|
3
|
-
interface EntityPermissions {
|
4
|
-
[key: string]: string[];
|
5
|
-
}
|
6
|
-
|
7
|
-
export interface UserPayload {
|
8
|
-
businessModels: EntityPermissions;
|
9
|
-
fleets: EntityPermissions;
|
10
|
-
demandSources: EntityPermissions;
|
11
|
-
createdAt?: string;
|
12
|
-
}
|
13
|
-
|
14
|
-
export default class ApiUser {
|
15
|
-
id: string | undefined;
|
16
|
-
|
17
|
-
privatePermissions: UserPayload | undefined;
|
18
|
-
|
19
|
-
privatePermissionsLegacy: any;
|
20
|
-
|
21
|
-
emptyUser: boolean;
|
22
|
-
|
23
|
-
constructor(id? : string) {
|
24
|
-
this.id = id;
|
25
|
-
this.emptyUser = !!id;
|
26
|
-
}
|
27
|
-
|
28
|
-
async getUserPermissions() {
|
29
|
-
if (!this.id) {
|
30
|
-
return null;
|
31
|
-
}
|
32
|
-
|
33
|
-
if (this.privatePermissions) {
|
34
|
-
return this.privatePermissions;
|
35
|
-
}
|
36
|
-
const { data } = await IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`);
|
37
|
-
|
38
|
-
this.privatePermissions = data;
|
39
|
-
return this.privatePermissions;
|
40
|
-
}
|
41
|
-
|
42
|
-
get businessModels(): string[] | undefined {
|
43
|
-
return this.getUserProperty('businessModels');
|
44
|
-
}
|
45
|
-
|
46
|
-
get fleets(): string[] | undefined {
|
47
|
-
return this.getUserProperty('fleets');
|
48
|
-
}
|
49
|
-
|
50
|
-
get demandSources(): string[] | undefined {
|
51
|
-
return this.getUserProperty('demandSources');
|
52
|
-
}
|
53
|
-
|
54
|
-
getUserProperty(key): string[] | undefined {
|
55
|
-
if (!this.privatePermissions) {
|
56
|
-
throw new Error(`Cannot get ${key} without calling (async) getUserPermissions before`);
|
57
|
-
}
|
58
|
-
|
59
|
-
return Object.keys(this.privatePermissions[key] || {});
|
60
|
-
}
|
61
|
-
|
62
|
-
get permissions(): UserPayload | undefined {
|
63
|
-
if (!this.privatePermissions) {
|
64
|
-
throw new Error('Cannot get permissions without calling (async) getUserPermissions before');
|
65
|
-
}
|
66
|
-
|
67
|
-
return this.privatePermissions;
|
68
|
-
}
|
69
|
-
|
70
|
-
async getUserPermissionsLegacy() {
|
71
|
-
if (!this.id) {
|
72
|
-
return null;
|
73
|
-
}
|
74
|
-
|
75
|
-
if (this.privatePermissionsLegacy) {
|
76
|
-
return this.privatePermissionsLegacy;
|
77
|
-
}
|
78
|
-
|
79
|
-
const { data } = await IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload-legacy`);
|
80
|
-
|
81
|
-
this.privatePermissionsLegacy = data;
|
82
|
-
return this.privatePermissionsLegacy;
|
83
|
-
}
|
84
|
-
|
85
|
-
get permissionsLegacy(): any {
|
86
|
-
if (!this.privatePermissionsLegacy) {
|
87
|
-
throw new Error('Cannot get permissionsLegacy without calling (async) getUserPermissionsLegacy before');
|
88
|
-
}
|
89
|
-
return this.privatePermissionsLegacy;
|
90
|
-
}
|
91
|
-
}
|
package/src/identity/index.ts
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
import ApiUser from './ApiUser';
|
2
|
-
import {
|
3
|
-
middleware,
|
4
|
-
middlewareWithDecode,
|
5
|
-
eagerLoadPermissionsMiddleware,
|
6
|
-
getDecodedBearer,
|
7
|
-
} from './middlewares';
|
8
|
-
|
9
|
-
export {
|
10
|
-
ApiUser,
|
11
|
-
middleware,
|
12
|
-
middlewareWithDecode,
|
13
|
-
eagerLoadPermissionsMiddleware,
|
14
|
-
getDecodedBearer,
|
15
|
-
};
|
@@ -1,76 +0,0 @@
|
|
1
|
-
import ApiUser from './ApiUser';
|
2
|
-
import { newTrace } from '../tracer';
|
3
|
-
import { decodeBearer } from './utils';
|
4
|
-
|
5
|
-
export const middleware = (options: {
|
6
|
-
eagerLoadUserPermissions?: boolean;
|
7
|
-
eagerLoadUserPermissionsLegacy?: boolean;
|
8
|
-
} = {}) => async (req, res, next): Promise<void> => {
|
9
|
-
const userId = req.headers['x-af-user-id'];
|
10
|
-
const trace = newTrace('userPayload');
|
11
|
-
if (!userId) {
|
12
|
-
trace.context.set('userObject', {});
|
13
|
-
return next();
|
14
|
-
}
|
15
|
-
|
16
|
-
const userObject = new ApiUser(userId);
|
17
|
-
|
18
|
-
if (options.eagerLoadUserPermissions) {
|
19
|
-
await userObject.getUserPermissions();
|
20
|
-
}
|
21
|
-
|
22
|
-
if (options.eagerLoadUserPermissionsLegacy) {
|
23
|
-
await userObject.getUserPermissionsLegacy();
|
24
|
-
}
|
25
|
-
|
26
|
-
req.user = userObject;
|
27
|
-
trace.context.set('userObject', userObject);
|
28
|
-
|
29
|
-
return next();
|
30
|
-
};
|
31
|
-
|
32
|
-
export const middlewareWithDecode = (options: {
|
33
|
-
eagerLoadUserPermissions?: boolean;
|
34
|
-
eagerLoadUserPermissionsLegacy?: boolean;
|
35
|
-
} = {}) => async (req, res, next): Promise<void> => {
|
36
|
-
const trace = newTrace('userPayload');
|
37
|
-
if (req.headers.authorization) {
|
38
|
-
const decoded = decodeBearer(req.headers.authorization);
|
39
|
-
const userId = decoded?.user?.id;
|
40
|
-
|
41
|
-
if (userId) {
|
42
|
-
req.headers['X-AF-USER-ID'] = userId;
|
43
|
-
}
|
44
|
-
|
45
|
-
const userObject = new ApiUser(userId);
|
46
|
-
|
47
|
-
if (options.eagerLoadUserPermissions) {
|
48
|
-
await userObject.getUserPermissions();
|
49
|
-
}
|
50
|
-
|
51
|
-
if (options.eagerLoadUserPermissionsLegacy) {
|
52
|
-
await userObject.getUserPermissionsLegacy();
|
53
|
-
}
|
54
|
-
|
55
|
-
req.user = userObject;
|
56
|
-
trace.context.set('userObject', userObject);
|
57
|
-
}
|
58
|
-
|
59
|
-
trace.context.set('userObject', {});
|
60
|
-
return next();
|
61
|
-
};
|
62
|
-
|
63
|
-
export const eagerLoadPermissionsMiddleware = async (req, res, next) => {
|
64
|
-
await req.user.getUserPermissions();
|
65
|
-
return next();
|
66
|
-
};
|
67
|
-
|
68
|
-
export const getDecodedBearer = (req) => {
|
69
|
-
if (req.headers.authorization) {
|
70
|
-
return decodeBearer(req.headers.authorization);
|
71
|
-
}
|
72
|
-
|
73
|
-
return null;
|
74
|
-
};
|
75
|
-
|
76
|
-
export default ApiUser;
|
package/src/identity/utils.ts
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
/* eslint-disable prefer-destructuring */
|
2
|
-
import * as jwt from 'jsonwebtoken';
|
3
|
-
|
4
|
-
const CONTEXT_PROPS = ['fleetId', 'businessModelId', 'demandSourceId'];
|
5
|
-
const CONTEXT_MAP_PROPS = {
|
6
|
-
fleet: 'fleets',
|
7
|
-
business: 'businessModels',
|
8
|
-
demand: 'demandSources',
|
9
|
-
};
|
10
|
-
|
11
|
-
export const getAuthFromBearer = (bearer: string): string => bearer.replace('Bearer ', '');
|
12
|
-
|
13
|
-
export const decodeBearer = (bearer: string): any => {
|
14
|
-
const token = bearer.replace('Bearer ', '');
|
15
|
-
const decoded = jwt.decode(token);
|
16
|
-
return decoded;
|
17
|
-
};
|
18
|
-
|
19
|
-
export const parsePermissions = (contextId, decodedToken): any => {
|
20
|
-
if (!decodedToken) { return []; }
|
21
|
-
const contexts = decodedToken.contexts;
|
22
|
-
const activeContext = contexts.find((context) => context.id === contextId);
|
23
|
-
|
24
|
-
const permissionsByContext = {};
|
25
|
-
const permissionsValue = `${activeContext.permissions?.map((cp) => `${cp},`)}`;
|
26
|
-
|
27
|
-
|
28
|
-
return {
|
29
|
-
key: activeContext.entityId,
|
30
|
-
value: permissionsValue,
|
31
|
-
};
|
32
|
-
};
|
33
|
-
|
34
|
-
export const getEntitiesFromContext = (contextId: string, decodedToken: any): any => {
|
35
|
-
if (!decodedToken) { return []; }
|
36
|
-
let contexts = decodedToken.contexts;
|
37
|
-
if (contextId) {
|
38
|
-
contexts = contexts.filter((context) => context.id === contextId);
|
39
|
-
}
|
40
|
-
|
41
|
-
const attributes = {};
|
42
|
-
contexts.forEach((context) => {
|
43
|
-
const prop = CONTEXT_MAP_PROPS[context.subSystem || 'business'];
|
44
|
-
|
45
|
-
const permissions = parsePermissions(context.id, decodedToken);
|
46
|
-
// eslint-disable-next-line no-unused-expressions
|
47
|
-
attributes[prop]
|
48
|
-
? attributes[prop][permissions.key] = permissions.value
|
49
|
-
: attributes[prop] = { [permissions.key]: permissions.value };
|
50
|
-
});
|
51
|
-
|
52
|
-
return attributes;
|
53
|
-
};
|
54
|
-
|
55
|
-
export const getContextAttributes = (contextId: string, decodedToken: any): any => {
|
56
|
-
if (!decodedToken) { return []; }
|
57
|
-
let contexts = decodedToken.contexts;
|
58
|
-
if (contextId) {
|
59
|
-
contexts = contexts.filter((context) => context.id === contextId);
|
60
|
-
}
|
61
|
-
const attributes = {};
|
62
|
-
contexts.forEach((context) => {
|
63
|
-
CONTEXT_PROPS.forEach((prop) => {
|
64
|
-
if (context[prop]) {
|
65
|
-
const contextPropWrapped = [context[prop]];
|
66
|
-
// eslint-disable-next-line no-unused-expressions
|
67
|
-
attributes[prop]
|
68
|
-
? attributes[prop] = attributes[prop].concat(contextPropWrapped)
|
69
|
-
: attributes[prop] = contextPropWrapped;
|
70
|
-
}
|
71
|
-
});
|
72
|
-
});
|
73
|
-
return attributes;
|
74
|
-
};
|
package/src/network/index.ts
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
/* eslint-disable import/prefer-default-export */
|
2
|
-
import Network from '@autofleet/network';
|
3
|
-
|
4
|
-
const CACHE_LIFETIME_IN_SEC = 10;
|
5
|
-
|
6
|
-
export const IdentityNetwork = new Network({
|
7
|
-
serviceName: 'IDENTITY_MS',
|
8
|
-
retries: 3,
|
9
|
-
retryCondition: () => true,
|
10
|
-
cache: process.env.NODE_ENV !== 'test' ? {
|
11
|
-
maxAge: CACHE_LIFETIME_IN_SEC * 1000,
|
12
|
-
} : undefined,
|
13
|
-
});
|