@autofleet/zehut 1.2.6 → 1.2.7
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/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/lib/user/index.d.ts +4 -0
- package/lib/user/index.js +23 -1
- package/lib/utils.d.ts +5 -0
- package/lib/utils.js +91 -0
- package/package.json +4 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import User, { middleware, eagerLoadPermissionsMiddleware } from './user';
|
|
2
|
-
export { User, middleware, eagerLoadPermissionsMiddleware, };
|
|
1
|
+
import User, { middleware, eagerLoadPermissionsMiddleware, middlewareWithDecode } from './user';
|
|
2
|
+
export { User, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, };
|
package/lib/index.js
CHANGED
|
@@ -19,8 +19,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.eagerLoadPermissionsMiddleware = exports.middleware = exports.User = void 0;
|
|
22
|
+
exports.eagerLoadPermissionsMiddleware = exports.middlewareWithDecode = exports.middleware = exports.User = void 0;
|
|
23
23
|
const user_1 = __importStar(require("./user"));
|
|
24
24
|
exports.User = user_1.default;
|
|
25
25
|
Object.defineProperty(exports, "middleware", { enumerable: true, get: function () { return user_1.middleware; } });
|
|
26
26
|
Object.defineProperty(exports, "eagerLoadPermissionsMiddleware", { enumerable: true, get: function () { return user_1.eagerLoadPermissionsMiddleware; } });
|
|
27
|
+
Object.defineProperty(exports, "middlewareWithDecode", { enumerable: true, get: function () { return user_1.middlewareWithDecode; } });
|
package/lib/user/index.d.ts
CHANGED
|
@@ -3,5 +3,9 @@ export declare const middleware: (options?: {
|
|
|
3
3
|
eagerLoadUserPermissions?: boolean;
|
|
4
4
|
eagerLoadUserPermissionsLegacy?: boolean;
|
|
5
5
|
}) => (req: any, res: any, next: any) => Promise<void>;
|
|
6
|
+
export declare const middlewareWithDecode: (options?: {
|
|
7
|
+
eagerLoadUserPermissions?: boolean;
|
|
8
|
+
eagerLoadUserPermissionsLegacy?: boolean;
|
|
9
|
+
}) => (req: any, res: any, next: any) => Promise<void>;
|
|
6
10
|
export declare const eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<void>;
|
|
7
11
|
export default ApiUser;
|
package/lib/user/index.js
CHANGED
|
@@ -12,8 +12,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.eagerLoadPermissionsMiddleware = exports.middleware = void 0;
|
|
15
|
+
exports.eagerLoadPermissionsMiddleware = exports.middlewareWithDecode = exports.middleware = void 0;
|
|
16
16
|
const ApiUser_1 = __importDefault(require("./ApiUser"));
|
|
17
|
+
const utils_1 = require("../utils");
|
|
17
18
|
exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
19
|
const userId = req.headers['x-af-user-id'];
|
|
19
20
|
if (!userId) {
|
|
@@ -29,6 +30,27 @@ exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, voi
|
|
|
29
30
|
}
|
|
30
31
|
next();
|
|
31
32
|
});
|
|
33
|
+
exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
var _a;
|
|
35
|
+
if (req.headers.authorization) {
|
|
36
|
+
const decoded = utils_1.decodeBearer(req.headers.authorization);
|
|
37
|
+
const userId = (_a = decoded === null || decoded === void 0 ? void 0 : decoded.user) === null || _a === void 0 ? void 0 : _a.id;
|
|
38
|
+
if (userId) {
|
|
39
|
+
req.headers['X-AF-USER-ID'] = userId;
|
|
40
|
+
}
|
|
41
|
+
const userObject = new ApiUser_1.default(userId);
|
|
42
|
+
req.user = userObject;
|
|
43
|
+
if (options.eagerLoadUserPermissions) {
|
|
44
|
+
yield req.user.getUserPermissions();
|
|
45
|
+
}
|
|
46
|
+
if (options.eagerLoadUserPermissionsLegacy) {
|
|
47
|
+
yield req.user.getUserPermissionsLegacy();
|
|
48
|
+
}
|
|
49
|
+
const entities = req.user.permissions;
|
|
50
|
+
req.headers['X-AF-ENTITY-ID'] = JSON.stringify(entities);
|
|
51
|
+
}
|
|
52
|
+
next();
|
|
53
|
+
});
|
|
32
54
|
exports.eagerLoadPermissionsMiddleware = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
55
|
yield req.user.getUserPermissions();
|
|
34
56
|
next();
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const getAuthFromBearer: (bearer: string) => string;
|
|
2
|
+
export declare const decodeBearer: (bearer: string) => any;
|
|
3
|
+
export declare const parsePermissions: (contextId: any, decodedToken: any) => any;
|
|
4
|
+
export declare const getEntitiesFromContext: (contextId: string, decodedToken: any) => any;
|
|
5
|
+
export declare const getContextAttributes: (contextId: string, decodedToken: any) => any;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.getContextAttributes = exports.getEntitiesFromContext = exports.parsePermissions = exports.decodeBearer = exports.getAuthFromBearer = void 0;
|
|
23
|
+
/* eslint-disable prefer-destructuring */
|
|
24
|
+
const jwt = __importStar(require("jsonwebtoken"));
|
|
25
|
+
const CONTEXT_PROPS = ['fleetId', 'businessModelId', 'demandSourceId'];
|
|
26
|
+
const CONTEXT_MAP_PROPS = {
|
|
27
|
+
fleet: 'fleets',
|
|
28
|
+
business: 'businessModels',
|
|
29
|
+
demand: 'demandSources',
|
|
30
|
+
};
|
|
31
|
+
exports.getAuthFromBearer = (bearer) => bearer.replace('Bearer ', '');
|
|
32
|
+
exports.decodeBearer = (bearer) => {
|
|
33
|
+
const token = bearer.replace('Bearer ', '');
|
|
34
|
+
const decoded = jwt.decode(token);
|
|
35
|
+
return decoded;
|
|
36
|
+
};
|
|
37
|
+
exports.parsePermissions = (contextId, decodedToken) => {
|
|
38
|
+
var _a;
|
|
39
|
+
if (!decodedToken) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
const contexts = decodedToken.contexts;
|
|
43
|
+
const activeContext = contexts.find(context => context.id === contextId);
|
|
44
|
+
const permissionsByContext = {};
|
|
45
|
+
const permissionsValue = `${(_a = activeContext.permissions) === null || _a === void 0 ? void 0 : _a.map(cp => `${cp},`)}`;
|
|
46
|
+
return {
|
|
47
|
+
key: activeContext.entityId,
|
|
48
|
+
value: permissionsValue,
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
exports.getEntitiesFromContext = (contextId, decodedToken) => {
|
|
52
|
+
if (!decodedToken) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
let contexts = decodedToken.contexts;
|
|
56
|
+
if (contextId) {
|
|
57
|
+
contexts = contexts.filter(context => context.id === contextId);
|
|
58
|
+
}
|
|
59
|
+
const attributes = {};
|
|
60
|
+
contexts.forEach((context) => {
|
|
61
|
+
const prop = CONTEXT_MAP_PROPS[context.subSystem || 'business'];
|
|
62
|
+
const permissions = exports.parsePermissions(context.id, decodedToken);
|
|
63
|
+
// eslint-disable-next-line no-unused-expressions
|
|
64
|
+
attributes[prop] ?
|
|
65
|
+
attributes[prop][permissions.key] = permissions.value
|
|
66
|
+
: attributes[prop] = { [permissions.key]: permissions.value };
|
|
67
|
+
});
|
|
68
|
+
return attributes;
|
|
69
|
+
};
|
|
70
|
+
exports.getContextAttributes = (contextId, decodedToken) => {
|
|
71
|
+
if (!decodedToken) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
let contexts = decodedToken.contexts;
|
|
75
|
+
if (contextId) {
|
|
76
|
+
contexts = contexts.filter(context => context.id === contextId);
|
|
77
|
+
}
|
|
78
|
+
const attributes = {};
|
|
79
|
+
contexts.forEach((context) => {
|
|
80
|
+
CONTEXT_PROPS.forEach((prop) => {
|
|
81
|
+
if (context[prop]) {
|
|
82
|
+
const contextPropWrapped = [context[prop]];
|
|
83
|
+
// eslint-disable-next-line no-unused-expressions
|
|
84
|
+
attributes[prop] ?
|
|
85
|
+
attributes[prop] = attributes[prop].concat(contextPropWrapped) :
|
|
86
|
+
attributes[prop] = contextPropWrapped;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
return attributes;
|
|
91
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/zehut",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.7",
|
|
4
4
|
"description": "manage user's identity",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -21,13 +21,14 @@
|
|
|
21
21
|
},
|
|
22
22
|
"author": "",
|
|
23
23
|
"license": "ISC",
|
|
24
|
-
"bugs": {
|
|
24
|
+
"bugs": {
|
|
25
25
|
"url": "https://github.com/Autofleet/zehut/issues"
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/Autofleet/zehut",
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@autofleet/network": "^1.1.4",
|
|
30
|
-
"axios": "^0.19.2"
|
|
30
|
+
"axios": "^0.19.2",
|
|
31
|
+
"jsonwebtoken": "^8.5.1"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"typescript": "^3.9.5"
|