@fabrika/auth-core 0.9.0-dev.2 → 0.9.0-dev.20

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 (47) hide show
  1. package/package.json +2 -2
  2. package/src/canonical-path.ts +18 -0
  3. package/src/client-address.ts +74 -0
  4. package/src/contract-canonical.ts +152 -1
  5. package/src/contract-encoding.ts +181 -0
  6. package/src/contract.ts +201 -9
  7. package/src/dev-environment.ts +6 -0
  8. package/src/egress.ts +97 -0
  9. package/src/endpoint-digest.ts +22 -2
  10. package/src/framework-release.ts +2 -0
  11. package/src/framework-variables.ts +15 -0
  12. package/src/index.ts +8 -0
  13. package/src/locale.ts +1 -1
  14. package/src/managed-environment.ts +140 -0
  15. package/src/membership.ts +12 -4
  16. package/src/ops.ts +1933 -0
  17. package/src/public-egress.ts +168 -0
  18. package/src/requirement.ts +5 -2
  19. package/src/rpc.ts +8 -8
  20. package/src/sha256.ts +52 -14
  21. package/src/terminal.ts +105 -0
  22. package/src/token.ts +73 -13
  23. package/src/types.ts +63 -2
  24. package/src/wire.ts +7 -0
  25. package/types/src/canonical-path.d.ts +6 -0
  26. package/types/src/client-address.d.ts +7 -0
  27. package/types/src/contract-canonical.d.ts +11 -1
  28. package/types/src/contract-encoding.d.ts +18 -0
  29. package/types/src/contract.d.ts +186 -8
  30. package/types/src/dev-environment.d.ts +5 -0
  31. package/types/src/egress.d.ts +27 -0
  32. package/types/src/endpoint-digest.d.ts +5 -0
  33. package/types/src/framework-release.d.ts +2 -0
  34. package/types/src/framework-variables.d.ts +12 -1
  35. package/types/src/index.d.ts +8 -0
  36. package/types/src/locale.d.ts +1 -1
  37. package/types/src/managed-environment.d.ts +32 -0
  38. package/types/src/membership.d.ts +8 -3
  39. package/types/src/ops.d.ts +657 -0
  40. package/types/src/public-egress.d.ts +31 -0
  41. package/types/src/requirement.d.ts +2 -2
  42. package/types/src/rpc.d.ts +5 -5
  43. package/types/src/sha256.d.ts +10 -0
  44. package/types/src/terminal.d.ts +72 -0
  45. package/types/src/token.d.ts +36 -11
  46. package/types/src/types.d.ts +42 -2
  47. package/types/src/wire.d.ts +6 -0
@@ -0,0 +1,140 @@
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_EGRESS_REFUSED_HOSTS,
7
+ FABRIKA_OPS_KEY_DIGESTS,
8
+ FABRIKA_TOKEN_KEYS,
9
+ FRAMEWORK_VARIABLE_PREFIXES,
10
+ type FrameworkVariableName,
11
+ isConnectKeyVariable,
12
+ isConnectUrlVariable,
13
+ isEmailTransportVariable,
14
+ isPlatformTransportVariable,
15
+ isWorkloadKeyVariable,
16
+ PLATFORM_RECEIVERS,
17
+ platformTransportVariables,
18
+ } from './framework-variables'
19
+ import { FABRIKA_PROXY_SECRET } from './wire'
20
+
21
+ /**
22
+ * The framework names Control hands every deploy, each with a value or `null` (the deploy removes it). Beside them, a
23
+ * deploy carries the families of `FRAMEWORK_VARIABLE_PREFIXES`, one variable per workload, import or receiver.
24
+ */
25
+ export const MANAGED_VARIABLE_NAMES = [
26
+ 'FABRIKA_RELEASE',
27
+ 'FABRIKA_OPERATIONS_DSN',
28
+ FABRIKA_IAM_ISSUER,
29
+ FABRIKA_IAM_APP_ID,
30
+ 'FABRIKA_APP_ID',
31
+ 'FABRIKA_ENVIRONMENT',
32
+ 'FABRIKA_SERVICE_KEY',
33
+ FABRIKA_PROXY_SECRET,
34
+ 'FABRIKA_IAM_RPC_URL',
35
+ 'FABRIKA_IAM_RPC_KEY',
36
+ FABRIKA_APP_ORIGIN,
37
+ FABRIKA_TOKEN_KEYS,
38
+ FABRIKA_OPS_KEY_DIGESTS,
39
+ FABRIKA_EGRESS_REFUSED_HOSTS,
40
+ ] as const satisfies readonly FrameworkVariableName[]
41
+
42
+ export type ManagedVariableName = (typeof MANAGED_VARIABLE_NAMES)[number]
43
+
44
+ const MANAGED_VARIABLES: ReadonlySet<string> = new Set(MANAGED_VARIABLE_NAMES)
45
+
46
+ /** The framework names that carry no credential: identities, origins and the release. */
47
+ const PUBLIC_VARIABLES: ReadonlySet<string> = new Set<FrameworkVariableName>([
48
+ 'FABRIKA_RELEASE',
49
+ FABRIKA_CONTRACT_DIGEST,
50
+ FABRIKA_IAM_ISSUER,
51
+ FABRIKA_IAM_APP_ID,
52
+ 'FABRIKA_APP_ID',
53
+ 'FABRIKA_ENVIRONMENT',
54
+ 'FABRIKA_IAM_RPC_URL',
55
+ FABRIKA_APP_ORIGIN,
56
+ // SHA-256 of 256-bit random keys: a digest neither is a key nor lets anyone forge one.
57
+ FABRIKA_OPS_KEY_DIGESTS,
58
+ // Host names anyone can look up in DNS.
59
+ FABRIKA_EGRESS_REFUSED_HOSTS,
60
+ ])
61
+
62
+ const PLATFORM_URL_VARIABLES: ReadonlySet<string> = new Set(PLATFORM_RECEIVERS.map((receiver) => platformTransportVariables(receiver).url))
63
+
64
+ /**
65
+ * Whether a managed variable carries a credential, so a provider writes it as a secret and never reads it back. Fail
66
+ * closed: a name not known to be public is a credential.
67
+ */
68
+ export function isManagedCredential(name: string): boolean {
69
+ return !PUBLIC_VARIABLES.has(name) && !isConnectUrlVariable(name) && !PLATFORM_URL_VARIABLES.has(name)
70
+ }
71
+
72
+ /** Whether a deploy writes `name` from what Control hands it, so a deploy that no longer hands it removes it. */
73
+ export function isManagedVariableName(name: string): boolean {
74
+ return MANAGED_VARIABLES.has(name) || FRAMEWORK_VARIABLE_PREFIXES.some((prefix) => name.length > prefix.length && name.startsWith(prefix))
75
+ }
76
+
77
+ /** One managed variable: its value, or `null` when the deploy removes it, and whether it is a credential. */
78
+ export interface ManagedEntry {
79
+ readonly value: string | null
80
+ readonly credential: boolean
81
+ }
82
+
83
+ /** What Control hands a deploy, parsed once: every name is one Control writes, and every family value has its shape. */
84
+ export type ManagedEnvironment = Readonly<Record<string, ManagedEntry>>
85
+
86
+ const isOrigin = (value: string, schemes: readonly string[]): boolean => {
87
+ try {
88
+ const url = new URL(value)
89
+ return url.origin === value && schemes.includes(url.protocol)
90
+ } catch {
91
+ return false
92
+ }
93
+ }
94
+
95
+ const KEY = /^px_./s
96
+
97
+ const isKey = (value: string | null): boolean => value !== null && KEY.test(value)
98
+
99
+ /** The reason a family variable's value is refused, or `null`. Messages name the variable, never its value. */
100
+ const familyProblem = (name: string, value: string | null): string | null => {
101
+ if (isEmailTransportVariable(name)) return value === null || value === '' ? 'must not be empty' : null
102
+ if (PLATFORM_URL_VARIABLES.has(name)) {
103
+ // Control's public origin is http in the local stack (`http://control.fabrika.localhost:18080`).
104
+ return value !== null && isOrigin(value, ['http:', 'https:']) ? null : 'must be an http(s) origin'
105
+ }
106
+ if (isConnectUrlVariable(name)) return value !== null && isOrigin(value, ['https:']) ? null : 'must be an https origin'
107
+ if (isPlatformTransportVariable(name) || isWorkloadKeyVariable(name) || isConnectKeyVariable(name)) {
108
+ return isKey(value) ? null : 'must be a px_ key'
109
+ }
110
+ return 'is not a variable a deploy writes'
111
+ }
112
+
113
+ /**
114
+ * Parse what Control hands a deploy. Control parses it before any provider effect, and the Cloudflare executor parses
115
+ * what its job carries; a name or a value Control would not produce fails either.
116
+ */
117
+ export function parseManagedEnvironment(value: Readonly<Record<string, string | null>>): ManagedEnvironment {
118
+ const parsed: Record<string, ManagedEntry> = {}
119
+ for (const [name, entry] of Object.entries(value)) {
120
+ if (!MANAGED_VARIABLES.has(name)) {
121
+ const problem = familyProblem(name, entry)
122
+ if (problem !== null) throw new Error(`managed environment: ${name} ${problem}`)
123
+ }
124
+ parsed[name] = { value: entry, credential: isManagedCredential(name) }
125
+ }
126
+ return parsed
127
+ }
128
+
129
+ /** The value a deploy writes under `name`, or `null` when it writes none. */
130
+ export function managedValue(environment: ManagedEnvironment, name: string): string | null {
131
+ return environment[name]?.value ?? null
132
+ }
133
+
134
+ /**
135
+ * The managed variables a service or Worker still holds that the deploy no longer writes: handed as `null`, or a
136
+ * family member no longer handed. `written` names what the deploy writes there.
137
+ */
138
+ export function staleManagedVariables(present: Iterable<string>, written: ReadonlySet<string>): string[] {
139
+ return [...present].filter((name) => isManagedVariableName(name) && !written.has(name))
140
+ }
package/src/membership.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  // and a platform component that implements `IamRpc` does not have to.
7
7
 
8
8
  import type { ContractArtifact, DimensionArtifact, EndpointRow } from './contract'
9
+ import { isLocale, type Locale, LOCALES } from './locale'
9
10
  import type { PrincipalAffiliation, Scope } from './types'
10
11
 
11
12
  /**
@@ -174,17 +175,24 @@ export const MAX_INVITATION_MESSAGE_LENGTH = 1000
174
175
  * What an invitation by address carries besides its role. `label` is the invited person's company as the application
175
176
  * names it, `message` the inviter's personal note; both are plain text IAM escapes wherever it inserts them. `resend`
176
177
  * rotates the open invitation's link, restarts its lifetime and sends it again; without it, repeating an invitation
177
- * answers the open one and sends nothing.
178
+ * answers the open one and sends nothing. `locale` is the invited person's language, when the application knows it: IAM
179
+ * writes the invitation and the pages its link opens in it, and its acceptance keeps it as the person's, so IAM's later
180
+ * e-mails (a password reset) and pages speak it too. Without one, IAM speaks the language the person already has, else the
181
+ * application's. A resend that names one replaces the stored one.
178
182
  */
179
183
  export interface InvitationOptions {
180
184
  readonly label?: string
181
185
  readonly message?: string
182
186
  readonly resend?: boolean
187
+ readonly locale?: Locale
183
188
  }
184
189
 
185
- /** Why IAM refuses an invitation's label or message, or `null`. A message may break lines; nothing else is a control character. */
190
+ /** Why IAM refuses an invitation's label, message or locale, or `null`. A message may break lines; nothing else is a control character. */
186
191
  export function invitationOptionsProblem(options: InvitationOptions): string | null {
187
- const { label, message } = options
192
+ const { label, message, locale } = options
193
+ if (locale !== undefined && !isLocale(locale)) {
194
+ return `an invitation locale is one of ${LOCALES.map((value) => `'${value}'`).join(', ')}`
195
+ }
188
196
  if (label !== undefined && (label.length > MAX_INVITATION_LABEL_LENGTH || /\p{Cc}/u.test(label))) {
189
197
  return `an invitation label is at most ${MAX_INVITATION_LABEL_LENGTH} characters on one line`
190
198
  }
@@ -233,7 +241,7 @@ export interface AssignMemberInput extends MembershipCaller {
233
241
  scope: Scope
234
242
  role: string
235
243
  member: MemberTarget
236
- /** Only with an address: the invitation's label, message and resend (`InvitationOptions`). */
244
+ /** Only with an address: the invitation's label, message, resend and locale (`InvitationOptions`). */
237
245
  invitation?: InvitationOptions
238
246
  }
239
247