@fabrika/iam 0.9.0-dev.34 → 0.9.0-dev.36
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-postgres/0035_grant_requests.sql +41 -0
- package/package.json +11 -11
- package/src/admin/app-people.ts +54 -1
- package/src/admin/grant-requests.ts +253 -0
- package/src/admin/grants.ts +73 -4
- package/src/admin/rpc.ts +36 -0
- package/src/db.ts +4 -0
- package/src/node/runtime.ts +2 -1
- package/src/oauth-config.ts +12 -3
- package/src/oauth-device-page.ts +18 -7
- package/src/oauth-http.ts +64 -12
- package/src/oauth.ts +85 -10
- package/src/repositories/audit.ts +5 -2
- package/src/repositories/grant-requests.ts +134 -0
- package/src/repositories/grants.ts +105 -0
- package/src/services.ts +2 -2
- package/src/tokens.ts +1 -1
- package/types/src/admin/grant-requests.d.ts +42 -0
- package/types/src/admin/grants.d.ts +4 -4
- package/types/src/db.d.ts +3 -0
- package/types/src/oauth-config.d.ts +3 -2
- package/types/src/oauth.d.ts +23 -7
- package/types/src/repositories/audit.d.ts +2 -0
- package/types/src/repositories/grant-requests.d.ts +64 -0
- package/types/src/repositories/grants.d.ts +16 -0
- package/types/src/services.d.ts +6 -0
- package/types/src/tokens.d.ts +7 -0
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { type SqlDatabase, type SqlStatement, uuidv7 } from '@fabrika/platform'
|
|
2
|
+
import { z } from 'zod'
|
|
2
3
|
import { type AuditEventInput, AuditWriter, type SqlChange, type SqlDialect } from './audit'
|
|
4
|
+
import type { GrantRequestRow } from './grant-requests'
|
|
3
5
|
import { unixNow } from './sql'
|
|
4
6
|
|
|
5
7
|
/** A grant written for a workload principal: its authority is its declared actions, never a grant. */
|
|
@@ -353,6 +355,102 @@ export class GrantRepository {
|
|
|
353
355
|
}
|
|
354
356
|
}
|
|
355
357
|
|
|
358
|
+
/**
|
|
359
|
+
* Write the change an approved grant request asks for (ADR-0083), in one transaction with the approval's claim: the
|
|
360
|
+
* request moves `pending` → `approved` under `decision.id`, and the change carries a guard on that claim, so of two
|
|
361
|
+
* approvals one claims and writes, the other writes nothing. `current` is the grant an `update` edits as the approver's
|
|
362
|
+
* checks read it; the edit applies only over it. `claimed` false: decided or lapsed meanwhile. `changed` null with
|
|
363
|
+
* `claimed`: the change wrote nothing (a role grant already held, an ineligible grant, a grant gone or edited since).
|
|
364
|
+
*/
|
|
365
|
+
async applyRequest(
|
|
366
|
+
request: GrantRequestRow,
|
|
367
|
+
current: GrantRow | null,
|
|
368
|
+
decision: { readonly id: string; readonly by: string; readonly at: number },
|
|
369
|
+
audit: (grantId: string) => AuditEventInput,
|
|
370
|
+
): Promise<{ claimed: boolean; changed: GrantRow | null }> {
|
|
371
|
+
const claim = this.db
|
|
372
|
+
.prepare(`UPDATE grant_requests SET status = 'approved', decision_id = ?, decided_by = ?, decided_at = ?
|
|
373
|
+
WHERE id = ? AND status = 'pending' AND expires_at > ? RETURNING id`)
|
|
374
|
+
.bind(decision.id, decision.by, decision.at, request.id, decision.at)
|
|
375
|
+
const guard = { sql: 'EXISTS (SELECT 1 FROM grant_requests WHERE id = ? AND decision_id = ?)', binds: [request.id, decision.id] }
|
|
376
|
+
const input: CreateGrantInput = {
|
|
377
|
+
principalId: request.principal_id,
|
|
378
|
+
app: request.app,
|
|
379
|
+
roleKey: request.role_key,
|
|
380
|
+
permissions: request.permissions === null ? null : storedActions(request.permissions),
|
|
381
|
+
scopeType: request.scope_type,
|
|
382
|
+
scopeValue: request.scope_value,
|
|
383
|
+
grantedBy: decision.by,
|
|
384
|
+
expiresAt: request.grant_expires_at,
|
|
385
|
+
}
|
|
386
|
+
let grantId = request.grant_id ?? uuidv7()
|
|
387
|
+
let change: SqlChange
|
|
388
|
+
const leading: SqlStatement[] = []
|
|
389
|
+
if (request.kind === 'create') {
|
|
390
|
+
let conflict = ''
|
|
391
|
+
const conflictBinds: number[] = []
|
|
392
|
+
if (input.roleKey !== null && input.scopeValue != null) {
|
|
393
|
+
conflict =
|
|
394
|
+
`ON CONFLICT (principal_id, role_key, scope_type, scope_value, COALESCE(app, '*')) WHERE role_key IS NOT NULL AND scope_value IS NOT NULL DO NOTHING`
|
|
395
|
+
} else if (input.roleKey !== null) {
|
|
396
|
+
// As `createDelegatedRoleGrant`: an expired grant of the role is renewed in place, keeping its id.
|
|
397
|
+
const expired = await this.db
|
|
398
|
+
.prepare(`SELECT id FROM grants WHERE principal_id = ? AND role_key = ? AND app = ? AND scope_value IS NULL
|
|
399
|
+
AND expires_at IS NOT NULL AND expires_at <= ?`)
|
|
400
|
+
.bind(request.principal_id, input.roleKey, request.app, decision.at)
|
|
401
|
+
.first<{ id: string }>()
|
|
402
|
+
grantId = expired?.id ?? grantId
|
|
403
|
+
conflict = `ON CONFLICT (principal_id, role_key, COALESCE(app, '*')) WHERE role_key IS NOT NULL AND scope_value IS NULL
|
|
404
|
+
DO UPDATE SET expires_at = excluded.expires_at, granted_by = excluded.granted_by, created_at = excluded.created_at
|
|
405
|
+
WHERE grants.expires_at IS NOT NULL AND grants.expires_at <= ?`
|
|
406
|
+
conflictBinds.push(decision.at)
|
|
407
|
+
}
|
|
408
|
+
const insert = this.grantChange(grantId, input, conflict, guard)
|
|
409
|
+
change = { sql: insert.sql, binds: [...insert.binds, ...conflictBinds] }
|
|
410
|
+
} else if (request.kind === 'update') {
|
|
411
|
+
if (current === null) return { claimed: false, changed: null }
|
|
412
|
+
leading.push(grantRemovalStatement(this.db, { principalId: request.principal_id }))
|
|
413
|
+
change = {
|
|
414
|
+
sql: `UPDATE grants SET role_key = ?, permissions = ?, scope_type = ?, scope_value = ?, expires_at = ?
|
|
415
|
+
WHERE id = ? AND app = ? AND role_key IS NOT DISTINCT FROM ? AND permissions IS NOT DISTINCT FROM ?
|
|
416
|
+
AND scope_type IS NOT DISTINCT FROM ? AND scope_value IS NOT DISTINCT FROM ? AND expires_at IS NOT DISTINCT FROM ?
|
|
417
|
+
AND ${eligibleGrantSql(this.writer.dialect)} AND ${guard.sql} RETURNING *`,
|
|
418
|
+
binds: [
|
|
419
|
+
input.roleKey ?? null,
|
|
420
|
+
request.permissions,
|
|
421
|
+
input.scopeType ?? null,
|
|
422
|
+
input.scopeValue ?? null,
|
|
423
|
+
input.expiresAt ?? null,
|
|
424
|
+
current.id,
|
|
425
|
+
request.app,
|
|
426
|
+
current.role_key,
|
|
427
|
+
current.permissions,
|
|
428
|
+
current.scope_type,
|
|
429
|
+
current.scope_value,
|
|
430
|
+
current.expires_at,
|
|
431
|
+
...eligibleGrantBinds(input),
|
|
432
|
+
...guard.binds,
|
|
433
|
+
],
|
|
434
|
+
}
|
|
435
|
+
} else {
|
|
436
|
+
leading.push(grantRemovalStatement(this.db, { principalId: request.principal_id }))
|
|
437
|
+
change = {
|
|
438
|
+
sql: `DELETE FROM grants WHERE id = ? AND app = ? AND principal_id = ? AND ${guard.sql} RETURNING *`,
|
|
439
|
+
binds: [grantId, request.app, request.principal_id, ...guard.binds],
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
leading.push(claim)
|
|
443
|
+
const audited = this.writer.changed([change], audit(grantId))
|
|
444
|
+
// A claim whose change wrote nothing (no record) closes the request `failed` in the same transaction.
|
|
445
|
+
const failed = this.db
|
|
446
|
+
.prepare(`UPDATE grant_requests SET status = 'failed' WHERE id = ? AND decision_id = ?
|
|
447
|
+
AND NOT EXISTS (SELECT 1 FROM audit_events WHERE id = ?)`)
|
|
448
|
+
.bind(request.id, decision.id, audited.recordId)
|
|
449
|
+
const results = await this.db.batch<GrantRow>([...leading, ...audited.statements, failed])
|
|
450
|
+
const claimed = (results[leading.length - 1]?.results.length ?? 0) > 0
|
|
451
|
+
return { claimed, changed: audited.rows(results, leading.length)[0] ?? null }
|
|
452
|
+
}
|
|
453
|
+
|
|
356
454
|
/** Keep the audit's before-value true if another administrator edited this grant after it was read. */
|
|
357
455
|
async updateGrant(grant: GrantRow, input: CreateGrantInput, audit: AuditEventInput): Promise<GrantRow | null> {
|
|
358
456
|
await this.refuseWorkload(grant.principal_id)
|
|
@@ -402,3 +500,10 @@ export class GrantRepository {
|
|
|
402
500
|
return { sql: 'DELETE FROM grants WHERE principal_id = ? RETURNING id', binds: [principalId] }
|
|
403
501
|
}
|
|
404
502
|
}
|
|
503
|
+
|
|
504
|
+
const storedActionsSchema = z.array(z.string())
|
|
505
|
+
|
|
506
|
+
/** A stored inline action set (a JSON array of patterns), read once where it leaves the row. */
|
|
507
|
+
function storedActions(json: string): string[] {
|
|
508
|
+
return storedActionsSchema.parse(JSON.parse(json))
|
|
509
|
+
}
|
package/src/services.ts
CHANGED
|
@@ -97,7 +97,7 @@ const APP_ID = /^[a-z0-9][a-z0-9-]*$/
|
|
|
97
97
|
* empty = no JWT may administer. A malformed entry is REFUSED rather than skipped: an entry no `aud`
|
|
98
98
|
* can ever equal would otherwise read as a registry while admitting nobody.
|
|
99
99
|
*/
|
|
100
|
-
function parseAdminAudiences(raw: string | undefined): ReadonlySet<string> {
|
|
100
|
+
export function parseAdminAudiences(raw: string | undefined): ReadonlySet<string> {
|
|
101
101
|
if (raw === undefined) {
|
|
102
102
|
return DEFAULT_ADMIN_AUDIENCES
|
|
103
103
|
}
|
|
@@ -152,7 +152,7 @@ export function buildServices(env: Env): Services {
|
|
|
152
152
|
oidc: { enabled: oidcEnabled, sessionCapSeconds: parseOidcSessionSeconds(env.OIDC_SESSION_HOURS) },
|
|
153
153
|
password: { enabled: passwordEnabled, minLengthFloor: passwordMinLengthFloor(passwordCost) },
|
|
154
154
|
},
|
|
155
|
-
oauth: parseOAuthConfig(env.OAUTH_CONFIG),
|
|
155
|
+
oauth: parseOAuthConfig(env.OAUTH_CONFIG, platformAudiencesFor(adminAudiences)),
|
|
156
156
|
retention: parseRetention({ authLogDays: env.AUTH_LOG_RETENTION_DAYS, auditDays: env.AUDIT_RETENTION_DAYS }),
|
|
157
157
|
issuer: env.ISSUER,
|
|
158
158
|
},
|
package/src/tokens.ts
CHANGED
|
@@ -457,7 +457,7 @@ export async function resolveCredential(services: Services, cred: CredentialRow,
|
|
|
457
457
|
* A user's mailbox for its token: `principals.email`, normalized by `PrincipalRepository`, the address the person signs
|
|
458
458
|
* in with. An application mails it as `ctx.principal.email`, so it is never `label`, the spelling the person sees.
|
|
459
459
|
*/
|
|
460
|
-
function mailboxClaim(principal: PrincipalRow): { email?: string } {
|
|
460
|
+
export function mailboxClaim(principal: PrincipalRow): { email?: string } {
|
|
461
461
|
return principal.type === 'user' && principal.email !== null ? { email: principal.email } : {}
|
|
462
462
|
}
|
|
463
463
|
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type DecideGrantRequestInput, type GrantRequestDto, type ListGrantRequestsInput, type SetGrantApprovalRequest } from '@fabrika/iam-contract';
|
|
2
|
+
import { type AuditEventInput, type CreateGrantRequestInput } from '../db';
|
|
3
|
+
import type { Services } from '../services';
|
|
4
|
+
import { type AdminContext } from './context';
|
|
5
|
+
import type { ResolvedAdmin } from './router';
|
|
6
|
+
/** Whether a grant change at `app` waits; a cross-app grant reaches no application and never does. */
|
|
7
|
+
export declare function grantsWait(services: Services, app: string | null): Promise<boolean>;
|
|
8
|
+
/** What a grant change asks for, before IAM records it; the request's timestamps are IAM's. */
|
|
9
|
+
export type GrantChangeRequest = Omit<CreateGrantRequestInput, 'requestedAt' | 'expiresAt'>;
|
|
10
|
+
/** Record the change as a request, with its `iam.grant.request` record built by `event`, and answer it. */
|
|
11
|
+
export declare function requestGrantChange(services: Services, change: GrantChangeRequest, event: (requestId: string, metadata: Record<string, unknown>) => AuditEventInput): Promise<GrantRequestDto>;
|
|
12
|
+
/** The open requests at the applications the caller administers, the oldest first. */
|
|
13
|
+
export declare function listGrantRequestsUseCase(c: AdminContext, input: ListGrantRequestsInput): Promise<{
|
|
14
|
+
items: GrantRequestDto[];
|
|
15
|
+
}>;
|
|
16
|
+
/**
|
|
17
|
+
* Approve or reject one open request. Only a person in a signed-in session decides (`person`), never a `px_` key; an
|
|
18
|
+
* application the caller does not administer and an unknown request are one 404. The requester may reject (withdraw)
|
|
19
|
+
* their own request, never approve it.
|
|
20
|
+
*/
|
|
21
|
+
export declare function decideGrantRequestUseCase(c: AdminContext, person: boolean, input: DecideGrantRequestInput): Promise<GrantRequestDto>;
|
|
22
|
+
/** The caller of a grant request procedure who administers no IAM application: an application's own administrator. */
|
|
23
|
+
export interface GrantRequester {
|
|
24
|
+
readonly services: Services;
|
|
25
|
+
readonly requestId: string;
|
|
26
|
+
readonly admin: ResolvedAdmin;
|
|
27
|
+
}
|
|
28
|
+
/** A requester without IAM administration sees only the requests they made (an application administrator's own). */
|
|
29
|
+
export declare function listOwnGrantRequestsUseCase(c: GrantRequester, input: ListGrantRequestsInput): Promise<{
|
|
30
|
+
items: GrantRequestDto[];
|
|
31
|
+
}>;
|
|
32
|
+
/**
|
|
33
|
+
* A requester without IAM administration withdraws a request they made, as a person in a signed-in session; any other
|
|
34
|
+
* request, and approving, is the 404 of an unknown one. The record is filed under the request's application.
|
|
35
|
+
*/
|
|
36
|
+
export declare function withdrawOwnGrantRequestUseCase(c: GrantRequester, input: DecideGrantRequestInput): Promise<GrantRequestDto>;
|
|
37
|
+
/**
|
|
38
|
+
* Make grant changes at `app` wait, or apply at once again (ADR-0083 §4). Only Control sets it, with the provisioning
|
|
39
|
+
* key, when an environment chooses or drops the `grants` category: an installation administrator who could switch it
|
|
40
|
+
* off could grant past it. Only an application environment id qualifies.
|
|
41
|
+
*/
|
|
42
|
+
export declare function setGrantApprovalUseCase(c: AdminContext, input: SetGrantApprovalRequest): Promise<SetGrantApprovalRequest>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type CreateGrantRequest, type
|
|
1
|
+
import { type CreateGrantRequest, type GrantChangeResponse, type OkResponse, type PendingGrantChange, type UpdateGrantInput } from '@fabrika/iam-contract';
|
|
2
2
|
import { type AdminContext } from './context';
|
|
3
|
-
export declare function createGrantUseCase(c: AdminContext, input: CreateGrantRequest): Promise<
|
|
3
|
+
export declare function createGrantUseCase(c: AdminContext, input: CreateGrantRequest): Promise<GrantChangeResponse>;
|
|
4
4
|
export declare function deleteGrantUseCase(c: AdminContext, input: {
|
|
5
5
|
id: string;
|
|
6
|
-
}): Promise<OkResponse
|
|
6
|
+
}): Promise<OkResponse & Partial<PendingGrantChange>>;
|
|
7
7
|
/** Edit a grant in place within its original application. Both paths use the same role, scope and external-person rules. */
|
|
8
|
-
export declare function updateGrantUseCase(c: AdminContext, input: UpdateGrantInput): Promise<
|
|
8
|
+
export declare function updateGrantUseCase(c: AdminContext, input: UpdateGrantInput): Promise<GrantChangeResponse>;
|
package/types/src/db.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { AuditRepository, type SqlDialect } from './repositories/audit';
|
|
|
5
5
|
import { InstallationBrandRepository } from './repositories/brand';
|
|
6
6
|
import { CredentialRepository } from './repositories/credentials';
|
|
7
7
|
import { DeviceAuthorizationRepository } from './repositories/device';
|
|
8
|
+
import { GrantRequestRepository } from './repositories/grant-requests';
|
|
8
9
|
import { GrantRepository } from './repositories/grants';
|
|
9
10
|
import { HandoffRepository } from './repositories/handoff';
|
|
10
11
|
import { MembershipRepository } from './repositories/membership';
|
|
@@ -21,6 +22,7 @@ export { type AuditedChange, type AuditEventInput, type AuditEventRow, auditEven
|
|
|
21
22
|
export { type InstallationBrandImages, InstallationBrandRepository, type InstallationBrandRow } from './repositories/brand';
|
|
22
23
|
export { type CredentialGrantRow, CredentialRepository, type CredentialRow } from './repositories/credentials';
|
|
23
24
|
export { DeviceAuthorizationRepository, type DeviceAuthorizationRow, type DeviceReceiptRow } from './repositories/device';
|
|
25
|
+
export { type CreateGrantRequestInput, GrantRequestRepository, type GrantRequestRow } from './repositories/grant-requests';
|
|
24
26
|
export { type DelegatedAuthority, ExternalGrantError, GrantRepository, type GrantRow } from './repositories/grants';
|
|
25
27
|
export { type AuthCodeRow, HandoffRepository } from './repositories/handoff';
|
|
26
28
|
export { type DelegationGrantRow, type InvitationAcceptance, type InvitationRow, type InviterCheck, type MemberGrantRow, MembershipRepository, type MembershipRepositoryOptions, type NewPrincipalAcceptance, type NewPrincipalEvents, } from './repositories/membership';
|
|
@@ -37,6 +39,7 @@ export interface IamRepositories {
|
|
|
37
39
|
device: DeviceAuthorizationRepository;
|
|
38
40
|
principals: PrincipalRepository;
|
|
39
41
|
grants: GrantRepository;
|
|
42
|
+
grantRequests: GrantRequestRepository;
|
|
40
43
|
appSchema: AppSchemaRepository;
|
|
41
44
|
credentials: CredentialRepository;
|
|
42
45
|
sessions: SessionRepository;
|
|
@@ -11,6 +11,7 @@ export interface OAuthConfig {
|
|
|
11
11
|
/**
|
|
12
12
|
* Parse `OAUTH_CONFIG`. Absent or empty disables OAuth; anything malformed THROWS, so a typo fails the boot instead of
|
|
13
13
|
* quietly serving fewer clients or scopes than the operator wrote. Unknown keys are refused for the same reason.
|
|
14
|
-
* `clients` may be empty: the command-line client is IAM's own and never listed.
|
|
14
|
+
* `clients` may be empty: the command-line client is IAM's own and never listed. A client's identity resource names
|
|
15
|
+
* neither a registered resource nor a `platformAudiences` app, so its token is never one an installation service accepts.
|
|
15
16
|
*/
|
|
16
|
-
export declare function parseOAuthConfig(raw: string | undefined): OAuthConfig | null;
|
|
17
|
+
export declare function parseOAuthConfig(raw: string | undefined, platformAudiences: ReadonlySet<string>): OAuthConfig | null;
|
package/types/src/oauth.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type KeyGrant, type PermissionEntry } from '@fabrika/auth-core';
|
|
2
|
-
import { OAUTH_DEVICE_CODE_GRANT_TYPE, type OAuthAuthorizationRequest, type OAuthClientMetadataDocument, type OAuthResourceDto, type OAuthRevocationRequest, type OAuthTokenRequest, type OAuthTokenResponse } from '@fabrika/iam-contract';
|
|
2
|
+
import { OAUTH_DEVICE_CODE_GRANT_TYPE, type OAuthAuthorizationRequest, type OAuthClientMetadataDocument, type OAuthIdentityRole, type OAuthResourceDto, type OAuthRevocationRequest, type OAuthTokenRequest, type OAuthTokenResponse } from '@fabrika/iam-contract';
|
|
3
3
|
import { type AuditEventInput, type OAuthGrantRow, type PrincipalRow, type SessionRow } from './db';
|
|
4
4
|
import type { Env } from './env';
|
|
5
5
|
import type { ManagedOAuthClient, OAuthConfig } from './oauth-config';
|
|
@@ -34,10 +34,12 @@ export declare function storedResourceList(text: string): string[] | null;
|
|
|
34
34
|
* `redirect_uri`. An unregistered client id is refused before any metadata fetch.
|
|
35
35
|
*/
|
|
36
36
|
export declare function establishClient(config: OAuthConfig, clientId: string, redirectUri: string): Promise<ManagedClient | null>;
|
|
37
|
+
/** What a consent grants: a registered resource, or an identity-only client's own resource. */
|
|
38
|
+
export type AuthorizationTarget = Pick<OAuthResourceDto, 'app' | 'resource'>;
|
|
37
39
|
/** Record the consent and return the plaintext authorization code — the only time it exists. */
|
|
38
40
|
export declare function grantAuthorization(services: Services, input: {
|
|
39
41
|
request: OAuthAuthorizationRequest;
|
|
40
|
-
resource:
|
|
42
|
+
resource: AuthorizationTarget;
|
|
41
43
|
scopes: string[];
|
|
42
44
|
session: SessionRow;
|
|
43
45
|
requestId: string;
|
|
@@ -59,16 +61,30 @@ export declare function exchangeOAuthToken(services: Services, env: SigningEnv,
|
|
|
59
61
|
export declare function revokeOAuthToken(services: Services, env: SigningEnv, input: OAuthRevocationRequest, requestId: string): Promise<void>;
|
|
60
62
|
/** An access token for `resource`, one of the grant's resources, carrying the grant's live authority there. */
|
|
61
63
|
export declare function mintForGrant(services: Services, env: SigningEnv, grant: OAuthGrantRow, resource: string, now: number): Promise<OAuthTokenResponse | null>;
|
|
64
|
+
/** What a grant's next token carries. `identity` is present on an identity resource's: its roles, when it declares them. */
|
|
65
|
+
export interface GrantAuthority {
|
|
66
|
+
principal: PrincipalRow;
|
|
67
|
+
scopes: string[];
|
|
68
|
+
permissions: PermissionEntry[];
|
|
69
|
+
expiresAt: number | null;
|
|
70
|
+
identity?: {
|
|
71
|
+
roles?: string[];
|
|
72
|
+
};
|
|
73
|
+
}
|
|
62
74
|
/**
|
|
63
75
|
* Everything a grant needs to still mean something, checked at every code exchange and refresh: the
|
|
64
76
|
* grant live, its client and resource still configured, the IAM session it was given under live and
|
|
65
77
|
* usable, the principal an active user. The permissions are resolved live, never from the consent.
|
|
66
78
|
*/
|
|
67
|
-
export declare function grantAuthority(services: Services, grant: OAuthGrantRow, now: number, resourceUri?: string): Promise<
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
79
|
+
export declare function grantAuthority(services: Services, grant: OAuthGrantRow, now: number, resourceUri?: string): Promise<GrantAuthority | null>;
|
|
80
|
+
/**
|
|
81
|
+
* The declared roles `principal` holds at an external application, in declared order, and when the earliest of them
|
|
82
|
+
* ends (null: none ends); null when it holds none. Only a global grant of a declared role counts, and only for an
|
|
83
|
+
* internal principal: an external one holds nothing but roles its application declares external (ADR-0051).
|
|
84
|
+
*/
|
|
85
|
+
export declare function heldIdentityRoles(services: Services, principal: PrincipalRow, app: string, declared: readonly OAuthIdentityRole[]): Promise<{
|
|
86
|
+
keys: string[];
|
|
87
|
+
until: number | null;
|
|
72
88
|
} | null>;
|
|
73
89
|
/** The union of the scopes' grants, or null when a scope is no longer configured. */
|
|
74
90
|
export declare function scopeCeiling(resource: OAuthResourceDto, scopes: readonly string[]): KeyGrant[] | null;
|
|
@@ -104,6 +104,8 @@ export interface AuditedChange {
|
|
|
104
104
|
readonly statements: SqlStatement[];
|
|
105
105
|
/** The rows the LAST change returned, read from the results of a batch whose statements start at `offset`. */
|
|
106
106
|
rows<T>(results: SqlQueryResult<T>[], offset?: number): T[];
|
|
107
|
+
/** The id of the record, which exists after the batch exactly when the change changed a row; null without a record. */
|
|
108
|
+
readonly recordId: string | null;
|
|
107
109
|
}
|
|
108
110
|
/**
|
|
109
111
|
* Writes the record of a guarded change exactly when the change changed a row, decided by the change itself and never
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
scope_value: string | null;
|
|
19
|
+
grant_expires_at: number | null;
|
|
20
|
+
requested_by: string;
|
|
21
|
+
requested_at: number;
|
|
22
|
+
expires_at: number;
|
|
23
|
+
status: 'pending' | 'approved' | 'rejected' | 'failed';
|
|
24
|
+
/** The approval that claimed it, which the change it wrote carries in its guard. */
|
|
25
|
+
decision_id: string | null;
|
|
26
|
+
decided_by: string | null;
|
|
27
|
+
decided_at: number | null;
|
|
28
|
+
}
|
|
29
|
+
export interface CreateGrantRequestInput {
|
|
30
|
+
app: string;
|
|
31
|
+
kind: GrantRequestRow['kind'];
|
|
32
|
+
principalId: string;
|
|
33
|
+
grantId: string | null;
|
|
34
|
+
roleKey: string | null;
|
|
35
|
+
permissions: string[] | null;
|
|
36
|
+
scopeType: string | null;
|
|
37
|
+
scopeValue: string | null;
|
|
38
|
+
grantExpiresAt: number | null;
|
|
39
|
+
requestedBy: string;
|
|
40
|
+
requestedAt: number;
|
|
41
|
+
expiresAt: number;
|
|
42
|
+
}
|
|
43
|
+
export declare class GrantRequestRepository {
|
|
44
|
+
private readonly db;
|
|
45
|
+
private readonly writer;
|
|
46
|
+
constructor(db: SqlDatabase, writer?: AuditWriter);
|
|
47
|
+
/** Whether a grant change at `app` waits for approval. */
|
|
48
|
+
grantsWait(app: string): Promise<boolean>;
|
|
49
|
+
/**
|
|
50
|
+
* Set whether grant changes at `app` wait, as of Control's protection `revision`. An older revision than the stored one
|
|
51
|
+
* changes nothing, so a caller that lost the environment's lease cannot undo its successor's gate. The record is written
|
|
52
|
+
* only when `required` changes.
|
|
53
|
+
*/
|
|
54
|
+
setGrantsWait(app: string, required: boolean, revision: number, now: number, audit: AuditEventInput): Promise<{
|
|
55
|
+
required: boolean;
|
|
56
|
+
revision: number;
|
|
57
|
+
}>;
|
|
58
|
+
create(input: CreateGrantRequestInput, audit: (requestId: string) => AuditEventInput): Promise<GrantRequestRow>;
|
|
59
|
+
get(id: string): Promise<GrantRequestRow | null>;
|
|
60
|
+
/** Every request still open at `now`, the oldest first. */
|
|
61
|
+
listOpen(now: number): Promise<GrantRequestRow[]>;
|
|
62
|
+
/** Reject an open request; null when it was decided or lapsed meanwhile. */
|
|
63
|
+
reject(id: string, decidedBy: string, now: number, audit: AuditEventInput): Promise<GrantRequestRow | null>;
|
|
64
|
+
}
|
|
@@ -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";
|
|
@@ -163,6 +164,21 @@ export declare class GrantRepository {
|
|
|
163
164
|
/** The INSERT of one grant with a caller-generated id, for a batch that also writes its principal and record. */
|
|
164
165
|
grantStatement(id: string, input: CreateGrantInput, onConflict?: string): SqlStatement;
|
|
165
166
|
private grantChange;
|
|
167
|
+
/**
|
|
168
|
+
* Write the change an approved grant request asks for (ADR-0083), in one transaction with the approval's claim: the
|
|
169
|
+
* request moves `pending` → `approved` under `decision.id`, and the change carries a guard on that claim, so of two
|
|
170
|
+
* approvals one claims and writes, the other writes nothing. `current` is the grant an `update` edits as the approver's
|
|
171
|
+
* checks read it; the edit applies only over it. `claimed` false: decided or lapsed meanwhile. `changed` null with
|
|
172
|
+
* `claimed`: the change wrote nothing (a role grant already held, an ineligible grant, a grant gone or edited since).
|
|
173
|
+
*/
|
|
174
|
+
applyRequest(request: GrantRequestRow, current: GrantRow | null, decision: {
|
|
175
|
+
readonly id: string;
|
|
176
|
+
readonly by: string;
|
|
177
|
+
readonly at: number;
|
|
178
|
+
}, audit: (grantId: string) => AuditEventInput): Promise<{
|
|
179
|
+
claimed: boolean;
|
|
180
|
+
changed: GrantRow | null;
|
|
181
|
+
}>;
|
|
166
182
|
/** Keep the audit's before-value true if another administrator edited this grant after it was read. */
|
|
167
183
|
updateGrant(grant: GrantRow, input: CreateGrantInput, audit: AuditEventInput): Promise<GrantRow | null>;
|
|
168
184
|
/** Delete a grant, after its principal's revision. */
|
package/types/src/services.d.ts
CHANGED
|
@@ -73,4 +73,10 @@ export declare const DEFAULT_ADMIN_AUDIENCES: ReadonlySet<string>;
|
|
|
73
73
|
export declare const DEFAULT_PLATFORM_AUDIENCES: ReadonlySet<string>;
|
|
74
74
|
/** An admin audience is a console gateway, so it is a platform service too, whatever it is named. */
|
|
75
75
|
export declare function platformAudiencesFor(adminAudiences: ReadonlySet<string>): ReadonlySet<string>;
|
|
76
|
+
/**
|
|
77
|
+
* Parse `ADMIN_AUDIENCES` (comma-separated app ids). Unset = `DEFAULT_ADMIN_AUDIENCES`; explicitly
|
|
78
|
+
* empty = no JWT may administer. A malformed entry is REFUSED rather than skipped: an entry no `aud`
|
|
79
|
+
* can ever equal would otherwise read as a registry while admitting nobody.
|
|
80
|
+
*/
|
|
81
|
+
export declare function parseAdminAudiences(raw: string | undefined): ReadonlySet<string>;
|
|
76
82
|
export declare function buildServices(env: Env): Services;
|
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.
|