@fabrika/auth-core 0.9.0-dev.22 → 0.9.0-dev.26

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/auth-core",
3
- "version": "0.9.0-dev.22",
3
+ "version": "0.9.0-dev.26",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -4,6 +4,7 @@
4
4
  import type {
5
5
  BodyAdmission,
6
6
  Caller,
7
+ ColumnPolicyArtifact,
7
8
  ColumnTestArtifact,
8
9
  ConditionArtifact,
9
10
  ConditionLiteralArtifact,
@@ -160,6 +161,7 @@ function canonicalTable(table: TablePolicyArtifact): TablePolicyArtifact {
160
161
  scope: table.scope.mode === 'child' ? { ...table.scope, inherit: sorted(table.scope.inherit) } : table.scope,
161
162
  insert: sorted(table.insert),
162
163
  update: sorted(table.update),
164
+ columns: mapValues(table.columns, canonicalColumn),
163
165
  ...(table.when === undefined ? {} : { when: mapValues(table.when, canonicalCondition) }),
164
166
  ...(table.relationWhen === undefined
165
167
  ? {}
@@ -170,6 +172,11 @@ function canonicalTable(table: TablePolicyArtifact): TablePolicyArtifact {
170
172
  }
171
173
  }
172
174
 
175
+ /** A column's list of read actions sorted; one action, and a column without its own read, as they are. */
176
+ function canonicalColumn(column: ColumnPolicyArtifact): ColumnPolicyArtifact {
177
+ return column.read === undefined || typeof column.read === 'string' ? column : { ...column, read: sorted(column.read) }
178
+ }
179
+
173
180
  function canonicalConfers(confers: ConfersArtifact): ConfersArtifact {
174
181
  return confers.when === undefined ? confers : { ...confers, when: canonicalCondition(confers.when) }
175
182
  }
package/src/contract.ts CHANGED
@@ -1020,9 +1020,9 @@ export interface ColumnTestArtifact {
1020
1020
  readonly gte?: ConditionLiteralArtifact | { readonly now: true }
1021
1021
  }
1022
1022
 
1023
- /** A column's own read action, its own write action, or both; an absent one is left out. */
1023
+ /** A column's own read actions (one as its text, several as a list), its own write action, or both; an absent one is left out. */
1024
1024
  export interface ColumnPolicyArtifact {
1025
- readonly read?: string
1025
+ readonly read?: TableActionArtifact
1026
1026
  readonly write?: string
1027
1027
  }
1028
1028
 
@@ -1,2 +1,72 @@
1
1
  /** A framework release as npm publishes it; the only form one may take before it reaches a generated build command. */
2
2
  export const FRAMEWORK_RELEASE_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/
3
+
4
+ /**
5
+ * Whether framework release `release` is at least `since`. A prerelease counts as its `major.minor`; a release that
6
+ * does not start with `<major>.<minor>.` is never at least anything (ADR-0062).
7
+ */
8
+ export const releaseAtLeast = (release: string, since: { readonly major: number; readonly minor: number }): boolean => {
9
+ const match = /^(\d+)\.(\d+)\./.exec(release)
10
+ if (match === null) return false
11
+ const major = Number(match[1])
12
+ return major > since.major || (major === since.major && Number(match[2]) >= since.minor)
13
+ }
14
+
15
+ /** A release as semver orders it: the numeric core, then the prerelease identifiers (none sorts above any). */
16
+ interface ReleaseVersion {
17
+ readonly core: readonly number[]
18
+ readonly prerelease: readonly string[]
19
+ }
20
+
21
+ const parseReleaseVersion = (release: string): ReleaseVersion | null => {
22
+ const match = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z.-]+)?$/.exec(release)
23
+ if (match === null) return null
24
+ return { core: [Number(match[1]), Number(match[2]), Number(match[3])], prerelease: match[4] === undefined ? [] : match[4].split('.') }
25
+ }
26
+
27
+ /** Semver's order of two prerelease identifiers: numerically when both are numeric, a numeric one first, else by ASCII. */
28
+ const compareIdentifiers = (a: string, b: string): number => {
29
+ const numericA = /^\d+$/.test(a)
30
+ const numericB = /^\d+$/.test(b)
31
+ if (numericA && numericB) return Number(a) - Number(b)
32
+ if (numericA !== numericB) return numericA ? -1 : 1
33
+ return a < b ? -1 : a > b ? 1 : 0
34
+ }
35
+
36
+ const compareReleases = (a: ReleaseVersion, b: ReleaseVersion): number => {
37
+ for (const [index, part] of a.core.entries()) {
38
+ const other = b.core[index] ?? 0
39
+ if (part !== other) return part - other
40
+ }
41
+ if (a.prerelease.length === 0 || b.prerelease.length === 0) return b.prerelease.length - a.prerelease.length
42
+ for (const [index, identifier] of a.prerelease.entries()) {
43
+ const other = b.prerelease[index]
44
+ if (other === undefined) return 1
45
+ const order = compareIdentifiers(identifier, other)
46
+ if (order !== 0) return order
47
+ }
48
+ return a.prerelease.length - b.prerelease.length
49
+ }
50
+
51
+ /**
52
+ * Whether framework release `release` is at or above the exact version `since` in semver's precedence, prerelease
53
+ * included; a release that is not a semver version never is (ADR-0062).
54
+ */
55
+ export const releaseFrom = (release: string, since: string): boolean => {
56
+ const floor = parseReleaseVersion(since)
57
+ if (floor === null) throw new Error(`releaseFrom: \`${since}\` is not a semver version`)
58
+ const version = parseReleaseVersion(release)
59
+ return version !== null && compareReleases(version, floor) >= 0
60
+ }
61
+
62
+ /**
63
+ * The release a gate reads for an approved access: the application's pin, never the reader's `checker`, which is this
64
+ * platform's patch of the pin's minor and so passes a `releaseFrom` gate a prerelease pin does not. An access approved before Control recorded the pin is read as the oldest release of its
65
+ * reader's minor (`<major>.<minor>.0-0`): every `releaseAtLeast` gate answers as for that minor, and every `releaseFrom`
66
+ * gate of a prerelease or a patch fails closed, as for an older application, until a deploy reads its pin.
67
+ */
68
+ export const applicationRelease = (access: { readonly checker: string; readonly framework?: string }): string => {
69
+ if (access.framework !== undefined) return access.framework
70
+ const minor = /^(\d+)\.(\d+)\./.exec(access.checker)
71
+ return minor === null ? access.checker : `${minor[1]}.${minor[2]}.0-0`
72
+ }
@@ -29,6 +29,22 @@ export function matchAction(pattern: string, action: string): boolean {
29
29
  return pattern === action
30
30
  }
31
31
 
32
+ /**
33
+ * `entries` with each identical action, scope and source once, the first kept in order: how IAM resolves a principal's
34
+ * permissions, so two roles granting one action at one scope put one entry into the token.
35
+ */
36
+ export function uniquePermissions(entries: readonly PermissionEntry[]): PermissionEntry[] {
37
+ const seen = new Set<string>()
38
+ return entries.filter((entry) => {
39
+ const key = JSON.stringify([entry.action, entry.scope?.type ?? null, entry.scope?.value ?? null, entry.source])
40
+ if (seen.has(key)) {
41
+ return false
42
+ }
43
+ seen.add(key)
44
+ return true
45
+ })
46
+ }
47
+
32
48
  /**
33
49
  * Does `entries` grant `action` at the given scope? Mirrors `can()` semantics exactly:
34
50
  * - scope === undefined → scope-less: ONLY entries with scope === null satisfy (global perms only).
@@ -937,9 +937,9 @@ export interface ColumnTestArtifact {
937
937
  readonly now: true;
938
938
  };
939
939
  }
940
- /** A column's own read action, its own write action, or both; an absent one is left out. */
940
+ /** A column's own read actions (one as its text, several as a list), its own write action, or both; an absent one is left out. */
941
941
  export interface ColumnPolicyArtifact {
942
- readonly read?: string;
942
+ readonly read?: TableActionArtifact;
943
943
  readonly write?: string;
944
944
  }
945
945
  export type TableScope = {
@@ -1,2 +1,25 @@
1
1
  /** A framework release as npm publishes it; the only form one may take before it reaches a generated build command. */
2
2
  export declare const FRAMEWORK_RELEASE_PATTERN: RegExp;
3
+ /**
4
+ * Whether framework release `release` is at least `since`. A prerelease counts as its `major.minor`; a release that
5
+ * does not start with `<major>.<minor>.` is never at least anything (ADR-0062).
6
+ */
7
+ export declare const releaseAtLeast: (release: string, since: {
8
+ readonly major: number;
9
+ readonly minor: number;
10
+ }) => boolean;
11
+ /**
12
+ * Whether framework release `release` is at or above the exact version `since` in semver's precedence, prerelease
13
+ * included; a release that is not a semver version never is (ADR-0062).
14
+ */
15
+ export declare const releaseFrom: (release: string, since: string) => boolean;
16
+ /**
17
+ * The release a gate reads for an approved access: the application's pin, never the reader's `checker`, which is this
18
+ * platform's patch of the pin's minor and so passes a `releaseFrom` gate a prerelease pin does not. An access approved before Control recorded the pin is read as the oldest release of its
19
+ * reader's minor (`<major>.<minor>.0-0`): every `releaseAtLeast` gate answers as for that minor, and every `releaseFrom`
20
+ * gate of a prerelease or a patch fails closed, as for an older application, until a deploy reads its pin.
21
+ */
22
+ export declare const applicationRelease: (access: {
23
+ readonly checker: string;
24
+ readonly framework?: string;
25
+ }) => string;
@@ -16,6 +16,11 @@ import type { PermissionEntry, Scope } from './types';
16
16
  * matchAction('project.*', 'projects.read') === false
17
17
  */
18
18
  export declare function matchAction(pattern: string, action: string): boolean;
19
+ /**
20
+ * `entries` with each identical action, scope and source once, the first kept in order: how IAM resolves a principal's
21
+ * permissions, so two roles granting one action at one scope put one entry into the token.
22
+ */
23
+ export declare function uniquePermissions(entries: readonly PermissionEntry[]): PermissionEntry[];
19
24
  /**
20
25
  * Does `entries` grant `action` at the given scope? Mirrors `can()` semantics exactly:
21
26
  * - scope === undefined → scope-less: ONLY entries with scope === null satisfy (global perms only).