@autofleet/zehut 1.7.2 → 1.7.3-beta

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/services.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export declare const IdentityNetwork: any;
2
+ export declare const AutofleetApiNetwork: any;
package/lib/services.js CHANGED
@@ -3,9 +3,10 @@ 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.IdentityNetwork = void 0;
6
+ exports.AutofleetApiNetwork = exports.IdentityNetwork = void 0;
7
7
  const network_1 = __importDefault(require("@autofleet/network"));
8
8
  const CACHE_LIFETIME_IN_SEC = 10;
9
+ const apiGwUrl = process.env.API_GATEWAY_URL || 'https://api.autofleet.io';
9
10
  // eslint-disable-next-line import/prefer-default-export
10
11
  exports.IdentityNetwork = new network_1.default({
11
12
  serviceName: 'IDENTITY_MS',
@@ -15,3 +16,12 @@ exports.IdentityNetwork = new network_1.default({
15
16
  maxAge: CACHE_LIFETIME_IN_SEC * 1000,
16
17
  } : undefined,
17
18
  });
19
+ exports.AutofleetApiNetwork = new network_1.default({
20
+ baseURL: apiGwUrl,
21
+ serviceUrl: apiGwUrl,
22
+ retries: 3,
23
+ retryCondition: () => true,
24
+ cache: process.env.NODE_ENV !== 'test' ? {
25
+ maxAge: CACHE_LIFETIME_IN_SEC * 1000,
26
+ } : undefined,
27
+ });
@@ -96,7 +96,7 @@ class ApiUser {
96
96
  }
97
97
  getUserAppPermissions(appId) {
98
98
  return __awaiter(this, void 0, void 0, function* () {
99
- if (!this.id) {
99
+ if (!this.id || appId) {
100
100
  return;
101
101
  }
102
102
  const currentAppPermission = this.appPermission[appId];
@@ -108,7 +108,7 @@ class ApiUser {
108
108
  this.appPermission[appId] = cachedResult;
109
109
  return cachedResult;
110
110
  }
111
- const { data } = yield services_1.IdentityNetwork.post(`/api/v1/apps/${appId}/get-user-payload`, {
111
+ const { data } = yield services_1.AutofleetApiNetwork.post(`/api/v1/apps/${appId}/get-user-payload`, {
112
112
  userId: this.id,
113
113
  });
114
114
  userCache.set(this.id, data);
@@ -13,6 +13,11 @@ export declare const middlewareWithDecode: (options?: {
13
13
  clientSecret?: string;
14
14
  returnErrorIfNoToken?: boolean;
15
15
  }) => (req: any, res: any, next: any) => Promise<void>;
16
+ export declare const appMiddleware: (options: {
17
+ appId: string;
18
+ clientSecret: string;
19
+ appSecret: string;
20
+ }) => (req: any, res: any, next: any) => Promise<void>;
16
21
  export declare const eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<any>;
17
22
  export declare const getDecodedBearer: (req: any) => any;
18
23
  export default ApiUser;
package/lib/user/index.js CHANGED
@@ -12,7 +12,7 @@ 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.getDecodedBearer = exports.eagerLoadPermissionsMiddleware = exports.middlewareWithDecode = exports.middleware = void 0;
15
+ exports.getDecodedBearer = exports.eagerLoadPermissionsMiddleware = exports.appMiddleware = exports.middlewareWithDecode = exports.middleware = void 0;
16
16
  const jsonwebtoken_1 = require("jsonwebtoken");
17
17
  const ApiUser_1 = __importDefault(require("./ApiUser"));
18
18
  const utils_1 = require("../utils");
@@ -108,6 +108,54 @@ exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(v
108
108
  // eslint-disable-next-line consistent-return
109
109
  return next();
110
110
  });
111
+ exports.appMiddleware = (options) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
112
+ var _b;
113
+ const { appId, clientSecret, appSecret, } = options;
114
+ const trace = tracer_1.newTrace('userPayload');
115
+ let decoded;
116
+ if (!req.headers.authorization) {
117
+ res.status(401);
118
+ return res.json({
119
+ errors: ['No token provided'],
120
+ });
121
+ }
122
+ try {
123
+ decoded = utils_1.decodeBearer(req.headers.authorization, appSecret);
124
+ }
125
+ catch (e) {
126
+ if (e instanceof jsonwebtoken_1.TokenExpiredError) {
127
+ res.status(401);
128
+ return res.json({
129
+ errors: ['Access token expired'],
130
+ });
131
+ }
132
+ if (e instanceof jsonwebtoken_1.JsonWebTokenError) {
133
+ res.status(400);
134
+ return res.json({
135
+ errors: [e.message],
136
+ });
137
+ }
138
+ res.status(500);
139
+ return res.json({
140
+ errors: ['Server error while parsing token'],
141
+ });
142
+ }
143
+ const userId = (_b = decoded === null || decoded === void 0 ? void 0 : decoded.user) === null || _b === void 0 ? void 0 : _b.id;
144
+ if (userId) {
145
+ req.headers['X-AF-USER-ID'] = userId;
146
+ }
147
+ const userObject = new ApiUser_1.default(userId);
148
+ if (appId && clientSecret) {
149
+ req.headers['x-autofleet-apps-secret'] = clientSecret;
150
+ // Won't work until we find a better solution for identity ms
151
+ yield userObject.getUserAppPermissions(appId);
152
+ }
153
+ req.user = userObject;
154
+ trace.context.set('userObject', userObject);
155
+ // Added in order to support outbreak.
156
+ req.headers['x-af-user-permissions'] = userObject;
157
+ return next();
158
+ });
111
159
  exports.eagerLoadPermissionsMiddleware = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
112
160
  yield req.user.getUserPermissions();
113
161
  return next();
package/lib/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export declare const getAuthFromBearer: (bearer: string) => string;
2
- export declare const decodeBearer: (bearer: string) => any;
2
+ export declare const decodeBearer: (bearer: string, appSecret?: string) => any;
3
3
  export declare const parsePermissions: (contextId: any, decodedToken: any) => any;
4
4
  export declare const getEntitiesFromContext: (contextId: string, decodedToken: any) => any;
5
5
  export declare const getContextAttributes: (contextId: string, decodedToken: any) => any;
package/lib/utils.js CHANGED
@@ -30,9 +30,9 @@ const CONTEXT_MAP_PROPS = {
30
30
  demand: 'demandSources',
31
31
  };
32
32
  exports.getAuthFromBearer = (bearer) => bearer.replace('Bearer ', '');
33
- exports.decodeBearer = (bearer) => {
34
- const token = bearer.replace('Bearer ', '');
35
- const decoded = jwt.verify(token, secret_getter_1.getTokenSecret(token));
33
+ exports.decodeBearer = (bearer, appSecret) => {
34
+ const token = exports.getAuthFromBearer(bearer);
35
+ const decoded = jwt.verify(token, appSecret || secret_getter_1.getTokenSecret(token));
36
36
  return decoded;
37
37
  };
38
38
  exports.parsePermissions = (contextId, decodedToken) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "1.7.2",
3
+ "version": "1.7.3-beta",
4
4
  "description": "manage user's identity",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",