@pegasimed.com/pegasi-sail-connect-shared 0.1.0-dev.2 → 0.2.0-dev.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 (45) hide show
  1. package/README.md +190 -67
  2. package/dist/authorization/index.d.ts +3 -0
  3. package/dist/authorization/index.d.ts.map +1 -0
  4. package/dist/authorization/index.js +3 -0
  5. package/dist/authorization/index.js.map +1 -0
  6. package/dist/authorization/internal-authorization.d.ts +105 -0
  7. package/dist/authorization/internal-authorization.d.ts.map +1 -0
  8. package/dist/authorization/internal-authorization.js +287 -0
  9. package/dist/authorization/internal-authorization.js.map +1 -0
  10. package/dist/capabilities/catalog-data.d.ts +268 -858
  11. package/dist/capabilities/catalog-data.d.ts.map +1 -1
  12. package/dist/capabilities/catalog-data.js +273 -883
  13. package/dist/capabilities/catalog-data.js.map +1 -1
  14. package/dist/capabilities/catalog.d.ts +17 -20
  15. package/dist/capabilities/catalog.d.ts.map +1 -1
  16. package/dist/capabilities/catalog.js +34 -13
  17. package/dist/capabilities/catalog.js.map +1 -1
  18. package/dist/capabilities/list.d.ts +25 -3
  19. package/dist/capabilities/list.d.ts.map +1 -1
  20. package/dist/capabilities/list.js +39 -5
  21. package/dist/capabilities/list.js.map +1 -1
  22. package/dist/capabilities/match.d.ts +13 -14
  23. package/dist/capabilities/match.d.ts.map +1 -1
  24. package/dist/capabilities/match.js +25 -38
  25. package/dist/capabilities/match.js.map +1 -1
  26. package/dist/capabilities/migration-data.d.ts +157 -97
  27. package/dist/capabilities/migration-data.d.ts.map +1 -1
  28. package/dist/capabilities/migration-data.js +160 -93
  29. package/dist/capabilities/migration-data.js.map +1 -1
  30. package/dist/capabilities/migration.d.ts +52 -28
  31. package/dist/capabilities/migration.d.ts.map +1 -1
  32. package/dist/capabilities/migration.js +36 -70
  33. package/dist/capabilities/migration.js.map +1 -1
  34. package/dist/capabilities/types.d.ts +26 -10
  35. package/dist/capabilities/types.d.ts.map +1 -1
  36. package/dist/capabilities/validate.d.ts +4 -2
  37. package/dist/capabilities/validate.d.ts.map +1 -1
  38. package/dist/capabilities/validate.js +32 -78
  39. package/dist/capabilities/validate.js.map +1 -1
  40. package/dist/index.d.ts +1 -0
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +1 -0
  43. package/dist/index.js.map +1 -1
  44. package/package.json +6 -2
  45. package/scripts/check-service-keys.mjs +46 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pegasimed.com/pegasi-sail-connect-shared",
3
- "version": "0.1.0-dev.2",
3
+ "version": "0.2.0-dev.0",
4
4
  "description": "Versioned shared TypeScript helpers for PEGASI SAIL/CONNECT services.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -53,6 +53,10 @@
53
53
  "./capabilities": {
54
54
  "types": "./dist/capabilities/index.d.ts",
55
55
  "import": "./dist/capabilities/index.js"
56
+ },
57
+ "./authorization": {
58
+ "types": "./dist/authorization/index.d.ts",
59
+ "import": "./dist/authorization/index.js"
56
60
  }
57
61
  },
58
62
  "files": [
@@ -66,7 +70,7 @@
66
70
  },
67
71
  "scripts": {
68
72
  "build": "tsc -p tsconfig.json",
69
- "check": "tsc -p tsconfig.json --noEmit && tsc -p test/capabilities/tsconfig.json",
73
+ "check": "tsc -p tsconfig.json --noEmit && tsc -p test/capabilities/tsconfig.json && tsc -p test/authorization/tsconfig.json",
70
74
  "prepare": "npm run build",
71
75
  "lint": "eslint .",
72
76
  "test": "vitest run --coverage",
@@ -11,8 +11,12 @@
11
11
  // `patient:read`, `access-api:users:write`). The service still uses the
12
12
  // old grammar; migrate it to its canonical key.
13
13
  // - "drift": the literal has the shape `servicio:recurso:acción` (three
14
- // segments of the catalog grammar, `*` allowed in resource and action)
15
- // but is neither a catalog operation nor a listed wildcard grant.
14
+ // segments of the catalog grammar) but is not a catalog key; or it is a
15
+ // key-shaped literal with `*` anywhere in a segment (`patient:dom*:read`,
16
+ // `patient:**:read`, `*:*:*`), a two-segment `servicio:*` of a catalog
17
+ // service, or an old key with `*` (`meetings:*`). A literal with `*` is
18
+ // always drift, before "old": there are no wildcard grants, every grant is
19
+ // an explicit catalog key.
16
20
  //
17
21
  // Keys built at run time (template literals with substitutions,
18
22
  // concatenations) are not visible here: the authorization helper of the
@@ -45,8 +49,31 @@ const SOURCE_EXTENSIONS = new Set([".ts", ".tsx", ".mts", ".cts", ".js", ".jsx",
45
49
  const SKIP_DIRS = new Set(["node_modules", "dist", "build", "coverage", ".git", ".next", ".turbo"]);
46
50
  const TEST_FILE_PATTERNS = [/\.test\.[mc]?[jt]sx?$/i, /\.spec\.[mc]?[jt]sx?$/i, /(^|[\\/])(test|tests|__tests__)[\\/]/i];
47
51
  const SEGMENT = "[a-z][a-z0-9]*(?:-[a-z0-9]+)*";
48
- /** Three segments of the catalog grammar; `*` only in resource and action. */
49
- export const KEY_SHAPE = new RegExp(`^${SEGMENT}:(?:${SEGMENT}|\\*):(?:${SEGMENT}|\\*)$`);
52
+ const SHAPED_SEGMENT = `(?:${SEGMENT}|\\*)`;
53
+ /** Three segments of the catalog grammar, or `*` in any of them (always drift: wildcards are forbidden). */
54
+ export const KEY_SHAPE = new RegExp(`^${SHAPED_SEGMENT}:${SHAPED_SEGMENT}:${SHAPED_SEGMENT}$`);
55
+ // A segment that may hold `*` anywhere: `dom*`, `pat*ient`, `**`, `*`.
56
+ const WILDCARD_SEGMENT = "[a-z*][a-z0-9*]*(?:-[a-z0-9*]+)*";
57
+ /** Three key-shaped segments with `*` allowed anywhere in them. */
58
+ export const WILDCARD_KEY_SHAPE = new RegExp(`^${WILDCARD_SEGMENT}:${WILDCARD_SEGMENT}:${WILDCARD_SEGMENT}$`);
59
+ const TWO_SEGMENT_WILDCARD = new RegExp(`^(${SEGMENT}):${WILDCARD_SEGMENT}$`);
60
+
61
+ /**
62
+ * True for a literal with `*` that has the shape of a key: three segments
63
+ * with `*` anywhere, or two segments (`servicio:*`, `servicio:recu*`) whose
64
+ * first segment is a catalog service. Other text with `*` (a MIME type, a
65
+ * glob, `image:*` of no service) is not a permission.
66
+ */
67
+ export function isWildcardKeyLiteral(text, services) {
68
+ if (!text.includes("*")) {
69
+ return false;
70
+ }
71
+ if (WILDCARD_KEY_SHAPE.test(text)) {
72
+ return true;
73
+ }
74
+ const match = TWO_SEGMENT_WILDCARD.exec(text);
75
+ return match !== null && services.has(match[1]);
76
+ }
50
77
  const ALLOW_COMMENT = /\/\/\s*allow-service-key\s*:\s*\S/i;
51
78
  const USAGE = "Usage: node scripts/check-service-keys.mjs <source-dir> [<source-dir>...] [--json] [--include-tests]\n";
52
79
 
@@ -104,19 +131,23 @@ export function collectStringLiterals(sourceText, fileName) {
104
131
 
105
132
  /**
106
133
  * Findings of the literals of the given source files. `catalog` supplies
107
- * `findUnknownPermissionKeys` and the old keys of PERMISSION_MIGRATION.
134
+ * `findUnknownPermissionKeys`, the old keys of PERMISSION_MIGRATION and the
135
+ * catalog services.
108
136
  */
109
137
  export function scanFiles(files, catalog, { root = cwd() } = {}) {
110
138
  const oldKeys = new Set(catalog.oldKeys);
139
+ const services = new Set(catalog.services ?? []);
111
140
  const findings = [];
112
141
  let allowed = 0;
113
142
  for (const file of files) {
114
143
  for (const literal of collectStringLiterals(readFileSync(file, "utf8"), file)) {
144
+ // A literal with `*` in key shape (or an old key with `*`) is always drift, before "old".
145
+ const wildcard = literal.text.includes("*") && (oldKeys.has(literal.text) || isWildcardKeyLiteral(literal.text, services));
115
146
  // A catalog key is never a finding, even when it is also an old key kept as is.
116
- if (!(oldKeys.has(literal.text) || KEY_SHAPE.test(literal.text)) || catalog.findUnknownPermissionKeys([literal.text]).length === 0) {
147
+ if (!wildcard && (!(oldKeys.has(literal.text) || KEY_SHAPE.test(literal.text)) || catalog.findUnknownPermissionKeys([literal.text]).length === 0)) {
117
148
  continue;
118
149
  }
119
- const kind = oldKeys.has(literal.text) ? "old" : "drift";
150
+ const kind = !wildcard && oldKeys.has(literal.text) ? "old" : "drift";
120
151
  if (literal.allowed) {
121
152
  allowed += 1;
122
153
  continue;
@@ -172,7 +203,12 @@ export function run(args, { catalog, write = (text) => stdout.write(text), write
172
203
  write(`${JSON.stringify({ catalogVersion: catalog.version, ...result, ...summary }, null, 2)}\n`);
173
204
  } else {
174
205
  for (const finding of result.findings) {
175
- const hint = finding.kind === "old" ? "old key: use its canonical key (PERMISSION_MIGRATION)" : "not in the capability catalog";
206
+ const hint =
207
+ finding.kind === "old"
208
+ ? "old key: use its canonical key (PERMISSION_MIGRATION)"
209
+ : finding.key.includes("*")
210
+ ? "wildcards are forbidden: require and grant explicit catalog keys"
211
+ : "not in the capability catalog";
176
212
  write(`${finding.file}:${finding.line}:${finding.column} ${finding.kind} ${finding.key} — ${hint}\n`);
177
213
  }
178
214
  write(
@@ -188,7 +224,8 @@ async function loadCatalog() {
188
224
  return {
189
225
  version: module.CAPABILITY_CATALOG_VERSION,
190
226
  findUnknownPermissionKeys: module.findUnknownPermissionKeys,
191
- oldKeys: module.PERMISSION_MIGRATION.entries.map((entry) => entry.old)
227
+ oldKeys: module.PERMISSION_MIGRATION.entries.map((entry) => entry.old),
228
+ services: module.CAPABILITY_SERVICES
192
229
  };
193
230
  }
194
231