@fabrika/auth-core 0.9.0-dev.1 → 0.9.0-dev.3

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.
@@ -0,0 +1,318 @@
1
+ import type { ContractArtifact, EndpointRow } from './contract';
2
+ import type { PrincipalAffiliation, Scope } from './types';
3
+ /**
4
+ * What an endpoint may ask IAM about the members of its own target (`ctx.members`): `manage` where its action is the
5
+ * dimension's `members.manage`, `list` where it is the dimension's `members.list`, on or in that dimension. IAM checks
6
+ * the same action live, so the endpoint's own access already names what IAM requires.
7
+ */
8
+ export declare function endpointMembers(artifact: Pick<ContractArtifact, 'dimensions'>, row: EndpointRow): 'manage' | 'list' | null;
9
+ /**
10
+ * The member directory an endpoint reads through `ctx.members` when it does not manage members: the nearest dimension,
11
+ * its own or one containing it, that declares members. Every such endpoint (access `on` or `in`) may look up one member
12
+ * it names; `lists` where its action is that dimension's `members.list`, which alone may list them. IAM checks either
13
+ * action live on the directory's value itself.
14
+ */
15
+ export interface EndpointDirectory {
16
+ readonly dimension: string;
17
+ readonly lists: boolean;
18
+ }
19
+ /**
20
+ * The directory reach an endpoint's access form gives it, from the dimensions alone: what `buildArtifact` records in
21
+ * `ContractArtifact.directories`. Nothing else derives it, so an approved artifact holds every reach the runtime grants.
22
+ */
23
+ export declare function directoryReach(dimensions: ContractArtifact['dimensions'], row: EndpointRow): EndpointDirectory | null;
24
+ /** The directory reach the artifact records for the endpoint; an artifact compiled before it recorded any grants none. */
25
+ export declare function endpointDirectory(artifact: Pick<ContractArtifact, 'directories'>, row: EndpointRow): EndpointDirectory | null;
26
+ /**
27
+ * The actions of the endpoints whose directory is `dimension`, sorted: what IAM's schema holds as the dimension's
28
+ * `lookups`, so IAM answers a lookup only for an action an approved endpoint below the dimension holds.
29
+ */
30
+ export declare function directoryLookups(artifact: Pick<ContractArtifact, 'directories' | 'endpoints'>, dimension: string): string[];
31
+ /**
32
+ * Whether an endpoint reads the application's own directory (`ctx.members.list()` of `listAppMembers`): a procedure of
33
+ * an RPC mount whose access is `app` and whose action is the application's `members.list`, which IAM checks live.
34
+ */
35
+ export declare function endpointAppDirectory(artifact: Pick<ContractArtifact, 'members'>, row: EndpointRow): boolean;
36
+ /** Every request carries the caller's own credential; `app` is superseded by the credential's verified app. */
37
+ export interface MembershipCaller {
38
+ app: string;
39
+ /** The caller's verified access token, or a `px_` key bound to a user principal. */
40
+ credential: string | null;
41
+ requestId: string;
42
+ }
43
+ /**
44
+ * Why IAM refused. `not_allowed` is a lack of authority; `invalid_delegation` a missing, spent or
45
+ * mismatched delegation grant; `invalid_request` a request the app's schema cannot express, with a
46
+ * message naming the problem.
47
+ */
48
+ export type MembershipFailure = {
49
+ ok: false;
50
+ reason: 'missing_token' | 'invalid_token' | 'unknown_principal' | 'disabled' | 'not_allowed' | 'invalid_delegation' | 'not_found';
51
+ }
52
+ /** Revoking would leave the value without a holder of its creator role. */
53
+ | {
54
+ ok: false;
55
+ reason: 'last_creator';
56
+ }
57
+ /** The installation sends no email, so it cannot verify an invited address. */
58
+ | {
59
+ ok: false;
60
+ reason: 'invitations_disabled' | 'delivery_failed';
61
+ }
62
+ /**
63
+ * An invitation to a role declared external for an address the organization names (a bootstrap admin, or one the
64
+ * OIDC allowlist names by address or domain): IAM never creates such a mailbox as an external person (ADR-0051).
65
+ */
66
+ | {
67
+ ok: false;
68
+ reason: 'internal_mailbox';
69
+ } | {
70
+ ok: false;
71
+ reason: 'invalid_request';
72
+ message: string;
73
+ };
74
+ export type MembershipFailureReason = MembershipFailure['reason'];
75
+ /** The longest scope type or value a membership operation takes; a lookup of a longer principal id finds no member. */
76
+ export declare const MAX_MEMBERSHIP_SCOPE_PART_LENGTH = 200;
77
+ /** The most values a share link's chain names: its object and each value containing it. */
78
+ export declare const MAX_LINK_CHAIN_LENGTH = 8;
79
+ /** The longest address IAM invites, after `normalizeEmailIdentity`. */
80
+ export declare const MAX_INVITATION_EMAIL_LENGTH = 254;
81
+ /** Why IAM refuses `scope` as a membership operation's coordinate, or `null`. */
82
+ export declare function membershipScopeProblem(scope: Scope): string | null;
83
+ /** Normalize an email identity for case-insensitive exact-mailbox comparisons. */
84
+ export declare function normalizeEmailIdentity(value: string): string;
85
+ /**
86
+ * A mailbox as IAM stores it: a trimmed, lower-cased address. It never normalizes again: NFC before lower-casing is not
87
+ * idempotent (`J\u030C` stores as `j\u030C`, which NFC would compose to `\u01F0`), so a second pass would refuse a
88
+ * stored mailbox.
89
+ */
90
+ export declare function isNormalizedMailbox(value: string): boolean;
91
+ /** The normalized address IAM invites for `address`, or `null` for one it refuses as no address. */
92
+ export declare function invitedAddress(address: string): string | null;
93
+ /** The longest company label an invitation carries: display text the inviting application names. */
94
+ export declare const MAX_INVITATION_LABEL_LENGTH = 200;
95
+ /** The longest personal message an invitation carries. */
96
+ export declare const MAX_INVITATION_MESSAGE_LENGTH = 1000;
97
+ /**
98
+ * What an invitation by address carries besides its role. `label` is the invited person's company as the application
99
+ * names it, `message` the inviter's personal note; both are plain text IAM escapes wherever it inserts them. `resend`
100
+ * rotates the open invitation's link, restarts its lifetime and sends it again; without it, repeating an invitation
101
+ * answers the open one and sends nothing.
102
+ */
103
+ export interface InvitationOptions {
104
+ readonly label?: string;
105
+ readonly message?: string;
106
+ readonly resend?: boolean;
107
+ }
108
+ /** Why IAM refuses an invitation's label or message, or `null`. A message may break lines; nothing else is a control character. */
109
+ export declare function invitationOptionsProblem(options: InvitationOptions): string | null;
110
+ /** How long a delegation grant may be held before it is spent. One request, so seconds. */
111
+ export declare const DELEGATION_GRANT_TTL_SECONDS = 120;
112
+ /**
113
+ * Obtain a single-use delegation grant for ONE request to one endpoint. The endpoint must be one the
114
+ * app's schema lists under `delegations`, and the grant is spent by one membership operation of the same
115
+ * caller and request id.
116
+ */
117
+ export interface RequestDelegationInput extends MembershipCaller {
118
+ endpoint: string;
119
+ }
120
+ export type RequestDelegationResult = {
121
+ ok: true;
122
+ grant: string;
123
+ expiresAt: number;
124
+ } | MembershipFailure;
125
+ /** Create a value of `dimension`: IAM generates it and grants the caller the dimension's creator role, atomically. */
126
+ export interface CreateScopeInput extends MembershipCaller {
127
+ /** A grant for an endpoint whose purpose is `creates` for this dimension. */
128
+ delegation: string;
129
+ dimension: string;
130
+ }
131
+ export type CreateScopeResult = {
132
+ ok: true;
133
+ scope: Scope;
134
+ role: string;
135
+ } | MembershipFailure;
136
+ /** Who a membership operation is about: an existing principal, or an address IAM invites. */
137
+ export type MemberTarget = {
138
+ principalId: string;
139
+ } | {
140
+ email: string;
141
+ };
142
+ /**
143
+ * Give `role` in `scope`. The caller holds the dimension's `members.manage` there, holds a role there
144
+ * that may assign `role`, and holds every action `role` grants there. An existing principal receives the
145
+ * grant; an address receives an invitation that only its verified owner can accept, and for a role declared
146
+ * external that IAM creates as an external principal at acceptance when no principal holds it. Repeating the
147
+ * call answers the same result.
148
+ */
149
+ export interface AssignMemberInput extends MembershipCaller {
150
+ /** A grant for an endpoint whose purpose is `members` for `scope.type`. */
151
+ delegation: string;
152
+ scope: Scope;
153
+ role: string;
154
+ member: MemberTarget;
155
+ /** Only with an address: the invitation's label, message and resend (`InvitationOptions`). */
156
+ invitation?: InvitationOptions;
157
+ }
158
+ export type AssignMemberResult = {
159
+ ok: true;
160
+ assigned: {
161
+ principalId: string;
162
+ role: string;
163
+ };
164
+ } | {
165
+ ok: true;
166
+ invited: {
167
+ invitationId: string;
168
+ role: string;
169
+ expiresAt: number;
170
+ };
171
+ } | MembershipFailure;
172
+ /**
173
+ * Remove a principal's membership roles in `scope` (only `role` when given), or cancel an invitation.
174
+ * Each removed role must be one the caller could assign; the last holder of the creator role stays.
175
+ * Repeating the call answers the same result.
176
+ */
177
+ export interface RevokeMemberInput extends MembershipCaller {
178
+ delegation: string;
179
+ scope: Scope;
180
+ member: {
181
+ principalId: string;
182
+ role?: string | null;
183
+ } | {
184
+ invitationId: string;
185
+ };
186
+ }
187
+ export type RevokeMemberResult = {
188
+ ok: true;
189
+ } | MembershipFailure;
190
+ /**
191
+ * The members of `scope`, with their labels. Needs the dimension's `members.list` there; emails, last sign-ins and
192
+ * invitations only to `members.manage`.
193
+ */
194
+ export interface ListMembersInput extends MembershipCaller {
195
+ scope: Scope;
196
+ }
197
+ export interface MemberItem {
198
+ principalId: string;
199
+ /** The principal's display label (a user's spelling of its address); null from an IAM that shows it only to `members.manage`. */
200
+ label: string | null;
201
+ /** Null unless the caller holds the dimension's `members.manage` in the scope. */
202
+ email: string | null;
203
+ /**
204
+ * When the member last signed in to the application (unix seconds): null for one that never did, and unless the caller
205
+ * holds the dimension's `members.manage` in the scope, since it is personal data like the address.
206
+ */
207
+ lastSignInAt: number | null;
208
+ roles: string[];
209
+ disabled: boolean;
210
+ /** An `external` member is a person from outside the organization (ADR-0051). */
211
+ affiliation: PrincipalAffiliation;
212
+ }
213
+ export interface InvitationItem {
214
+ id: string;
215
+ email: string;
216
+ role: string;
217
+ expiresAt: number;
218
+ }
219
+ export type ListMembersResult = {
220
+ ok: true;
221
+ members: MemberItem[];
222
+ invitations: InvitationItem[];
223
+ } | MembershipFailure;
224
+ /**
225
+ * Look up ONE principal in the members of `scope`, for an endpoint on, in or below its dimension. `action` is the
226
+ * endpoint's own: IAM answers only when the schema lists it among the dimension's `lookups` and the caller holds it on
227
+ * `scope` itself, so the application names no chain IAM would have to trust. Listing stays with `members.list`.
228
+ */
229
+ export interface FindMemberInput extends MembershipCaller {
230
+ scope: Scope;
231
+ principalId: string;
232
+ action: string;
233
+ }
234
+ export interface FoundMember {
235
+ principalId: string;
236
+ /** The principal's display label; null unless the dimension declares `directory: 'labels'` or the caller holds `members.manage` there. */
237
+ label: string | null;
238
+ disabled: boolean;
239
+ affiliation: PrincipalAffiliation;
240
+ }
241
+ /** `member` is null for a principal that holds no membership role in `scope`, unknown ones included. */
242
+ export type FindMemberResult = {
243
+ ok: true;
244
+ member: FoundMember | null;
245
+ } | MembershipFailure;
246
+ /** The most members one page of an application's directory answers. */
247
+ export declare const MAX_APP_MEMBERS_PAGE = 100;
248
+ /**
249
+ * One page of the application's own people (`AppSchema.members`): every principal holding a role of the application,
250
+ * globally or in a value. Needs the application's `members.list` held globally; labels by the same rule as a value's
251
+ * directory, emails and last sign-ins only to its `members.manage` held globally. Pages run by principal id: `after`
252
+ * is the previous page's `next`.
253
+ */
254
+ export interface ListAppMembersInput extends MembershipCaller {
255
+ after?: string;
256
+ /** 1 to `MAX_APP_MEMBERS_PAGE`; absent, the most. */
257
+ limit?: number;
258
+ }
259
+ /** One role a principal holds at the application: globally (`scope: null`), or in one value. */
260
+ export interface AppMemberRole {
261
+ role: string;
262
+ scope: Scope | null;
263
+ }
264
+ export interface AppMemberItem {
265
+ principalId: string;
266
+ /** The principal's display label; null unless the application declares `directory: 'labels'` or the caller manages its people. */
267
+ label: string | null;
268
+ /** Null unless the caller holds the application's `members.manage` globally. */
269
+ email: string | null;
270
+ /** As `MemberItem.lastSignInAt`, to a holder of the application's `members.manage` only. */
271
+ lastSignInAt: number | null;
272
+ /** Sorted by role, then scope; a global grant first. */
273
+ roles: AppMemberRole[];
274
+ disabled: boolean;
275
+ affiliation: PrincipalAffiliation;
276
+ }
277
+ /** `next` is the `after` of the following page, null on the last one. */
278
+ export type ListAppMembersResult = {
279
+ ok: true;
280
+ members: AppMemberItem[];
281
+ next: string | null;
282
+ } | MembershipFailure;
283
+ /**
284
+ * Issue a share link of `mount` for the object `chain[0]`. `chain` names the object and every value
285
+ * containing it, outward, as the app resolved it from its database; the framework re-checks it on every
286
+ * link request, so a link whose object moved stops working. The caller holds the mount's `issuedBy` and
287
+ * every granted action on some link of the chain.
288
+ */
289
+ export interface IssueLinkInput extends MembershipCaller {
290
+ mount: string;
291
+ chain: Scope[];
292
+ /** Required, at most the mount's `maxDays`. */
293
+ days: number;
294
+ }
295
+ export type IssueLinkResult = {
296
+ ok: true;
297
+ key: string;
298
+ id: string;
299
+ expiresAt: number;
300
+ } | MembershipFailure;
301
+ /** Revoke a share link: its issuer, or a holder of `members.manage` on a value of its chain. Idempotent. */
302
+ export interface RevokeLinkInput extends MembershipCaller {
303
+ id: string;
304
+ }
305
+ export type RevokeLinkResult = {
306
+ ok: true;
307
+ } | MembershipFailure;
308
+ export interface IamMembershipRpc {
309
+ requestDelegation(input: RequestDelegationInput): Promise<RequestDelegationResult>;
310
+ createScope(input: CreateScopeInput): Promise<CreateScopeResult>;
311
+ assignMember(input: AssignMemberInput): Promise<AssignMemberResult>;
312
+ revokeMember(input: RevokeMemberInput): Promise<RevokeMemberResult>;
313
+ listMembers(input: ListMembersInput): Promise<ListMembersResult>;
314
+ listAppMembers(input: ListAppMembersInput): Promise<ListAppMembersResult>;
315
+ findMember(input: FindMemberInput): Promise<FindMemberResult>;
316
+ issueLink(input: IssueLinkInput): Promise<IssueLinkResult>;
317
+ revokeLink(input: RevokeLinkInput): Promise<RevokeLinkResult>;
318
+ }
@@ -0,0 +1,7 @@
1
+ /** OAuth 2.1 primitives shared by IAM (issuer), the proxy (enforcement) and resource servers (verification). */
2
+ /** RFC 8707 resources are byte-exact; plain HTTP is reserved for loopback development. */
3
+ export declare function isOAuthResourceUri(value: unknown): value is string;
4
+ /** Client ID Metadata Document identifier: HTTPS, explicit path, no userinfo, fragment or dot segments. */
5
+ export declare function isCimdClientId(value: unknown): value is string;
6
+ /** RFC 6749 scope-token syntax; duplicates and empty sets are refused. */
7
+ export declare function parseOAuthScopes(value: unknown): string[] | null;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The name of a `:name` path segment, or null for any other segment. Every endpoint table, an application's and an
3
+ * installation service's, reads a parameter by this rule: it matches exactly one non-empty segment.
4
+ */
5
+ export declare function parameterName(segment: string): string | null;
@@ -0,0 +1,50 @@
1
+ import type { PermissionEntry, Scope } from './types';
2
+ /**
3
+ * Wildcard action matching. The pattern is one of:
4
+ * - '*' — matches every action
5
+ * - 'prefix.*' — matches the `prefix` namespace and anything nested under it
6
+ * (e.g. 'project.*' matches 'project.read' and 'project.settings.update',
7
+ * but NOT 'projects.read' — the boundary is the dot)
8
+ * - exact — matches only that exact action string
9
+ *
10
+ * Examples:
11
+ * matchAction('*', 'x.y') === true
12
+ * matchAction('project.*', 'project.read') === true
13
+ * matchAction('project.*', 'project.settings.update') === true
14
+ * matchAction('project.read', 'project.read') === true
15
+ * matchAction('project.read', 'project.write') === false
16
+ * matchAction('project.*', 'projects.read') === false
17
+ */
18
+ export declare function matchAction(pattern: string, action: string): boolean;
19
+ /**
20
+ * Does `entries` grant `action` at the given scope? Mirrors `can()` semantics exactly:
21
+ * - scope === undefined → scope-less: ONLY entries with scope === null satisfy (global perms only).
22
+ * - scope === <Scope> → entries with scope === null OR an entry scoped to the SAME
23
+ * (type, value) pair satisfy.
24
+ *
25
+ * Within the satisfying entries, the entry's `action` is matched against the requested
26
+ * `action` using `matchAction` (so a wildcard entry like 'project.*' grants 'project.read').
27
+ */
28
+ export declare function permits(entries: PermissionEntry[], action: string, scope?: Scope): boolean;
29
+ /**
30
+ * The set of scope values `entries` grant `action` on within a single `dimension`
31
+ * (scope type) — the resolution behind `scopedTo()`, shared so the real SDK context and
32
+ * any fake/test context agree exactly:
33
+ * - `null` → unrestricted: a matching GLOBAL entry (scope === null) exists, so the
34
+ * principal holds the action everywhere (e.g. an admin / app-wide grant);
35
+ * - non-empty → exactly the scope values of the matching entries scoped to `dimension`;
36
+ * - `[]` → no matching entry: no scoped access at all within `dimension`.
37
+ * A matching global entry short-circuits to `null` (it dominates any scoped entries).
38
+ * Entries scoped to a DIFFERENT dimension are ignored (they say nothing about `dimension`).
39
+ */
40
+ export declare function scopedValues(entries: PermissionEntry[], action: string, dimension: string): string[] | null;
41
+ /**
42
+ * Validate a role/policy/inline action PATTERN against an app's action `catalog`.
43
+ * The catalog holds concrete action strings the app actually exposes (no wildcards).
44
+ * - '*' → always valid (the universal wildcard).
45
+ * - exact match → valid iff the catalog contains the pattern verbatim.
46
+ * - 'prefix.*' → valid iff some catalog action starts with `prefix.` (i.e. the
47
+ * namespace is non-empty), so we never grant a wildcard over nothing.
48
+ * - otherwise → invalid.
49
+ */
50
+ export declare function isActionAllowed(pattern: string, catalog: readonly string[]): boolean;
@@ -0,0 +1,24 @@
1
+ import { type EndpointRequirement } from './contract';
2
+ import { type AuthenticatedRequirement, type TokenCaller } from './installation-endpoint';
3
+ import type { AccessTokenClaims } from './token';
4
+ import type { PermissionEntry } from './types';
5
+ /**
6
+ * Whether a token's permissions meet an endpoint's requirement. This is authorization, not a gate: it reads only the
7
+ * token. `member` is any permission of this application; an action is that action in some scope, global included.
8
+ * The access form narrows it to one scope afterwards, against the application's own data. The `app` form is the
9
+ * action held as a global grant, so an entry scoped to any value does not meet it. Several capabilities need every
10
+ * one of their actions. `authenticated`, which only an installation row carries, needs no permission.
11
+ */
12
+ export declare function satisfies(requirement: EndpointRequirement | AuthenticatedRequirement, perms: readonly PermissionEntry[]): boolean;
13
+ /**
14
+ * The caller kind a verified token presents: a `user` principal is `human`, a `service` principal is `machine`, a
15
+ * connector key of application `<id>` is `app:<id>`, and a token bound to no principal is a share link. An OAuth
16
+ * resource token is `oauth`, which only an installation row admits. A job, webhook or public procedure token is no
17
+ * caller: `null` is refused by every endpoint.
18
+ */
19
+ export declare function callerOf(claims: AccessTokenClaims): TokenCaller | null;
20
+ /**
21
+ * Whether a caller may be admitted at an endpoint path. A connector caller `app:<id>` reaches only an export's paths
22
+ * (ADR-0022 "Principal kinds"); every other caller is not limited by the path.
23
+ */
24
+ export declare function callerAdmittedAt(caller: TokenCaller, path: string): boolean;