@vsaas/loopback 10.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.
Files changed (69) hide show
  1. package/LICENSE +25 -0
  2. package/README.md +91 -0
  3. package/common/models/README.md +109 -0
  4. package/common/models/access-token.json +37 -0
  5. package/common/models/acl.json +17 -0
  6. package/common/models/application.json +130 -0
  7. package/common/models/change.json +25 -0
  8. package/common/models/checkpoint.json +14 -0
  9. package/common/models/email.json +11 -0
  10. package/common/models/key-value-model.json +4 -0
  11. package/common/models/role-mapping.json +26 -0
  12. package/common/models/role.json +30 -0
  13. package/common/models/scope.json +14 -0
  14. package/common/models/user.json +118 -0
  15. package/dist/_virtual/_rolldown/runtime.cjs +32 -0
  16. package/dist/common/models/access-token.cjs +144 -0
  17. package/dist/common/models/access-token2.cjs +43 -0
  18. package/dist/common/models/acl.cjs +428 -0
  19. package/dist/common/models/acl2.cjs +27 -0
  20. package/dist/common/models/application.cjs +100 -0
  21. package/dist/common/models/application2.cjs +118 -0
  22. package/dist/common/models/change.cjs +404 -0
  23. package/dist/common/models/change2.cjs +25 -0
  24. package/dist/common/models/checkpoint.cjs +43 -0
  25. package/dist/common/models/checkpoint2.cjs +18 -0
  26. package/dist/common/models/email.cjs +18 -0
  27. package/dist/common/models/email2.cjs +30 -0
  28. package/dist/common/models/key-value-model.cjs +140 -0
  29. package/dist/common/models/key-value-model2.cjs +14 -0
  30. package/dist/common/models/role-mapping.cjs +57 -0
  31. package/dist/common/models/role-mapping2.cjs +34 -0
  32. package/dist/common/models/role.cjs +396 -0
  33. package/dist/common/models/role2.cjs +38 -0
  34. package/dist/common/models/scope.cjs +30 -0
  35. package/dist/common/models/scope2.cjs +21 -0
  36. package/dist/common/models/user.cjs +810 -0
  37. package/dist/common/models/user2.cjs +118 -0
  38. package/dist/index.cjs +16 -0
  39. package/dist/lib/access-context.cjs +228 -0
  40. package/dist/lib/application.cjs +450 -0
  41. package/dist/lib/builtin-models.cjs +60 -0
  42. package/dist/lib/configure-shared-methods.cjs +41 -0
  43. package/dist/lib/connectors/base-connector.cjs +23 -0
  44. package/dist/lib/connectors/mail-direct-transport.cjs +375 -0
  45. package/dist/lib/connectors/mail-stub-transport.cjs +86 -0
  46. package/dist/lib/connectors/mail.cjs +128 -0
  47. package/dist/lib/connectors/memory.cjs +19 -0
  48. package/dist/lib/current-context.cjs +22 -0
  49. package/dist/lib/globalize.cjs +29 -0
  50. package/dist/lib/loopback.cjs +313 -0
  51. package/dist/lib/model.cjs +1009 -0
  52. package/dist/lib/persisted-model.cjs +1835 -0
  53. package/dist/lib/registry.cjs +291 -0
  54. package/dist/lib/runtime.cjs +25 -0
  55. package/dist/lib/server-app.cjs +231 -0
  56. package/dist/lib/utils.cjs +154 -0
  57. package/dist/package.cjs +124 -0
  58. package/dist/server/middleware/context.cjs +7 -0
  59. package/dist/server/middleware/error-handler.cjs +6 -0
  60. package/dist/server/middleware/favicon.cjs +13 -0
  61. package/dist/server/middleware/rest.cjs +44 -0
  62. package/dist/server/middleware/static.cjs +14 -0
  63. package/dist/server/middleware/status.cjs +28 -0
  64. package/dist/server/middleware/token.cjs +66 -0
  65. package/dist/server/middleware/url-not-found.cjs +20 -0
  66. package/favicon.ico +0 -0
  67. package/package.json +121 -0
  68. package/templates/reset-form.ejs +3 -0
  69. package/templates/verify.ejs +9 -0
@@ -0,0 +1,118 @@
1
+ //#region common/models/user.json
2
+ var require_user = /* @__PURE__ */ require("../../_virtual/_rolldown/runtime.cjs").__commonJSMin(((exports, module) => {
3
+ module.exports = {
4
+ "name": "User",
5
+ "properties": {
6
+ "realm": { "type": "string" },
7
+ "username": { "type": "string" },
8
+ "password": {
9
+ "type": "string",
10
+ "required": true
11
+ },
12
+ "email": {
13
+ "type": "string",
14
+ "required": true
15
+ },
16
+ "emailVerified": "boolean",
17
+ "verificationToken": "string"
18
+ },
19
+ "options": { "caseSensitiveEmail": true },
20
+ "hidden": ["password", "verificationToken"],
21
+ "acls": [
22
+ {
23
+ "principalType": "ROLE",
24
+ "principalId": "$everyone",
25
+ "permission": "DENY"
26
+ },
27
+ {
28
+ "principalType": "ROLE",
29
+ "principalId": "$everyone",
30
+ "permission": "ALLOW",
31
+ "property": "create"
32
+ },
33
+ {
34
+ "principalType": "ROLE",
35
+ "principalId": "$owner",
36
+ "permission": "ALLOW",
37
+ "property": "deleteById"
38
+ },
39
+ {
40
+ "principalType": "ROLE",
41
+ "principalId": "$everyone",
42
+ "permission": "ALLOW",
43
+ "property": "login"
44
+ },
45
+ {
46
+ "principalType": "ROLE",
47
+ "principalId": "$everyone",
48
+ "permission": "ALLOW",
49
+ "property": "logout"
50
+ },
51
+ {
52
+ "principalType": "ROLE",
53
+ "principalId": "$owner",
54
+ "permission": "ALLOW",
55
+ "property": "findById"
56
+ },
57
+ {
58
+ "principalType": "ROLE",
59
+ "principalId": "$owner",
60
+ "permission": "ALLOW",
61
+ "property": "patchAttributes"
62
+ },
63
+ {
64
+ "principalType": "ROLE",
65
+ "principalId": "$owner",
66
+ "permission": "ALLOW",
67
+ "property": "replaceById"
68
+ },
69
+ {
70
+ "principalType": "ROLE",
71
+ "principalId": "$everyone",
72
+ "permission": "ALLOW",
73
+ "property": "verify",
74
+ "accessType": "EXECUTE"
75
+ },
76
+ {
77
+ "principalType": "ROLE",
78
+ "principalId": "$everyone",
79
+ "permission": "ALLOW",
80
+ "property": "confirm"
81
+ },
82
+ {
83
+ "principalType": "ROLE",
84
+ "principalId": "$everyone",
85
+ "permission": "ALLOW",
86
+ "property": "resetPassword",
87
+ "accessType": "EXECUTE"
88
+ },
89
+ {
90
+ "principalType": "ROLE",
91
+ "principalId": "$authenticated",
92
+ "permission": "ALLOW",
93
+ "property": "changePassword",
94
+ "accessType": "EXECUTE"
95
+ },
96
+ {
97
+ "principalType": "ROLE",
98
+ "principalId": "$authenticated",
99
+ "permission": "ALLOW",
100
+ "property": "setPassword",
101
+ "accessType": "EXECUTE"
102
+ }
103
+ ],
104
+ "relations": { "accessTokens": {
105
+ "type": "hasMany",
106
+ "model": "AccessToken",
107
+ "foreignKey": "userId",
108
+ "options": { "disableInclude": true }
109
+ } }
110
+ };
111
+ }));
112
+ //#endregion
113
+ Object.defineProperty(exports, "default", {
114
+ enumerable: true,
115
+ get: function() {
116
+ return require_user();
117
+ }
118
+ });
package/dist/index.cjs ADDED
@@ -0,0 +1,16 @@
1
+ const require_lib_loopback = require("./lib/loopback.cjs");
2
+ const require_lib_connectors_base_connector = require("./lib/connectors/base-connector.cjs");
3
+ const require_lib_connectors_memory = require("./lib/connectors/memory.cjs");
4
+ const require_lib_connectors_mail = require("./lib/connectors/mail.cjs");
5
+ //#region src/index.ts
6
+ const loopback = require_lib_loopback;
7
+ require("@vsaas/loopback-datasource-juggler");
8
+ loopback.Connector = require_lib_connectors_base_connector;
9
+ loopback.Memory = require_lib_connectors_memory;
10
+ loopback.Mail = require_lib_connectors_mail;
11
+ loopback.Remote = require("@vsaas/loopback-connector-remote");
12
+ loopback.GeoPoint = require("@vsaas/loopback-datasource-juggler/geo").GeoPoint;
13
+ loopback.DateString = require("@vsaas/loopback-datasource-juggler/date-string");
14
+ loopback.ValidationError = loopback.Model.ValidationError;
15
+ module.exports = loopback;
16
+ //#endregion
@@ -0,0 +1,228 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const require_runtime$1 = require("../_virtual/_rolldown/runtime.cjs");
4
+ const require_lib_runtime = require("./runtime.cjs");
5
+ //#region src/lib/access-context.ts
6
+ var access_context_exports = /* @__PURE__ */ require_runtime$1.__exportAll({
7
+ AccessContext: () => AccessContext,
8
+ AccessRequest: () => AccessRequest,
9
+ DEFAULT_SCOPES: () => DEFAULT_SCOPES,
10
+ Principal: () => Principal
11
+ });
12
+ function AccessContext(context) {
13
+ if (!(this instanceof AccessContext)) return new AccessContext(context);
14
+ context = context || {};
15
+ assert(context.registry, "Application registry is mandatory in AccessContext but missing in provided context");
16
+ this.registry = context.registry;
17
+ this.principals = context.principals || [];
18
+ let model = context.model;
19
+ model = typeof model === "string" ? this.registry.getModel(model) : model;
20
+ this.model = model;
21
+ this.modelName = model && model.modelName;
22
+ this.modelId = context.id || context.modelId;
23
+ this.property = context.property || AccessContext.ALL;
24
+ this.method = context.method;
25
+ this.sharedMethod = context.sharedMethod;
26
+ this.sharedClass = this.sharedMethod && this.sharedMethod.sharedClass;
27
+ if (this.sharedMethod) this.methodNames = this.sharedMethod.aliases.concat([this.sharedMethod.name]);
28
+ else this.methodNames = [];
29
+ if (this.sharedMethod) this.accessType = this.model._getAccessTypeForMethod(this.sharedMethod);
30
+ this.accessType = context.accessType || AccessContext.ALL;
31
+ const loopback = runtime.loopback;
32
+ assert(loopback.AccessToken, "AccessToken model must be defined before AccessContext model");
33
+ this.accessToken = context.accessToken || loopback.AccessToken.ANONYMOUS;
34
+ const principalType = context.principalType || Principal.USER;
35
+ const principalId = context.principalId || void 0;
36
+ const principalName = context.principalName || void 0;
37
+ if (principalId != null) this.addPrincipal(principalType, principalId, principalName);
38
+ const token = this.accessToken;
39
+ if (token.userId != null) this.addPrincipal(token.principalType || Principal.USER, token.userId);
40
+ if (token.appId != null) this.addPrincipal(Principal.APPLICATION, token.appId);
41
+ this.remotingContext = context.remotingContext;
42
+ }
43
+ function Principal(type, id, name) {
44
+ if (!(this instanceof Principal)) return new Principal(type, id, name);
45
+ this.type = type;
46
+ this.id = id;
47
+ this.name = name;
48
+ }
49
+ function AccessRequest(model, property, accessType, permission, methodNames, registry) {
50
+ if (!(this instanceof AccessRequest)) return new AccessRequest(model, property, accessType, permission, methodNames, registry);
51
+ if (arguments.length === 1 && typeof model === "object") {
52
+ const obj = model || {};
53
+ this.model = obj.model || AccessContext.ALL;
54
+ this.property = obj.property || AccessContext.ALL;
55
+ this.accessType = obj.accessType || AccessContext.ALL;
56
+ this.permission = obj.permission || AccessContext.DEFAULT;
57
+ this.methodNames = obj.methodNames || [];
58
+ this.registry = obj.registry;
59
+ } else {
60
+ this.model = model || AccessContext.ALL;
61
+ this.property = property || AccessContext.ALL;
62
+ this.accessType = accessType || AccessContext.ALL;
63
+ this.permission = permission || AccessContext.DEFAULT;
64
+ this.methodNames = methodNames || [];
65
+ this.registry = registry;
66
+ }
67
+ assert(this.registry, "Application registry is mandatory in AccessRequest but missing in provided argument(s)");
68
+ }
69
+ var assert, runtime, debug, DEFAULT_SCOPES;
70
+ var init_access_context = require_runtime$1.__esmMin((() => {
71
+ assert = require("assert");
72
+ runtime = require_lib_runtime;
73
+ debug = require("debug")("loopback:security:access-context");
74
+ DEFAULT_SCOPES = ["DEFAULT"];
75
+ AccessContext.ALL = "*";
76
+ AccessContext.READ = "READ";
77
+ AccessContext.REPLICATE = "REPLICATE";
78
+ AccessContext.WRITE = "WRITE";
79
+ AccessContext.EXECUTE = "EXECUTE";
80
+ AccessContext.DEFAULT = "DEFAULT";
81
+ AccessContext.ALLOW = "ALLOW";
82
+ AccessContext.ALARM = "ALARM";
83
+ AccessContext.AUDIT = "AUDIT";
84
+ AccessContext.DENY = "DENY";
85
+ AccessContext.permissionOrder = {
86
+ DEFAULT: 0,
87
+ ALLOW: 1,
88
+ ALARM: 2,
89
+ AUDIT: 3,
90
+ DENY: 4
91
+ };
92
+ AccessContext.prototype.addPrincipal = function(principalType, principalId, principalName) {
93
+ const principal = new Principal(principalType, principalId, principalName);
94
+ for (let i = 0; i < this.principals.length; i++) if (this.principals[i].equals(principal)) return false;
95
+ this.principals.push(principal);
96
+ return true;
97
+ };
98
+ AccessContext.prototype.getUserId = function() {
99
+ const user = this.getUser();
100
+ return user && user.id;
101
+ };
102
+ AccessContext.prototype.getUser = function() {
103
+ const BaseUser = this.registry.getModel("User");
104
+ for (let i = 0; i < this.principals.length; i++) {
105
+ const p = this.principals[i];
106
+ if (p.type === Principal.APP || p.type === Principal.ROLE || p.type == Principal.SCOPE) continue;
107
+ if (p.type === Principal.USER) return {
108
+ id: p.id,
109
+ principalType: p.type
110
+ };
111
+ const userModel = this.registry.findModel(p.type);
112
+ if (!userModel) continue;
113
+ if (userModel.prototype instanceof BaseUser) return {
114
+ id: p.id,
115
+ principalType: p.type
116
+ };
117
+ }
118
+ };
119
+ AccessContext.prototype.getAppId = function() {
120
+ for (let i = 0; i < this.principals.length; i++) {
121
+ const p = this.principals[i];
122
+ if (p.type === Principal.APPLICATION) return p.id;
123
+ }
124
+ return null;
125
+ };
126
+ AccessContext.prototype.isAuthenticated = function() {
127
+ return this.getUserId() != null || this.getAppId() != null;
128
+ };
129
+ AccessContext.prototype.getScopes = function() {
130
+ if (!this.sharedMethod) return DEFAULT_SCOPES;
131
+ const methodLevel = this.sharedMethod.accessScopes || DEFAULT_SCOPES;
132
+ debug("--Context scopes of %s()--", this.sharedMethod.stringName);
133
+ debug(" method-level: %j", methodLevel);
134
+ return methodLevel;
135
+ };
136
+ AccessContext.prototype.isScopeAllowed = function() {
137
+ if (!this.accessToken) return false;
138
+ const tokenScopes = this.accessToken.scopes || DEFAULT_SCOPES;
139
+ const resourceScopes = this.getScopes();
140
+ return Array.isArray(tokenScopes) && Array.isArray(resourceScopes) && resourceScopes.some((scope) => tokenScopes.indexOf(scope) !== -1);
141
+ };
142
+ AccessContext.prototype.debug = function() {
143
+ if (debug.enabled) {
144
+ debug("---AccessContext---");
145
+ if (this.principals && this.principals.length) {
146
+ debug("principals:");
147
+ this.principals.forEach(function(principal) {
148
+ debug("principal: %j", principal);
149
+ });
150
+ } else debug("principals: %j", this.principals);
151
+ debug("modelName %s", this.modelName);
152
+ debug("modelId %s", this.modelId);
153
+ debug("property %s", this.property);
154
+ debug("method %s", this.method);
155
+ debug("accessType %s", this.accessType);
156
+ debug("accessScopes %j", this.getScopes());
157
+ if (this.accessToken) {
158
+ debug("accessToken:");
159
+ debug(" id %j", this.accessToken.id);
160
+ debug(" ttl %j", this.accessToken.ttl);
161
+ debug(" scopes %j", this.accessToken.scopes || DEFAULT_SCOPES);
162
+ }
163
+ debug("getUserId() %s", this.getUserId());
164
+ debug("isAuthenticated() %s", this.isAuthenticated());
165
+ }
166
+ };
167
+ Principal.USER = "USER";
168
+ Principal.APP = Principal.APPLICATION = "APP";
169
+ Principal.ROLE = "ROLE";
170
+ Principal.SCOPE = "SCOPE";
171
+ Principal.prototype.equals = function(p) {
172
+ const other = p;
173
+ if (other instanceof Principal) return this.type === other.type && String(this.id) === String(other.id);
174
+ return false;
175
+ };
176
+ AccessRequest.prototype.isWildcard = function() {
177
+ return this.model === AccessContext.ALL || this.property === AccessContext.ALL || this.accessType === AccessContext.ALL;
178
+ };
179
+ AccessRequest.prototype.exactlyMatches = function(acl) {
180
+ const matchesModel = acl.model === this.model;
181
+ const matchesProperty = acl.property === this.property;
182
+ const matchesMethodName = this.methodNames.indexOf(acl.property) !== -1;
183
+ const matchesAccessType = acl.accessType === this.accessType;
184
+ if (matchesModel && matchesAccessType) return matchesProperty || matchesMethodName;
185
+ return false;
186
+ };
187
+ AccessRequest.prototype.settleDefaultPermission = function(defaultPermission) {
188
+ if (this.permission !== "DEFAULT") return;
189
+ const modelName = this.model;
190
+ if (!defaultPermission) {
191
+ const modelClass = this.registry.findModel(modelName);
192
+ defaultPermission = modelClass && modelClass.settings.defaultPermission;
193
+ }
194
+ this.permission = defaultPermission || "ALLOW";
195
+ };
196
+ AccessRequest.prototype.isAllowed = function() {
197
+ return this.permission !== this.registry.getModel("ACL").DENY;
198
+ };
199
+ AccessRequest.prototype.debug = function() {
200
+ if (debug.enabled) {
201
+ debug("---AccessRequest---");
202
+ debug(" model %s", this.model);
203
+ debug(" property %s", this.property);
204
+ debug(" accessType %s", this.accessType);
205
+ debug(" permission %s", this.permission);
206
+ debug(" isWildcard() %s", this.isWildcard());
207
+ debug(" isAllowed() %s", this.isAllowed());
208
+ }
209
+ };
210
+ }));
211
+ //#endregion
212
+ init_access_context();
213
+ exports.AccessContext = AccessContext;
214
+ exports.AccessRequest = AccessRequest;
215
+ exports.DEFAULT_SCOPES = DEFAULT_SCOPES;
216
+ exports.Principal = Principal;
217
+ Object.defineProperty(exports, "access_context_exports", {
218
+ enumerable: true,
219
+ get: function() {
220
+ return access_context_exports;
221
+ }
222
+ });
223
+ Object.defineProperty(exports, "init_access_context", {
224
+ enumerable: true,
225
+ get: function() {
226
+ return init_access_context;
227
+ }
228
+ });