@autofleet/zehut 1.7.5-beta.0 → 1.7.6-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.
@@ -0,0 +1,2 @@
1
+ export declare const decodeAppBearer: (bearer: string, appId: string) => Promise<any>;
2
+ export declare const getClientSecret: (appId: string) => Promise<any>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getClientSecret = exports.decodeAppBearer = void 0;
16
+ const network_1 = __importDefault(require("@autofleet/network"));
17
+ const integrationMsNetwork = new network_1.default({
18
+ serviceName: 'INTEGRATION_MS',
19
+ });
20
+ exports.decodeAppBearer = (bearer, appId) => __awaiter(void 0, void 0, void 0, function* () {
21
+ const { data: decoded } = yield integrationMsNetwork.post('/api/v1/auth', { bearer, appId });
22
+ return decoded;
23
+ });
24
+ exports.getClientSecret = (appId) => __awaiter(void 0, void 0, void 0, function* () {
25
+ const { data: secret } = yield integrationMsNetwork.get(`/api/v1/auth/client-secret/${appId}`);
26
+ return secret;
27
+ });
package/lib/index.d.ts CHANGED
@@ -27,8 +27,6 @@ declare const _default: {
27
27
  UnauthorizedAccessError: typeof UnauthorizedAccessError;
28
28
  appMiddleware: (options: {
29
29
  appId: string;
30
- clientSecret: string;
31
- appSecret: string;
32
30
  }) => (req: any, res: any, next: any) => Promise<void>;
33
31
  };
34
32
  export default _default;
@@ -11,8 +11,6 @@ export declare const middlewareWithDecode: (options?: {
11
11
  }) => (req: any, res: any, next: any) => Promise<void>;
12
12
  export declare const appMiddleware: (options: {
13
13
  appId: string;
14
- clientSecret: string;
15
- appSecret: string;
16
14
  }) => (req: any, res: any, next: any) => Promise<void>;
17
15
  export declare const eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<any>;
18
16
  export declare const getDecodedBearer: (req: any) => any;
package/lib/user/index.js CHANGED
@@ -17,6 +17,7 @@ const jsonwebtoken_1 = require("jsonwebtoken");
17
17
  const ApiUser_1 = __importDefault(require("./ApiUser"));
18
18
  const utils_1 = require("../utils");
19
19
  const tracer_1 = require("../tracer");
20
+ const app_auth_1 = require("../app-auth");
20
21
  exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
21
22
  const { eagerLoadUserPermissions, eagerLoadUserPermissionsLegacy, customPermissionLoader, } = options;
22
23
  const userId = req.headers['x-af-user-id'];
@@ -50,7 +51,7 @@ exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(v
50
51
  let decoded;
51
52
  if (req.headers.authorization) {
52
53
  try {
53
- decoded = utils_1.decodeBearer(req.headers.authorization);
54
+ decoded = yield utils_1.decodeBearer(req.headers.authorization);
54
55
  }
55
56
  catch (e) {
56
57
  if (e instanceof jsonwebtoken_1.TokenExpiredError) {
@@ -101,7 +102,7 @@ exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(v
101
102
  return next();
102
103
  });
103
104
  exports.appMiddleware = (options) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
104
- const { appId, clientSecret, appSecret, } = options;
105
+ const { appId, } = options;
105
106
  const trace = tracer_1.newTrace('userPayload');
106
107
  let decoded;
107
108
  if (!req.headers.authorization) {
@@ -111,7 +112,10 @@ exports.appMiddleware = (options) => (req, res, next) => __awaiter(void 0, void
111
112
  });
112
113
  }
113
114
  try {
114
- decoded = utils_1.decodeBearer(req.headers.authorization, appSecret);
115
+ decoded = yield app_auth_1.decodeAppBearer(req.headers.authorization, appId);
116
+ if (!decoded) {
117
+ throw new Error('app does not exists');
118
+ }
115
119
  }
116
120
  catch (e) {
117
121
  if (e instanceof jsonwebtoken_1.TokenExpiredError) {
@@ -136,7 +140,8 @@ exports.appMiddleware = (options) => (req, res, next) => __awaiter(void 0, void
136
140
  req.headers['X-AF-USER-ID'] = userId;
137
141
  }
138
142
  const userObject = new ApiUser_1.default(userId);
139
- if (appId && clientSecret) {
143
+ if (appId) {
144
+ const clientSecret = yield app_auth_1.getClientSecret(appId);
140
145
  req.headers['x-autofleet-apps-secret'] = clientSecret;
141
146
  // Won't work until we find a better solution for identity ms
142
147
  yield userObject.getUserAppPermissions(appId, clientSecret);
package/lib/utils.js CHANGED
@@ -1,22 +1,38 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
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;
4
20
  };
5
21
  Object.defineProperty(exports, "__esModule", { value: true });
6
22
  exports.getContextAttributes = exports.getEntitiesFromContext = exports.parsePermissions = exports.decodeBearer = exports.getAuthFromBearer = void 0;
7
- const network_1 = __importDefault(require("@autofleet/network"));
23
+ /* eslint-disable prefer-destructuring */
24
+ const jwt = __importStar(require("jsonwebtoken"));
25
+ const secret_getter_1 = require("./secret-getter");
8
26
  const CONTEXT_PROPS = ['fleetId', 'businessModelId', 'demandSourceId'];
9
27
  const CONTEXT_MAP_PROPS = {
10
28
  fleet: 'fleets',
11
29
  business: 'businessModels',
12
30
  demand: 'demandSources',
13
31
  };
14
- const integrationMsNetwork = new network_1.default({
15
- serviceName: 'INTEGRATION_MS',
16
- });
17
32
  exports.getAuthFromBearer = (bearer) => bearer.replace('Bearer ', '');
18
33
  exports.decodeBearer = (bearer, appSecret) => {
19
- const decoded = yield integrationMsNetwork;
34
+ const token = exports.getAuthFromBearer(bearer);
35
+ const decoded = jwt.verify(token, appSecret || secret_getter_1.getTokenSecret(token));
20
36
  return decoded;
21
37
  };
22
38
  exports.parsePermissions = (contextId, decodedToken) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "1.7.5-beta.0",
3
+ "version": "1.7.6-beta.1",
4
4
  "description": "manage user's identity",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",