@fabrika/auth-core 0.9.0-dev.37 → 0.9.0-dev.39

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.37",
3
+ "version": "0.9.0-dev.39",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "repository": {
package/src/contract.ts CHANGED
@@ -500,6 +500,11 @@ export interface DimensionArtifact {
500
500
  readonly directory?: 'labels'
501
501
  /** The values the console offers for a grant in this dimension (ADR-0085). Absent unless declared, so digests stay. */
502
502
  readonly values?: readonly DimensionValue[]
503
+ /**
504
+ * The id of the query procedure IAM asks, as the administrator, for this dimension's values (ADR-0085 §4). Absent
505
+ * unless declared, so digests stay.
506
+ */
507
+ readonly valuesFrom?: string
503
508
  }
504
509
 
505
510
  export interface RoleArtifact {
@@ -1,5 +1,6 @@
1
1
  // The values a dimension lists for the person who grants (ADR-0085): information for the console, never authorization.
2
2
 
3
+ import type { OutputShape, SchemaShape } from './contract'
3
4
  import { MAX_MEMBERSHIP_SCOPE_PART_LENGTH } from './membership'
4
5
 
5
6
  /** One value a dimension lists, with the text the console shows beside it. */
@@ -34,3 +35,89 @@ export function dimensionValuesProblem(values: readonly DimensionValue[]): strin
34
35
  }
35
36
  return null
36
37
  }
38
+
39
+ /**
40
+ * Where IAM asks the application for a dimension's values (ADR-0085 §4): the procedure's endpoint id, its path on the
41
+ * application's origin and the action its requirement names. IAM calls it as the administrator, through the proxy.
42
+ */
43
+ export interface DimensionValueSource {
44
+ readonly endpoint: string
45
+ readonly path: string
46
+ readonly action: string
47
+ }
48
+
49
+ /** How long IAM waits for a value source's whole answer before the console falls back. */
50
+ export const SCOPE_VALUE_SOURCE_TIMEOUT_MS = 3000
51
+
52
+ /** The largest answer IAM reads from a value source. */
53
+ export const MAX_SCOPE_VALUE_SOURCE_BYTES = 256 * 1024
54
+
55
+ /** The row of a value-source procedure, as far as its rule reads it. */
56
+ interface ValueSourceRow {
57
+ readonly id: string
58
+ readonly kind: string
59
+ readonly method: string
60
+ readonly callers: readonly string[]
61
+ readonly requirement:
62
+ | { readonly kind: string; readonly action?: string; readonly access?: { readonly form: string; readonly dimension?: string } }
63
+ | null
64
+ readonly input: SchemaShape | null
65
+ readonly output: OutputShape | null
66
+ }
67
+
68
+ /**
69
+ * Why `row` cannot be `dimension`'s value source, or null: a person's `POST` query without input, requiring an action
70
+ * `over` that dimension other than its `members.manage` (`managedBy`), whose output is
71
+ * `{ values: [{ value: string, label?: string }] }`. The proxy decides the call from the row like any other; the
72
+ * requirement's action is what IAM's key for the call carries, and a row of `members.manage` would carry a membership
73
+ * delegation into a call the administrator only reads with.
74
+ */
75
+ export function valueSourceProblem(row: ValueSourceRow, dimension: string, managedBy: string | null): string | null {
76
+ if (row.kind !== 'query' || row.method !== 'POST') return `'${row.id}' is not a query procedure (allowed: r.query(…) of an rpc mount)`
77
+ if (!row.callers.includes('human')) return `'${row.id}' admits no person (allowed: callers including 'human')`
78
+ const requirement = row.requirement
79
+ if (requirement?.kind !== 'action' || requirement.access?.form !== 'over' || requirement.access.dimension !== dimension) {
80
+ return `'${row.id}' does not require an action over '${dimension}' (allowed: access: { action: '<action>', over: '${dimension}' })`
81
+ }
82
+ if (managedBy !== null && requirement.action === managedBy) {
83
+ return `'${row.id}' requires '${managedBy}', which manages the members of '${dimension}' (allowed: an action that reads, not members.manage)`
84
+ }
85
+ if (row.input !== null) return `'${row.id}' takes an input (allowed: no input)`
86
+ if (row.output === null || !isDimensionValuesShape(row.output)) {
87
+ return `'${row.id}' does not answer { values: [{ value: string, label?: string }] }`
88
+ }
89
+ return null
90
+ }
91
+
92
+ /** The output shape `{ values: [{ value: string, label?: string }] }`, strings with any checks. */
93
+ export function isDimensionValuesShape(shape: OutputShape): boolean {
94
+ if (shape.type !== 'object' || Object.keys(shape.fields).length !== 1) return false
95
+ const values = shape.fields['values']
96
+ if (values?.type !== 'array') return false
97
+ const item = values.items
98
+ if (item.type !== 'object') return false
99
+ const keys = Object.keys(item.fields)
100
+ if (!keys.includes('value') || keys.some((key) => key !== 'value' && key !== 'label')) return false
101
+ const label = item.fields['label']
102
+ return item.fields['value']?.type === 'string' && (label === undefined || (label.type === 'optional' && label.inner.type === 'string'))
103
+ }
104
+
105
+ /**
106
+ * The values a source answered, or null when the answer is not `{ values }` of at most `MAX_DIMENSION_VALUES` values
107
+ * `dimensionValuesProblem` admits. An answer is the application's word: the console shows it, IAM decides nothing by it.
108
+ */
109
+ export function parseDimensionValues(result: unknown): DimensionValue[] | null {
110
+ if (typeof result !== 'object' || result === null || Array.isArray(result)) return null
111
+ const list: unknown = Reflect.get(result, 'values')
112
+ if (!Array.isArray(list)) return null
113
+ const values: DimensionValue[] = []
114
+ for (const entry of list) {
115
+ if (typeof entry !== 'object' || entry === null || Array.isArray(entry)) return null
116
+ const value: unknown = Reflect.get(entry, 'value')
117
+ const label: unknown = Reflect.get(entry, 'label')
118
+ if (typeof value !== 'string' || (label !== undefined && typeof label !== 'string')) return null
119
+ values.push(label === undefined ? { value } : { value, label })
120
+ }
121
+ if (values.length === 0) return values
122
+ return dimensionValuesProblem(values) === null ? values : null
123
+ }
package/src/types.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Locale } from './locale'
2
2
  import type { MailTemplates } from './mail'
3
- import type { DimensionValue } from './scope-values'
3
+ import type { DimensionValue, DimensionValueSource } from './scope-values'
4
4
 
5
5
  export type PrincipalType = 'user' | 'service'
6
6
 
@@ -166,6 +166,8 @@ export interface AppScopeDef {
166
166
  lookups?: string[]
167
167
  /** The values the console offers for a grant in this dimension (ADR-0085); absent, it takes free text. */
168
168
  values?: DimensionValue[]
169
+ /** The procedure IAM asks for this dimension's values as the administrator (ADR-0085 §4); absent, only `values` count. */
170
+ valueSource?: DimensionValueSource
169
171
  }
170
172
 
171
173
  /**
@@ -447,6 +447,11 @@ export interface DimensionArtifact {
447
447
  readonly directory?: 'labels';
448
448
  /** The values the console offers for a grant in this dimension (ADR-0085). Absent unless declared, so digests stay. */
449
449
  readonly values?: readonly DimensionValue[];
450
+ /**
451
+ * The id of the query procedure IAM asks, as the administrator, for this dimension's values (ADR-0085 §4). Absent
452
+ * unless declared, so digests stay.
453
+ */
454
+ readonly valuesFrom?: string;
450
455
  }
451
456
  export interface RoleArtifact {
452
457
  readonly grants: readonly string[];
@@ -1,3 +1,4 @@
1
+ import type { OutputShape, SchemaShape } from './contract';
1
2
  /** One value a dimension lists, with the text the console shows beside it. */
2
3
  export interface DimensionValue {
3
4
  readonly value: string;
@@ -12,3 +13,49 @@ export declare const MAX_DIMENSION_VALUE_LABEL_LENGTH = 120;
12
13
  * non-empty value no longer than a scope value IAM takes, listed once, with a non-empty label when it has one.
13
14
  */
14
15
  export declare function dimensionValuesProblem(values: readonly DimensionValue[]): string | null;
16
+ /**
17
+ * Where IAM asks the application for a dimension's values (ADR-0085 §4): the procedure's endpoint id, its path on the
18
+ * application's origin and the action its requirement names. IAM calls it as the administrator, through the proxy.
19
+ */
20
+ export interface DimensionValueSource {
21
+ readonly endpoint: string;
22
+ readonly path: string;
23
+ readonly action: string;
24
+ }
25
+ /** How long IAM waits for a value source's whole answer before the console falls back. */
26
+ export declare const SCOPE_VALUE_SOURCE_TIMEOUT_MS = 3000;
27
+ /** The largest answer IAM reads from a value source. */
28
+ export declare const MAX_SCOPE_VALUE_SOURCE_BYTES: number;
29
+ /** The row of a value-source procedure, as far as its rule reads it. */
30
+ interface ValueSourceRow {
31
+ readonly id: string;
32
+ readonly kind: string;
33
+ readonly method: string;
34
+ readonly callers: readonly string[];
35
+ readonly requirement: {
36
+ readonly kind: string;
37
+ readonly action?: string;
38
+ readonly access?: {
39
+ readonly form: string;
40
+ readonly dimension?: string;
41
+ };
42
+ } | null;
43
+ readonly input: SchemaShape | null;
44
+ readonly output: OutputShape | null;
45
+ }
46
+ /**
47
+ * Why `row` cannot be `dimension`'s value source, or null: a person's `POST` query without input, requiring an action
48
+ * `over` that dimension other than its `members.manage` (`managedBy`), whose output is
49
+ * `{ values: [{ value: string, label?: string }] }`. The proxy decides the call from the row like any other; the
50
+ * requirement's action is what IAM's key for the call carries, and a row of `members.manage` would carry a membership
51
+ * delegation into a call the administrator only reads with.
52
+ */
53
+ export declare function valueSourceProblem(row: ValueSourceRow, dimension: string, managedBy: string | null): string | null;
54
+ /** The output shape `{ values: [{ value: string, label?: string }] }`, strings with any checks. */
55
+ export declare function isDimensionValuesShape(shape: OutputShape): boolean;
56
+ /**
57
+ * The values a source answered, or null when the answer is not `{ values }` of at most `MAX_DIMENSION_VALUES` values
58
+ * `dimensionValuesProblem` admits. An answer is the application's word: the console shows it, IAM decides nothing by it.
59
+ */
60
+ export declare function parseDimensionValues(result: unknown): DimensionValue[] | null;
61
+ export {};
@@ -1,6 +1,6 @@
1
1
  import type { Locale } from './locale';
2
2
  import type { MailTemplates } from './mail';
3
- import type { DimensionValue } from './scope-values';
3
+ import type { DimensionValue, DimensionValueSource } from './scope-values';
4
4
  export type PrincipalType = 'user' | 'service';
5
5
  /**
6
6
  * Whether a person belongs to the organization (ADR-0051). An `external` principal holds only grants of a role its
@@ -135,6 +135,8 @@ export interface AppScopeDef {
135
135
  lookups?: string[];
136
136
  /** The values the console offers for a grant in this dimension (ADR-0085); absent, it takes free text. */
137
137
  values?: DimensionValue[];
138
+ /** The procedure IAM asks for this dimension's values as the administrator (ADR-0085 §4); absent, only `values` count. */
139
+ valueSource?: DimensionValueSource;
138
140
  }
139
141
  /**
140
142
  * The role keys IAM defines itself for every app. A built-in wins over an app role of the same key at