@aeriajs/security 0.0.46 → 0.0.47

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/define.mjs CHANGED
@@ -1,3 +1,4 @@
1
- export var defineSecurityPolicy = function(policy) {
2
- return policy;
1
+ "use strict";
2
+ export const defineSecurityPolicy = (policy) => {
3
+ return policy;
3
4
  };
@@ -1,326 +1,50 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
- try {
3
- var info = gen[key](arg);
4
- var value = info.value;
5
- } catch (error) {
6
- reject(error);
7
- return;
8
- }
9
- if (info.done) {
10
- resolve(value);
11
- } else {
12
- Promise.resolve(value).then(_next, _throw);
13
- }
14
- }
15
- function _async_to_generator(fn) {
16
- return function() {
17
- var self = this, args = arguments;
18
- return new Promise(function(resolve, reject) {
19
- var gen = fn.apply(self, args);
20
- function _next(value) {
21
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
- }
23
- function _throw(err) {
24
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
- }
26
- _next(undefined);
27
- });
28
- };
29
- }
30
- function _define_property(obj, key, value) {
31
- if (key in obj) {
32
- Object.defineProperty(obj, key, {
33
- value: value,
34
- enumerable: true,
35
- configurable: true,
36
- writable: true
37
- });
38
- } else {
39
- obj[key] = value;
40
- }
41
- return obj;
42
- }
43
- function _object_spread(target) {
44
- for(var i = 1; i < arguments.length; i++){
45
- var source = arguments[i] != null ? arguments[i] : {};
46
- var ownKeys = Object.keys(source);
47
- if (typeof Object.getOwnPropertySymbols === "function") {
48
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
49
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
50
- }));
51
- }
52
- ownKeys.forEach(function(key) {
53
- _define_property(target, key, source[key]);
54
- });
55
- }
56
- return target;
57
- }
58
- function ownKeys(object, enumerableOnly) {
59
- var keys = Object.keys(object);
60
- if (Object.getOwnPropertySymbols) {
61
- var symbols = Object.getOwnPropertySymbols(object);
62
- if (enumerableOnly) {
63
- symbols = symbols.filter(function(sym) {
64
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
65
- });
66
- }
67
- keys.push.apply(keys, symbols);
68
- }
69
- return keys;
70
- }
71
- function _object_spread_props(target, source) {
72
- source = source != null ? source : {};
73
- if (Object.getOwnPropertyDescriptors) {
74
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
75
- } else {
76
- ownKeys(Object(source)).forEach(function(key) {
77
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
78
- });
79
- }
80
- return target;
81
- }
82
- function _ts_generator(thisArg, body) {
83
- var f, y, t, g, _ = {
84
- label: 0,
85
- sent: function() {
86
- if (t[0] & 1) throw t[1];
87
- return t[1];
88
- },
89
- trys: [],
90
- ops: []
91
- };
92
- return g = {
93
- next: verb(0),
94
- "throw": verb(1),
95
- "return": verb(2)
96
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
97
- return this;
98
- }), g;
99
- function verb(n) {
100
- return function(v) {
101
- return step([
102
- n,
103
- v
104
- ]);
105
- };
106
- }
107
- function step(op) {
108
- if (f) throw new TypeError("Generator is already executing.");
109
- while(_)try {
110
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
111
- if (y = 0, t) op = [
112
- op[0] & 2,
113
- t.value
114
- ];
115
- switch(op[0]){
116
- case 0:
117
- case 1:
118
- t = op;
119
- break;
120
- case 4:
121
- _.label++;
122
- return {
123
- value: op[1],
124
- done: false
125
- };
126
- case 5:
127
- _.label++;
128
- y = op[1];
129
- op = [
130
- 0
131
- ];
132
- continue;
133
- case 7:
134
- op = _.ops.pop();
135
- _.trys.pop();
136
- continue;
137
- default:
138
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
139
- _ = 0;
140
- continue;
141
- }
142
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
143
- _.label = op[1];
144
- break;
145
- }
146
- if (op[0] === 6 && _.label < t[1]) {
147
- _.label = t[1];
148
- t = op;
149
- break;
150
- }
151
- if (t && _.label < t[2]) {
152
- _.label = t[2];
153
- _.ops.push(op);
154
- break;
155
- }
156
- if (t[2]) _.ops.pop();
157
- _.trys.pop();
158
- continue;
159
- }
160
- op = body.call(thisArg, _);
161
- } catch (e) {
162
- op = [
163
- 6,
164
- e
165
- ];
166
- y = 0;
167
- } finally{
168
- f = t = 0;
169
- }
170
- if (op[0] & 5) throw op[1];
171
- return {
172
- value: op[0] ? op[1] : void 0,
173
- done: true
174
- };
175
- }
176
- }
1
+ "use strict";
177
2
  import { ACErrors } from "@aeriajs/types";
178
3
  import { ObjectId } from "mongodb";
179
4
  import { left, right, isLeft } from "@aeriajs/common";
180
- var internalCheckImmutability = function() {
181
- var _ref = _async_to_generator(function(props, context) {
182
- var _props_propertyName, propertyName, parentId, childId, payload, description, source, property, immutable, currentDocument, fulfilled;
183
- return _ts_generator(this, function(_state) {
184
- switch(_state.label){
185
- case 0:
186
- _props_propertyName = props.propertyName, propertyName = _props_propertyName === void 0 ? "" : _props_propertyName, parentId = props.parentId, childId = props.childId, payload = props.payload;
187
- description = context.description;
188
- source = "what" in payload ? payload.what : payload.filters;
189
- property = description.properties[propertyName];
190
- if (!property) {
191
- return [
192
- 2,
193
- right(props.payload)
194
- ];
195
- }
196
- immutable = parentId && (description.immutable === true || Array.isArray(description.immutable) && description.immutable.includes(propertyName));
197
- return [
198
- 4,
199
- context.collection.model.findOne({
200
- _id: new ObjectId(parentId)
201
- })
202
- ];
203
- case 1:
204
- currentDocument = _state.sent();
205
- if (!currentDocument) {
206
- return [
207
- 2,
208
- left(ACErrors.ImmutabilityParentNotFound)
209
- ];
210
- }
211
- if (childId) {
212
- if (Array.isArray(currentDocument[propertyName]) && !currentDocument[propertyName].some(function(child) {
213
- return child.toString() === childId;
214
- }) || !Array.isArray(currentDocument[propertyName]) && currentDocument[propertyName] && currentDocument[propertyName] !== childId.toString()) {
215
- return [
216
- 2,
217
- left(ACErrors.ImmutabilityIncorrectChild)
218
- ];
219
- }
220
- }
221
- fulfilled = currentDocument[propertyName] && typeof currentDocument[propertyName] === "object" && !Object.keys(currentDocument[propertyName]).length;
222
- if (immutable && fulfilled && (property.inline || currentDocument[propertyName].toString() !== source[propertyName])) {
223
- return [
224
- 2,
225
- left(ACErrors.ImmutabilityTargetImmutable)
226
- ];
227
- }
228
- return [
229
- 2,
230
- right(props.payload)
231
- ];
232
- }
233
- });
234
- });
235
- return function internalCheckImmutability(props, context) {
236
- return _ref.apply(this, arguments);
237
- };
238
- }();
239
- export var checkImmutability = function() {
240
- var _ref = _async_to_generator(function(props, context) {
241
- var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, propertyName, result, err;
242
- return _ts_generator(this, function(_state) {
243
- switch(_state.label){
244
- case 0:
245
- if (!props.parentId) {
246
- return [
247
- 2,
248
- right(props.payload)
249
- ];
250
- }
251
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
252
- _state.label = 1;
253
- case 1:
254
- _state.trys.push([
255
- 1,
256
- 6,
257
- 7,
258
- 8
259
- ]);
260
- _iterator = Object.keys(props.payload)[Symbol.iterator]();
261
- _state.label = 2;
262
- case 2:
263
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
264
- 3,
265
- 5
266
- ];
267
- propertyName = _step.value;
268
- return [
269
- 4,
270
- internalCheckImmutability(_object_spread_props(_object_spread({}, props), {
271
- propertyName: propertyName
272
- }), context)
273
- ];
274
- case 3:
275
- result = _state.sent();
276
- if (isLeft(result)) {
277
- return [
278
- 2,
279
- result
280
- ];
281
- }
282
- _state.label = 4;
283
- case 4:
284
- _iteratorNormalCompletion = true;
285
- return [
286
- 3,
287
- 2
288
- ];
289
- case 5:
290
- return [
291
- 3,
292
- 8
293
- ];
294
- case 6:
295
- err = _state.sent();
296
- _didIteratorError = true;
297
- _iteratorError = err;
298
- return [
299
- 3,
300
- 8
301
- ];
302
- case 7:
303
- try {
304
- if (!_iteratorNormalCompletion && _iterator.return != null) {
305
- _iterator.return();
306
- }
307
- } finally{
308
- if (_didIteratorError) {
309
- throw _iteratorError;
310
- }
311
- }
312
- return [
313
- 7
314
- ];
315
- case 8:
316
- return [
317
- 2,
318
- internalCheckImmutability(props, context)
319
- ];
320
- }
321
- });
322
- });
323
- return function checkImmutability(props, context) {
324
- return _ref.apply(this, arguments);
325
- };
326
- }();
5
+ const internalCheckImmutability = async (props, context) => {
6
+ const {
7
+ propertyName = "",
8
+ parentId,
9
+ childId,
10
+ payload
11
+ } = props;
12
+ const { description } = context;
13
+ const source = "what" in payload ? payload.what : payload.filters;
14
+ const property = description.properties[propertyName];
15
+ if (!property) {
16
+ return right(props.payload);
17
+ }
18
+ const immutable = parentId && (description.immutable === true || Array.isArray(description.immutable) && description.immutable.includes(propertyName));
19
+ const currentDocument = await context.collection.model.findOne({
20
+ _id: new ObjectId(parentId)
21
+ });
22
+ if (!currentDocument) {
23
+ return left(ACErrors.ImmutabilityParentNotFound);
24
+ }
25
+ if (childId) {
26
+ if (Array.isArray(currentDocument[propertyName]) && !currentDocument[propertyName].some((child) => child.toString() === childId) || !Array.isArray(currentDocument[propertyName]) && currentDocument[propertyName] && currentDocument[propertyName] !== childId.toString()) {
27
+ return left(ACErrors.ImmutabilityIncorrectChild);
28
+ }
29
+ }
30
+ const fulfilled = currentDocument[propertyName] && (typeof currentDocument[propertyName] === "object" && !Object.keys(currentDocument[propertyName]).length);
31
+ if (immutable && fulfilled && (property.inline || currentDocument[propertyName].toString() !== source[propertyName])) {
32
+ return left(ACErrors.ImmutabilityTargetImmutable);
33
+ }
34
+ return right(props.payload);
35
+ };
36
+ export const checkImmutability = async (props, context) => {
37
+ if (!props.parentId) {
38
+ return right(props.payload);
39
+ }
40
+ for (const propertyName of Object.keys(props.payload)) {
41
+ const result = await internalCheckImmutability({
42
+ ...props,
43
+ propertyName
44
+ }, context);
45
+ if (isLeft(result)) {
46
+ return result;
47
+ }
48
+ }
49
+ return internalCheckImmutability(props, context);
50
+ };
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  export * from "./define.mjs";
2
3
  export * from "./immutability.mjs";
3
4
  export * from "./ownership.mjs";
@@ -1,180 +1,29 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
- try {
3
- var info = gen[key](arg);
4
- var value = info.value;
5
- } catch (error) {
6
- reject(error);
7
- return;
1
+ "use strict";
2
+ import { ACErrors } from "@aeriajs/types";
3
+ import { left, right } from "@aeriajs/common";
4
+ export const checkOwnershipRead = async (props, context) => {
5
+ const { token, description } = context;
6
+ const payload = Object.assign({}, props.payload);
7
+ if (token.authenticated && description.owned) {
8
+ if (!token.roles.includes("root")) {
9
+ payload.filters.owner = token.sub;
8
10
  }
9
- if (info.done) {
10
- resolve(value);
11
+ }
12
+ return right(payload);
13
+ };
14
+ export const checkOwnershipWrite = async (props, context) => {
15
+ const { token, description } = context;
16
+ const { parentId } = props;
17
+ const payload = Object.assign({}, props.payload);
18
+ if (token.authenticated && description.owned) {
19
+ if (!payload.what._id || description.owned === "always") {
20
+ payload.what.owner = token.sub;
11
21
  } else {
12
- Promise.resolve(value).then(_next, _throw);
13
- }
14
- }
15
- function _async_to_generator(fn) {
16
- return function() {
17
- var self = this, args = arguments;
18
- return new Promise(function(resolve, reject) {
19
- var gen = fn.apply(self, args);
20
- function _next(value) {
21
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
- }
23
- function _throw(err) {
24
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
- }
26
- _next(undefined);
27
- });
28
- };
29
- }
30
- function _ts_generator(thisArg, body) {
31
- var f, y, t, g, _ = {
32
- label: 0,
33
- sent: function() {
34
- if (t[0] & 1) throw t[1];
35
- return t[1];
36
- },
37
- trys: [],
38
- ops: []
39
- };
40
- return g = {
41
- next: verb(0),
42
- "throw": verb(1),
43
- "return": verb(2)
44
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
45
- return this;
46
- }), g;
47
- function verb(n) {
48
- return function(v) {
49
- return step([
50
- n,
51
- v
52
- ]);
53
- };
22
+ return right(payload);
54
23
  }
55
- function step(op) {
56
- if (f) throw new TypeError("Generator is already executing.");
57
- while(_)try {
58
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
- if (y = 0, t) op = [
60
- op[0] & 2,
61
- t.value
62
- ];
63
- switch(op[0]){
64
- case 0:
65
- case 1:
66
- t = op;
67
- break;
68
- case 4:
69
- _.label++;
70
- return {
71
- value: op[1],
72
- done: false
73
- };
74
- case 5:
75
- _.label++;
76
- y = op[1];
77
- op = [
78
- 0
79
- ];
80
- continue;
81
- case 7:
82
- op = _.ops.pop();
83
- _.trys.pop();
84
- continue;
85
- default:
86
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
87
- _ = 0;
88
- continue;
89
- }
90
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
- _.label = op[1];
92
- break;
93
- }
94
- if (op[0] === 6 && _.label < t[1]) {
95
- _.label = t[1];
96
- t = op;
97
- break;
98
- }
99
- if (t && _.label < t[2]) {
100
- _.label = t[2];
101
- _.ops.push(op);
102
- break;
103
- }
104
- if (t[2]) _.ops.pop();
105
- _.trys.pop();
106
- continue;
107
- }
108
- op = body.call(thisArg, _);
109
- } catch (e) {
110
- op = [
111
- 6,
112
- e
113
- ];
114
- y = 0;
115
- } finally{
116
- f = t = 0;
117
- }
118
- if (op[0] & 5) throw op[1];
119
- return {
120
- value: op[0] ? op[1] : void 0,
121
- done: true
122
- };
123
- }
124
- }
125
- import { ACErrors } from "@aeriajs/types";
126
- import { left, right } from "@aeriajs/common";
127
- export var checkOwnershipRead = function() {
128
- var _ref = _async_to_generator(function(props, context) {
129
- var token, description, payload;
130
- return _ts_generator(this, function(_state) {
131
- token = context.token, description = context.description;
132
- payload = Object.assign({}, props.payload);
133
- if (token.authenticated && description.owned) {
134
- if (!token.roles.includes("root")) {
135
- payload.filters.owner = token.sub;
136
- }
137
- }
138
- return [
139
- 2,
140
- right(payload)
141
- ];
142
- });
143
- });
144
- return function checkOwnershipRead(props, context) {
145
- return _ref.apply(this, arguments);
146
- };
147
- }();
148
- export var checkOwnershipWrite = function() {
149
- var _ref = _async_to_generator(function(props, context) {
150
- var token, description, parentId, payload;
151
- return _ts_generator(this, function(_state) {
152
- token = context.token, description = context.description;
153
- parentId = props.parentId;
154
- payload = Object.assign({}, props.payload);
155
- if (token.authenticated && description.owned) {
156
- if (!payload.what._id || description.owned === "always") {
157
- payload.what.owner = token.sub;
158
- } else {
159
- return [
160
- 2,
161
- right(payload)
162
- ];
163
- }
164
- }
165
- if (!payload.what.owner && !parentId && context.description.owned) {
166
- return [
167
- 2,
168
- left(ACErrors.OwnershipError)
169
- ];
170
- }
171
- return [
172
- 2,
173
- right(payload)
174
- ];
175
- });
176
- });
177
- return function checkOwnershipWrite(props, context) {
178
- return _ref.apply(this, arguments);
179
- };
180
- }();
24
+ }
25
+ if (!payload.what.owner && !parentId && context.description.owned) {
26
+ return left(ACErrors.OwnershipError);
27
+ }
28
+ return right(payload);
29
+ };