@fabrika/auth-core 0.9.0-dev.4 → 0.9.0-dev.5

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.4",
3
+ "version": "0.9.0-dev.5",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -74,6 +74,7 @@ export function canonicalizeArtifact(artifact: ContractArtifact): ContractArtifa
74
74
  ...(artifact.mail === undefined ? {} : { mail: artifact.mail }),
75
75
  ...(artifact.members === undefined ? {} : { members: artifact.members }),
76
76
  ...(artifact.directory === undefined ? {} : { directory: artifact.directory }),
77
+ ...(artifact.terminal === undefined ? {} : { terminal: { identify: artifact.terminal.identify } }),
77
78
  ...(artifact.procedures === undefined
78
79
  ? {}
79
80
  : {
package/src/contract.ts CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  import type { Locale } from './locale'
5
5
  import type { MailTemplates } from './mail'
6
+ import type { TerminalArtifact } from './terminal'
6
7
 
7
8
  /**
8
9
  * The artifact format this release writes. A release that changes the artifact raises it, and Control upcasts every
@@ -61,6 +62,11 @@ export interface ContractArtifact extends EndpointTable {
61
62
  readonly members?: { readonly manage: string; readonly list: string }
62
63
  /** `labels`: the application's directory shows labels to `members.list` holders. Absent unless declared, so digests stay. */
63
64
  readonly directory?: 'labels'
65
+ /**
66
+ * The page on the application's own origin that identifies a person at an enrolled terminal (ADR-0067): IAM sends a
67
+ * terminal's browser there, and only an application that declares it may assert a person. Absent unless declared.
68
+ */
69
+ readonly terminal?: TerminalArtifact
64
70
  /**
65
71
  * What each procedure of the staged kit admits and how long it runs, by endpoint id, with every default applied: the
66
72
  * rules the application applies itself, beside the row the proxy applies. Absent for a contract with no such
@@ -13,6 +13,12 @@ export const DEV_LAUNCHER_MODULE = '@fabrika/app/dev'
13
13
  /** Where the launcher records what it holds, so a restart of the same container resumes it. A new container has none. */
14
14
  export const DEV_LAUNCHER_STATE_FILE = 'fabrika/dev-state.json'
15
15
 
16
+ /**
17
+ * The launcher writes it once its boot reconcile reported, served or failed. A dev container's readiness check passes on
18
+ * it too, so a boot that failed still activates, and `reset_dev` can reach the launcher that reports the failure.
19
+ */
20
+ export const DEV_LAUNCHER_BOOTED_FILE = 'fabrika/dev-booted'
21
+
16
22
  /** One command of the build's check chain, run by the shell in the application root. */
17
23
  export interface DevLauncherCheck {
18
24
  readonly name: string
@@ -0,0 +1,2 @@
1
+ /** A framework release as npm publishes it; the only form one may take before it reaches a generated build command. */
2
+ export const FRAMEWORK_RELEASE_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/
@@ -16,6 +16,12 @@ export const FABRIKA_SOURCE_MAP_UPLOAD_KEY = 'FABRIKA_SOURCE_MAP_UPLOAD_KEY'
16
16
  export const FABRIKA_TOKEN_KEYS = 'FABRIKA_TOKEN_KEYS'
17
17
  /** The environment's canonical origin (`https://crm.example`), which `ctx.url` builds absolute links on. */
18
18
  export const FABRIKA_APP_ORIGIN = 'FABRIKA_APP_ORIGIN'
19
+ /** The digests of the environment's ops keys, which the object checks Control's key against (`parseOpsKeyDigests`, ADR-0069). */
20
+ export const FABRIKA_OPS_KEY_DIGESTS = 'FABRIKA_OPS_KEY_DIGESTS'
21
+ /** The name of the object that serves an environment's ordinary traffic on Cloudflare; `application` without it. */
22
+ export const FABRIKA_APPLICATION_OBJECT = 'FABRIKA_APPLICATION_OBJECT'
23
+ /** The object names an ops request may target, all in the environment's own namespace (`parseApplicationObjects`). */
24
+ export const FABRIKA_APPLICATION_OBJECTS = 'FABRIKA_APPLICATION_OBJECTS'
19
25
 
20
26
  /**
21
27
  * The environment names only the platform writes into an application
@@ -47,6 +53,9 @@ export const FRAMEWORK_VARIABLE_NAMES = [
47
53
  FABRIKA_SOURCE_MAP_UPLOAD_KEY,
48
54
  FABRIKA_TOKEN_KEYS,
49
55
  FABRIKA_APP_ORIGIN,
56
+ FABRIKA_OPS_KEY_DIGESTS,
57
+ FABRIKA_APPLICATION_OBJECT,
58
+ FABRIKA_APPLICATION_OBJECTS,
50
59
  ] as const
51
60
 
52
61
  export type FrameworkVariableName = (typeof FRAMEWORK_VARIABLE_NAMES)[number]
package/src/index.ts CHANGED
@@ -8,17 +8,21 @@ export * from './device'
8
8
  export * from './endpoint-digest'
9
9
  export * from './endpoints'
10
10
  export * from './environment'
11
+ export * from './framework-release'
11
12
  export * from './framework-variables'
12
13
  export * from './installation-endpoint'
13
14
  export { parseJsonStringArray } from './json'
14
15
  export * from './locale'
15
16
  export * from './mail'
17
+ export * from './managed-environment'
16
18
  export * from './membership'
17
19
  export * from './oauth'
20
+ export * from './ops'
18
21
  export * from './permissions'
19
22
  export * from './requirement'
20
23
  export * from './rpc'
21
24
  export { sha256Hex } from './sha256'
25
+ export * from './terminal'
22
26
  export * from './token'
23
27
  export * from './types'
24
28
  export * from './wire'
@@ -0,0 +1,136 @@
1
+ import { FABRIKA_IAM_APP_ID } from './app-environment'
2
+ import { FABRIKA_CONTRACT_DIGEST } from './endpoint-digest'
3
+ import { FABRIKA_IAM_ISSUER } from './environment'
4
+ import {
5
+ FABRIKA_APP_ORIGIN,
6
+ FABRIKA_OPS_KEY_DIGESTS,
7
+ FABRIKA_TOKEN_KEYS,
8
+ FRAMEWORK_VARIABLE_PREFIXES,
9
+ type FrameworkVariableName,
10
+ isConnectKeyVariable,
11
+ isConnectUrlVariable,
12
+ isEmailTransportVariable,
13
+ isPlatformTransportVariable,
14
+ isWorkloadKeyVariable,
15
+ PLATFORM_RECEIVERS,
16
+ platformTransportVariables,
17
+ } from './framework-variables'
18
+ import { FABRIKA_PROXY_SECRET } from './wire'
19
+
20
+ /**
21
+ * The framework names Control hands every deploy, each with a value or `null` (the deploy removes it). Beside them, a
22
+ * deploy carries the families of `FRAMEWORK_VARIABLE_PREFIXES`, one variable per workload, import or receiver.
23
+ */
24
+ export const MANAGED_VARIABLE_NAMES = [
25
+ 'FABRIKA_RELEASE',
26
+ 'FABRIKA_OPERATIONS_DSN',
27
+ FABRIKA_IAM_ISSUER,
28
+ FABRIKA_IAM_APP_ID,
29
+ 'FABRIKA_APP_ID',
30
+ 'FABRIKA_ENVIRONMENT',
31
+ 'FABRIKA_SERVICE_KEY',
32
+ FABRIKA_PROXY_SECRET,
33
+ 'FABRIKA_IAM_RPC_URL',
34
+ 'FABRIKA_IAM_RPC_KEY',
35
+ FABRIKA_APP_ORIGIN,
36
+ FABRIKA_TOKEN_KEYS,
37
+ FABRIKA_OPS_KEY_DIGESTS,
38
+ ] as const satisfies readonly FrameworkVariableName[]
39
+
40
+ export type ManagedVariableName = (typeof MANAGED_VARIABLE_NAMES)[number]
41
+
42
+ const MANAGED_VARIABLES: ReadonlySet<string> = new Set(MANAGED_VARIABLE_NAMES)
43
+
44
+ /** The framework names that carry no credential: identities, origins and the release. */
45
+ const PUBLIC_VARIABLES: ReadonlySet<string> = new Set<FrameworkVariableName>([
46
+ 'FABRIKA_RELEASE',
47
+ FABRIKA_CONTRACT_DIGEST,
48
+ FABRIKA_IAM_ISSUER,
49
+ FABRIKA_IAM_APP_ID,
50
+ 'FABRIKA_APP_ID',
51
+ 'FABRIKA_ENVIRONMENT',
52
+ 'FABRIKA_IAM_RPC_URL',
53
+ FABRIKA_APP_ORIGIN,
54
+ // SHA-256 of 256-bit random keys: a digest neither is a key nor lets anyone forge one.
55
+ FABRIKA_OPS_KEY_DIGESTS,
56
+ ])
57
+
58
+ const PLATFORM_URL_VARIABLES: ReadonlySet<string> = new Set(PLATFORM_RECEIVERS.map((receiver) => platformTransportVariables(receiver).url))
59
+
60
+ /**
61
+ * Whether a managed variable carries a credential, so a provider writes it as a secret and never reads it back. Fail
62
+ * closed: a name not known to be public is a credential.
63
+ */
64
+ export function isManagedCredential(name: string): boolean {
65
+ return !PUBLIC_VARIABLES.has(name) && !isConnectUrlVariable(name) && !PLATFORM_URL_VARIABLES.has(name)
66
+ }
67
+
68
+ /** Whether a deploy writes `name` from what Control hands it, so a deploy that no longer hands it removes it. */
69
+ export function isManagedVariableName(name: string): boolean {
70
+ return MANAGED_VARIABLES.has(name) || FRAMEWORK_VARIABLE_PREFIXES.some((prefix) => name.length > prefix.length && name.startsWith(prefix))
71
+ }
72
+
73
+ /** One managed variable: its value, or `null` when the deploy removes it, and whether it is a credential. */
74
+ export interface ManagedEntry {
75
+ readonly value: string | null
76
+ readonly credential: boolean
77
+ }
78
+
79
+ /** What Control hands a deploy, parsed once: every name is one Control writes, and every family value has its shape. */
80
+ export type ManagedEnvironment = Readonly<Record<string, ManagedEntry>>
81
+
82
+ const isOrigin = (value: string, schemes: readonly string[]): boolean => {
83
+ try {
84
+ const url = new URL(value)
85
+ return url.origin === value && schemes.includes(url.protocol)
86
+ } catch {
87
+ return false
88
+ }
89
+ }
90
+
91
+ const KEY = /^px_./s
92
+
93
+ const isKey = (value: string | null): boolean => value !== null && KEY.test(value)
94
+
95
+ /** The reason a family variable's value is refused, or `null`. Messages name the variable, never its value. */
96
+ const familyProblem = (name: string, value: string | null): string | null => {
97
+ if (isEmailTransportVariable(name)) return value === null || value === '' ? 'must not be empty' : null
98
+ if (PLATFORM_URL_VARIABLES.has(name)) {
99
+ // Control's public origin is http in the local stack (`http://control.fabrika.localhost:18080`).
100
+ return value !== null && isOrigin(value, ['http:', 'https:']) ? null : 'must be an http(s) origin'
101
+ }
102
+ if (isConnectUrlVariable(name)) return value !== null && isOrigin(value, ['https:']) ? null : 'must be an https origin'
103
+ if (isPlatformTransportVariable(name) || isWorkloadKeyVariable(name) || isConnectKeyVariable(name)) {
104
+ return isKey(value) ? null : 'must be a px_ key'
105
+ }
106
+ return 'is not a variable a deploy writes'
107
+ }
108
+
109
+ /**
110
+ * Parse what Control hands a deploy. Control parses it before any provider effect, and the Cloudflare executor parses
111
+ * what its job carries; a name or a value Control would not produce fails either.
112
+ */
113
+ export function parseManagedEnvironment(value: Readonly<Record<string, string | null>>): ManagedEnvironment {
114
+ const parsed: Record<string, ManagedEntry> = {}
115
+ for (const [name, entry] of Object.entries(value)) {
116
+ if (!MANAGED_VARIABLES.has(name)) {
117
+ const problem = familyProblem(name, entry)
118
+ if (problem !== null) throw new Error(`managed environment: ${name} ${problem}`)
119
+ }
120
+ parsed[name] = { value: entry, credential: isManagedCredential(name) }
121
+ }
122
+ return parsed
123
+ }
124
+
125
+ /** The value a deploy writes under `name`, or `null` when it writes none. */
126
+ export function managedValue(environment: ManagedEnvironment, name: string): string | null {
127
+ return environment[name]?.value ?? null
128
+ }
129
+
130
+ /**
131
+ * The managed variables a service or Worker still holds that the deploy no longer writes: handed as `null`, or a
132
+ * family member no longer handed. `written` names what the deploy writes there.
133
+ */
134
+ export function staleManagedVariables(present: Iterable<string>, written: ReadonlySet<string>): string[] {
135
+ return [...present].filter((name) => isManagedVariableName(name) && !written.has(name))
136
+ }