@r2d2bzh/moleculer-authz-helpers 0.0.2 → 0.0.3

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/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@r2d2bzh/moleculer-authz-helpers",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Moleculer mixin to implement preflight runner",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "test": "c8 ava -v"
8
+ "pretest": "npm run lint",
9
+ "test": "c8 ava -v",
10
+ "lint": "eslint ."
9
11
  },
10
12
  "keywords": [],
11
13
  "author": "",
@@ -17,6 +19,7 @@
17
19
  "@r2d2bzh/js-rules": "^0.7.1",
18
20
  "ava": "^3.15.0",
19
21
  "c8": "^7.10.0",
22
+ "eslint": "^7.32.0",
20
23
  "moleculer": "^0.14.18",
21
24
  "uuid": "^8.3.2"
22
25
  },
@@ -33,7 +36,7 @@
33
36
  ]
34
37
  },
35
38
  "engines": {
36
- "node": ">=10.0.0"
39
+ "node": ">=12.0.0"
37
40
  },
38
41
  "peerDependencies": {
39
42
  "lodash": "^4.17.21",
@@ -1,7 +1,7 @@
1
1
  import cloneDeep from 'lodash/cloneDeep.js';
2
2
  import moleculer from 'moleculer';
3
+
3
4
  const {
4
- Service,
5
5
  Errors: { MoleculerError },
6
6
  } = moleculer;
7
7
 
@@ -10,14 +10,13 @@ export const unsafeAddPreflightMixin = (serviceSchema) => {
10
10
  ([, actionSpecification]) => actionSpecification?.preflight
11
11
  );
12
12
 
13
- const preflightActions = actionsWithPreflight.map(([actionName, { params, preflight } = {}]) => ([
14
- `${actionName}-preflight`,
15
- {
16
- handler: preflight instanceof Function ? preflight : preflight.handler,
17
- ...(params ? { params } : {}),
18
- },
19
- ])
20
- );
13
+ const preflightActions = actionsWithPreflight.map(([actionName, { params, preflight } = {}]) => [
14
+ `${actionName}-preflight`,
15
+ {
16
+ handler: preflight instanceof Function ? preflight : preflight.handler,
17
+ ...(params ? { params } : {}),
18
+ },
19
+ ]);
21
20
 
22
21
  const schema = cloneDeep(serviceSchema);
23
22
 
@@ -28,20 +27,20 @@ export const unsafeAddPreflightMixin = (serviceSchema) => {
28
27
  ...(serviceSchema.mixins || []),
29
28
  {
30
29
  actions: Object.fromEntries(preflightActions),
31
- }
30
+ },
32
31
  ];
33
32
 
34
33
  return schema;
35
34
  };
36
35
 
37
36
  export default (serviceSchema) => {
38
- Object.entries(serviceSchema.actions || {}).filter(
39
- ([, actionSpecification]) => (actionSpecification?.rest?.authorization !== false)
40
- ).forEach(([actionName, actionSpecification]) => {
41
- if (!((actionSpecification?.preflight instanceof Function) || actionSpecification?.preflight?.handler)) {
42
- throw new MoleculerError('missing preflight handler', 500, 'MISSING_PREFLIGHT', { actionName });
43
- }
44
- });
37
+ Object.entries(serviceSchema.actions || {})
38
+ .filter(([, actionSpecification]) => actionSpecification?.rest?.authorization !== false)
39
+ .forEach(([actionName, actionSpecification]) => {
40
+ if (!(actionSpecification?.preflight instanceof Function || actionSpecification?.preflight?.handler)) {
41
+ throw new MoleculerError('missing preflight handler', 500, 'MISSING_PREFLIGHT', { actionName });
42
+ }
43
+ });
45
44
 
46
45
  return unsafeAddPreflightMixin(serviceSchema);
47
- };
46
+ };
@@ -1,6 +1,19 @@
1
- const ensureBoolArray = (something) =>
2
- (Array.isArray(something) ? something : [something]).flat(Infinity).filter((response) => response !== undefined);
1
+ const removeUnanswered = (authzAnswers) => authzAnswers.filter((answer) => answer !== undefined);
3
2
 
4
- const someRequestsFailed = (booleanArray) => !booleanArray.length || booleanArray.includes(false);
3
+ const doAnswersAuthorize = (authzAnswers) =>
4
+ authzAnswers.length
5
+ ? authzAnswers.reduce((isAuthorized, authzAnswer) => isAuthorized && doesAnswerAuthorize(authzAnswer), true)
6
+ : false;
5
7
 
6
- export default (something) => !someRequestsFailed(ensureBoolArray(something));
8
+ const doesAnswerAuthorize = function (something) {
9
+ switch (Object.prototype.toString.call(something)) {
10
+ case '[object Boolean]':
11
+ return something;
12
+ case '[object Array]':
13
+ return doAnswersAuthorize(removeUnanswered(something));
14
+ default:
15
+ return false;
16
+ }
17
+ };
18
+
19
+ export default doesAnswerAuthorize;