@fabrika/auth-core 0.3.3 → 0.4.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.
package/src/types.ts CHANGED
@@ -1,5 +1,30 @@
1
+ import type { Locale } from './locale'
2
+ import type { MailTemplates } from './mail'
3
+
1
4
  export type PrincipalType = 'user' | 'service'
2
5
 
6
+ /**
7
+ * Whether a person belongs to the organization (ADR-0051). An `external` principal holds only grants of a role its
8
+ * application declares `external`; a service is always `internal`.
9
+ */
10
+ export type PrincipalAffiliation = 'internal' | 'external'
11
+
12
+ /**
13
+ * The principal kind of a workload credential (ADR-0022 "Principal kinds"): a contract job, a webhook, or the connector
14
+ * of a consuming application. IAM stores a workload principal as a `service` row and mints its key with this kind, so
15
+ * a workload key is never a `machine` caller.
16
+ */
17
+ export type WorkloadKind = 'job' | 'webhook' | 'app'
18
+
19
+ export const WORKLOAD_KINDS: readonly WorkloadKind[] = ['job', 'webhook', 'app']
20
+
21
+ /** The principal kind a token carries in `ptype`: a stored principal type, or a workload kind. */
22
+ export type TokenPrincipalType = PrincipalType | WorkloadKind
23
+
24
+ export function isWorkloadKind(value: unknown): value is WorkloadKind {
25
+ return value === 'job' || value === 'webhook' || value === 'app'
26
+ }
27
+
3
28
  /**
4
29
  * Where a resolved permission entry came from. Used for debuggability in the admin UI
5
30
  * ("why does this user have this permission?"); `can()`/`scopedTo()` ignore it.
@@ -45,73 +70,128 @@ export interface RoleDef {
45
70
  name: string
46
71
  description?: string
47
72
  permissions: string[]
73
+ /**
74
+ * A membership role is granted only in a value of `scope`, never globally. `assignableBy` names the
75
+ * roles whose holders may assign it there: a membership role of the same scope, held there, or an
76
+ * application-wide role (one with no `member`), held there or globally. Absent, only an IAM
77
+ * administrator assigns it.
78
+ */
79
+ member?: { scope: string; assignableBy?: string[] }
80
+ /** An external principal may hold this membership role (ADR-0051). Only a membership role may declare it. */
81
+ external?: true
48
82
  }
49
83
 
50
- /** One scope dimension an app exposes (flat, independent). */
84
+ /**
85
+ * One scope dimension an app exposes. IAM evaluates every value as a flat scope; `in` only tells IAM
86
+ * which dimension a share link's containment chain names next.
87
+ */
51
88
  export interface AppScopeDef {
52
89
  type: string
53
90
  label?: string
91
+ /** The dimension that contains every value of this one (`note` is in `workspace`). */
92
+ in?: string
93
+ /** The membership role IAM grants the caller in a value it creates for them (`createScope`). */
94
+ creator?: string
95
+ /** The action that manages the members of a value, and the one that lists them. */
96
+ members?: { manage: string; list: string }
97
+ /**
98
+ * `labels`: a holder of `members.list` also sees each member's label (a user's spelling of its address). Absent, the
99
+ * labels stay with `members.manage`.
100
+ */
101
+ directory?: 'labels'
102
+ /**
103
+ * The actions of the endpoints on, in or below this dimension that look up one of its members (`findMember`). IAM
104
+ * answers a lookup only for one of them, held on the directory's value. Absent, no endpoint looks one up.
105
+ */
106
+ lookups?: string[]
54
107
  }
55
108
 
109
+ /**
110
+ * The role keys IAM defines itself for every app. A built-in wins over an app role of the same key at
111
+ * resolution, so a membership role, and any role of a contract application, may not use one.
112
+ */
113
+ const BUILTIN_ROLE_KEY_LITERALS = ['admin'] as const
114
+
115
+ /** A role key IAM defines itself, as a type: `defineVocabulary` refuses a role of that name. */
116
+ export type BuiltinRoleKey = (typeof BUILTIN_ROLE_KEY_LITERALS)[number]
117
+
118
+ export const BUILTIN_ROLE_KEYS: readonly string[] = BUILTIN_ROLE_KEY_LITERALS
119
+
56
120
  /** One action in an app's catalog (used for validation + admin UI discovery). */
57
121
  export interface AppActionDef {
58
122
  action: string
59
123
  description?: string
60
124
  }
61
125
 
126
+ /** A share-link mount: what a link key of the mount carries and who may issue one. */
127
+ export interface AppLinkMountDef {
128
+ /** The dimension of the object a link is bound to. */
129
+ dimension: string
130
+ /** Exact actions frozen into every key, on the bound object. */
131
+ grants: string[]
132
+ /** The action the issuer must hold on the object or a value containing it. */
133
+ issuedBy: string
134
+ /** The longest a link of the mount may live, in days. */
135
+ maxDays: number
136
+ }
137
+
138
+ /**
139
+ * An endpoint whose request may spend the caller's membership authority. IAM issues a delegation grant
140
+ * only for one of these: `members` for an endpoint whose action is the dimension's `members.manage`,
141
+ * `creates` for one that creates a value of the dimension.
142
+ */
143
+ export interface AppDelegationDef {
144
+ action: string
145
+ dimension: string
146
+ purpose: 'members' | 'creates'
147
+ }
148
+
62
149
  /** An app's full authz vocabulary, declared in the app's code, reconciled into Propustka. */
63
150
  export interface AppSchema {
64
151
  scopes: AppScopeDef[]
65
152
  actions: AppActionDef[]
66
153
  /** role_key -> def; these are origin='app' roles. */
67
154
  roles: Record<string, RoleDef>
68
- }
69
-
70
- // ── Per-path access gates (the propustka-native front door, enforced by the proxy) ──
71
- //
72
- // Replaces the deleted Cloudflare-Access edge model (AppAccess/AccessAppDecl/AccessRule). There is
73
- // no IAM reconcile for these: gate rules are deployed verbatim in the proxy manifest, and the proxy
74
- // decides WHICH credential KIND a path requires. Three kinds mirror the three old edge decisions:
75
- // - public → no credential (the old `bypass`/everyone carve-out)
76
- // - service → a machine `px_` key (the old `non_identity`/service-token gate)
77
- // - human → a logged-in human via `px_session`/`px_token` (the old `allow`-by-audience gate)
78
- // WHO is an admitted human stays centrally owned by propustka (decided at `/auth/callback`, the
79
- // admission allowlist), exactly as the old central edge audience did — a `human` gate only asserts
80
- // "a resolved user principal exists". Auth-surface paths (`/auth/*`, `/.well-known/jwks.json`) live
81
- // on PROPUSTKA's host, not the app's, so apps never declare them.
82
-
83
- /** Where a propustka `px_` credential rides on a request. */
84
- export interface CredentialLocation {
85
- /** Transport carrying the credential. */
86
- in: 'header' | 'query' | 'cookie'
87
- /** Header / query-param / cookie name. A header value may be bare or `Bearer <token>`. */
88
- name: string
89
- }
90
-
91
- /** What a matched path requires. The discriminant `kind` mirrors the three old edge decisions. */
92
- export type GateKind =
93
- /** Anyone — no credential. Terminal; resolves to an ANONYMOUS AuthContext (`principal: null`). */
94
- | { kind: 'public' }
155
+ /** Compiled from a contract (`schemaFromContract`): IAM then refuses every wildcard action written for the app. */
156
+ contract?: boolean
157
+ /** Link mount id -> its definition. Removing a mount revokes its keys. */
158
+ links?: Record<string, AppLinkMountDef>
159
+ /** Endpoint id -> what the endpoint may spend through a delegation grant. */
160
+ delegations?: Record<string, AppDelegationDef>
161
+ /**
162
+ * The workloads of a contract application and their exact actions. A workload key resolves to these actions, app-wide,
163
+ * and to nothing once its workload is no longer declared; removing one revokes its keys.
164
+ */
165
+ workloads?: AppWorkloadDef[]
166
+ /** The language IAM speaks to the app's people; absent, IAM's default. */
167
+ locale?: Locale
168
+ /** The name IAM shows on its pages and e-mails for the app; absent, a wording without a name. */
169
+ displayName?: string
170
+ /** The app's wording of IAM's e-mails, by message and language; absent, IAM's own. */
171
+ mail?: MailTemplates
95
172
  /**
96
- * A machine `px_` key — a bearer (default `Authorization: Bearer`) or the
97
- * declared `credential` location. ABSENT → falls through to the next matching rule; PRESENT but
98
- * invalid → fail closed (no fall-through).
173
+ * The application's own people, every holder of one of its roles. A holder of `manage`, globally at the app, grants and
174
+ * revokes the app's global roles in IAM's console within its own actions; a holder of `list`, globally, reads the
175
+ * directory (`listAppMembers`). Absent, only an IAM administrator grants them and nobody lists them.
99
176
  */
100
- | { kind: 'service'; credential?: CredentialLocation }
101
- /** A logged-in human (a `user` principal via the `px_session`/`px_token` cookies). */
102
- | { kind: 'human' }
177
+ members?: { manage: string; list: string }
178
+ /** `labels`: a holder of the app's `members.list` also sees each member's label. Absent, labels stay with `members.manage`. */
179
+ directory?: 'labels'
180
+ }
103
181
 
104
182
  /**
105
- * One per-path gate rule. The array order on `AppGates.rules` is the PRECEDENCE: the first rule whose
106
- * `path` matches AND whose required credential is present is enforced. A matching rule whose credential
107
- * is ABSENT falls through to the next matching rule; a request matching NO rule is denied (fail-closed).
108
- * `path` is a glob where `*` matches any run of chars (e.g. `/api/v1/*`); anchored.
183
+ * A job or a webhook of a contract application, or a consuming application of its exports (`app`, named by that
184
+ * application's id): the actions its credential carries.
109
185
  */
110
- export type GateRule = { path: string } & GateKind
111
-
112
- /** An app's full per-path gate declaration, enforced by the Fabrika proxy. */
113
- export interface AppGates {
114
- rules: GateRule[]
186
+ export interface AppWorkloadDef {
187
+ kind: WorkloadKind
188
+ name: string
189
+ actions: string[]
190
+ /**
191
+ * A job only: the link mount whose links it may issue (`issueLink`) and revoke, its own only. Its actions hold the
192
+ * mount's `issuedBy` and every grant, app-wide, so the job issues for any object of the mount's dimension.
193
+ */
194
+ issues?: string
115
195
  }
116
196
 
117
197
  /**
package/src/wire.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { sha256Hex } from './sha256'
2
+
1
3
  /** Header carrying the proxy-verified access token to the upstream app. */
2
4
  export const PROXY_TOKEN_HEADER = 'X-Fabrika-Token'
3
5
 
@@ -28,3 +30,49 @@ export const CLIENT_ADDRESS_HEADER = 'X-Fabrika-Client-Ip'
28
30
  * header with one meaning everywhere, supplied only by the enforcement point.
29
31
  */
30
32
  export const UNTRUSTED_FORWARD_HEADERS: readonly string[] = ['X-Forwarded-For', 'CF-Connecting-IP']
33
+
34
+ /**
35
+ * Header carrying the endpoint the proxy selected and the digest of the rules it applied, `<id>@<digest>`
36
+ * (`formatEndpointHeader`). Set by the proxy on every allowed request to a contract application, after the strip of
37
+ * a client's own; the application serves only that endpoint and answers 503 when its digest differs.
38
+ */
39
+ export const ENDPOINT_HEADER = 'X-Fabrika-Endpoint'
40
+
41
+ /**
42
+ * Header carrying the single-use delegation grant the proxy obtained from IAM for a membership or scope-creating
43
+ * endpoint (ADR-0022 rule 6). The framework forwards it to IAM; nothing else reads it.
44
+ */
45
+ export const DELEGATION_HEADER = 'X-Fabrika-Delegation'
46
+
47
+ /**
48
+ * Header carrying the proxy secret of one application environment (ADR-0022 rule 5), on Zerops only: a namespace is
49
+ * one private network, so the application refuses a request that does not prove it came through the proxy. On
50
+ * Cloudflare the service binding is the authenticated hop and this header is never set.
51
+ */
52
+ export const PROXY_SECRET_HEADER = 'X-Fabrika-Proxy-Secret'
53
+
54
+ /** The application's framework variable holding the secret its proxy sends in `PROXY_SECRET_HEADER`. */
55
+ export const FABRIKA_PROXY_SECRET = 'FABRIKA_PROXY_SECRET'
56
+
57
+ /**
58
+ * The proxy service's variable holding the secret of application environment `id` (`<app>@<env>`). One proxy fronts
59
+ * every application of a namespace, so each secret needs its own name; an environment name may hold any character, so
60
+ * the name carries a digest of the id rather than the id.
61
+ */
62
+ export function proxySecretVariable(id: string): string {
63
+ return `${FABRIKA_PROXY_SECRET}_${sha256Hex(id).slice(0, 32).toUpperCase()}`
64
+ }
65
+
66
+ /**
67
+ * Every `X-Fabrika-*` name a proxy hop carries to an application. Caddy's first step and the Cloudflare proxy Worker
68
+ * delete each of them from the client's request before anything else, then set only what the decision asserts. A new
69
+ * header the proxy sets joins this list in the same change. The Operations upload headers are not hop headers: a
70
+ * client sends them to Operations through the platform proxy, so the proxy must let them pass.
71
+ */
72
+ export const FABRIKA_HOP_HEADERS: readonly string[] = [
73
+ PROXY_TOKEN_HEADER,
74
+ CLIENT_ADDRESS_HEADER,
75
+ ENDPOINT_HEADER,
76
+ DELEGATION_HEADER,
77
+ PROXY_SECRET_HEADER,
78
+ ]
package/src/gates.ts DELETED
@@ -1,29 +0,0 @@
1
- import type { AppGates, GateRule } from './types'
2
-
3
- /** A gate rule with its path glob precompiled. */
4
- export interface CompiledGate {
5
- readonly rule: GateRule
6
- matches(pathname: string): boolean
7
- }
8
-
9
- /** Precompile an app's gate rules while preserving declaration order. */
10
- export function compileGates(gates: AppGates): CompiledGate[] {
11
- return gates.rules.map((rule) => {
12
- const regex = globToRegExp(rule.path)
13
- return { rule, matches: (pathname: string) => regex.test(pathname) }
14
- })
15
- }
16
-
17
- /** Return matching rules in declaration order. */
18
- export function applicableGates(compiled: readonly CompiledGate[], pathname: string): CompiledGate[] {
19
- return compiled.filter((gate) => gate.matches(pathname))
20
- }
21
-
22
- /** Glob where `*` crosses path separators; all other characters are literal. */
23
- function globToRegExp(pattern: string): RegExp {
24
- return new RegExp(`^${pattern.split('*').map(escapeRegExp).join('.*')}$`)
25
- }
26
-
27
- function escapeRegExp(literal: string): string {
28
- return literal.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
29
- }