@minimaltech/node-infra 0.5.9-22 → 0.5.9-24
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/dist/base/applications/base.application.js +40 -28
- package/dist/base/applications/default.application.js +8 -2
- package/dist/base/base.component.js +2 -2
- package/dist/base/base.helper.js +3 -4
- package/dist/base/base.provider.js +2 -2
- package/dist/base/base.sequence.js +26 -17
- package/dist/base/controllers/common.js +9 -8
- package/dist/base/controllers/crud.controller.js +67 -55
- package/dist/base/controllers/express-request-handler.js +0 -2
- package/dist/base/controllers/kv.controller.js +18 -7
- package/dist/base/controllers/relational.controller.d.ts +3 -1
- package/dist/base/controllers/relational.controller.js +57 -40
- package/dist/base/controllers/service-crud.controller.js +46 -46
- package/dist/base/datasources/base.datasource.js +0 -1
- package/dist/base/loopback/@lb/core/index.js +1 -0
- package/dist/base/loopback/@lb/repository/index.js +1 -0
- package/dist/base/loopback/authentication/index.js +2 -0
- package/dist/base/loopback/filter/index.js +2 -0
- package/dist/base/loopback/http-server/index.js +2 -0
- package/dist/base/models/base.model.d.ts +7 -0
- package/dist/base/models/base.model.js +23 -5
- package/dist/base/repositories/base.repository.d.ts +3 -0
- package/dist/base/repositories/base.repository.js +24 -5
- package/dist/base/repositories/relations/has-many-polymorphic/decorator.d.ts +8 -0
- package/dist/base/repositories/relations/has-many-polymorphic/decorator.js +10 -11
- package/dist/base/repositories/relations/has-many-polymorphic/factory.d.ts +9 -0
- package/dist/base/repositories/relations/has-many-polymorphic/factory.js +30 -12
- package/dist/base/repositories/searchable-tz-crud.repository.js +160 -129
- package/dist/base/repositories/tz-crud.repository.js +58 -23
- package/dist/base/services/base-crud.service.js +9 -6
- package/dist/base/services/base.service.js +2 -2
- package/dist/common/constants.js +89 -81
- package/dist/common/environments.js +25 -25
- package/dist/common/keys.js +2 -2
- package/dist/common/statuses.js +31 -28
- package/dist/common/types.d.ts +3 -0
- package/dist/components/authenticate/common/constants.js +13 -11
- package/dist/components/authenticate/common/keys.js +7 -7
- package/dist/components/authenticate/common/types.js +3 -11
- package/dist/components/authenticate/component.js +28 -20
- package/dist/components/authenticate/controllers/auth.controller.js +11 -13
- package/dist/components/authenticate/controllers/oauth2.controller.js +20 -18
- package/dist/components/authenticate/middleware.js +35 -25
- package/dist/components/authenticate/models/oauth2-client.model.js +3 -8
- package/dist/components/authenticate/models/oauth2-scope.model.js +0 -4
- package/dist/components/authenticate/models/oauth2-token.model.js +0 -7
- package/dist/components/authenticate/oauth2-handlers/authorization-code.handler.js +5 -4
- package/dist/components/authenticate/oauth2-handlers/base.js +110 -95
- package/dist/components/authenticate/oauth2-handlers/client-credential.handler.js +3 -2
- package/dist/components/authenticate/oauth2-handlers/oauth2.js +2 -3
- package/dist/components/authenticate/repositories/oauth2.repository.js +0 -2
- package/dist/components/authenticate/services/basic-token.service.js +29 -18
- package/dist/components/authenticate/services/basic.strategy.js +17 -7
- package/dist/components/authenticate/services/jwt-token.service.js +40 -27
- package/dist/components/authenticate/services/jwt.strategy.js +1 -2
- package/dist/components/authenticate/services/oauth2.service.js +110 -98
- package/dist/components/authenticate/services/oauth2.strategy.js +31 -19
- package/dist/components/authorize/adapters/adapter-builder.js +0 -1
- package/dist/components/authorize/adapters/base.adapter.js +9 -2
- package/dist/components/authorize/adapters/casbin-postgres-adapter.helper.js +55 -37
- package/dist/components/authorize/adapters/casbin-redis-adapter.helper.js +1 -0
- package/dist/components/authorize/common/constants.js +12 -10
- package/dist/components/authorize/common/keys.js +15 -15
- package/dist/components/authorize/common/types.js +2 -2
- package/dist/components/authorize/component.js +59 -44
- package/dist/components/authorize/decorators/index.d.ts +11 -0
- package/dist/components/authorize/decorators/index.js +14 -1
- package/dist/components/authorize/interceptor.js +80 -76
- package/dist/components/authorize/migrations/0000-create-view-policy.js +14 -3
- package/dist/components/authorize/models/defs.js +5 -24
- package/dist/components/authorize/models/role.model.js +1 -1
- package/dist/components/authorize/models/user-role.model.js +1 -0
- package/dist/components/authorize/models/view-authorize-policy.model.js +1 -4
- package/dist/components/authorize/provider.js +96 -81
- package/dist/components/authorize/repositories/authorize.repository.js +6 -3
- package/dist/components/authorize/services/enforcer.service.js +27 -20
- package/dist/components/authorize/services/generator.service.d.ts +20 -0
- package/dist/components/authorize/services/generator.service.js +96 -62
- package/dist/components/crash-report/common/keys.js +10 -8
- package/dist/components/crash-report/component.js +44 -31
- package/dist/components/crash-report/providers/provider.js +0 -1
- package/dist/components/crash-report/providers/third-parties/base.js +2 -2
- package/dist/components/crash-report/services/mt-crash-report.service.js +5 -5
- package/dist/components/grpc/common/keys.js +4 -4
- package/dist/components/grpc/components/server.component.js +8 -8
- package/dist/components/grpc/controllers/base.js +0 -1
- package/dist/components/grpc/helpers/grpc-client.js +3 -9
- package/dist/components/grpc/helpers/grpc-server.js +17 -14
- package/dist/components/grpc/persistents/grpc.connector.js +2 -8
- package/dist/components/grpc/persistents/grpc.datasource.js +3 -2
- package/dist/components/grpc/persistents/grpc.repository.js +2 -3
- package/dist/components/health-check/component.js +0 -1
- package/dist/components/migration/common/keys.js +3 -3
- package/dist/components/migration/component.js +8 -5
- package/dist/components/migration/models/migration.model.js +0 -2
- package/dist/components/socket-io/common/constants.js +10 -10
- package/dist/components/socket-io/common/keys.js +6 -6
- package/dist/components/socket-io/component.js +8 -9
- package/dist/components/socket-io/helpers/socket-io-client.helper.js +11 -7
- package/dist/components/socket-io/helpers/socket-io-server.helper.js +41 -26
- package/dist/components/static-asset/common/keys.js +4 -4
- package/dist/components/static-asset/component.js +8 -9
- package/dist/components/static-asset/controllers/asset.controller.d.ts +8 -0
- package/dist/components/static-asset/controllers/asset.controller.js +93 -71
- package/dist/components/static-asset/controllers/resource.controller.js +9 -8
- package/dist/datasources/memory/datasource.js +3 -3
- package/dist/datasources/postgres/datasource.js +10 -10
- package/dist/datasources/redis/connector.js +7 -18
- package/dist/datasources/redis/datasource.js +7 -7
- package/dist/helpers/application-environment.helper.js +3 -3
- package/dist/helpers/cron.helper.js +14 -9
- package/dist/helpers/crypto/algorithms/aes.algorithm.js +7 -8
- package/dist/helpers/crypto/algorithms/base.algorithm.js +4 -3
- package/dist/helpers/crypto/algorithms/rsa.algorithm.js +5 -4
- package/dist/helpers/database/query-builder.helper.js +5 -2
- package/dist/helpers/logger/application-logger.js +17 -7
- package/dist/helpers/logger/common/types.js +22 -20
- package/dist/helpers/logger/default-logger.js +12 -2
- package/dist/helpers/logger/factory.js +1 -1
- package/dist/helpers/logger/transports/dgram.transport.js +25 -16
- package/dist/helpers/network/http-request/base-network-request.helper.js +34 -36
- package/dist/helpers/network/http-request/fetcher/axios-fetcher.js +22 -10
- package/dist/helpers/network/http-request/fetcher/base-fetcher.js +36 -13
- package/dist/helpers/network/http-request/fetcher/node-fetcher.js +59 -37
- package/dist/helpers/network/tcp-socket/base-tcp-client.helper.js +27 -30
- package/dist/helpers/network/tcp-socket/base-tcp-server.helper.js +19 -21
- package/dist/helpers/network/tcp-socket/network-tcp-client.helper.js +1 -5
- package/dist/helpers/network/tcp-socket/network-tcp-server.helper.js +1 -5
- package/dist/helpers/network/tcp-socket/network-tls-tcp-client.helper.js +1 -5
- package/dist/helpers/network/tcp-socket/network-tls-tcp-server.helper.js +1 -5
- package/dist/helpers/network/udp-socket/network-udp-client.helper.js +18 -21
- package/dist/helpers/queue/bullmq.helper.js +22 -21
- package/dist/helpers/queue/mqtt.helper.js +17 -15
- package/dist/helpers/queue/queue.helper.js +145 -74
- package/dist/helpers/redis/default.helper.js +162 -117
- package/dist/helpers/redis/redis-cluster.helper.js +2 -7
- package/dist/helpers/redis/redis.helper.js +5 -8
- package/dist/helpers/storage/di-container.helper.js +0 -2
- package/dist/helpers/storage/minio.helper.js +128 -88
- package/dist/helpers/testing/common.js +3 -3
- package/dist/helpers/testing/describe.js +21 -14
- package/dist/helpers/testing/test-case.js +0 -5
- package/dist/helpers/testing/test-handler.js +30 -23
- package/dist/helpers/testing/test-plan/base.js +6 -9
- package/dist/helpers/worker-thread/base.js +20 -10
- package/dist/helpers/worker-thread/worker-bus.js +12 -11
- package/dist/helpers/worker-thread/worker-pool.js +25 -17
- package/dist/interceptors/content-range.interceptor.js +132 -115
- package/dist/middlewares/request-body-parser.middleware.js +16 -4
- package/dist/middlewares/request-spy.middleware.js +5 -4
- package/dist/migrations/handler.js +22 -13
- package/dist/mixins/data-type.mixin.js +0 -5
- package/dist/mixins/deprecated/user-audit.mixin.d.ts +3 -0
- package/dist/mixins/deprecated/user-audit.mixin.js +5 -2
- package/dist/mixins/duplicatable.mixin.js +0 -1
- package/dist/mixins/object-search.mixin.js +0 -1
- package/dist/mixins/principal.mixin.js +0 -2
- package/dist/mixins/soft-delete.mixin.js +0 -1
- package/dist/mixins/soft-persistent.mixin.js +0 -1
- package/dist/mixins/text-search.mixin.js +0 -1
- package/dist/mixins/tz.mixin.js +0 -2
- package/dist/mixins/user-audit.mixin.js +3 -5
- package/dist/mixins/vector.mixin.js +6 -9
- package/dist/tsconfig.base.json +6 -5
- package/dist/utilities/date.utility.js +23 -5
- package/dist/utilities/error.utility.js +0 -2
- package/dist/utilities/model.utility.js +6 -2
- package/dist/utilities/parse.utility.d.ts +16 -0
- package/dist/utilities/parse.utility.js +41 -6
- package/dist/utilities/performance.utility.js +2 -1
- package/dist/utilities/promise.utility.d.ts +17 -0
- package/dist/utilities/promise.utility.js +31 -5
- package/dist/utilities/query.utility.js +16 -8
- package/dist/utilities/request.utility.js +8 -2
- package/package.json +2 -3
|
@@ -1,4 +1,13 @@
|
|
|
1
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
|
+
};
|
|
2
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
13
|
};
|
|
@@ -9,7 +18,9 @@ const utilities_1 = require("../../../utilities");
|
|
|
9
18
|
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
10
19
|
const common_1 = require("../common");
|
|
11
20
|
const sqls = [
|
|
21
|
+
// UUID
|
|
12
22
|
`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`,
|
|
23
|
+
// View policies
|
|
13
24
|
'DROP VIEW IF EXISTS "ViewAuthorizePolicy";',
|
|
14
25
|
`CREATE OR REPLACE VIEW "ViewAuthorizePolicy"
|
|
15
26
|
AS (
|
|
@@ -46,7 +57,7 @@ const sqls = [
|
|
|
46
57
|
];
|
|
47
58
|
const createViewPolicy = (opts) => ({
|
|
48
59
|
name: __filename.slice(__dirname.length + 1),
|
|
49
|
-
fn:
|
|
60
|
+
fn: (application) => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
61
|
if (!opts.datasourceKey || (0, isEmpty_1.default)(opts.datasourceKey)) {
|
|
51
62
|
throw (0, utilities_1.getError)({
|
|
52
63
|
statusCode: 500,
|
|
@@ -63,9 +74,9 @@ const createViewPolicy = (opts) => ({
|
|
|
63
74
|
}
|
|
64
75
|
for (const sql of sqls) {
|
|
65
76
|
helpers_1.applicationLogger.info('[creatViewPolicy] START | Execute SQL: %s', sql);
|
|
66
|
-
|
|
77
|
+
yield datasource.execute(sql);
|
|
67
78
|
helpers_1.applicationLogger.info('[createViewPolicy] DONE | Execute SQL: %s', sql);
|
|
68
79
|
}
|
|
69
|
-
},
|
|
80
|
+
}),
|
|
70
81
|
});
|
|
71
82
|
exports.createViewPolicy = createViewPolicy;
|
|
@@ -14,14 +14,9 @@ const models_1 = require("../../../base/models");
|
|
|
14
14
|
const common_1 = require("../../../common");
|
|
15
15
|
const mixins_1 = require("../../../mixins");
|
|
16
16
|
const repository_1 = require("@loopback/repository");
|
|
17
|
+
// -----------------------------------------------------------------------
|
|
17
18
|
const defineUser = () => {
|
|
18
19
|
class User extends models_1.BaseTzEntity {
|
|
19
|
-
realm;
|
|
20
|
-
status;
|
|
21
|
-
userType;
|
|
22
|
-
activatedAt;
|
|
23
|
-
lastLoginAt;
|
|
24
|
-
parentId;
|
|
25
20
|
constructor(data) {
|
|
26
21
|
super(data);
|
|
27
22
|
}
|
|
@@ -86,13 +81,9 @@ const defineUser = () => {
|
|
|
86
81
|
return User;
|
|
87
82
|
};
|
|
88
83
|
exports.defineUser = defineUser;
|
|
84
|
+
// -----------------------------------------------------------------------
|
|
89
85
|
const defineRole = () => {
|
|
90
86
|
class Role extends models_1.BaseTzEntity {
|
|
91
|
-
identifier;
|
|
92
|
-
name;
|
|
93
|
-
description;
|
|
94
|
-
priority;
|
|
95
|
-
status;
|
|
96
87
|
constructor(data) {
|
|
97
88
|
super(data);
|
|
98
89
|
}
|
|
@@ -133,16 +124,9 @@ const defineRole = () => {
|
|
|
133
124
|
return Role;
|
|
134
125
|
};
|
|
135
126
|
exports.defineRole = defineRole;
|
|
127
|
+
// -----------------------------------------------------------------------
|
|
136
128
|
const definePermission = () => {
|
|
137
129
|
class Permission extends models_1.BaseTzEntity {
|
|
138
|
-
code;
|
|
139
|
-
name;
|
|
140
|
-
subject;
|
|
141
|
-
pType;
|
|
142
|
-
action;
|
|
143
|
-
scope;
|
|
144
|
-
parentId;
|
|
145
|
-
details;
|
|
146
130
|
constructor(data) {
|
|
147
131
|
super(data);
|
|
148
132
|
}
|
|
@@ -204,12 +188,9 @@ const definePermission = () => {
|
|
|
204
188
|
return Permission;
|
|
205
189
|
};
|
|
206
190
|
exports.definePermission = definePermission;
|
|
191
|
+
// -----------------------------------------------------------------------
|
|
207
192
|
const definePermissionMapping = () => {
|
|
208
193
|
class PermissionMapping extends models_1.BaseTzEntity {
|
|
209
|
-
userId;
|
|
210
|
-
roleId;
|
|
211
|
-
permissionId;
|
|
212
|
-
effect;
|
|
213
194
|
constructor(data) {
|
|
214
195
|
super(data);
|
|
215
196
|
}
|
|
@@ -242,9 +223,9 @@ const definePermissionMapping = () => {
|
|
|
242
223
|
return PermissionMapping;
|
|
243
224
|
};
|
|
244
225
|
exports.definePermissionMapping = definePermissionMapping;
|
|
226
|
+
// -----------------------------------------------------------------------
|
|
245
227
|
const defineUserRole = () => {
|
|
246
228
|
class UserRole extends (0, mixins_1.PrincipalMixin)(models_1.BaseTzEntity, 'Role', 'number') {
|
|
247
|
-
userId;
|
|
248
229
|
constructor(data) {
|
|
249
230
|
super(data);
|
|
250
231
|
}
|
|
@@ -14,8 +14,8 @@ const repository_1 = require("@loopback/repository");
|
|
|
14
14
|
const _1 = require(".");
|
|
15
15
|
const defs_1 = require("./defs");
|
|
16
16
|
const BaseRole = (0, defs_1.defineRole)();
|
|
17
|
+
// ---------------------------------------------------------------
|
|
17
18
|
let Role = class Role extends BaseRole {
|
|
18
|
-
permissions;
|
|
19
19
|
constructor(data) {
|
|
20
20
|
super(data);
|
|
21
21
|
}
|
|
@@ -13,6 +13,7 @@ exports.UserRole = void 0;
|
|
|
13
13
|
const repository_1 = require("@loopback/repository");
|
|
14
14
|
const defs_1 = require("./defs");
|
|
15
15
|
const BaseUserRole = (0, defs_1.defineUserRole)();
|
|
16
|
+
// ---------------------------------------------------------------
|
|
16
17
|
let UserRole = class UserRole extends BaseUserRole {
|
|
17
18
|
constructor(data) {
|
|
18
19
|
super(data);
|
|
@@ -12,11 +12,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.ViewAuthorizePolicy = void 0;
|
|
13
13
|
const models_1 = require("../../../base/models");
|
|
14
14
|
const repository_1 = require("@loopback/repository");
|
|
15
|
+
// ---------------------------------------------------------------
|
|
15
16
|
let ViewAuthorizePolicy = class ViewAuthorizePolicy extends models_1.BaseEntity {
|
|
16
|
-
id;
|
|
17
|
-
subject;
|
|
18
|
-
subjectType;
|
|
19
|
-
subjectId;
|
|
20
17
|
constructor(data) {
|
|
21
18
|
super(data);
|
|
22
19
|
}
|
|
@@ -11,6 +11,15 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
15
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
14
23
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
24
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
25
|
};
|
|
@@ -26,10 +35,6 @@ const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
|
26
35
|
const common_1 = require("./common");
|
|
27
36
|
const services_1 = require("./services");
|
|
28
37
|
let AuthorizeProvider = AuthorizeProvider_1 = class AuthorizeProvider {
|
|
29
|
-
enforcerService;
|
|
30
|
-
alwaysAllowRoles;
|
|
31
|
-
normalizePayloadFn;
|
|
32
|
-
logger;
|
|
33
38
|
constructor(enforcerService, alwaysAllowRoles, normalizePayloadFn) {
|
|
34
39
|
this.enforcerService = enforcerService;
|
|
35
40
|
this.alwaysAllowRoles = alwaysAllowRoles;
|
|
@@ -39,97 +44,107 @@ let AuthorizeProvider = AuthorizeProvider_1 = class AuthorizeProvider {
|
|
|
39
44
|
value() {
|
|
40
45
|
return this.authorize.bind(this);
|
|
41
46
|
}
|
|
47
|
+
// -------------------------------------------------------------------------------------------------------------------
|
|
42
48
|
normalizeEnforcePayload(opts) {
|
|
49
|
+
var _a, _b, _c;
|
|
43
50
|
const { subject, object, scope } = opts;
|
|
44
51
|
return {
|
|
45
|
-
subject: subject
|
|
46
|
-
object: scope
|
|
47
|
-
(object?.toLowerCase() || '')?.replace(/controller/g, '')?.replace(/.prototype/g, ''),
|
|
52
|
+
subject: (subject === null || subject === void 0 ? void 0 : subject.toLowerCase()) || '',
|
|
53
|
+
object: (_a = scope === null || scope === void 0 ? void 0 : scope.toLowerCase()) !== null && _a !== void 0 ? _a : (_c = (_b = ((object === null || object === void 0 ? void 0 : object.toLowerCase()) || '')) === null || _b === void 0 ? void 0 : _b.replace(/controller/g, '')) === null || _c === void 0 ? void 0 : _c.replace(/.prototype/g, ''),
|
|
48
54
|
action: common_1.EnforcerDefinitions.ACTION_EXECUTE,
|
|
49
55
|
};
|
|
50
56
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
57
|
+
// -------------------------------------------------------------------------------------------------------------------
|
|
58
|
+
authorizePermission(userId, object, scopes) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
var _a, _b, _c, _d;
|
|
61
|
+
let isSingleAuthRs = false;
|
|
62
|
+
let isScopeAuthRs = true;
|
|
63
|
+
const enforcer = yield this.enforcerService.getTypeEnforcer(userId);
|
|
64
|
+
if (!enforcer) {
|
|
65
|
+
this.logger.debug('[authorizePermission] Skip authorization for NULL enforcer!');
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
const subject = `${common_1.EnforcerDefinitions.PREFIX_USER}_${userId}`;
|
|
69
|
+
for (const scope of scopes !== null && scopes !== void 0 ? scopes : []) {
|
|
70
|
+
const enforcePayload = (_b = (_a = this.normalizePayloadFn) === null || _a === void 0 ? void 0 : _a.call(this, { subject, object, scope })) !== null && _b !== void 0 ? _b : this.normalizeEnforcePayload({ subject, object, scope });
|
|
71
|
+
isScopeAuthRs = yield enforcer.enforce(enforcePayload.subject, enforcePayload.object, enforcePayload.action);
|
|
72
|
+
this.logger.debug('[authorizePermission] Payload: %j | scopeAuthRs: %s', enforcePayload, isScopeAuthRs);
|
|
73
|
+
if (!isScopeAuthRs) {
|
|
74
|
+
this.logger.debug('[authorizePermission] Permission denied | Payload: %j', enforcePayload);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
65
78
|
if (!isScopeAuthRs) {
|
|
66
|
-
|
|
67
|
-
break;
|
|
79
|
+
return isScopeAuthRs;
|
|
68
80
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
isSingleAuthRs = await enforcer.enforce(enforcePayload.subject, enforcePayload.object, enforcePayload.action);
|
|
77
|
-
this.logger.debug('[authorizePermission] Payload: %j | singleAuthRs: %s', enforcePayload, isSingleAuthRs);
|
|
78
|
-
}
|
|
79
|
-
return isScopeAuthRs && isSingleAuthRs;
|
|
81
|
+
if (object) {
|
|
82
|
+
const enforcePayload = (_d = (_c = this.normalizePayloadFn) === null || _c === void 0 ? void 0 : _c.call(this, { subject, object })) !== null && _d !== void 0 ? _d : this.normalizeEnforcePayload({ subject, object });
|
|
83
|
+
isSingleAuthRs = yield enforcer.enforce(enforcePayload.subject, enforcePayload.object, enforcePayload.action);
|
|
84
|
+
this.logger.debug('[authorizePermission] Payload: %j | singleAuthRs: %s', enforcePayload, isSingleAuthRs);
|
|
85
|
+
}
|
|
86
|
+
return isScopeAuthRs && isSingleAuthRs;
|
|
87
|
+
});
|
|
80
88
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const roleIdentifiers = [];
|
|
89
|
-
const roles = [];
|
|
90
|
-
for (const encodedRole of encodedRoles) {
|
|
91
|
-
if (!encodedRole || (0, isEmpty_1.default)(encodedRole)) {
|
|
92
|
-
continue;
|
|
89
|
+
// -------------------------------------------------------------------------------------------------------------------
|
|
90
|
+
authorize(context, metadata) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
var _a, _b;
|
|
93
|
+
const t = new Date().getTime();
|
|
94
|
+
if ((context === null || context === void 0 ? void 0 : context.principals.length) <= 0) {
|
|
95
|
+
return authorization_1.AuthorizationDecision.DENY;
|
|
93
96
|
}
|
|
94
|
-
const {
|
|
95
|
-
roleIds
|
|
96
|
-
roleIdentifiers
|
|
97
|
-
roles
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
const { resource, allowedRoles = [], scopes, voters } = metadata;
|
|
103
|
-
const requestResource = resource ?? context.resource;
|
|
104
|
-
if ((0, intersection_1.default)(this.alwaysAllowRoles, roleIdentifiers)?.length > 0 ||
|
|
105
|
-
(0, intersection_1.default)(allowedRoles, roleIdentifiers)?.length > 0) {
|
|
106
|
-
return authorization_1.AuthorizationDecision.ALLOW;
|
|
107
|
-
}
|
|
108
|
-
if (voters && voters?.length > 0) {
|
|
109
|
-
const voterRs = await Promise.all(voters?.map(el => {
|
|
110
|
-
switch (typeof el) {
|
|
111
|
-
case 'function': {
|
|
112
|
-
return el?.(context, metadata);
|
|
113
|
-
}
|
|
114
|
-
default: {
|
|
115
|
-
throw (0, utilities_1.getError)({
|
|
116
|
-
message: '[authorize][voter] voter implementation must be function type!',
|
|
117
|
-
});
|
|
118
|
-
}
|
|
97
|
+
const { userId, roles: encodedRoles } = context.principals[0];
|
|
98
|
+
const roleIds = [];
|
|
99
|
+
const roleIdentifiers = [];
|
|
100
|
+
const roles = [];
|
|
101
|
+
for (const encodedRole of encodedRoles) {
|
|
102
|
+
if (!encodedRole || (0, isEmpty_1.default)(encodedRole)) {
|
|
103
|
+
continue;
|
|
119
104
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
105
|
+
const { id, identifier } = encodedRole;
|
|
106
|
+
roleIds.push((0, utilities_1.int)(id));
|
|
107
|
+
roleIdentifiers.push(identifier);
|
|
108
|
+
roles.push({ id, identifier });
|
|
124
109
|
}
|
|
125
|
-
|
|
110
|
+
// DENY all unknown user and unknow roles
|
|
111
|
+
if (!userId || !(roles === null || roles === void 0 ? void 0 : roles.length)) {
|
|
126
112
|
return authorization_1.AuthorizationDecision.DENY;
|
|
127
113
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
114
|
+
const { resource, allowedRoles = [], scopes, voters } = metadata;
|
|
115
|
+
const requestResource = resource !== null && resource !== void 0 ? resource : context.resource;
|
|
116
|
+
// Verify static roles
|
|
117
|
+
if (((_a = (0, intersection_1.default)(this.alwaysAllowRoles, roleIdentifiers)) === null || _a === void 0 ? void 0 : _a.length) > 0 ||
|
|
118
|
+
((_b = (0, intersection_1.default)(allowedRoles, roleIdentifiers)) === null || _b === void 0 ? void 0 : _b.length) > 0) {
|
|
119
|
+
return authorization_1.AuthorizationDecision.ALLOW;
|
|
120
|
+
}
|
|
121
|
+
if (voters && (voters === null || voters === void 0 ? void 0 : voters.length) > 0) {
|
|
122
|
+
const voterRs = yield Promise.all(voters === null || voters === void 0 ? void 0 : voters.map(el => {
|
|
123
|
+
switch (typeof el) {
|
|
124
|
+
case 'function': {
|
|
125
|
+
return el === null || el === void 0 ? void 0 : el(context, metadata);
|
|
126
|
+
}
|
|
127
|
+
default: {
|
|
128
|
+
throw (0, utilities_1.getError)({
|
|
129
|
+
message: '[authorize][voter] voter implementation must be function type!',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}));
|
|
134
|
+
const voterSet = new Set(voterRs);
|
|
135
|
+
if (voterSet.size === 1 && voterSet.has(authorization_1.AuthorizationDecision.ALLOW)) {
|
|
136
|
+
return authorization_1.AuthorizationDecision.ALLOW;
|
|
137
|
+
}
|
|
138
|
+
if (voterSet.has(authorization_1.AuthorizationDecision.DENY)) {
|
|
139
|
+
return authorization_1.AuthorizationDecision.DENY;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// Authorize by role and user permissions
|
|
143
|
+
const isAuthorized = yield this.authorizePermission(userId, requestResource, scopes);
|
|
144
|
+
const rs = isAuthorized ? authorization_1.AuthorizationDecision.ALLOW : authorization_1.AuthorizationDecision.DENY;
|
|
145
|
+
this.logger.debug('[authorize] Authorizing... | Resource: %s | allowedRoles: %j | scopes: %j | Took: %d(ms)', requestResource, allowedRoles, scopes, new Date().getTime() - t);
|
|
146
|
+
return rs;
|
|
147
|
+
});
|
|
133
148
|
}
|
|
134
149
|
};
|
|
135
150
|
exports.AuthorizeProvider = AuthorizeProvider;
|
|
@@ -24,6 +24,7 @@ const repository_1 = require("@loopback/repository");
|
|
|
24
24
|
const models_1 = require("../models");
|
|
25
25
|
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
26
26
|
const DS_AUTHORIZE = process.env.APP_ENV_APPLICATION_DS_AUTHORIZE;
|
|
27
|
+
// ----------------------------------------------------------------------------
|
|
27
28
|
class AbstractAuthorizeRepository extends repositories_1.TzCrudRepository {
|
|
28
29
|
constructor(entityClass, dataSource) {
|
|
29
30
|
if (!DS_AUTHORIZE || (0, isEmpty_1.default)(DS_AUTHORIZE)) {
|
|
@@ -36,10 +37,8 @@ class AbstractAuthorizeRepository extends repositories_1.TzCrudRepository {
|
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
exports.AbstractAuthorizeRepository = AbstractAuthorizeRepository;
|
|
40
|
+
// ----------------------------------------------------------------------------
|
|
39
41
|
let RoleRepository = class RoleRepository extends AbstractAuthorizeRepository {
|
|
40
|
-
permissionRepositoryGetter;
|
|
41
|
-
permissionMappingRepositoryGetter;
|
|
42
|
-
permissions;
|
|
43
42
|
constructor(dataSource, permissionRepositoryGetter, permissionMappingRepositoryGetter) {
|
|
44
43
|
super(models_1.Role, dataSource);
|
|
45
44
|
this.permissionRepositoryGetter = permissionRepositoryGetter;
|
|
@@ -56,6 +55,7 @@ exports.RoleRepository = RoleRepository = __decorate([
|
|
|
56
55
|
__param(2, repository_1.repository.getter('PermissionMappingRepository')),
|
|
57
56
|
__metadata("design:paramtypes", [datasources_1.BaseDataSource, Function, Function])
|
|
58
57
|
], RoleRepository);
|
|
58
|
+
// ----------------------------------------------------------------------------
|
|
59
59
|
let PermissionRepository = class PermissionRepository extends AbstractAuthorizeRepository {
|
|
60
60
|
constructor(dataSource) {
|
|
61
61
|
super(models_1.Permission, dataSource);
|
|
@@ -67,6 +67,7 @@ exports.PermissionRepository = PermissionRepository = __decorate([
|
|
|
67
67
|
__param(0, (0, core_1.inject)(`datasources.${DS_AUTHORIZE}`)),
|
|
68
68
|
__metadata("design:paramtypes", [datasources_1.BaseDataSource])
|
|
69
69
|
], PermissionRepository);
|
|
70
|
+
// ----------------------------------------------------------------------------
|
|
70
71
|
let PermissionMappingRepository = class PermissionMappingRepository extends AbstractAuthorizeRepository {
|
|
71
72
|
constructor(dataSource) {
|
|
72
73
|
super(models_1.PermissionMapping, dataSource);
|
|
@@ -78,6 +79,7 @@ exports.PermissionMappingRepository = PermissionMappingRepository = __decorate([
|
|
|
78
79
|
__param(0, (0, core_1.inject)(`datasources.${DS_AUTHORIZE}`)),
|
|
79
80
|
__metadata("design:paramtypes", [datasources_1.BaseDataSource])
|
|
80
81
|
], PermissionMappingRepository);
|
|
82
|
+
// ----------------------------------------------------------------------------
|
|
81
83
|
let UserRoleRepository = class UserRoleRepository extends AbstractAuthorizeRepository {
|
|
82
84
|
constructor(dataSource) {
|
|
83
85
|
super(models_1.UserRole, dataSource);
|
|
@@ -89,6 +91,7 @@ exports.UserRoleRepository = UserRoleRepository = __decorate([
|
|
|
89
91
|
__param(0, (0, core_1.inject)(`datasources.${DS_AUTHORIZE}`)),
|
|
90
92
|
__metadata("design:paramtypes", [datasources_1.BaseDataSource])
|
|
91
93
|
], UserRoleRepository);
|
|
94
|
+
// ----------------------------------------------------------------------------
|
|
92
95
|
let ViewAuthorizePolicyRepository = class ViewAuthorizePolicyRepository extends repositories_1.ViewRepository {
|
|
93
96
|
constructor(dataSource) {
|
|
94
97
|
super(models_1.ViewAuthorizePolicy, dataSource);
|
|
@@ -11,6 +11,15 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
15
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
14
23
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
24
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
25
|
};
|
|
@@ -27,10 +36,6 @@ const node_fs_1 = __importDefault(require("node:fs"));
|
|
|
27
36
|
const adapters_1 = require("../adapters");
|
|
28
37
|
const common_1 = require("../common");
|
|
29
38
|
let EnforcerService = EnforcerService_1 = class EnforcerService {
|
|
30
|
-
options;
|
|
31
|
-
dataSource;
|
|
32
|
-
logger;
|
|
33
|
-
enforcer;
|
|
34
39
|
constructor(options, dataSource) {
|
|
35
40
|
this.options = options;
|
|
36
41
|
this.dataSource = dataSource;
|
|
@@ -58,28 +63,30 @@ let EnforcerService = EnforcerService_1 = class EnforcerService {
|
|
|
58
63
|
});
|
|
59
64
|
}
|
|
60
65
|
this.logger.info('[getEnforcer] Creating new Enforcer with configure path: %s | dataSource: %s', confPath, this.dataSource.name);
|
|
61
|
-
const casbinAdapter = adapter
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
});
|
|
66
|
+
const casbinAdapter = adapter !== null && adapter !== void 0 ? adapter : adapters_1.CasbinAdapterBuilder.getInstance().build({
|
|
67
|
+
type: adapterType,
|
|
68
|
+
dataSource: this.dataSource,
|
|
69
|
+
});
|
|
66
70
|
if (useCache) {
|
|
67
71
|
return (0, casbin_1.newCachedEnforcer)(confPath, casbinAdapter);
|
|
68
72
|
}
|
|
69
73
|
this.logger.debug('[getEnforcer] Created new enforcer | Configure path: %s', confPath);
|
|
70
74
|
return (0, casbin_1.newEnforcer)(confPath, casbinAdapter);
|
|
71
75
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
// -----------------------------------------------------------------------------------------
|
|
77
|
+
getTypeEnforcer(id) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const enforcer = yield this.getEnforcer();
|
|
80
|
+
if (!enforcer) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
const filterValue = {
|
|
84
|
+
principalType: 'User',
|
|
85
|
+
principalValue: id,
|
|
86
|
+
};
|
|
87
|
+
yield enforcer.loadFilteredPolicy(filterValue);
|
|
88
|
+
return enforcer;
|
|
89
|
+
});
|
|
83
90
|
}
|
|
84
91
|
};
|
|
85
92
|
exports.EnforcerService = EnforcerService;
|
|
@@ -43,9 +43,29 @@ export declare class GeneratePermissionService {
|
|
|
43
43
|
permissionRepository: PermissionRepository;
|
|
44
44
|
controllers: Array<Constructor<IController>>;
|
|
45
45
|
}): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Obtain all permission codes for a controller
|
|
48
|
+
*
|
|
49
|
+
* @returns {string[]} List of permission codes
|
|
50
|
+
*/
|
|
46
51
|
getPermissionCodes(opts: {
|
|
47
52
|
controllers: Array<Constructor<IController>>;
|
|
48
53
|
}): string[];
|
|
54
|
+
/**
|
|
55
|
+
* Write all permission codes for a list of controllers to a file
|
|
56
|
+
*
|
|
57
|
+
* @param outputPath - Path to write
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* const generatePermissionService = new GeneratePermissionService();
|
|
61
|
+
*
|
|
62
|
+
* generatePermissionService.getPermissionCodesAndWriteToFile({
|
|
63
|
+
* controllers: [XboxController, PSController, NintendoController],
|
|
64
|
+
* outputPath: './src/migrations/',
|
|
65
|
+
* fileName: 'permissionCodes',
|
|
66
|
+
* fileType: 'ts',
|
|
67
|
+
* });
|
|
68
|
+
*/
|
|
49
69
|
getPermissionCodesAndWriteToFile(opts: {
|
|
50
70
|
controllers: Array<Constructor<IController>>;
|
|
51
71
|
outputPath?: string;
|