@autofleet/zehut 1.8.0-beta9 → 1.8.1-beta0
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/README.md +4 -0
- package/lib/app-auth.d.ts +2 -0
- package/lib/app-auth.js +27 -0
- package/lib/exceptions/appDoesNotExist.d.ts +3 -0
- package/lib/exceptions/appDoesNotExist.js +5 -0
- package/lib/index.d.ts +0 -2
- package/lib/index.js +5 -2
- package/lib/user/ApiUser.js +1 -1
- package/lib/user/index.d.ts +0 -2
- package/lib/user/index.js +20 -5
- package/package.json +2 -2
package/README.md
CHANGED
package/lib/app-auth.js
ADDED
|
@@ -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
|
@@ -30,8 +30,6 @@ declare const _default: {
|
|
|
30
30
|
UnauthorizedAccessError: typeof UnauthorizedAccessError;
|
|
31
31
|
appMiddleware: (options: {
|
|
32
32
|
appId: string;
|
|
33
|
-
clientSecret: string;
|
|
34
|
-
appSecret: string;
|
|
35
33
|
}) => (req: any, res: any, next: any) => Promise<void>;
|
|
36
34
|
};
|
|
37
35
|
export default _default;
|
package/lib/index.js
CHANGED
|
@@ -18,9 +18,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
18
18
|
__setModuleDefault(result, mod);
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
21
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
25
|
exports.appMiddleware = exports.UnauthorizedAccessError = exports.getTokenSecret = exports.getRefreshTokenSecret = exports.getUser = exports.isUserExist = exports.checkDemandSourcePermission = exports.checkBusinessModelPermission = exports.checkFleetPermission = exports.getDecodedBearer = exports.getCurrentPayload = exports.eagerLoadPermissionsMiddleware = exports.middlewareWithDecode = exports.middleware = exports.User = exports.enableTracing = void 0;
|
|
23
|
-
const outbreak_1 = require("@autofleet/outbreak");
|
|
26
|
+
const outbreak_1 = __importDefault(require("@autofleet/outbreak"));
|
|
24
27
|
const user_1 = __importStar(require("./user"));
|
|
25
28
|
exports.User = user_1.default;
|
|
26
29
|
Object.defineProperty(exports, "middleware", { enumerable: true, get: function () { return user_1.middleware; } });
|
|
@@ -43,7 +46,7 @@ Object.defineProperty(exports, "getTokenSecret", { enumerable: true, get: functi
|
|
|
43
46
|
const getCurrentPayload = tracer_1.getCurrentTrace;
|
|
44
47
|
exports.getCurrentPayload = getCurrentPayload;
|
|
45
48
|
const enableTracing = ({ outbreakOptions = {} } = {}) => {
|
|
46
|
-
outbreak_1.
|
|
49
|
+
outbreak_1.default(Object.assign({ headersPrefix: 'x-af' }, outbreakOptions));
|
|
47
50
|
tracer_1.enable();
|
|
48
51
|
};
|
|
49
52
|
exports.enableTracing = enableTracing;
|
package/lib/user/ApiUser.js
CHANGED
|
@@ -89,7 +89,7 @@ class ApiUser {
|
|
|
89
89
|
if (this.elevationCallsCounter > 1) {
|
|
90
90
|
throw new Error('Maximum one elvation call in parallel is allowed');
|
|
91
91
|
}
|
|
92
|
-
const currentUserTrace = outbreak_1.
|
|
92
|
+
const currentUserTrace = outbreak_1.getCurrentContext();
|
|
93
93
|
if (!currentUserTrace) {
|
|
94
94
|
throw new Error('Cannot find current user cross services trace');
|
|
95
95
|
}
|
package/lib/user/index.d.ts
CHANGED
|
@@ -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
|
@@ -27,12 +27,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
27
27
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
28
|
});
|
|
29
29
|
};
|
|
30
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
|
+
};
|
|
30
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
34
|
exports.getDecodedBearer = exports.eagerLoadPermissionsMiddleware = exports.appMiddleware = exports.middlewareWithDecode = exports.middleware = void 0;
|
|
32
35
|
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
33
36
|
const ApiUser_1 = __importStar(require("./ApiUser"));
|
|
34
|
-
const utils_1 = require("../utils");
|
|
35
37
|
const tracer_1 = require("../tracer");
|
|
38
|
+
const app_auth_1 = require("../app-auth");
|
|
39
|
+
const appDoesNotExist_1 = __importDefault(require("../exceptions/appDoesNotExist"));
|
|
40
|
+
const utils_1 = require("../utils");
|
|
36
41
|
exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
42
|
const { eagerLoadUserPermissions, eagerLoadUserPermissionsLegacy, customPermissionLoader, } = options;
|
|
38
43
|
const userId = req.headers['x-af-user-id'];
|
|
@@ -68,7 +73,7 @@ exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(v
|
|
|
68
73
|
let decoded;
|
|
69
74
|
if (req.headers.authorization) {
|
|
70
75
|
try {
|
|
71
|
-
decoded = utils_1.decodeBearer(req.headers.authorization);
|
|
76
|
+
decoded = yield utils_1.decodeBearer(req.headers.authorization);
|
|
72
77
|
}
|
|
73
78
|
catch (e) {
|
|
74
79
|
if (e instanceof jsonwebtoken_1.TokenExpiredError) {
|
|
@@ -118,7 +123,7 @@ exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(v
|
|
|
118
123
|
return next();
|
|
119
124
|
});
|
|
120
125
|
exports.appMiddleware = (options) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
121
|
-
const { appId,
|
|
126
|
+
const { appId, } = options;
|
|
122
127
|
const trace = tracer_1.newTrace('userPayload');
|
|
123
128
|
let decoded;
|
|
124
129
|
if (!req.headers.authorization) {
|
|
@@ -128,7 +133,10 @@ exports.appMiddleware = (options) => (req, res, next) => __awaiter(void 0, void
|
|
|
128
133
|
});
|
|
129
134
|
}
|
|
130
135
|
try {
|
|
131
|
-
decoded =
|
|
136
|
+
decoded = yield app_auth_1.decodeAppBearer(req.headers.authorization, appId);
|
|
137
|
+
if (!decoded) {
|
|
138
|
+
throw new appDoesNotExist_1.default();
|
|
139
|
+
}
|
|
132
140
|
}
|
|
133
141
|
catch (e) {
|
|
134
142
|
if (e instanceof jsonwebtoken_1.TokenExpiredError) {
|
|
@@ -143,6 +151,12 @@ exports.appMiddleware = (options) => (req, res, next) => __awaiter(void 0, void
|
|
|
143
151
|
errors: [e.message],
|
|
144
152
|
});
|
|
145
153
|
}
|
|
154
|
+
if (e instanceof appDoesNotExist_1.default) {
|
|
155
|
+
res.status(400);
|
|
156
|
+
return res.json({
|
|
157
|
+
errors: [e.message],
|
|
158
|
+
});
|
|
159
|
+
}
|
|
146
160
|
res.status(500);
|
|
147
161
|
return res.json({
|
|
148
162
|
errors: ['Server error while parsing token'],
|
|
@@ -153,7 +167,8 @@ exports.appMiddleware = (options) => (req, res, next) => __awaiter(void 0, void
|
|
|
153
167
|
req.headers['X-AF-USER-ID'] = userId;
|
|
154
168
|
}
|
|
155
169
|
const userObject = new ApiUser_1.default(userId);
|
|
156
|
-
if (appId
|
|
170
|
+
if (appId) {
|
|
171
|
+
const clientSecret = yield app_auth_1.getClientSecret(appId);
|
|
157
172
|
req.headers['x-autofleet-apps-secret'] = clientSecret;
|
|
158
173
|
// Won't work until we find a better solution for identity ms
|
|
159
174
|
yield userObject.getUserAppPermissions(appId, clientSecret);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/zehut",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.1-beta0",
|
|
4
4
|
"description": "manage user's identity",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"homepage": "https://github.com/Autofleet/zehut",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@autofleet/network": "^1.4.2",
|
|
31
|
-
"@autofleet/outbreak": "0.1
|
|
31
|
+
"@autofleet/outbreak": "0.2.1",
|
|
32
32
|
"@types/jest": "^22.0.0",
|
|
33
33
|
"axios": "^0.27.2",
|
|
34
34
|
"express": "^4.18.1",
|