@autofleet/zehut 1.0.0-gery-ba-beta → 1.0.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.
@@ -0,0 +1,26 @@
1
+ version: 2
2
+ jobs:
3
+ test:
4
+ docker:
5
+ - image: circleci/node:11.14.0
6
+ steps:
7
+ - checkout
8
+ - restore_cache:
9
+ keys:
10
+ - v1-dependencies-{{ checksum "package.json" }}
11
+ # fallback to using the latest cache if no exact match is found
12
+ - v1-dependencies-
13
+ - run: npm i
14
+ - save_cache:
15
+ paths:
16
+ - node_modules
17
+ key: v1-dependencies-{{ checksum "package.json" }}
18
+ - run: npm run coverage
19
+ - run: rm -rf ./coverage
20
+ - run: npm run linter
21
+
22
+ workflows:
23
+ version: 2
24
+ build_and_test:
25
+ jobs:
26
+ - test
package/.eslintrc.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": [
3
+ "airbnb/base"
4
+ ],
5
+ "env": {
6
+ "jest": true
7
+ }
8
+ }
package/.gitlab-ci.yml ADDED
@@ -0,0 +1,13 @@
1
+ image: node:11.14.0
2
+
3
+ stages:
4
+ - test
5
+
6
+ before_script:
7
+ - node -v
8
+ - npm -v
9
+ - npm install
10
+ test:
11
+ stage: test
12
+ script:
13
+ - npm run coverage
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ testEnvironment: 'node',
3
+ coverageThreshold: {
4
+ global: {
5
+ lines: 80,
6
+ },
7
+ },
8
+ };
package/README.md CHANGED
@@ -70,8 +70,4 @@ and run
70
70
  ```
71
71
  npm publish
72
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
73
  # zehut
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ const Mocks = require('./src/mocks');
2
+
3
+ module.exports = {
4
+ Mocks
5
+ };
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "1.0.0-gery-ba-beta",
3
+ "version": "1.0.0",
4
4
  "description": "manage user's identity",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
5
  "scripts": {
8
- "build": "rm -rf lib && tsc",
9
- "prepublish": "npm run build",
10
- "coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage",
6
+ "build": "tsc",
7
+ "coverage": "jest --coverage --forceExit --runInBand",
11
8
  "test": "jest --forceExit --runInBand",
12
9
  "test-auto": "jest --watch --runInBand",
13
10
  "linter": "./node_modules/.bin/eslint ."
14
11
  },
12
+ "jest": {
13
+ "setupTestFrameworkScriptFile": "jest-extended",
14
+ "testURL": "http://localhost:8085/"
15
+ },
15
16
  "repository": {
16
17
  "type": "git",
17
18
  "url": "git+ssh://git@gitlab.com/AutoFleet/zehut.git"
@@ -23,32 +24,10 @@
23
24
  },
24
25
  "homepage": "https://github.com/Autofleet/zehut",
25
26
  "dependencies": {
26
- "@autofleet/network": "^1.5.0",
27
- "@autofleet/outbreak": "^1.0.4",
28
- "axios": "^0.27.2",
29
- "express": "^4.18.1",
30
- "jsonwebtoken": "^8.5.1",
31
- "merge-deep": "^3.0.3",
32
- "methods": "^1.1.2",
33
- "moment": "^2.29.1",
34
- "nock": "^13.2.9",
35
- "node-cache": "^5.1.2",
36
- "supertest": "^6.2.4",
37
- "uuid": "^8.3.2"
27
+ "@autofleet/network": "^1.1.4",
28
+ "axios": "^0.19.2"
38
29
  },
39
30
  "devDependencies": {
40
- "@types/jest": "^29.5.4",
41
- "@types/node": "^16.14.2",
42
- "@typescript-eslint/eslint-plugin": "^6.5.0",
43
- "@typescript-eslint/parser": "^6.5.0",
44
- "eslint": "^8.48.0",
45
- "eslint-config-airbnb-typescript": "^17.1.0",
46
- "eslint-plugin-import": "^2.28.1",
47
- "jest": "^29.6.4",
48
- "ts-jest": "^29.1.1",
49
- "typescript": "^4.9.5"
50
- },
51
- "files": [
52
- "lib/**/*"
53
- ]
31
+ "typescript": "^3.9.5"
32
+ }
54
33
  }
package/src/.env ADDED
File without changes
@@ -0,0 +1,93 @@
1
+ import Network from '@autofleet/network';
2
+ const CACHE_LIFETIME_IN_SEC = 10;
3
+ const IdentityNetwork = new Network({
4
+ serviceName: 'IDENTITY_MS',
5
+ retries: 3,
6
+ retryCondition: () => true,
7
+ cache: process.env.NODE_ENV !== 'test' ? {
8
+ maxAge: CACHE_LIFETIME_IN_SEC * 1000,
9
+ } : undefined,
10
+ });
11
+
12
+ interface EntityPermissions {
13
+ [key: string]: string[];
14
+ }
15
+
16
+ export interface UserPayload {
17
+ businessModels: EntityPermissions;
18
+ fleets: EntityPermissions;
19
+ demandSources: EntityPermissions;
20
+ createdAt?: string;
21
+ }
22
+
23
+ class ApiUser {
24
+ id: string | undefined;
25
+ privatePermissions: UserPayload | undefined;
26
+ privatePermissionsLegacy: any;
27
+ emptyUser: boolean;
28
+
29
+ constructor(id? : string) {
30
+ this.id = id;
31
+ this.emptyUser = !!id;
32
+ }
33
+
34
+ async getUserPermissions() {
35
+ if (this.privatePermissions) {
36
+ return this.privatePermissions;
37
+ }
38
+ const { data } = await IdentityNetwork(`/api/v1/users/${this.id}/authorization-payload`);
39
+
40
+ this.privatePermissions = data;
41
+ return this.privatePermissions;
42
+ }
43
+
44
+ get permissions(): UserPayload | undefined {
45
+ if (!this.privatePermissionsLegacy) {
46
+ throw new Error('Cannot get permissions without calling (async) getUserPermissions before');
47
+ }
48
+ return this.privatePermissions;
49
+ }
50
+
51
+ async getUserPermissionsLegacy() {
52
+ if (this.privatePermissionsLegacy) {
53
+ return this.privatePermissionsLegacy;
54
+ }
55
+ const { data } = await IdentityNetwork(`/api/v1/users/${this.id}/authorization-payload-legacy`);
56
+
57
+ this.privatePermissionsLegacy = data;
58
+ return this.privatePermissionsLegacy;
59
+ }
60
+
61
+ get permissionsLegacy(): UserPayload {
62
+ if (!this.privatePermissionsLegacy) {
63
+ throw new Error('Cannot get permissionsLegacy without calling (async) getUserPermissionsLegacy before');
64
+ }
65
+ return this.privatePermissionsLegacy;
66
+ }
67
+ }
68
+
69
+ export default (options: {
70
+ eagerLoadUserPermissions?: boolean;
71
+ eagerLoadUserPermissionsLegacy?: boolean;
72
+ } = {}) => async (req, res, next): Promise<void> => {
73
+ if (req.headers['x-af-user-id']) {
74
+ req.user = new ApiUser(req.headers['x-af-user-id']);
75
+ } else {
76
+ req.user = new ApiUser();
77
+ }
78
+
79
+ if (options.eagerLoadUserPermissions) {
80
+ await req.user.getUserPermissions();
81
+ }
82
+
83
+ if (options.eagerLoadUserPermissionsLegacy) {
84
+ await req.user.getUserPermissionsLegacy();
85
+ }
86
+
87
+ next();
88
+ };
89
+
90
+ export const eagerLoadPermissionsMiddleware = async (req , res, next) => {
91
+ await req.user.getUserPermissions();
92
+ next();
93
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es6",
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./lib",
7
+ "esModuleInterop": true,
8
+ "experimentalDecorators": true,
9
+ "emitDecoratorMetadata": true,
10
+ "allowJs": true,
11
+ },
12
+ "include": ["src"],
13
+ "exclude": ["node_modules"]
14
+ }