@autofleet/zehut 3.1.2 → 3.2.0
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 +2 -76
- package/lib/check-permission.d.ts +3 -3
- package/lib/check-permission.js +7 -26
- package/lib/errors.d.ts +3 -3
- package/lib/errors.js +1 -1
- package/lib/exceptions/appDoesNotExist.d.ts +2 -1
- package/lib/exceptions/appDoesNotExist.js +5 -0
- package/lib/index.d.ts +16 -11
- package/lib/index.js +5 -3
- package/lib/secret-getter.d.ts +2 -2
- package/lib/services.d.ts +3 -2
- package/lib/tracer.js +2 -2
- package/lib/user/ApiUser.d.ts +9 -11
- package/lib/user/ApiUser.js +17 -18
- package/lib/user/index.d.ts +14 -6
- package/lib/user/index.js +58 -78
- package/lib/utils.d.ts +3 -0
- package/lib/utils.js +17 -13
- package/package.json +21 -21
- package/lib/secret-getter.test.d.ts +0 -1
- package/lib/secret-getter.test.js +0 -90
- package/lib/test-helpers/index.d.ts +0 -9
- package/lib/test-helpers/index.js +0 -35
- package/lib/user/api-user-flows.test.d.ts +0 -1
- package/lib/user/api-user-flows.test.js +0 -302
package/README.md
CHANGED
|
@@ -1,77 +1,3 @@
|
|
|
1
|
-
# AutoFleet
|
|
2
|
-
This respostory is made in order have as much of common code as we can.
|
|
1
|
+
# AutoFleet Zehut
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Make sure you have:
|
|
7
|
-
* Tests
|
|
8
|
-
* Docs
|
|
9
|
-
|
|
10
|
-
## Consts
|
|
11
|
-
|
|
12
|
-
Currently we suppurt:
|
|
13
|
-
```
|
|
14
|
-
{
|
|
15
|
-
'OK'
|
|
16
|
-
'ERROR'
|
|
17
|
-
'FAIL'
|
|
18
|
-
}
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Network
|
|
22
|
-
Server 2 Servers communication.
|
|
23
|
-
|
|
24
|
-
Implemented:
|
|
25
|
-
* Retriving service urls from environment
|
|
26
|
-
* Retry - Using https://github.com/softonic/axios-retry
|
|
27
|
-
* Caching - TBD
|
|
28
|
-
* Syntatic response for fail - TBD
|
|
29
|
-
* Circuit Breaking - TBD
|
|
30
|
-
|
|
31
|
-
The API is just like [axios](https://github.com/axios/axios) api but the creation of new instance **must** have either `serviceName` or `serviceUrl` in options.
|
|
32
|
-
|
|
33
|
-
In case `serviceName` used the constractor will look for an environment varible with the the name `<SERVICE_NAME>_SERVICE_HOST`.
|
|
34
|
-
|
|
35
|
-
For Example:
|
|
36
|
-
```
|
|
37
|
-
const { Network } = require('@autofleet/node-common');
|
|
38
|
-
|
|
39
|
-
n = new Network({ serviceName: 'TEST' });
|
|
40
|
-
|
|
41
|
-
n.get('/posts/1');
|
|
42
|
-
```
|
|
43
|
-
.env file:
|
|
44
|
-
```
|
|
45
|
-
RIDE_SERVICE_HOST=jsonplaceholder.typicode.com
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
To learn more [click here](https://blog.risingstack.com/designing-microservices-architecture-for-failure/).
|
|
49
|
-
|
|
50
|
-
## Settings
|
|
51
|
-
|
|
52
|
-
### Adding settings
|
|
53
|
-
For adding new setting you need to add it to the map.js file, please specify
|
|
54
|
-
* name - descriptive name
|
|
55
|
-
* description - few words about what it does + unit
|
|
56
|
-
* type - supportable types: 'number', 'string', 'json'
|
|
57
|
-
* defaultValue - default value
|
|
58
|
-
* context - 'security' and 'operation' will not show in the simulator configuration
|
|
59
|
-
|
|
60
|
-
See example.
|
|
61
|
-
|
|
62
|
-
## DeLorean
|
|
63
|
-
|
|
64
|
-
Use this model to mock time on server - more info TBD.
|
|
65
|
-
|
|
66
|
-
## Publish package
|
|
67
|
-
|
|
68
|
-
bump the version number in package.json
|
|
69
|
-
and run
|
|
70
|
-
```
|
|
71
|
-
npm publish
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
# Environment Variables
|
|
75
|
-
|
|
76
|
-
when using this package locally or outside autofleet-prod project you must set INTEGRATION_MS_SERVICE_HOST in .env file
|
|
77
|
-
# zehut
|
|
3
|
+
This package handles authorization and authentication for AutoFleet services.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import ApiUser from './user/ApiUser';
|
|
2
2
|
export declare const getUser: () => ApiUser | undefined;
|
|
3
3
|
export declare const isUserExist: () => string;
|
|
4
|
-
export declare const checkFleetPermission: (fleetId:
|
|
5
|
-
export declare const checkBusinessModelPermission: (businessModelId:
|
|
6
|
-
export declare const checkDemandSourcePermission: (demandSourceId:
|
|
4
|
+
export declare const checkFleetPermission: (fleetId: string) => boolean;
|
|
5
|
+
export declare const checkBusinessModelPermission: (businessModelId: string) => boolean;
|
|
6
|
+
export declare const checkDemandSourcePermission: (demandSourceId: string) => boolean;
|
package/lib/check-permission.js
CHANGED
|
@@ -2,34 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.checkDemandSourcePermission = exports.checkBusinessModelPermission = exports.checkFleetPermission = exports.isUserExist = exports.getUser = void 0;
|
|
4
4
|
const tracer_1 = require("./tracer");
|
|
5
|
-
const
|
|
5
|
+
const user_1 = require("./user");
|
|
6
|
+
const getUser = () => (0, tracer_1.getCurrentTrace)().context?.get(user_1.USER_OBJECT);
|
|
6
7
|
exports.getUser = getUser;
|
|
7
|
-
const isUserExist = () =>
|
|
8
|
-
const u = (0, exports.getUser)();
|
|
9
|
-
return u && u.id;
|
|
10
|
-
};
|
|
8
|
+
const isUserExist = () => (0, exports.getUser)()?.id;
|
|
11
9
|
exports.isUserExist = isUserExist;
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const user = (0, exports.getUser)();
|
|
15
|
-
return !user || Object.keys(user.permissions.fleets).includes(fleetId);
|
|
16
|
-
}
|
|
17
|
-
return true;
|
|
18
|
-
};
|
|
10
|
+
const checkUserPermissions = (entityId, entityType) => !(0, exports.isUserExist)() || Object.hasOwn((0, exports.getUser)().permissions[entityType], entityId);
|
|
11
|
+
const checkFleetPermission = (fleetId) => checkUserPermissions(fleetId, 'fleets');
|
|
19
12
|
exports.checkFleetPermission = checkFleetPermission;
|
|
20
|
-
const checkBusinessModelPermission = (businessModelId) =>
|
|
21
|
-
if ((0, exports.isUserExist)()) {
|
|
22
|
-
const user = (0, exports.getUser)();
|
|
23
|
-
return !user || Object.keys(user.permissions.businessModels).includes(businessModelId);
|
|
24
|
-
}
|
|
25
|
-
return true;
|
|
26
|
-
};
|
|
13
|
+
const checkBusinessModelPermission = (businessModelId) => checkUserPermissions(businessModelId, 'businessModels');
|
|
27
14
|
exports.checkBusinessModelPermission = checkBusinessModelPermission;
|
|
28
|
-
const checkDemandSourcePermission = (demandSourceId) =>
|
|
29
|
-
if ((0, exports.isUserExist)()) {
|
|
30
|
-
const user = (0, exports.getUser)();
|
|
31
|
-
return !user || Object.keys(user.permissions.demandSources).includes(demandSourceId);
|
|
32
|
-
}
|
|
33
|
-
return true;
|
|
34
|
-
};
|
|
15
|
+
const checkDemandSourcePermission = (demandSourceId) => checkUserPermissions(demandSourceId, 'demandSources');
|
|
35
16
|
exports.checkDemandSourcePermission = checkDemandSourcePermission;
|
package/lib/errors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import ApiUser from './user';
|
|
1
|
+
import type ApiUser from './user';
|
|
2
2
|
export declare class UnauthorizedAccessError extends Error {
|
|
3
|
-
user: ApiUser;
|
|
4
|
-
constructor(user?:
|
|
3
|
+
user: ApiUser | null;
|
|
4
|
+
constructor(user?: ApiUser | null, message?: string);
|
|
5
5
|
}
|
package/lib/errors.js
CHANGED
|
@@ -5,8 +5,8 @@ exports.UnauthorizedAccessError = void 0;
|
|
|
5
5
|
class UnauthorizedAccessError extends Error {
|
|
6
6
|
constructor(user = null, message = 'UnauthorizedAccessError') {
|
|
7
7
|
super(message);
|
|
8
|
-
this.name = 'UnauthorizedAccessError';
|
|
9
8
|
this.user = user;
|
|
9
|
+
this.name = 'UnauthorizedAccessError';
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.UnauthorizedAccessError = UnauthorizedAccessError;
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
class AppDoesNotExist extends Error {
|
|
4
|
+
constructor() {
|
|
5
|
+
super(...arguments);
|
|
6
|
+
this.name = 'AppDoesNotExist';
|
|
7
|
+
this.message = 'app does not exist';
|
|
8
|
+
}
|
|
4
9
|
}
|
|
5
10
|
exports.default = AppDoesNotExist;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
/// <reference types="express" />
|
|
2
|
+
/// <reference types="qs" />
|
|
1
3
|
import * as outbreak from '@autofleet/outbreak';
|
|
2
4
|
import User, { middleware, eagerLoadPermissionsMiddleware, middlewareWithDecode, getDecodedBearer, appMiddleware, createOrSetRabbitTrace } from './user';
|
|
5
|
+
import { type UserPayload, CONTEXTS_IDS_HEADER } from './user/ApiUser';
|
|
3
6
|
import { newTrace, traceTypes } from './tracer';
|
|
4
7
|
import { checkFleetPermission, checkBusinessModelPermission, checkDemandSourcePermission, isUserExist, getUser } from './check-permission';
|
|
5
8
|
import { UnauthorizedAccessError } from './errors';
|
|
@@ -8,10 +11,11 @@ import { AUTHORIZATION_METHODS, getAuthorizationHeader } from './authorization';
|
|
|
8
11
|
declare const getCurrentPayload: () => import("./tracer").Trace | {
|
|
9
12
|
[x: string]: never;
|
|
10
13
|
};
|
|
14
|
+
type OutbreakOptions = Parameters<typeof outbreak.default>[0];
|
|
11
15
|
declare const enableTracing: ({ outbreakOptions }?: {
|
|
12
|
-
outbreakOptions?:
|
|
16
|
+
outbreakOptions?: OutbreakOptions;
|
|
13
17
|
}) => void;
|
|
14
|
-
export { traceTypes, newTrace, enableTracing, User, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, getCurrentPayload, getDecodedBearer, checkFleetPermission, checkBusinessModelPermission, checkDemandSourcePermission, isUserExist, getUser, getRefreshTokenSecret, getTokenSecret, UnauthorizedAccessError, appMiddleware, createOrSetRabbitTrace, outbreak, AUTHORIZATION_METHODS, getAuthorizationHeader, };
|
|
18
|
+
export { traceTypes, newTrace, enableTracing, User, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, getCurrentPayload, getDecodedBearer, checkFleetPermission, checkBusinessModelPermission, checkDemandSourcePermission, isUserExist, getUser, getRefreshTokenSecret, getTokenSecret, UnauthorizedAccessError, appMiddleware, createOrSetRabbitTrace, outbreak, AUTHORIZATION_METHODS, getAuthorizationHeader, type UserPayload, CONTEXTS_IDS_HEADER, };
|
|
15
19
|
declare const _default: {
|
|
16
20
|
traceTypes: {
|
|
17
21
|
HTTP_REQUEST: string;
|
|
@@ -24,28 +28,28 @@ declare const _default: {
|
|
|
24
28
|
eagerLoadUserPermissions?: boolean;
|
|
25
29
|
eagerLoadUserPermissionsLegacy?: boolean;
|
|
26
30
|
customPermissionLoader?: import("./user/ApiUser").CustomPermissionLoader;
|
|
27
|
-
}) => (
|
|
31
|
+
}) => import("express").Handler;
|
|
28
32
|
middlewareWithDecode: (options?: {
|
|
29
33
|
eagerLoadUserPermissions?: boolean;
|
|
30
34
|
eagerLoadUserPermissionsLegacy?: boolean;
|
|
31
35
|
returnErrorIfNoToken?: boolean;
|
|
32
|
-
}) => (
|
|
33
|
-
eagerLoadPermissionsMiddleware: (
|
|
36
|
+
}) => import("express").Handler;
|
|
37
|
+
eagerLoadPermissionsMiddleware: import("express").Handler;
|
|
34
38
|
getCurrentPayload: () => import("./tracer").Trace | {
|
|
35
39
|
[x: string]: never;
|
|
36
40
|
};
|
|
37
|
-
getDecodedBearer: (req: any) => any;
|
|
38
|
-
checkFleetPermission: (fleetId:
|
|
39
|
-
checkBusinessModelPermission: (businessModelId:
|
|
40
|
-
checkDemandSourcePermission: (demandSourceId:
|
|
41
|
+
getDecodedBearer: (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>) => any;
|
|
42
|
+
checkFleetPermission: (fleetId: string) => boolean;
|
|
43
|
+
checkBusinessModelPermission: (businessModelId: string) => boolean;
|
|
44
|
+
checkDemandSourcePermission: (demandSourceId: string) => boolean;
|
|
41
45
|
isUserExist: () => string;
|
|
42
46
|
getUser: () => User;
|
|
43
47
|
UnauthorizedAccessError: typeof UnauthorizedAccessError;
|
|
44
48
|
appMiddleware: (options: {
|
|
45
49
|
appId: string;
|
|
46
50
|
clientSecret: string;
|
|
47
|
-
}) => (
|
|
48
|
-
createOrSetRabbitTrace: (trace:
|
|
51
|
+
}) => import("express").Handler;
|
|
52
|
+
createOrSetRabbitTrace: (trace: import("./tracer").Trace, userId: string) => Promise<void>;
|
|
49
53
|
outbreak: typeof outbreak;
|
|
50
54
|
AUTHORIZATION_METHODS: {
|
|
51
55
|
NONE: string;
|
|
@@ -55,5 +59,6 @@ declare const _default: {
|
|
|
55
59
|
getAuthorizationHeader: (authorizationSettings: {
|
|
56
60
|
method: string;
|
|
57
61
|
}) => string;
|
|
62
|
+
CONTEXTS_IDS_HEADER: string;
|
|
58
63
|
};
|
|
59
64
|
export default _default;
|
package/lib/index.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.getAuthorizationHeader = exports.AUTHORIZATION_METHODS = exports.outbreak = exports.createOrSetRabbitTrace = 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 = exports.newTrace = exports.traceTypes = void 0;
|
|
26
|
+
exports.CONTEXTS_IDS_HEADER = exports.getAuthorizationHeader = exports.AUTHORIZATION_METHODS = exports.outbreak = exports.createOrSetRabbitTrace = 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 = exports.newTrace = exports.traceTypes = void 0;
|
|
27
27
|
const outbreak = __importStar(require("@autofleet/outbreak"));
|
|
28
28
|
exports.outbreak = outbreak;
|
|
29
29
|
const user_1 = __importStar(require("./user"));
|
|
@@ -34,6 +34,8 @@ Object.defineProperty(exports, "middlewareWithDecode", { enumerable: true, get:
|
|
|
34
34
|
Object.defineProperty(exports, "getDecodedBearer", { enumerable: true, get: function () { return user_1.getDecodedBearer; } });
|
|
35
35
|
Object.defineProperty(exports, "appMiddleware", { enumerable: true, get: function () { return user_1.appMiddleware; } });
|
|
36
36
|
Object.defineProperty(exports, "createOrSetRabbitTrace", { enumerable: true, get: function () { return user_1.createOrSetRabbitTrace; } });
|
|
37
|
+
const ApiUser_1 = require("./user/ApiUser");
|
|
38
|
+
Object.defineProperty(exports, "CONTEXTS_IDS_HEADER", { enumerable: true, get: function () { return ApiUser_1.CONTEXTS_IDS_HEADER; } });
|
|
37
39
|
const tracer_1 = require("./tracer");
|
|
38
40
|
Object.defineProperty(exports, "newTrace", { enumerable: true, get: function () { return tracer_1.newTrace; } });
|
|
39
41
|
Object.defineProperty(exports, "traceTypes", { enumerable: true, get: function () { return tracer_1.traceTypes; } });
|
|
@@ -51,11 +53,10 @@ Object.defineProperty(exports, "getTokenSecret", { enumerable: true, get: functi
|
|
|
51
53
|
const authorization_1 = require("./authorization");
|
|
52
54
|
Object.defineProperty(exports, "AUTHORIZATION_METHODS", { enumerable: true, get: function () { return authorization_1.AUTHORIZATION_METHODS; } });
|
|
53
55
|
Object.defineProperty(exports, "getAuthorizationHeader", { enumerable: true, get: function () { return authorization_1.getAuthorizationHeader; } });
|
|
54
|
-
const headersTracer = outbreak.default;
|
|
55
56
|
const getCurrentPayload = tracer_1.getCurrentTrace;
|
|
56
57
|
exports.getCurrentPayload = getCurrentPayload;
|
|
57
58
|
const enableTracing = ({ outbreakOptions = {} } = {}) => {
|
|
58
|
-
|
|
59
|
+
outbreak.default({
|
|
59
60
|
headersPrefix: 'x-af',
|
|
60
61
|
...outbreakOptions,
|
|
61
62
|
});
|
|
@@ -82,4 +83,5 @@ exports.default = {
|
|
|
82
83
|
outbreak,
|
|
83
84
|
AUTHORIZATION_METHODS: authorization_1.AUTHORIZATION_METHODS,
|
|
84
85
|
getAuthorizationHeader: authorization_1.getAuthorizationHeader,
|
|
86
|
+
CONTEXTS_IDS_HEADER: ApiUser_1.CONTEXTS_IDS_HEADER,
|
|
85
87
|
};
|
package/lib/secret-getter.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const getRefreshTokenSecret: (token?:
|
|
2
|
-
export declare const getTokenSecret: (token?:
|
|
1
|
+
export declare const getRefreshTokenSecret: (token?: string) => string;
|
|
2
|
+
export declare const getTokenSecret: (token?: string) => string;
|
package/lib/services.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const
|
|
1
|
+
import Network from '@autofleet/network';
|
|
2
|
+
export declare const IdentityNetwork: Network;
|
|
3
|
+
export declare const AutofleetApiNetwork: Network;
|
package/lib/tracer.js
CHANGED
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getCurrentTrace = exports.traceTypes = exports.newTrace = exports.enable = exports.Trace = void 0;
|
|
7
7
|
const async_hooks_1 = __importDefault(require("async_hooks"));
|
|
8
|
-
const
|
|
8
|
+
const node_crypto_1 = require("node:crypto");
|
|
9
9
|
const prevStates = {};
|
|
10
10
|
const tracer = {
|
|
11
11
|
currentTrace: null,
|
|
@@ -40,7 +40,7 @@ const hook = async_hooks_1.default.createHook({
|
|
|
40
40
|
});
|
|
41
41
|
class Trace {
|
|
42
42
|
constructor(type) {
|
|
43
|
-
this.id = (0,
|
|
43
|
+
this.id = (0, node_crypto_1.randomUUID)();
|
|
44
44
|
this.type = type;
|
|
45
45
|
this.context = new Map();
|
|
46
46
|
}
|
package/lib/user/ApiUser.d.ts
CHANGED
|
@@ -23,23 +23,21 @@ export interface PartialUserPayload {
|
|
|
23
23
|
}
|
|
24
24
|
export type CustomPermissionLoader = (string: any) => Promise<UserPayload>;
|
|
25
25
|
export default class ApiUser {
|
|
26
|
-
id
|
|
27
|
-
|
|
28
|
-
privateElevatedPermissionsHash: Map<string, PartialUserPayload | undefined>;
|
|
29
|
-
privatePermissionsLegacy: any;
|
|
30
|
-
appPermission: {
|
|
31
|
-
[key: string]: any;
|
|
32
|
-
};
|
|
33
|
-
emptyUser: boolean;
|
|
34
|
-
accountType: AccountType | undefined;
|
|
26
|
+
id?: string;
|
|
27
|
+
accountType?: AccountType;
|
|
35
28
|
contextIds?: string[];
|
|
29
|
+
private privatePermissions;
|
|
30
|
+
private readonly privateElevatedPermissionsHash;
|
|
31
|
+
private privatePermissionsLegacy;
|
|
32
|
+
private readonly appPermission;
|
|
33
|
+
readonly emptyUser: boolean;
|
|
36
34
|
constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload, contextIds?: string[]);
|
|
37
35
|
getUserPermissions(): Promise<UserPayload>;
|
|
38
|
-
useCustomPermissionLoader(customPermissionLoader:
|
|
36
|
+
useCustomPermissionLoader(customPermissionLoader: (userId: string) => UserPayload | PromiseLike<UserPayload>): Promise<UserPayload>;
|
|
39
37
|
get businessModels(): string[] | undefined;
|
|
40
38
|
get fleets(): string[] | undefined;
|
|
41
39
|
get demandSources(): string[] | undefined;
|
|
42
|
-
getUserProperty
|
|
40
|
+
private getUserProperty;
|
|
43
41
|
get elevatedPermissions(): UserPayload;
|
|
44
42
|
get permissions(): UserPayload | undefined;
|
|
45
43
|
elevatePermissions(addedPermissions: PartialUserPayload): () => void;
|
package/lib/user/ApiUser.js
CHANGED
|
@@ -4,18 +4,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.CONTEXTS_IDS_HEADER = exports.ELEVATED_PERMISSIONS_HEADER = void 0;
|
|
7
|
-
/* eslint-disable consistent-return */
|
|
8
7
|
const node_cache_1 = __importDefault(require("node-cache"));
|
|
9
8
|
const object_hash_1 = __importDefault(require("object-hash"));
|
|
10
|
-
const uuid_1 = require("uuid");
|
|
11
9
|
const outbreak_1 = require("@autofleet/outbreak");
|
|
10
|
+
const utils_1 = require("../utils");
|
|
12
11
|
const services_1 = require("../services");
|
|
13
12
|
exports.ELEVATED_PERMISSIONS_HEADER = 'x-af-elevated-permissions';
|
|
14
13
|
exports.CONTEXTS_IDS_HEADER = 'x-af-context-ids';
|
|
15
14
|
const userCache = new node_cache_1.default({ stdTTL: 10 });
|
|
16
15
|
const mergePermissions = (target, sources) => {
|
|
17
16
|
const permissions = {
|
|
18
|
-
...
|
|
17
|
+
...target,
|
|
19
18
|
fleets: { ...target?.fleets },
|
|
20
19
|
businessModels: { ...target?.businessModels },
|
|
21
20
|
demandSources: { ...target?.demandSources },
|
|
@@ -37,20 +36,18 @@ const mergePermissions = (target, sources) => {
|
|
|
37
36
|
class ApiUser {
|
|
38
37
|
constructor(id, accountType, elevatedPermissions, contextIds) {
|
|
39
38
|
this.id = id;
|
|
40
|
-
this.
|
|
41
|
-
this.appPermission = {};
|
|
42
|
-
this.privateElevatedPermissionsHash = new Map();
|
|
39
|
+
this.accountType = accountType;
|
|
43
40
|
this.contextIds = contextIds;
|
|
41
|
+
this.privateElevatedPermissionsHash = new Map();
|
|
42
|
+
this.appPermission = {};
|
|
43
|
+
this.emptyUser = !!id;
|
|
44
44
|
if (elevatedPermissions) {
|
|
45
|
-
this.privateElevatedPermissionsHash.set('initial', elevatedPermissions);
|
|
46
|
-
}
|
|
47
|
-
if (accountType) {
|
|
48
|
-
this.accountType = accountType;
|
|
45
|
+
this.privateElevatedPermissionsHash.set(Symbol('initial'), elevatedPermissions);
|
|
49
46
|
}
|
|
50
47
|
}
|
|
51
48
|
async getUserPermissions() {
|
|
52
49
|
if (!this.id) {
|
|
53
|
-
return;
|
|
50
|
+
return undefined;
|
|
54
51
|
}
|
|
55
52
|
if (this.privatePermissions) {
|
|
56
53
|
return this.privatePermissions;
|
|
@@ -70,7 +67,7 @@ class ApiUser {
|
|
|
70
67
|
}
|
|
71
68
|
async useCustomPermissionLoader(customPermissionLoader) {
|
|
72
69
|
if (!this.id) {
|
|
73
|
-
return;
|
|
70
|
+
return undefined;
|
|
74
71
|
}
|
|
75
72
|
if (this.privatePermissions) {
|
|
76
73
|
return this.privatePermissions;
|
|
@@ -111,11 +108,13 @@ class ApiUser {
|
|
|
111
108
|
return mergePermissions(this.privatePermissions, this.privateElevatedPermissionsHash.values());
|
|
112
109
|
}
|
|
113
110
|
elevatePermissions(addedPermissions) {
|
|
114
|
-
|
|
111
|
+
// @itayankri is concerned about memory consumption, so create a symbol with no description, to avoid assigning memory for the description string
|
|
112
|
+
// eslint-disable-next-line symbol-description
|
|
113
|
+
const elevationId = Symbol();
|
|
115
114
|
// Validate that the added permissions are valid UUIDs
|
|
116
|
-
Object.
|
|
117
|
-
Object.keys(
|
|
118
|
-
if (!(0,
|
|
115
|
+
Object.values(addedPermissions).forEach((entityIds) => {
|
|
116
|
+
Object.keys(entityIds).forEach((entityId) => {
|
|
117
|
+
if (!(0, utils_1.validateUUID)(entityId)) {
|
|
119
118
|
throw new Error(`Entity id on elevatePermissions is not a valid UUID, provided: ${entityId}`);
|
|
120
119
|
}
|
|
121
120
|
});
|
|
@@ -135,7 +134,7 @@ class ApiUser {
|
|
|
135
134
|
}
|
|
136
135
|
async getUserPermissionsLegacy() {
|
|
137
136
|
if (!this.id) {
|
|
138
|
-
return;
|
|
137
|
+
return undefined;
|
|
139
138
|
}
|
|
140
139
|
if (this.privatePermissionsLegacy) {
|
|
141
140
|
return this.privatePermissionsLegacy;
|
|
@@ -152,7 +151,7 @@ class ApiUser {
|
|
|
152
151
|
}
|
|
153
152
|
async getUserAppPermissions(appId, clientSecret) {
|
|
154
153
|
if (!this.id || !appId || !clientSecret) {
|
|
155
|
-
return;
|
|
154
|
+
return undefined;
|
|
156
155
|
}
|
|
157
156
|
const currentAppPermission = this.appPermission[appId];
|
|
158
157
|
if (currentAppPermission) {
|
package/lib/user/index.d.ts
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
|
+
import type { Handler, Request } from 'express';
|
|
1
2
|
import ApiUser, { CustomPermissionLoader } from './ApiUser';
|
|
3
|
+
import { newTrace } from '../tracer';
|
|
4
|
+
export declare const USER_OBJECT = "userObject";
|
|
5
|
+
declare module 'express-serve-static-core' {
|
|
6
|
+
interface Request {
|
|
7
|
+
user: ApiUser;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
2
10
|
export declare const middleware: (options?: {
|
|
3
11
|
eagerLoadUserPermissions?: boolean;
|
|
4
12
|
eagerLoadUserPermissionsLegacy?: boolean;
|
|
5
13
|
customPermissionLoader?: CustomPermissionLoader;
|
|
6
|
-
}) =>
|
|
14
|
+
}) => Handler;
|
|
7
15
|
export declare const middlewareWithDecode: (options?: {
|
|
8
16
|
eagerLoadUserPermissions?: boolean;
|
|
9
17
|
eagerLoadUserPermissionsLegacy?: boolean;
|
|
10
18
|
returnErrorIfNoToken?: boolean;
|
|
11
|
-
}) =>
|
|
19
|
+
}) => Handler;
|
|
12
20
|
export declare const appMiddleware: (options: {
|
|
13
21
|
appId: string;
|
|
14
22
|
clientSecret: string;
|
|
15
|
-
}) =>
|
|
16
|
-
export declare const eagerLoadPermissionsMiddleware:
|
|
17
|
-
export declare const getDecodedBearer: (req:
|
|
18
|
-
export declare const createOrSetRabbitTrace: (trace:
|
|
23
|
+
}) => Handler;
|
|
24
|
+
export declare const eagerLoadPermissionsMiddleware: Handler;
|
|
25
|
+
export declare const getDecodedBearer: (req: Request) => any;
|
|
26
|
+
export declare const createOrSetRabbitTrace: (trace: ReturnType<typeof newTrace> | undefined, userId: string | undefined) => Promise<void>;
|
|
19
27
|
export default ApiUser;
|