@fabrika/iam 0.9.0-dev.35 → 0.9.0-dev.37
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/migrations/0035_grant_requests.sql +41 -0
- package/migrations/0036_grant_scope_sets.sql +14 -0
- package/migrations-postgres/0035_grant_requests.sql +41 -0
- package/migrations-postgres/0036_grant_scope_sets.sql +14 -0
- package/package.json +11 -11
- package/src/admin/api-keys.ts +8 -5
- package/src/admin/app-people.ts +54 -1
- package/src/admin/app-schema.ts +10 -2
- package/src/admin/grant-requests.ts +276 -0
- package/src/admin/grant-support.ts +56 -18
- package/src/admin/grants.ts +174 -46
- package/src/admin/rpc.ts +37 -0
- package/src/db.ts +5 -1
- package/src/oauth-http.ts +22 -2
- package/src/oauth.ts +52 -6
- package/src/repositories/app-schema.ts +9 -4
- package/src/repositories/audit.ts +5 -2
- package/src/repositories/grant-requests.ts +139 -0
- package/src/repositories/grants.ts +260 -30
- package/src/stored-permissions.ts +25 -1
- package/src/tokens.ts +1 -1
- package/types/src/admin/grant-requests.d.ts +42 -0
- package/types/src/admin/grant-support.d.ts +15 -6
- package/types/src/admin/grants.d.ts +4 -4
- package/types/src/db.d.ts +4 -1
- package/types/src/oauth.d.ts +20 -6
- package/types/src/repositories/app-schema.d.ts +4 -1
- package/types/src/repositories/audit.d.ts +2 -0
- package/types/src/repositories/grant-requests.d.ts +68 -0
- package/types/src/repositories/grants.d.ts +63 -2
- package/types/src/stored-permissions.d.ts +6 -0
- package/types/src/tokens.d.ts +7 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type SqlDatabase } from '@fabrika/platform';
|
|
2
|
+
import { type AuditEventInput, AuditWriter } from './audit';
|
|
3
|
+
/**
|
|
4
|
+
* One requested grant change at an application whose console grants wait for approval (ADR-0083). Nothing that resolves
|
|
5
|
+
* permissions reads it; `GrantRepository.applyRequest` writes the change when a person approves it.
|
|
6
|
+
*/
|
|
7
|
+
export interface GrantRequestRow {
|
|
8
|
+
id: string;
|
|
9
|
+
app: string;
|
|
10
|
+
kind: 'create' | 'update' | 'revoke';
|
|
11
|
+
principal_id: string;
|
|
12
|
+
/** The grant an `update` edits or a `revoke` removes; null for `create`. */
|
|
13
|
+
grant_id: string | null;
|
|
14
|
+
role_key: string | null;
|
|
15
|
+
/** Inline action-pattern set (JSON array); XOR `role_key`. */
|
|
16
|
+
permissions: string | null;
|
|
17
|
+
scope_type: string | null;
|
|
18
|
+
/** The first of the requested values, or the only one; null for a global grant. */
|
|
19
|
+
scope_value: string | null;
|
|
20
|
+
/** Every requested value (JSON array) when there are several (ADR-0085); read through `requestScopeValues`. */
|
|
21
|
+
scope_values: string | null;
|
|
22
|
+
grant_expires_at: number | null;
|
|
23
|
+
requested_by: string;
|
|
24
|
+
requested_at: number;
|
|
25
|
+
expires_at: number;
|
|
26
|
+
status: 'pending' | 'approved' | 'rejected' | 'failed';
|
|
27
|
+
/** The approval that claimed it, which the change it wrote carries in its guard. */
|
|
28
|
+
decision_id: string | null;
|
|
29
|
+
decided_by: string | null;
|
|
30
|
+
decided_at: number | null;
|
|
31
|
+
}
|
|
32
|
+
export interface CreateGrantRequestInput {
|
|
33
|
+
app: string;
|
|
34
|
+
kind: GrantRequestRow['kind'];
|
|
35
|
+
principalId: string;
|
|
36
|
+
grantId: string | null;
|
|
37
|
+
roleKey: string | null;
|
|
38
|
+
permissions: string[] | null;
|
|
39
|
+
scopeType: string | null;
|
|
40
|
+
/** Sorted and distinct; null for a global grant. */
|
|
41
|
+
scopeValues: string[] | null;
|
|
42
|
+
grantExpiresAt: number | null;
|
|
43
|
+
requestedBy: string;
|
|
44
|
+
requestedAt: number;
|
|
45
|
+
expiresAt: number;
|
|
46
|
+
}
|
|
47
|
+
export declare class GrantRequestRepository {
|
|
48
|
+
private readonly db;
|
|
49
|
+
private readonly writer;
|
|
50
|
+
constructor(db: SqlDatabase, writer?: AuditWriter);
|
|
51
|
+
/** Whether a grant change at `app` waits for approval. */
|
|
52
|
+
grantsWait(app: string): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Set whether grant changes at `app` wait, as of Control's protection `revision`. An older revision than the stored one
|
|
55
|
+
* changes nothing, so a caller that lost the environment's lease cannot undo its successor's gate. The record is written
|
|
56
|
+
* only when `required` changes.
|
|
57
|
+
*/
|
|
58
|
+
setGrantsWait(app: string, required: boolean, revision: number, now: number, audit: AuditEventInput): Promise<{
|
|
59
|
+
required: boolean;
|
|
60
|
+
revision: number;
|
|
61
|
+
}>;
|
|
62
|
+
create(input: CreateGrantRequestInput, audit: (requestId: string) => AuditEventInput): Promise<GrantRequestRow>;
|
|
63
|
+
get(id: string): Promise<GrantRequestRow | null>;
|
|
64
|
+
/** Every request still open at `now`, the oldest first. */
|
|
65
|
+
listOpen(now: number): Promise<GrantRequestRow[]>;
|
|
66
|
+
/** Reject an open request; null when it was decided or lapsed meanwhile. */
|
|
67
|
+
reject(id: string, decidedBy: string, now: number, audit: AuditEventInput): Promise<GrantRequestRow | null>;
|
|
68
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type SqlDatabase, type SqlStatement } from '@fabrika/platform';
|
|
2
2
|
import { type AuditEventInput, AuditWriter, type SqlChange, type SqlDialect } from './audit';
|
|
3
|
+
import type { GrantRequestRow } from './grant-requests';
|
|
3
4
|
/** A grant written for a workload principal: its authority is its declared actions, never a grant. */
|
|
4
5
|
export declare class WorkloadGrantError extends Error {
|
|
5
6
|
readonly name = "WorkloadGrantError";
|
|
@@ -62,6 +63,8 @@ export interface GrantRow {
|
|
|
62
63
|
scope_type: string | null;
|
|
63
64
|
/** Opaque, app-owned scope value; NULL = global. */
|
|
64
65
|
scope_value: string | null;
|
|
66
|
+
/** The grant this row is one value of, shared by every row of a grant of several values; NULL = a grant of one (ADR-0085). */
|
|
67
|
+
scope_set: string | null;
|
|
65
68
|
granted_by: string | null;
|
|
66
69
|
expires_at: number | null;
|
|
67
70
|
created_at: number;
|
|
@@ -73,8 +76,21 @@ export interface CreateGrantInput {
|
|
|
73
76
|
permissions?: string[] | null;
|
|
74
77
|
scopeType?: string | null;
|
|
75
78
|
scopeValue?: string | null;
|
|
79
|
+
/** The grant id every row of a grant of several values shares (ADR-0085); null or omitted for a grant of one. */
|
|
80
|
+
scopeSet?: string | null;
|
|
76
81
|
grantedBy?: string | null;
|
|
77
82
|
expiresAt?: number | null;
|
|
83
|
+
/** When the grant was first written; an edit keeps it. Omitted = now. */
|
|
84
|
+
createdAt?: number;
|
|
85
|
+
}
|
|
86
|
+
/** A grant of one or more values of one dimension, or a global one (`scopeType` and `scopeValues` null). */
|
|
87
|
+
export interface CreateGrantSetInput extends Omit<CreateGrantInput, 'scopeValue' | 'scopeSet'> {
|
|
88
|
+
scopeValues: readonly string[] | null;
|
|
89
|
+
}
|
|
90
|
+
/** The rows of one grant (ADR-0085) and its principal's grant revision, read by one statement. */
|
|
91
|
+
export interface GrantSetRead {
|
|
92
|
+
readonly rows: [GrantRow, ...GrantRow[]];
|
|
93
|
+
readonly grantRevision: number;
|
|
78
94
|
}
|
|
79
95
|
export declare class GrantRepository {
|
|
80
96
|
protected readonly db: SqlDatabase;
|
|
@@ -102,6 +118,11 @@ export declare class GrantRepository {
|
|
|
102
118
|
*/
|
|
103
119
|
listGrantsFor(principalIds: readonly string[]): Promise<GrantRow[]>;
|
|
104
120
|
getGrantById(id: string): Promise<GrantRow | null>;
|
|
121
|
+
/**
|
|
122
|
+
* Every row of the grant `grantId` belongs to (ADR-0085), whichever of its rows the id names, with the principal's
|
|
123
|
+
* grant revision read in the same statement, so a removal after this read moves the revision `replaceGrantSet` checks.
|
|
124
|
+
*/
|
|
125
|
+
getGrantSet(grantId: string): Promise<GrantSetRead | null>;
|
|
105
126
|
/**
|
|
106
127
|
* Create a grant. EITHER `roleKey` (named role/policy) OR `permissions` (an inline
|
|
107
128
|
* action-pattern set, JSON-encoded here) — the caller MUST supply exactly one; the
|
|
@@ -111,6 +132,28 @@ export declare class GrantRepository {
|
|
|
111
132
|
* nothing, for a grant the principal may not hold (ADR-0051).
|
|
112
133
|
*/
|
|
113
134
|
createGrant(input: CreateGrantInput, audit?: (grantId: string) => AuditEventInput): Promise<GrantRow>;
|
|
135
|
+
/**
|
|
136
|
+
* Create a grant of several values of one dimension: one row per value, sharing `scope_set` (ADR-0085), all written or
|
|
137
|
+
* none, with ONE record. A duplicate role grant fails the unique index and throws, rolling the batch back. Throws
|
|
138
|
+
* `ExternalGrantError`, writing nothing, for a grant the principal may not hold.
|
|
139
|
+
*/
|
|
140
|
+
createGrantSet(input: CreateGrantSetInput, audit: (grantId: string) => AuditEventInput): Promise<GrantRow[]>;
|
|
141
|
+
/** The INSERTs of a grant's rows with a caller-generated grant id, for a batch that also writes its principal and record. */
|
|
142
|
+
grantSetStatements(id: string, input: CreateGrantSetInput): SqlStatement[];
|
|
143
|
+
/**
|
|
144
|
+
* One row per value; the first keeps the grant id, so a grant's id stays what it was through an edit. A grant of one
|
|
145
|
+
* value, and a global one, is one row without `scope_set`, as every grant before ADR-0085.
|
|
146
|
+
*/
|
|
147
|
+
private grantSetChanges;
|
|
148
|
+
/**
|
|
149
|
+
* Replace the rows of the grant `read` names with `input`, keeping its id, in one transaction with ONE record: null
|
|
150
|
+
* when the principal's grants changed since the read (its grant revision moved). Moves the revision first, because an
|
|
151
|
+
* edit can remove authority. A duplicate role grant fails the unique index and throws; a grant the principal may not
|
|
152
|
+
* hold throws `ExternalGrantError`; both roll the batch back.
|
|
153
|
+
*/
|
|
154
|
+
replaceGrantSet(read: GrantSetRead, input: CreateGrantSetInput, audit: AuditEventInput): Promise<GrantRow[] | null>;
|
|
155
|
+
/** Delete every row of the grant `grantId` names (ADR-0085), after its principal's revision, with ONE record. */
|
|
156
|
+
deleteGrantSet(grantId: string, principalId: string, audit: AuditEventInput): Promise<boolean>;
|
|
114
157
|
/** Whether `principalId` may hold this grant: it is internal, or the role is one its app declares external. */
|
|
115
158
|
isEligibleGrant(input: {
|
|
116
159
|
principalId: string;
|
|
@@ -163,8 +206,24 @@ export declare class GrantRepository {
|
|
|
163
206
|
/** The INSERT of one grant with a caller-generated id, for a batch that also writes its principal and record. */
|
|
164
207
|
grantStatement(id: string, input: CreateGrantInput, onConflict?: string): SqlStatement;
|
|
165
208
|
private grantChange;
|
|
166
|
-
/**
|
|
167
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Write the change an approved grant request asks for (ADR-0083), in one transaction with the approval's claim: the
|
|
211
|
+
* request moves `pending` → `approved` under `decision.id`, and the change carries a guard on that claim, so of two
|
|
212
|
+
* approvals one claims and writes, the other writes nothing. `current` is the grant an `update` edits or a `revoke`
|
|
213
|
+
* removes, as the approver's checks read it; an edit applies only while the principal's grants are as read. A grant of
|
|
214
|
+
* several values is written, replaced or removed whole (ADR-0085). `claimed` false: decided or lapsed meanwhile.
|
|
215
|
+
* `changed` null with `claimed`: the change wrote nothing (a role grant already held, a grant gone or edited since).
|
|
216
|
+
* A value of a new set the principal already holds the role in fails the unique index, and a replacement the principal
|
|
217
|
+
* may not hold throws `ExternalGrantError`; both roll the approval back and leave the request open.
|
|
218
|
+
*/
|
|
219
|
+
applyRequest(request: GrantRequestRow, current: GrantSetRead | null, decision: {
|
|
220
|
+
readonly id: string;
|
|
221
|
+
readonly by: string;
|
|
222
|
+
readonly at: number;
|
|
223
|
+
}, audit: (grantId: string) => AuditEventInput): Promise<{
|
|
224
|
+
claimed: boolean;
|
|
225
|
+
changed: GrantRow | null;
|
|
226
|
+
}>;
|
|
168
227
|
/** Delete a grant, after its principal's revision. */
|
|
169
228
|
deleteGrant(id: string, audit?: AuditEventInput): Promise<boolean>;
|
|
170
229
|
/** Delete all grants for a principal (revocation), after its revision (`grantRemovalStatement`). */
|
|
@@ -175,3 +234,5 @@ export declare class GrantRepository {
|
|
|
175
234
|
*/
|
|
176
235
|
deleteGrantsForPrincipalChange(principalId: string): SqlChange;
|
|
177
236
|
}
|
|
237
|
+
/** The values a grant request asks for (ADR-0085): its set, or its one value; null for a global grant. */
|
|
238
|
+
export declare function requestScopeValues(request: Pick<GrantRequestRow, 'scope_value' | 'scope_values'>): string[] | null;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type DimensionValue } from '@fabrika/auth-core';
|
|
1
2
|
/** A role's stored `via`: relation → actions. A malformed row contributes nothing, as an unparseable grant does. */
|
|
2
3
|
export declare function parseStoredVia(json: string | null): Record<string, string[]>;
|
|
3
4
|
/** Legacy rows retain valid patterns even when other entries are malformed. */
|
|
@@ -10,3 +11,8 @@ export declare function delegatedActions(role: {
|
|
|
10
11
|
readonly permissions: string;
|
|
11
12
|
readonly via: string | null;
|
|
12
13
|
}): string[];
|
|
14
|
+
/**
|
|
15
|
+
* A scope's stored `value_list` (ADR-0085), or null for none. A row that is not a list `dimensionValuesProblem` admits
|
|
16
|
+
* lists none: the console then takes free text, which is what it did before any list.
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseStoredValueList(json: string | null): DimensionValue[] | null;
|
package/types/src/tokens.d.ts
CHANGED
|
@@ -99,6 +99,13 @@ export type ResolvedCredential = {
|
|
|
99
99
|
* app, that is a downscope and is enforced the same way.
|
|
100
100
|
*/
|
|
101
101
|
export declare function resolveCredential(services: Services, cred: CredentialRow, app: string): Promise<ResolvedCredential>;
|
|
102
|
+
/**
|
|
103
|
+
* A user's mailbox for its token: `principals.email`, normalized by `PrincipalRepository`, the address the person signs
|
|
104
|
+
* in with. An application mails it as `ctx.principal.email`, so it is never `label`, the spelling the person sees.
|
|
105
|
+
*/
|
|
106
|
+
export declare function mailboxClaim(principal: PrincipalRow): {
|
|
107
|
+
email?: string;
|
|
108
|
+
};
|
|
102
109
|
/**
|
|
103
110
|
* The signed token, or null — with a warning naming the subject and the size — when it is above `who.bytes`
|
|
104
111
|
* (`MAX_ACCESS_TOKEN_BYTES` unless given). Every bearer IAM issues passes here: native tokens and OAuth resource tokens.
|