@autofleet/zehut 3.0.11-beta.1 → 3.1.0-beta.0
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/user/ApiUser.d.ts +7 -1
- package/lib/user/ApiUser.js +16 -8
- package/lib/user/index.js +20 -13
- package/package.json +2 -1
package/lib/user/ApiUser.d.ts
CHANGED
|
@@ -20,9 +20,15 @@ export interface PartialUserPayload {
|
|
|
20
20
|
drivers?: EntityPermissions;
|
|
21
21
|
businessAccounts?: EntityPermissions;
|
|
22
22
|
}
|
|
23
|
+
export interface AuthorizationFilters {
|
|
24
|
+
businessModelId?: string;
|
|
25
|
+
fleetId?: string;
|
|
26
|
+
demandSourceId?: string;
|
|
27
|
+
}
|
|
23
28
|
export type CustomPermissionLoader = (string: any) => Promise<UserPayload>;
|
|
24
29
|
export default class ApiUser {
|
|
25
30
|
id: string | undefined;
|
|
31
|
+
authorizationFilters: AuthorizationFilters;
|
|
26
32
|
privatePermissions: UserPayload | undefined;
|
|
27
33
|
privateElevatedPermissionsHash: Map<string, PartialUserPayload | undefined>;
|
|
28
34
|
privatePermissionsLegacy: any;
|
|
@@ -31,7 +37,7 @@ export default class ApiUser {
|
|
|
31
37
|
};
|
|
32
38
|
emptyUser: boolean;
|
|
33
39
|
accountType: AccountType | undefined;
|
|
34
|
-
constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload);
|
|
40
|
+
constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload, authorizationFilters?: AuthorizationFilters);
|
|
35
41
|
getUserPermissions(): Promise<UserPayload>;
|
|
36
42
|
useCustomPermissionLoader(customPermissionLoader: any): Promise<UserPayload>;
|
|
37
43
|
get businessModels(): string[] | undefined;
|
package/lib/user/ApiUser.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.ELEVATED_PERMISSIONS_HEADER = void 0;
|
|
7
7
|
/* eslint-disable consistent-return */
|
|
8
8
|
const node_cache_1 = __importDefault(require("node-cache"));
|
|
9
|
+
const object_hash_1 = __importDefault(require("object-hash"));
|
|
9
10
|
const merge_deep_1 = __importDefault(require("merge-deep"));
|
|
10
11
|
const uuid_1 = require("uuid");
|
|
11
12
|
const outbreak_1 = require("@autofleet/outbreak");
|
|
@@ -13,8 +14,9 @@ const services_1 = require("../services");
|
|
|
13
14
|
exports.ELEVATED_PERMISSIONS_HEADER = 'x-af-elevated-permissions';
|
|
14
15
|
const userCache = new node_cache_1.default({ stdTTL: 10 });
|
|
15
16
|
class ApiUser {
|
|
16
|
-
constructor(id, accountType, elevatedPermissions) {
|
|
17
|
+
constructor(id, accountType, elevatedPermissions, authorizationFilters = {}) {
|
|
17
18
|
this.id = id;
|
|
19
|
+
this.authorizationFilters = authorizationFilters;
|
|
18
20
|
this.emptyUser = !!id;
|
|
19
21
|
this.appPermission = {};
|
|
20
22
|
this.privateElevatedPermissionsHash = new Map();
|
|
@@ -32,10 +34,14 @@ class ApiUser {
|
|
|
32
34
|
if (this.privatePermissions) {
|
|
33
35
|
return this.privatePermissions;
|
|
34
36
|
}
|
|
35
|
-
|
|
37
|
+
const cacheKey = (0, object_hash_1.default)({
|
|
38
|
+
id: this.id,
|
|
39
|
+
authorizationFilters: this.authorizationFilters,
|
|
40
|
+
});
|
|
41
|
+
let data = userCache.get(cacheKey);
|
|
36
42
|
if (!data) {
|
|
37
|
-
({ data } = await services_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload
|
|
38
|
-
userCache.set(
|
|
43
|
+
({ data } = await services_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`, this.authorizationFilters));
|
|
44
|
+
userCache.set(cacheKey, data);
|
|
39
45
|
}
|
|
40
46
|
this.accountType = data.accountType;
|
|
41
47
|
this.privatePermissions = data;
|
|
@@ -48,13 +54,14 @@ class ApiUser {
|
|
|
48
54
|
if (this.privatePermissions) {
|
|
49
55
|
return this.privatePermissions;
|
|
50
56
|
}
|
|
51
|
-
const
|
|
57
|
+
const cacheKey = this.id;
|
|
58
|
+
const cachedResult = userCache.get(cacheKey);
|
|
52
59
|
if (cachedResult) {
|
|
53
60
|
this.privatePermissions = cachedResult;
|
|
54
61
|
return cachedResult;
|
|
55
62
|
}
|
|
56
63
|
const data = await customPermissionLoader(this.id);
|
|
57
|
-
userCache.set(
|
|
64
|
+
userCache.set(cacheKey, data);
|
|
58
65
|
this.privatePermissions = data;
|
|
59
66
|
return this.privatePermissions;
|
|
60
67
|
}
|
|
@@ -139,7 +146,8 @@ class ApiUser {
|
|
|
139
146
|
if (currentAppPermission) {
|
|
140
147
|
return currentAppPermission;
|
|
141
148
|
}
|
|
142
|
-
const
|
|
149
|
+
const cacheKey = `${this.id}:${appId}`;
|
|
150
|
+
const cachedResult = userCache.get(cacheKey);
|
|
143
151
|
if (cachedResult) {
|
|
144
152
|
this.appPermission[appId] = cachedResult;
|
|
145
153
|
return cachedResult;
|
|
@@ -151,7 +159,7 @@ class ApiUser {
|
|
|
151
159
|
'x-autofleet-apps-secret': clientSecret,
|
|
152
160
|
},
|
|
153
161
|
});
|
|
154
|
-
userCache.set(
|
|
162
|
+
userCache.set(cacheKey, data);
|
|
155
163
|
this.appPermission[appId] = data;
|
|
156
164
|
return this.appPermission[appId];
|
|
157
165
|
}
|
package/lib/user/index.js
CHANGED
|
@@ -34,7 +34,6 @@ const app_auth_1 = require("../app-auth");
|
|
|
34
34
|
const appDoesNotExist_1 = __importDefault(require("../exceptions/appDoesNotExist"));
|
|
35
35
|
const utils_1 = require("../utils");
|
|
36
36
|
const IDENTITY_MS = 'identity-ms';
|
|
37
|
-
const ANONYMOUS_USER = 'anonymous';
|
|
38
37
|
const middleware = (options = {}) => async (req, res, next) => {
|
|
39
38
|
try {
|
|
40
39
|
const originHeader = req.headers['X-IAF-ORIGIN-SERVICE'] || req.headers['x-iaf-origin-service'] || '';
|
|
@@ -43,6 +42,7 @@ const middleware = (options = {}) => async (req, res, next) => {
|
|
|
43
42
|
}
|
|
44
43
|
const { eagerLoadUserPermissions, eagerLoadUserPermissionsLegacy, customPermissionLoader, } = options;
|
|
45
44
|
const userId = req.headers['x-af-user-id'];
|
|
45
|
+
const { fleetId, businessModelId, demandSourceId, } = req.query;
|
|
46
46
|
const trace = (0, tracer_1.newTrace)('userPayload');
|
|
47
47
|
if (!userId) {
|
|
48
48
|
return next();
|
|
@@ -50,20 +50,22 @@ const middleware = (options = {}) => async (req, res, next) => {
|
|
|
50
50
|
const elevatedPermissionsFromHeader = req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER] && req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER].length > 0
|
|
51
51
|
? JSON.parse(req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER])
|
|
52
52
|
: {};
|
|
53
|
-
const userObject = new ApiUser_1.default(userId, 'user', elevatedPermissionsFromHeader
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
53
|
+
const userObject = new ApiUser_1.default(userId, 'user', elevatedPermissionsFromHeader, {
|
|
54
|
+
fleetId,
|
|
55
|
+
businessModelId,
|
|
56
|
+
demandSourceId,
|
|
57
|
+
});
|
|
58
|
+
if (eagerLoadUserPermissions) {
|
|
59
|
+
if (customPermissionLoader) {
|
|
60
|
+
await userObject.useCustomPermissionLoader(customPermissionLoader);
|
|
62
61
|
}
|
|
63
|
-
|
|
64
|
-
await userObject.
|
|
62
|
+
else {
|
|
63
|
+
await userObject.getUserPermissions();
|
|
65
64
|
}
|
|
66
65
|
}
|
|
66
|
+
if (eagerLoadUserPermissionsLegacy) {
|
|
67
|
+
await userObject.getUserPermissionsLegacy();
|
|
68
|
+
}
|
|
67
69
|
req.user = userObject;
|
|
68
70
|
trace.context.set('userObject', userObject);
|
|
69
71
|
// Added in order to support outbreak.
|
|
@@ -111,7 +113,12 @@ const middlewareWithDecode = (options = {}) => async (req, res, next) => {
|
|
|
111
113
|
if (userId) {
|
|
112
114
|
req.headers['X-AF-USER-ID'] = userId;
|
|
113
115
|
}
|
|
114
|
-
const
|
|
116
|
+
const { fleetId, businessModelId, demandSourceId, } = req.query;
|
|
117
|
+
const userObject = new ApiUser_1.default(userId, decoded?.user?.accountType, undefined, {
|
|
118
|
+
fleetId,
|
|
119
|
+
businessModelId,
|
|
120
|
+
demandSourceId,
|
|
121
|
+
});
|
|
115
122
|
if (eagerLoadUserPermissions) {
|
|
116
123
|
await userObject.getUserPermissions();
|
|
117
124
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/zehut",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0-beta.0",
|
|
4
4
|
"description": "manage user's identity",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"moment": "^2.29.1",
|
|
34
34
|
"nock": "^13.2.9",
|
|
35
35
|
"node-cache": "^5.1.2",
|
|
36
|
+
"object-hash": "^3.0.0",
|
|
36
37
|
"supertest": "^6.2.4",
|
|
37
38
|
"uuid": "^8.3.2"
|
|
38
39
|
},
|