@autofleet/zehut 3.0.11-beta.1 → 3.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.
@@ -3,6 +3,7 @@ interface EntityPermissions {
3
3
  [key: string]: string[];
4
4
  }
5
5
  export declare const ELEVATED_PERMISSIONS_HEADER = "x-af-elevated-permissions";
6
+ export declare const CONTEXTS_IDS_HEADER = "x-af-context-ids";
6
7
  export interface UserPayload {
7
8
  businessModels: EntityPermissions;
8
9
  fleets: EntityPermissions;
@@ -31,7 +32,8 @@ export default class ApiUser {
31
32
  };
32
33
  emptyUser: boolean;
33
34
  accountType: AccountType | undefined;
34
- constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload);
35
+ contextIds?: string[];
36
+ constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload, contextIds?: string[]);
35
37
  getUserPermissions(): Promise<UserPayload>;
36
38
  useCustomPermissionLoader(customPermissionLoader: any): Promise<UserPayload>;
37
39
  get businessModels(): string[] | undefined;
@@ -3,21 +3,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ELEVATED_PERMISSIONS_HEADER = void 0;
6
+ exports.CONTEXTS_IDS_HEADER = 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");
12
13
  const services_1 = require("../services");
13
14
  exports.ELEVATED_PERMISSIONS_HEADER = 'x-af-elevated-permissions';
15
+ exports.CONTEXTS_IDS_HEADER = 'x-af-context-ids';
14
16
  const userCache = new node_cache_1.default({ stdTTL: 10 });
15
17
  class ApiUser {
16
- constructor(id, accountType, elevatedPermissions) {
18
+ constructor(id, accountType, elevatedPermissions, contextIds) {
17
19
  this.id = id;
18
20
  this.emptyUser = !!id;
19
21
  this.appPermission = {};
20
22
  this.privateElevatedPermissionsHash = new Map();
23
+ this.contextIds = contextIds;
21
24
  if (elevatedPermissions) {
22
25
  this.privateElevatedPermissionsHash.set('initial', elevatedPermissions);
23
26
  }
@@ -32,10 +35,14 @@ class ApiUser {
32
35
  if (this.privatePermissions) {
33
36
  return this.privatePermissions;
34
37
  }
35
- let data = userCache.get(this.id);
38
+ const cacheKey = (0, object_hash_1.default)({
39
+ id: this.id,
40
+ contextIds: this.contextIds,
41
+ });
42
+ let data = userCache.get(cacheKey);
36
43
  if (!data) {
37
- ({ data } = await services_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`));
38
- userCache.set(this.id, data);
44
+ ({ data } = await services_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`, { contextIds: this.contextIds }));
45
+ userCache.set(cacheKey, data);
39
46
  }
40
47
  this.accountType = data.accountType;
41
48
  this.privatePermissions = data;
@@ -48,13 +55,14 @@ class ApiUser {
48
55
  if (this.privatePermissions) {
49
56
  return this.privatePermissions;
50
57
  }
51
- const cachedResult = userCache.get(this.id);
58
+ const cacheKey = this.id;
59
+ const cachedResult = userCache.get(cacheKey);
52
60
  if (cachedResult) {
53
61
  this.privatePermissions = cachedResult;
54
62
  return cachedResult;
55
63
  }
56
64
  const data = await customPermissionLoader(this.id);
57
- userCache.set(this.id, data);
65
+ userCache.set(cacheKey, data);
58
66
  this.privatePermissions = data;
59
67
  return this.privatePermissions;
60
68
  }
@@ -139,7 +147,8 @@ class ApiUser {
139
147
  if (currentAppPermission) {
140
148
  return currentAppPermission;
141
149
  }
142
- const cachedResult = userCache.get(`${this.id}:${appId}`);
150
+ const cacheKey = `${this.id}:${appId}`;
151
+ const cachedResult = userCache.get(cacheKey);
143
152
  if (cachedResult) {
144
153
  this.appPermission[appId] = cachedResult;
145
154
  return cachedResult;
@@ -151,7 +160,7 @@ class ApiUser {
151
160
  'x-autofleet-apps-secret': clientSecret,
152
161
  },
153
162
  });
154
- userCache.set(this.id, data);
163
+ userCache.set(cacheKey, data);
155
164
  this.appPermission[appId] = data;
156
165
  return this.appPermission[appId];
157
166
  }
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 contextIds = req.headers?.['x-af-contexts-ids']?.split(',');
46
46
  const trace = (0, tracer_1.newTrace)('userPayload');
47
47
  if (!userId) {
48
48
  return next();
@@ -50,20 +50,18 @@ 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
- if (userId !== ANONYMOUS_USER) {
55
- if (eagerLoadUserPermissions) {
56
- if (customPermissionLoader) {
57
- await userObject.useCustomPermissionLoader(customPermissionLoader);
58
- }
59
- else {
60
- await userObject.getUserPermissions();
61
- }
53
+ const userObject = new ApiUser_1.default(userId, 'user', elevatedPermissionsFromHeader, contextIds);
54
+ if (eagerLoadUserPermissions) {
55
+ if (customPermissionLoader) {
56
+ await userObject.useCustomPermissionLoader(customPermissionLoader);
62
57
  }
63
- if (eagerLoadUserPermissionsLegacy) {
64
- await userObject.getUserPermissionsLegacy();
58
+ else {
59
+ await userObject.getUserPermissions();
65
60
  }
66
61
  }
62
+ if (eagerLoadUserPermissionsLegacy) {
63
+ await userObject.getUserPermissionsLegacy();
64
+ }
67
65
  req.user = userObject;
68
66
  trace.context.set('userObject', userObject);
69
67
  // Added in order to support outbreak.
@@ -111,7 +109,8 @@ const middlewareWithDecode = (options = {}) => async (req, res, next) => {
111
109
  if (userId) {
112
110
  req.headers['X-AF-USER-ID'] = userId;
113
111
  }
114
- const userObject = new ApiUser_1.default(userId, decoded?.user?.accountType);
112
+ const contextIds = req.headers?.['x-af-contexts-ids']?.split(',');
113
+ const userObject = new ApiUser_1.default(userId, decoded?.user?.accountType, undefined, contextIds);
115
114
  if (eagerLoadUserPermissions) {
116
115
  await userObject.getUserPermissions();
117
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "3.0.11-beta.1",
3
+ "version": "3.1.0-beta.1",
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
  },