@autofleet/zehut 1.7.0 → 1.7.2

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 CHANGED
@@ -10,10 +10,15 @@ declare const _default: {
10
10
  eagerLoadUserPermissions?: boolean;
11
11
  eagerLoadUserPermissionsLegacy?: boolean;
12
12
  customPermissionLoader?: import("./user/ApiUser").CustomPermissionLoader;
13
+ appId?: string;
14
+ clientSecret?: string;
13
15
  }) => (req: any, res: any, next: any) => Promise<any>;
14
16
  middlewareWithDecode: (options?: {
15
17
  eagerLoadUserPermissions?: boolean;
16
18
  eagerLoadUserPermissionsLegacy?: boolean;
19
+ appId?: string;
20
+ clientSecret?: string;
21
+ returnErrorIfNoToken?: boolean;
17
22
  }) => (req: any, res: any, next: any) => Promise<void>;
18
23
  eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<any>;
19
24
  getCurrentPayload: () => any;
@@ -13,6 +13,9 @@ export default class ApiUser {
13
13
  id: string | undefined;
14
14
  privatePermissions: UserPayload | undefined;
15
15
  privatePermissionsLegacy: any;
16
+ appPermission: {
17
+ [key: string]: any;
18
+ };
16
19
  emptyUser: boolean;
17
20
  constructor(id?: string);
18
21
  getUserPermissions(): Promise<UserPayload>;
@@ -24,5 +27,6 @@ export default class ApiUser {
24
27
  get permissions(): UserPayload | undefined;
25
28
  getUserPermissionsLegacy(): Promise<any>;
26
29
  get permissionsLegacy(): any;
30
+ getUserAppPermissions(appId: any): Promise<any>;
27
31
  }
28
32
  export {};
@@ -20,6 +20,7 @@ class ApiUser {
20
20
  constructor(id) {
21
21
  this.id = id;
22
22
  this.emptyUser = !!id;
23
+ this.appPermission = {};
23
24
  }
24
25
  getUserPermissions() {
25
26
  return __awaiter(this, void 0, void 0, function* () {
@@ -48,7 +49,7 @@ class ApiUser {
48
49
  return cachedResult;
49
50
  }
50
51
  const data = yield customPermissionLoader(this.id);
51
- userCache.set(this.id, data, data);
52
+ userCache.set(this.id, data);
52
53
  this.privatePermissions = data;
53
54
  return this.privatePermissions;
54
55
  });
@@ -93,5 +94,27 @@ class ApiUser {
93
94
  }
94
95
  return this.privatePermissionsLegacy;
95
96
  }
97
+ getUserAppPermissions(appId) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ if (!this.id) {
100
+ return;
101
+ }
102
+ const currentAppPermission = this.appPermission[appId];
103
+ if (currentAppPermission) {
104
+ return currentAppPermission;
105
+ }
106
+ const cachedResult = userCache.get(`${this.id}:${appId}`);
107
+ if (cachedResult) {
108
+ this.appPermission[appId] = cachedResult;
109
+ return cachedResult;
110
+ }
111
+ const { data } = yield services_1.IdentityNetwork.post(`/api/v1/apps/${appId}/get-user-payload`, {
112
+ userId: this.id,
113
+ });
114
+ userCache.set(this.id, data);
115
+ this.appPermission[appId] = data;
116
+ return this.appPermission[appId];
117
+ });
118
+ }
96
119
  }
97
120
  exports.default = ApiUser;
@@ -3,10 +3,15 @@ export declare const middleware: (options?: {
3
3
  eagerLoadUserPermissions?: boolean;
4
4
  eagerLoadUserPermissionsLegacy?: boolean;
5
5
  customPermissionLoader?: CustomPermissionLoader;
6
+ appId?: string;
7
+ clientSecret?: string;
6
8
  }) => (req: any, res: any, next: any) => Promise<any>;
7
9
  export declare const middlewareWithDecode: (options?: {
8
10
  eagerLoadUserPermissions?: boolean;
9
11
  eagerLoadUserPermissionsLegacy?: boolean;
12
+ appId?: string;
13
+ clientSecret?: string;
14
+ returnErrorIfNoToken?: boolean;
10
15
  }) => (req: any, res: any, next: any) => Promise<void>;
11
16
  export declare const eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<any>;
12
17
  export declare const getDecodedBearer: (req: any) => any;
package/lib/user/index.js CHANGED
@@ -18,6 +18,7 @@ const ApiUser_1 = __importDefault(require("./ApiUser"));
18
18
  const utils_1 = require("../utils");
19
19
  const tracer_1 = require("../tracer");
20
20
  exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
21
+ const { eagerLoadUserPermissions, eagerLoadUserPermissionsLegacy, customPermissionLoader, appId, clientSecret, } = options;
21
22
  const userId = req.headers['x-af-user-id'];
22
23
  const trace = tracer_1.newTrace('userPayload');
23
24
  if (!userId) {
@@ -25,17 +26,21 @@ exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, voi
25
26
  return next();
26
27
  }
27
28
  const userObject = new ApiUser_1.default(userId);
28
- if (options.eagerLoadUserPermissions) {
29
- if (options.customPermissionLoader) {
30
- yield userObject.useCustomPermissionLoader(options.customPermissionLoader);
29
+ if (eagerLoadUserPermissions) {
30
+ if (customPermissionLoader) {
31
+ yield userObject.useCustomPermissionLoader(customPermissionLoader);
31
32
  }
32
33
  else {
33
34
  yield userObject.getUserPermissions();
34
35
  }
35
36
  }
36
- if (options.eagerLoadUserPermissionsLegacy) {
37
+ if (eagerLoadUserPermissionsLegacy) {
37
38
  yield userObject.getUserPermissionsLegacy();
38
39
  }
40
+ if (appId && clientSecret) {
41
+ req.headers['x-autofleet-apps-secret'] = clientSecret;
42
+ yield userObject.getUserAppPermissions(appId);
43
+ }
39
44
  req.user = userObject;
40
45
  trace.context.set('userObject', userObject);
41
46
  // Added in order to support outbreak.
@@ -44,6 +49,7 @@ exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, voi
44
49
  });
45
50
  exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
46
51
  var _a;
52
+ const { eagerLoadUserPermissions, eagerLoadUserPermissionsLegacy, appId, clientSecret, returnErrorIfNoToken, } = options;
47
53
  const trace = tracer_1.newTrace('userPayload');
48
54
  let decoded;
49
55
  if (req.headers.authorization) {
@@ -76,17 +82,28 @@ exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(v
76
82
  req.headers['X-AF-USER-ID'] = userId;
77
83
  }
78
84
  const userObject = new ApiUser_1.default(userId);
79
- if (options.eagerLoadUserPermissions) {
85
+ if (eagerLoadUserPermissions) {
80
86
  yield userObject.getUserPermissions();
81
87
  }
82
- if (options.eagerLoadUserPermissionsLegacy) {
88
+ if (eagerLoadUserPermissionsLegacy) {
83
89
  yield userObject.getUserPermissionsLegacy();
84
90
  }
91
+ if (appId && clientSecret) {
92
+ req.headers['x-autofleet-apps-secret'] = clientSecret;
93
+ yield userObject.getUserAppPermissions(appId);
94
+ }
85
95
  req.user = userObject;
86
96
  trace.context.set('userObject', userObject);
87
97
  // Added in order to support outbreak.
88
98
  req.headers['x-af-user-permissions'] = userObject;
89
99
  }
100
+ else if (returnErrorIfNoToken) {
101
+ res.status(401);
102
+ // eslint-disable-next-line consistent-return
103
+ return res.json({
104
+ errors: ['No token provided'],
105
+ });
106
+ }
90
107
  trace.context.set('userObject', {});
91
108
  // eslint-disable-next-line consistent-return
92
109
  return next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "1.7.0",
3
+ "version": "1.7.2",
4
4
  "description": "manage user's identity",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",