@fabrika/iam 0.0.0-stage → 0.9.0-dev.26
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/fabrika.config.ts +267 -0
- package/migrations/0001_init.sql +316 -0
- package/migrations/0002_oauth.sql +38 -0
- package/migrations/0003_contract_apps.sql +11 -0
- package/migrations/0004_membership.sql +93 -0
- package/migrations/0005_workloads.sql +23 -0
- package/migrations/0006_membership_transport_keys.sql +16 -0
- package/migrations/0007_member_directory.sql +7 -0
- package/migrations/0008_durable_audit.sql +28 -0
- package/migrations/0009_member_lookups.sql +7 -0
- package/migrations/0010_workload_issues.sql +6 -0
- package/migrations/0011_app_presentation.sql +15 -0
- package/migrations/0012_external_principals.sql +10 -0
- package/migrations/0013_app_mail_templates.sql +14 -0
- package/migrations/0014_invitation_wording.sql +8 -0
- package/migrations/0015_app_schema_revisions.sql +15 -0
- package/migrations/0016_principal_grant_revision.sql +9 -0
- package/migrations/0017_app_sign_ins.sql +12 -0
- package/migrations/0018_app_members.sql +13 -0
- package/migrations/0019_app_mail_html.sql +7 -0
- package/migrations/0020_public_workloads.sql +34 -0
- package/migrations/0021_installation_brand.sql +19 -0
- package/migrations/0022_password_policy.sql +12 -0
- package/migrations/0023_terminals.sql +155 -0
- package/migrations/0024_app_terminals.sql +10 -0
- package/migrations/0025_terminal_completion.sql +10 -0
- package/migrations/0026_terminal_generation.sql +12 -0
- package/migrations/0027_application_exact_actions.sql +28 -0
- package/migrations/0028_oauth_client_assertions.sql +10 -0
- package/migrations/0029_role_via.sql +6 -0
- package/migrations/0030_membership_role_via.sql +12 -0
- package/migrations/0031_person_locale.sql +11 -0
- package/migrations-postgres/0001_init.sql +331 -0
- package/migrations-postgres/0002_oauth.sql +36 -0
- package/migrations-postgres/0003_contract_apps.sql +8 -0
- package/migrations-postgres/0004_membership.sql +92 -0
- package/migrations-postgres/0005_workloads.sql +18 -0
- package/migrations-postgres/0006_membership_transport_keys.sql +12 -0
- package/migrations-postgres/0007_member_directory.sql +7 -0
- package/migrations-postgres/0008_durable_audit.sql +8 -0
- package/migrations-postgres/0009_member_lookups.sql +7 -0
- package/migrations-postgres/0010_workload_issues.sql +6 -0
- package/migrations-postgres/0011_app_presentation.sql +15 -0
- package/migrations-postgres/0012_external_principals.sql +10 -0
- package/migrations-postgres/0013_app_mail_templates.sql +14 -0
- package/migrations-postgres/0014_invitation_wording.sql +8 -0
- package/migrations-postgres/0015_app_schema_revisions.sql +15 -0
- package/migrations-postgres/0016_principal_grant_revision.sql +9 -0
- package/migrations-postgres/0017_app_sign_ins.sql +12 -0
- package/migrations-postgres/0018_app_members.sql +13 -0
- package/migrations-postgres/0019_app_mail_html.sql +7 -0
- package/migrations-postgres/0020_public_workloads.sql +9 -0
- package/migrations-postgres/0021_installation_brand.sql +19 -0
- package/migrations-postgres/0022_password_policy.sql +12 -0
- package/migrations-postgres/0023_terminals.sql +70 -0
- package/migrations-postgres/0024_app_terminals.sql +10 -0
- package/migrations-postgres/0025_terminal_completion.sql +10 -0
- package/migrations-postgres/0026_terminal_generation.sql +12 -0
- package/migrations-postgres/0027_application_exact_actions.sql +28 -0
- package/migrations-postgres/0028_oauth_client_assertions.sql +9 -0
- package/migrations-postgres/0029_role_via.sql +4 -0
- package/migrations-postgres/0030_membership_role_via.sql +7 -0
- package/migrations-postgres/0031_person_locale.sql +11 -0
- package/package.json +70 -5
- package/src/admin/access.ts +47 -0
- package/src/admin/api-keys.ts +280 -0
- package/src/admin/app-people.ts +209 -0
- package/src/admin/app-schema.ts +308 -0
- package/src/admin/audit.ts +81 -0
- package/src/admin/brand.ts +54 -0
- package/src/admin/connections.ts +48 -0
- package/src/admin/context.ts +70 -0
- package/src/admin/grant-support.ts +192 -0
- package/src/admin/grants.ts +189 -0
- package/src/admin/handlers.ts +102 -0
- package/src/admin/http.ts +24 -0
- package/src/admin/membership-keys.ts +38 -0
- package/src/admin/pagination.ts +28 -0
- package/src/admin/password-policy.ts +48 -0
- package/src/admin/passwords.ts +78 -0
- package/src/admin/policies.ts +136 -0
- package/src/admin/principals.ts +226 -0
- package/src/admin/router.ts +291 -0
- package/src/admin/rpc.ts +387 -0
- package/src/admin/sessions.ts +115 -0
- package/src/admin/share-links.ts +127 -0
- package/src/admin/terminals.ts +115 -0
- package/src/admin/user-admin.ts +76 -0
- package/src/admin/workloads.ts +85 -0
- package/src/app-presentation.ts +178 -0
- package/src/app.ts +158 -0
- package/src/auth/cookies.ts +64 -0
- package/src/auth/failure.ts +25 -0
- package/src/auth/handoff.ts +136 -0
- package/src/auth/http.ts +88 -0
- package/src/auth/invitations.ts +220 -0
- package/src/auth/known-device.ts +44 -0
- package/src/auth/login.ts +162 -0
- package/src/auth/oidc-flight.ts +91 -0
- package/src/auth/oidc.ts +148 -0
- package/src/auth/password.ts +416 -0
- package/src/auth/routes.ts +108 -0
- package/src/auth/sessions.ts +249 -0
- package/src/auth/terminals.ts +284 -0
- package/src/auth.ts +193 -0
- package/src/binding-identity.ts +25 -0
- package/src/bounded-body.ts +35 -0
- package/src/catalog/cs.ts +147 -0
- package/src/catalog/en.ts +148 -0
- package/src/catalog/index.ts +122 -0
- package/src/catalog/uk.ts +164 -0
- package/src/client-address.ts +39 -0
- package/src/common-passwords.ts +37 -0
- package/src/contract-rules.ts +80 -0
- package/src/cron.ts +54 -0
- package/src/db.ts +128 -0
- package/src/delegation.ts +100 -0
- package/src/device-http.ts +39 -0
- package/src/device.ts +269 -0
- package/src/email-identity.ts +2 -0
- package/src/env.ts +132 -0
- package/src/handoff.ts +175 -0
- package/src/html.ts +4 -0
- package/src/human-admission.ts +40 -0
- package/src/identification.ts +185 -0
- package/src/index.ts +292 -0
- package/src/installation-brand.ts +82 -0
- package/src/issue.ts +265 -0
- package/src/json.ts +30 -0
- package/src/log.ts +26 -0
- package/src/membership-http.ts +135 -0
- package/src/membership.ts +1210 -0
- package/src/messages.ts +44 -0
- package/src/narrow-permissions.ts +26 -0
- package/src/node/browser-identity.ts +69 -0
- package/src/node/migrate.ts +52 -0
- package/src/node/prune.ts +34 -0
- package/src/node/runtime.ts +221 -0
- package/src/node/server.ts +104 -0
- package/src/oauth-client-auth.ts +93 -0
- package/src/oauth-client.ts +145 -0
- package/src/oauth-config.ts +72 -0
- package/src/oauth-http.ts +262 -0
- package/src/oauth.ts +272 -0
- package/src/oidc.ts +260 -0
- package/src/origin.ts +29 -0
- package/src/password-crypto.ts +148 -0
- package/src/password-db.ts +419 -0
- package/src/password-minimum.ts +44 -0
- package/src/password-pages.ts +397 -0
- package/src/password-policy.ts +79 -0
- package/src/password-service.ts +67 -0
- package/src/person-locale.ts +103 -0
- package/src/proxy-proof.ts +36 -0
- package/src/relay-mail.ts +64 -0
- package/src/repositories/app-schema.ts +550 -0
- package/src/repositories/audit.ts +405 -0
- package/src/repositories/brand.ts +66 -0
- package/src/repositories/credentials.ts +331 -0
- package/src/repositories/device.ts +137 -0
- package/src/repositories/grants.ts +404 -0
- package/src/repositories/handoff.ts +103 -0
- package/src/repositories/membership-transport.ts +49 -0
- package/src/repositories/membership.ts +875 -0
- package/src/repositories/oauth.ts +122 -0
- package/src/repositories/password-policy.ts +44 -0
- package/src/repositories/principals.ts +358 -0
- package/src/repositories/sessions.ts +339 -0
- package/src/repositories/sql.ts +23 -0
- package/src/repositories/terminals.ts +346 -0
- package/src/repositories/workloads.ts +134 -0
- package/src/request-id.ts +46 -0
- package/src/resolve.ts +396 -0
- package/src/retention.ts +34 -0
- package/src/roles.ts +54 -0
- package/src/rpc-http.ts +333 -0
- package/src/rpc-inputs.ts +110 -0
- package/src/rpc.ts +440 -0
- package/src/secret.ts +45 -0
- package/src/service.ts +59 -0
- package/src/services.ts +199 -0
- package/src/signing.ts +260 -0
- package/src/stored-chain.ts +23 -0
- package/src/stored-permissions.ts +34 -0
- package/src/terminals.ts +15 -0
- package/src/tokens.ts +506 -0
- package/src/workload-token.ts +130 -0
- package/types/fabrika.config.d.ts +11 -0
- package/types/src/admin/access.d.ts +19 -0
- package/types/src/admin/api-keys.d.ts +26 -0
- package/types/src/admin/app-people.d.ts +8 -0
- package/types/src/admin/app-schema.d.ts +32 -0
- package/types/src/admin/audit.d.ts +10 -0
- package/types/src/admin/brand.d.ts +10 -0
- package/types/src/admin/connections.d.ts +5 -0
- package/types/src/admin/context.d.ts +46 -0
- package/types/src/admin/grant-support.d.ts +59 -0
- package/types/src/admin/grants.d.ts +8 -0
- package/types/src/admin/handlers.d.ts +82 -0
- package/types/src/admin/http.d.ts +9 -0
- package/types/src/admin/membership-keys.d.ts +8 -0
- package/types/src/admin/pagination.d.ts +5 -0
- package/types/src/admin/password-policy.d.ts +9 -0
- package/types/src/admin/passwords.d.ts +5 -0
- package/types/src/admin/policies.d.ts +15 -0
- package/types/src/admin/principals.d.ts +15 -0
- package/types/src/admin/router.d.ts +75 -0
- package/types/src/admin/rpc.d.ts +6 -0
- package/types/src/admin/sessions.d.ts +14 -0
- package/types/src/admin/share-links.d.ts +21 -0
- package/types/src/admin/terminals.d.ts +13 -0
- package/types/src/admin/user-admin.d.ts +30 -0
- package/types/src/admin/workloads.d.ts +5 -0
- package/types/src/app-presentation.d.ts +86 -0
- package/types/src/app.d.ts +62 -0
- package/types/src/auth/cookies.d.ts +29 -0
- package/types/src/auth/failure.d.ts +47 -0
- package/types/src/auth/handoff.d.ts +66 -0
- package/types/src/auth/http.d.ts +29 -0
- package/types/src/auth/invitations.d.ts +18 -0
- package/types/src/auth/known-device.d.ts +12 -0
- package/types/src/auth/login.d.ts +10 -0
- package/types/src/auth/oidc-flight.d.ts +39 -0
- package/types/src/auth/oidc.d.ts +7 -0
- package/types/src/auth/password.d.ts +7 -0
- package/types/src/auth/routes.d.ts +15 -0
- package/types/src/auth/sessions.d.ts +60 -0
- package/types/src/auth/terminals.d.ts +47 -0
- package/types/src/auth.d.ts +70 -0
- package/types/src/binding-identity.d.ts +12 -0
- package/types/src/bounded-body.d.ts +12 -0
- package/types/src/catalog/cs.d.ts +2 -0
- package/types/src/catalog/en.d.ts +2 -0
- package/types/src/catalog/index.d.ts +112 -0
- package/types/src/catalog/uk.d.ts +2 -0
- package/types/src/client-address.d.ts +31 -0
- package/types/src/common-passwords.d.ts +2 -0
- package/types/src/contract-rules.d.ts +25 -0
- package/types/src/cron.d.ts +15 -0
- package/types/src/db.d.ts +58 -0
- package/types/src/delegation.d.ts +38 -0
- package/types/src/device-http.d.ts +2 -0
- package/types/src/device.d.ts +11 -0
- package/types/src/email-identity.d.ts +1 -0
- package/types/src/env.d.ts +127 -0
- package/types/src/handoff.d.ts +57 -0
- package/types/src/html.d.ts +2 -0
- package/types/src/human-admission.d.ts +22 -0
- package/types/src/identification.d.ts +30 -0
- package/types/src/index.d.ts +62 -0
- package/types/src/installation-brand.d.ts +22 -0
- package/types/src/issue.d.ts +62 -0
- package/types/src/json.d.ts +15 -0
- package/types/src/log.d.ts +15 -0
- package/types/src/membership-http.d.ts +8 -0
- package/types/src/membership.d.ts +156 -0
- package/types/src/messages.d.ts +20 -0
- package/types/src/narrow-permissions.d.ts +7 -0
- package/types/src/node/browser-identity.d.ts +1 -0
- package/types/src/node/migrate.d.ts +4 -0
- package/types/src/node/prune.d.ts +1 -0
- package/types/src/node/runtime.d.ts +51 -0
- package/types/src/node/server.d.ts +5 -0
- package/types/src/oauth-client-auth.d.ts +26 -0
- package/types/src/oauth-client.d.ts +17 -0
- package/types/src/oauth-config.d.ts +16 -0
- package/types/src/oauth-http.d.ts +7 -0
- package/types/src/oauth.d.ts +51 -0
- package/types/src/oidc.d.ts +81 -0
- package/types/src/origin.d.ts +9 -0
- package/types/src/password-crypto.d.ts +37 -0
- package/types/src/password-db.d.ts +134 -0
- package/types/src/password-minimum.d.ts +23 -0
- package/types/src/password-pages.d.ts +143 -0
- package/types/src/password-policy.d.ts +19 -0
- package/types/src/password-service.d.ts +24 -0
- package/types/src/person-locale.d.ts +50 -0
- package/types/src/proxy-proof.d.ts +21 -0
- package/types/src/relay-mail.d.ts +25 -0
- package/types/src/repositories/app-schema.d.ts +244 -0
- package/types/src/repositories/audit.d.ts +187 -0
- package/types/src/repositories/brand.d.ts +41 -0
- package/types/src/repositories/credentials.d.ts +148 -0
- package/types/src/repositories/device.d.ts +50 -0
- package/types/src/repositories/grants.d.ts +177 -0
- package/types/src/repositories/handoff.d.ts +52 -0
- package/types/src/repositories/membership-transport.d.ts +31 -0
- package/types/src/repositories/membership.d.ts +273 -0
- package/types/src/repositories/oauth.d.ts +55 -0
- package/types/src/repositories/password-policy.d.ts +25 -0
- package/types/src/repositories/principals.d.ts +159 -0
- package/types/src/repositories/sessions.d.ts +150 -0
- package/types/src/repositories/sql.d.ts +13 -0
- package/types/src/repositories/terminals.d.ts +144 -0
- package/types/src/repositories/workloads.d.ts +61 -0
- package/types/src/request-id.d.ts +25 -0
- package/types/src/resolve.d.ts +118 -0
- package/types/src/retention.d.ts +18 -0
- package/types/src/roles.d.ts +34 -0
- package/types/src/rpc-http.d.ts +53 -0
- package/types/src/rpc-inputs.d.ts +707 -0
- package/types/src/rpc.d.ts +4 -0
- package/types/src/secret.d.ts +21 -0
- package/types/src/service.d.ts +19 -0
- package/types/src/services.d.ts +72 -0
- package/types/src/signing.d.ts +72 -0
- package/types/src/stored-chain.d.ts +6 -0
- package/types/src/stored-permissions.d.ts +12 -0
- package/types/src/terminals.d.ts +7 -0
- package/types/src/tokens.d.ts +113 -0
- package/types/src/workload-token.d.ts +7 -0
- package/README.md +0 -3
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { type WorkloadKind } from '@fabrika/auth-core';
|
|
2
|
+
import { type SqlDatabase } from '@fabrika/platform';
|
|
3
|
+
import { type AuditEventInput, AuditWriter } from './audit';
|
|
4
|
+
/** A role/policy bundle row (the `roles` table). `permissions` is a JSON array string. */
|
|
5
|
+
export interface RoleRow {
|
|
6
|
+
app: string;
|
|
7
|
+
role_key: string;
|
|
8
|
+
name: string;
|
|
9
|
+
description: string | null;
|
|
10
|
+
/** JSON array of action patterns, e.g. '["project.read","report.*"]'. */
|
|
11
|
+
permissions: string;
|
|
12
|
+
/** 'app' = reconciled from code, 'custom' = admin-composed. */
|
|
13
|
+
origin: 'app' | 'custom';
|
|
14
|
+
/** The scope dimension a membership role is granted in; NULL for every other role. */
|
|
15
|
+
member_scope: string | null;
|
|
16
|
+
/** JSON array of the membership roles whose holders may assign this one; NULL = only an administrator. */
|
|
17
|
+
assignable_by: string | null;
|
|
18
|
+
/** 1 when an external principal may hold this membership role (ADR-0051); 0 for every other role. */
|
|
19
|
+
external: 0 | 1;
|
|
20
|
+
/** JSON object relation → exact actions a grant of the role carries as `~rel` (global) or `~relin` (at a node) entries; NULL = none. */
|
|
21
|
+
via: string | null;
|
|
22
|
+
created_at: number;
|
|
23
|
+
}
|
|
24
|
+
/** A scope-dimension row (the `app_scopes` table). */
|
|
25
|
+
export interface AppScopeRow {
|
|
26
|
+
app: string;
|
|
27
|
+
scope_type: string;
|
|
28
|
+
label: string | null;
|
|
29
|
+
/** The dimension containing every value of this one. */
|
|
30
|
+
parent_scope: string | null;
|
|
31
|
+
/** The membership role `createScope` grants in a new value. */
|
|
32
|
+
creator_role: string | null;
|
|
33
|
+
members_manage: string | null;
|
|
34
|
+
members_list: string | null;
|
|
35
|
+
/** `labels`: `listMembers` shows labels to a `members.list` holder too. */
|
|
36
|
+
members_directory: 'labels' | null;
|
|
37
|
+
/** A JSON array of the actions that look up one member (`findMember`), or null for none. */
|
|
38
|
+
members_lookup: string | null;
|
|
39
|
+
}
|
|
40
|
+
/** A share-link mount (the `app_link_mounts` table). `grants` is a JSON array of exact actions. */
|
|
41
|
+
export interface LinkMountRow {
|
|
42
|
+
app: string;
|
|
43
|
+
mount: string;
|
|
44
|
+
dimension: string;
|
|
45
|
+
grants: string;
|
|
46
|
+
issued_by: string;
|
|
47
|
+
max_days: number;
|
|
48
|
+
}
|
|
49
|
+
/** An endpoint that may spend membership authority (the `app_delegated_endpoints` table). */
|
|
50
|
+
export interface DelegatedEndpointRow {
|
|
51
|
+
app: string;
|
|
52
|
+
endpoint: string;
|
|
53
|
+
action: string;
|
|
54
|
+
dimension: string;
|
|
55
|
+
purpose: 'members' | 'creates';
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* How IAM names an application (the `app_presentation` table). `locale` is stored as reconciled and parsed at read
|
|
59
|
+
* (`presentationFor`); NULL is "not declared".
|
|
60
|
+
*/
|
|
61
|
+
export interface AppPresentationRow {
|
|
62
|
+
app: string;
|
|
63
|
+
locale: string | null;
|
|
64
|
+
display_name: string | null;
|
|
65
|
+
}
|
|
66
|
+
/** An application's wording of one IAM e-mail in one language (the `app_mail_templates` table), parsed at render. */
|
|
67
|
+
export interface AppMailTemplateRow {
|
|
68
|
+
app: string;
|
|
69
|
+
kind: string;
|
|
70
|
+
locale: string;
|
|
71
|
+
subject: string;
|
|
72
|
+
body: string;
|
|
73
|
+
/** The HTML body in the subset of `mailHtmlTokens`, or NULL for none: IAM builds the HTML part from the text. */
|
|
74
|
+
html: string | null;
|
|
75
|
+
}
|
|
76
|
+
/** The application's own people (the `app_members` table): the actions that manage and list them. */
|
|
77
|
+
export interface AppMembersRow {
|
|
78
|
+
app: string;
|
|
79
|
+
manage: string;
|
|
80
|
+
list: string;
|
|
81
|
+
directory: 'labels' | null;
|
|
82
|
+
}
|
|
83
|
+
/** An action-catalog row (the `app_actions` table). */
|
|
84
|
+
export interface AppActionRow {
|
|
85
|
+
app: string;
|
|
86
|
+
action: string;
|
|
87
|
+
description: string | null;
|
|
88
|
+
}
|
|
89
|
+
export declare class AppSchemaRepository {
|
|
90
|
+
protected readonly db: SqlDatabase;
|
|
91
|
+
protected readonly writer: AuditWriter;
|
|
92
|
+
constructor(db: SqlDatabase, writer?: AuditWriter);
|
|
93
|
+
/**
|
|
94
|
+
* The set of app ids IAM knows about — the distinct `app` across the schema tables
|
|
95
|
+
* (`app_actions ∪ app_scopes ∪ roles ∪ app_schema_revisions`). An app registers itself by reconciling its vocabulary
|
|
96
|
+
* via `PUT /admin/apps/:app/schema`, so this is the live registry (no static config list). Every reconcile writes a
|
|
97
|
+
* revision, so an app that declares nothing is known too, and its return origins can be registered.
|
|
98
|
+
*/
|
|
99
|
+
listKnownApps(): Promise<string[]>;
|
|
100
|
+
/** The app's declared locale and display name, or null when it declares neither. */
|
|
101
|
+
getPresentation(app: string): Promise<AppPresentationRow | null>;
|
|
102
|
+
/**
|
|
103
|
+
* The path of the app's identification page at a terminal (ADR-0067), or null when the app declares none: an absolute
|
|
104
|
+
* path on its own origin, which a sign-in resolves against its return origin. The column's CHECK admits `//host`, a
|
|
105
|
+
* reference to another origin, so a stored path is parsed again here and one that is no identification page is none.
|
|
106
|
+
*/
|
|
107
|
+
getTerminalIdentifyPath(app: string): Promise<string | null>;
|
|
108
|
+
/** The app's template of one message in one language, or null when it declares none. */
|
|
109
|
+
getMailTemplate(app: string, kind: string, locale: string): Promise<AppMailTemplateRow | null>;
|
|
110
|
+
/** The actions that manage and list the app's own people, or null when it declares none. */
|
|
111
|
+
getAppMembers(app: string): Promise<AppMembersRow | null>;
|
|
112
|
+
/** Every app that declares its own people, for the apps an administrator of one may administer. */
|
|
113
|
+
listAppMembers(): Promise<AppMembersRow[]>;
|
|
114
|
+
/** True when the app's schema was compiled from a contract (ADR-0022 rule 6). */
|
|
115
|
+
isContractApp(app: string): Promise<boolean>;
|
|
116
|
+
/** Every role row for an app (origin='app' AND origin='custom') — for resolution + listing. */
|
|
117
|
+
listRoles(app: string): Promise<RoleRow[]>;
|
|
118
|
+
/** Role rows for an app filtered by origin — used to list just the app/custom set. */
|
|
119
|
+
listRolesByOrigin(app: string, origin: 'app' | 'custom'): Promise<RoleRow[]>;
|
|
120
|
+
getRole(app: string, roleKey: string): Promise<RoleRow | null>;
|
|
121
|
+
/**
|
|
122
|
+
* Upsert a single role. `permissions` is JSON-encoded here. ON CONFLICT keeps the
|
|
123
|
+
* row's PK (app, role_key) and overwrites the mutable columns — origin included, so
|
|
124
|
+
* a reconcile can (re)assert origin='app' on an existing key. `created_at` is left
|
|
125
|
+
* untouched on update.
|
|
126
|
+
*/
|
|
127
|
+
upsertRole(input: {
|
|
128
|
+
app: string;
|
|
129
|
+
roleKey: string;
|
|
130
|
+
name: string;
|
|
131
|
+
description?: string | null;
|
|
132
|
+
permissions: string[];
|
|
133
|
+
origin: 'app' | 'custom';
|
|
134
|
+
}, audit?: AuditEventInput): Promise<RoleRow>;
|
|
135
|
+
/** Delete a role. `audit` is written exactly when this call deleted it. */
|
|
136
|
+
deleteRole(app: string, roleKey: string, audit?: AuditEventInput): Promise<boolean>;
|
|
137
|
+
/**
|
|
138
|
+
* The revision of the app's schema and policies (`app_schema_revisions`); 0 for an app IAM never reconciled. A check
|
|
139
|
+
* that authorizes a later write reads it FIRST, before the rows it checks, and the write requires it unchanged
|
|
140
|
+
* (`schemaRevisionSql`): a reconcile in between then makes the write change nothing.
|
|
141
|
+
*/
|
|
142
|
+
getSchemaRevision(app: string): Promise<number>;
|
|
143
|
+
/** The first statement of every write of the app's schema or policies: it moves the revision on, and on Postgres locks it. */
|
|
144
|
+
private bumpRevisionStatement;
|
|
145
|
+
/** The app's action catalog as plain strings — the validation source for patterns. */
|
|
146
|
+
listActionCatalog(app: string): Promise<string[]>;
|
|
147
|
+
getScope(app: string, scopeType: string): Promise<AppScopeRow | null>;
|
|
148
|
+
getLinkMount(app: string, mount: string): Promise<LinkMountRow | null>;
|
|
149
|
+
listLinkMounts(app: string): Promise<LinkMountRow[]>;
|
|
150
|
+
getDelegatedEndpoint(app: string, endpoint: string): Promise<DelegatedEndpointRow | null>;
|
|
151
|
+
listDelegatedEndpoints(app: string): Promise<DelegatedEndpointRow[]>;
|
|
152
|
+
listAppScopes(app: string): Promise<AppScopeRow[]>;
|
|
153
|
+
listAppActions(app: string): Promise<AppActionRow[]>;
|
|
154
|
+
/**
|
|
155
|
+
* Idempotent reconcile of an app's declared vocabulary in one atomic batch. Validation (patterns
|
|
156
|
+
* vs. the new action catalog, and a collision with an admin's custom policy) is the caller's job —
|
|
157
|
+
* this layer just writes the reconciled state.
|
|
158
|
+
*
|
|
159
|
+
* **Clear, then write.** The previous shape upserted the incoming rows and then deleted whatever
|
|
160
|
+
* was `NOT IN` the incoming set, which put ONE BOUND PARAMETER PER KEPT VALUE in a single
|
|
161
|
+
* statement — D1 allows 100 bound parameters per query, so an app with a hundred actions could not
|
|
162
|
+
* reconcile at all (CORR-9). Deleting the app's rows first makes every statement's width constant
|
|
163
|
+
* and the reconcile independent of catalog size. The batch is one transaction, so there is no
|
|
164
|
+
* window in which the app has no vocabulary.
|
|
165
|
+
*
|
|
166
|
+
* `origin='custom'` roles are NEVER touched: the DELETE names `origin = 'app'`, and the upsert's
|
|
167
|
+
* `WHERE roles.origin = 'app'` makes a collision with a surviving custom row a no-op instead of
|
|
168
|
+
* silently flipping an admin's policy to `origin='app'` and making it unmanageable (SEC-3). The
|
|
169
|
+
* caller refuses that collision outright; this is the second lock on the same rule.
|
|
170
|
+
*/
|
|
171
|
+
reconcileAppSchema(input: {
|
|
172
|
+
app: string;
|
|
173
|
+
scopes: {
|
|
174
|
+
scopeType: string;
|
|
175
|
+
label?: string | null;
|
|
176
|
+
parentScope?: string | null;
|
|
177
|
+
creatorRole?: string | null;
|
|
178
|
+
members?: {
|
|
179
|
+
manage: string;
|
|
180
|
+
list: string;
|
|
181
|
+
} | null;
|
|
182
|
+
directory?: 'labels' | null;
|
|
183
|
+
lookups?: string[] | null;
|
|
184
|
+
}[];
|
|
185
|
+
actions: {
|
|
186
|
+
action: string;
|
|
187
|
+
description?: string | null;
|
|
188
|
+
}[];
|
|
189
|
+
roles: {
|
|
190
|
+
roleKey: string;
|
|
191
|
+
name: string;
|
|
192
|
+
description?: string | null;
|
|
193
|
+
permissions: string[];
|
|
194
|
+
memberScope?: string | null;
|
|
195
|
+
assignableBy?: string[] | null;
|
|
196
|
+
external?: boolean;
|
|
197
|
+
via?: Record<string, string[]> | null;
|
|
198
|
+
}[];
|
|
199
|
+
/** The contract flag is part of the vocabulary: written in the same batch, so it never outlives the roles it constrains. */
|
|
200
|
+
contract?: boolean;
|
|
201
|
+
links?: {
|
|
202
|
+
mount: string;
|
|
203
|
+
dimension: string;
|
|
204
|
+
grants: string[];
|
|
205
|
+
issuedBy: string;
|
|
206
|
+
maxDays: number;
|
|
207
|
+
}[];
|
|
208
|
+
delegations?: {
|
|
209
|
+
endpoint: string;
|
|
210
|
+
action: string;
|
|
211
|
+
dimension: string;
|
|
212
|
+
purpose: 'members' | 'creates';
|
|
213
|
+
}[];
|
|
214
|
+
workloads?: {
|
|
215
|
+
kind: WorkloadKind;
|
|
216
|
+
name: string;
|
|
217
|
+
actions: string[];
|
|
218
|
+
issues?: string;
|
|
219
|
+
}[];
|
|
220
|
+
/** How IAM names the app; a field left out is "not declared", and both left out remove the row. */
|
|
221
|
+
presentation?: {
|
|
222
|
+
locale: string | null;
|
|
223
|
+
displayName: string | null;
|
|
224
|
+
};
|
|
225
|
+
/** The app's e-mail templates; every row of the app is replaced, and none left out removes them all. */
|
|
226
|
+
mail?: {
|
|
227
|
+
kind: string;
|
|
228
|
+
locale: string;
|
|
229
|
+
subject: string;
|
|
230
|
+
text: string;
|
|
231
|
+
html?: string;
|
|
232
|
+
}[];
|
|
233
|
+
/** The actions that manage and list the app's own people; absent or null removes the row. */
|
|
234
|
+
members?: {
|
|
235
|
+
manage: string;
|
|
236
|
+
list: string;
|
|
237
|
+
directory: 'labels' | null;
|
|
238
|
+
} | null;
|
|
239
|
+
/** The app's identification page at a terminal; absent or null removes the row. */
|
|
240
|
+
terminal?: {
|
|
241
|
+
identify: string;
|
|
242
|
+
} | null;
|
|
243
|
+
}, audit?: AuditEventInput): Promise<void>;
|
|
244
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { type SqlDatabase, type SqlQueryResult, type SqlStatement } from '@fabrika/platform';
|
|
2
|
+
export interface AuditEventRow {
|
|
3
|
+
id: string;
|
|
4
|
+
request_id: string;
|
|
5
|
+
principal_id: string | null;
|
|
6
|
+
principal_label: string;
|
|
7
|
+
credential_id: string | null;
|
|
8
|
+
app: string;
|
|
9
|
+
action: string;
|
|
10
|
+
resource_type: string;
|
|
11
|
+
resource_id: string | null;
|
|
12
|
+
diff: string | null;
|
|
13
|
+
metadata: string | null;
|
|
14
|
+
created_at: number;
|
|
15
|
+
}
|
|
16
|
+
export interface AuthLogRow {
|
|
17
|
+
/**
|
|
18
|
+
* THE ONE COLUMN IN THIS SCHEMA WHOSE JS TYPE DEPENDS ON THE BACKEND. `auth_log.id` is engine-
|
|
19
|
+
* assigned and has no SQLite ∩ Postgres spelling (see the block comment on `auth_log` in
|
|
20
|
+
* `migrations/0001` and `migrations-postgres/0001`): SQLite's `INTEGER PRIMARY KEY` rowid alias
|
|
21
|
+
* hands back a `number`; Postgres needs `BIGINT GENERATED BY DEFAULT AS IDENTITY`, and Bun decodes
|
|
22
|
+
* `int8` as a STRING by column type OID — `'42'`, never `42`, even for small values.
|
|
23
|
+
*
|
|
24
|
+
* Narrowing this to `number` would be a lie on Postgres, and widening the column to `INTEGER` to
|
|
25
|
+
* make it true would cap the highest-volume table in the schema at 2147483647 rows. So the union
|
|
26
|
+
* stands and the SINGLE reader (`toAuthLogDto`, admin/audit.ts) normalises it. Anything new
|
|
27
|
+
* that reads this column must do the same — do not assume arithmetic works on it.
|
|
28
|
+
*/
|
|
29
|
+
id: number | string;
|
|
30
|
+
request_id: string;
|
|
31
|
+
app: string;
|
|
32
|
+
kind: AuthLogKind;
|
|
33
|
+
principal_id: string | null;
|
|
34
|
+
credential_id: string | null;
|
|
35
|
+
decision: 'allow' | 'deny';
|
|
36
|
+
reason: string | null;
|
|
37
|
+
created_at: number;
|
|
38
|
+
/** 1 when the session's principal held the installation-admin action at login; 0 otherwise. */
|
|
39
|
+
privileged: number;
|
|
40
|
+
/** The terminal of the session this row is about (ADR-0067), copied from the session row. */
|
|
41
|
+
terminal_id: string | null;
|
|
42
|
+
/** The application that asserted that session's person on `terminal_id`. */
|
|
43
|
+
asserted_by: string | null;
|
|
44
|
+
}
|
|
45
|
+
/** A login attempt (`authenticate`) or the end of a session (`logout`). */
|
|
46
|
+
export type AuthLogKind = 'authenticate' | 'logout';
|
|
47
|
+
export interface AuditEventInput {
|
|
48
|
+
requestId: string;
|
|
49
|
+
principalId: string | null;
|
|
50
|
+
principalLabel: string;
|
|
51
|
+
credentialId?: string | null;
|
|
52
|
+
app: string;
|
|
53
|
+
action: string;
|
|
54
|
+
resourceType: string;
|
|
55
|
+
resourceId?: string | null;
|
|
56
|
+
diff?: unknown;
|
|
57
|
+
metadata?: unknown;
|
|
58
|
+
}
|
|
59
|
+
export interface AuthLogInput {
|
|
60
|
+
requestId: string;
|
|
61
|
+
app: string;
|
|
62
|
+
kind: AuthLogKind;
|
|
63
|
+
principalId: string | null;
|
|
64
|
+
credentialId?: string | null;
|
|
65
|
+
decision: 'allow' | 'deny';
|
|
66
|
+
reason?: string | null;
|
|
67
|
+
/** The principal held the installation-admin action when this session was opened. */
|
|
68
|
+
privileged?: boolean;
|
|
69
|
+
/** The session's `terminal_id` and `asserted_by`, read from its row, never from what an application sent. */
|
|
70
|
+
terminal?: {
|
|
71
|
+
terminalId: string;
|
|
72
|
+
assertedBy: string;
|
|
73
|
+
} | null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The `principal_label` an audit row stores for a principal: a user's is its id, because a user's label spells its
|
|
77
|
+
* mailbox and an audit row holds no email; a service's is its key name. A caller of unknown kind is treated as a user.
|
|
78
|
+
*/
|
|
79
|
+
export declare function auditLabelOf(principal: {
|
|
80
|
+
readonly id: string;
|
|
81
|
+
readonly type: 'user' | 'service' | undefined;
|
|
82
|
+
readonly label: string | null;
|
|
83
|
+
}): string;
|
|
84
|
+
/** A write that ends in `RETURNING`, as SQL text and its bound values, so an `AuditWriter` can wrap it. */
|
|
85
|
+
export interface SqlChange {
|
|
86
|
+
readonly sql: string;
|
|
87
|
+
readonly binds: readonly unknown[];
|
|
88
|
+
}
|
|
89
|
+
/** The SQL dialect a composition root runs on: D1/SQLite or Postgres. */
|
|
90
|
+
export type SqlDialect = 'sqlite' | 'postgres';
|
|
91
|
+
/**
|
|
92
|
+
* The INSERT of one audit row, for the batch of an unconditional change it records: the row and the change commit
|
|
93
|
+
* together, and a failed insert rolls the change back. Diff and metadata are stored JSON-encoded.
|
|
94
|
+
*/
|
|
95
|
+
export declare function auditEventStatement(db: SqlDatabase, event: AuditEventInput): SqlStatement;
|
|
96
|
+
/**
|
|
97
|
+
* `auditEventStatement` whose `resource_id` is read in the batch by `resourceId`, a one-row SELECT of one column: for
|
|
98
|
+
* a resource whose id the batch itself settles, such as a principal another writer may have created first.
|
|
99
|
+
*/
|
|
100
|
+
export declare function auditEventAboutStatement(db: SqlDatabase, event: AuditEventInput, resourceId: SqlChange): SqlStatement;
|
|
101
|
+
/** A change and its record, as batch statements, and where the change's `RETURNING` rows land in the batch results. */
|
|
102
|
+
export interface AuditedChange {
|
|
103
|
+
readonly statements: SqlStatement[];
|
|
104
|
+
/** The rows the LAST change returned, read from the results of a batch whose statements start at `offset`. */
|
|
105
|
+
rows<T>(results: SqlQueryResult<T>[], offset?: number): T[];
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Writes the record of a guarded change exactly when the change changed a row, decided by the change itself and never
|
|
109
|
+
* by an earlier existence check another writer can invalidate.
|
|
110
|
+
*
|
|
111
|
+
* - **Postgres** runs the changes and the record as ONE statement: `WITH c0 AS (<change> RETURNING …), … INSERT INTO
|
|
112
|
+
* audit_events … WHERE EXISTS (SELECT 1 FROM c0) …`. The record sees exactly the rows this statement changed.
|
|
113
|
+
* Data-modifying CTEs share one snapshot, so the changes of one group must not depend on each other.
|
|
114
|
+
* - **SQLite/D1** has no data-modifying CTE, but it serializes writers, so `changes()` right after each change is
|
|
115
|
+
* exact: the record is inserted after the first change that changed a row, and its fixed id keeps it to one.
|
|
116
|
+
*/
|
|
117
|
+
export declare class AuditWriter {
|
|
118
|
+
private readonly db;
|
|
119
|
+
readonly dialect: SqlDialect;
|
|
120
|
+
constructor(db: SqlDatabase, dialect: SqlDialect);
|
|
121
|
+
/** The changes and, when `event` is given, their record; without one, the changes alone (a seed, a test). */
|
|
122
|
+
changed(changes: readonly SqlChange[], event: AuditEventInput | undefined): AuditedChange;
|
|
123
|
+
/** Run one audited group as its own batch and answer the rows its last change returned. */
|
|
124
|
+
run<T>(changes: readonly SqlChange[], event: AuditEventInput | undefined): Promise<T[]>;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* A one-row SELECT that statements after a guarded change in one batch require: they write only when it finds a row,
|
|
128
|
+
* so they follow the change exactly when it changed something.
|
|
129
|
+
*/
|
|
130
|
+
export interface SqlGuard {
|
|
131
|
+
readonly sql: string;
|
|
132
|
+
readonly binds: readonly unknown[];
|
|
133
|
+
}
|
|
134
|
+
/** `INSERT INTO <target> SELECT <placeholders>`, guarded by `guard` when there is one. */
|
|
135
|
+
export declare function guardedInsert(target: string, placeholders: string, values: readonly unknown[], guard?: SqlGuard): {
|
|
136
|
+
sql: string;
|
|
137
|
+
binds: unknown[];
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* The INSERT of one `auth_log` row. `id` is deliberately omitted — the column is auto-assigned (SQLite rowid alias /
|
|
141
|
+
* Postgres identity; see the note on `auth_log` in migration 0001). Everything else, `created_at` included, is bound.
|
|
142
|
+
* With `guard`, the row is written only when the guard finds its row.
|
|
143
|
+
*/
|
|
144
|
+
export declare function authLogStatement(db: SqlDatabase, input: AuthLogInput, guard?: SqlGuard): SqlStatement;
|
|
145
|
+
export declare class AuditRepository {
|
|
146
|
+
protected readonly db: SqlDatabase;
|
|
147
|
+
readonly writer: AuditWriter;
|
|
148
|
+
constructor(db: SqlDatabase, writer?: AuditWriter);
|
|
149
|
+
/** Write an audit row that records no change of IAM's own, such as an application's event or an admin's access. */
|
|
150
|
+
writeAuditEvent(input: AuditEventInput): Promise<void>;
|
|
151
|
+
writeAuthLog(input: AuthLogInput): Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Run a change of several repositories and its audit rows as ONE batch, in order. Every statement must come from
|
|
154
|
+
* the database this repository holds; a composition root builds all repositories over one.
|
|
155
|
+
*/
|
|
156
|
+
commit<T = Record<string, unknown>>(statements: SqlStatement[]): Promise<SqlQueryResult<T>[]>;
|
|
157
|
+
/** The statement form of `writeAuthLog`, for a use case that composes a `commit`. */
|
|
158
|
+
authLogStatement(input: AuthLogInput): SqlStatement;
|
|
159
|
+
/** The statement form of `auditEventStatement`, for a use case that composes a `commit`. */
|
|
160
|
+
eventStatement(event: AuditEventInput): SqlStatement;
|
|
161
|
+
listAuditEvents(filter: {
|
|
162
|
+
resourceType?: string;
|
|
163
|
+
resourceId?: string;
|
|
164
|
+
principalId?: string;
|
|
165
|
+
subjectPrincipalId?: string;
|
|
166
|
+
action?: string;
|
|
167
|
+
/** Actions to leave out, before the page is cut, so `limit` counts only the rows that remain. */
|
|
168
|
+
excludeActions?: readonly string[];
|
|
169
|
+
requestId?: string;
|
|
170
|
+
before?: string;
|
|
171
|
+
limit: number;
|
|
172
|
+
}): Promise<AuditEventRow[]>;
|
|
173
|
+
listAuthLog(filter: {
|
|
174
|
+
principalId?: string;
|
|
175
|
+
requestId?: string;
|
|
176
|
+
decision?: 'allow' | 'deny';
|
|
177
|
+
before?: number;
|
|
178
|
+
limit: number;
|
|
179
|
+
}): Promise<AuthLogRow[]>;
|
|
180
|
+
/**
|
|
181
|
+
* Prune `auth_log` rows older than `olderThanSeconds` (unix seconds). The rowid
|
|
182
|
+
* is time-correlated, so pruning by `created_at` needs no extra index.
|
|
183
|
+
*/
|
|
184
|
+
pruneAuthLog(olderThanSeconds: number): Promise<number>;
|
|
185
|
+
/** Prune `audit_events` older than `olderThanSeconds` — only where the operator set an audit retention. */
|
|
186
|
+
pruneAuditEvents(olderThanSeconds: number): Promise<number>;
|
|
187
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type SqlDatabase } from '@fabrika/platform';
|
|
2
|
+
import { type AuditEventInput, AuditWriter } from './audit';
|
|
3
|
+
/** The one row of `installation_brand` (migration 0021). A row without a light image is no brand. */
|
|
4
|
+
export interface InstallationBrandRow {
|
|
5
|
+
id: number;
|
|
6
|
+
light_type: string | null;
|
|
7
|
+
light_base64: string | null;
|
|
8
|
+
dark_type: string | null;
|
|
9
|
+
dark_base64: string | null;
|
|
10
|
+
/** When the brand was last set or cleared, in epoch seconds. */
|
|
11
|
+
updated_at: number;
|
|
12
|
+
}
|
|
13
|
+
/** The images a write stores: the light one always, the dark one when the brand has it. */
|
|
14
|
+
export interface InstallationBrandImages {
|
|
15
|
+
readonly light: {
|
|
16
|
+
readonly type: string;
|
|
17
|
+
readonly base64: string;
|
|
18
|
+
};
|
|
19
|
+
readonly dark: {
|
|
20
|
+
readonly type: string;
|
|
21
|
+
readonly base64: string;
|
|
22
|
+
} | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The installation's client logo (ADR-0066). The row is the installation's, so its key is the constant 1. The caller
|
|
26
|
+
* has checked the images; this stores them as given, and the reader checks them again.
|
|
27
|
+
*/
|
|
28
|
+
export declare class InstallationBrandRepository {
|
|
29
|
+
protected readonly db: SqlDatabase;
|
|
30
|
+
private readonly writer;
|
|
31
|
+
constructor(db: SqlDatabase, writer?: AuditWriter);
|
|
32
|
+
/** The stored row, or null when no brand was ever set. */
|
|
33
|
+
get(): Promise<InstallationBrandRow | null>;
|
|
34
|
+
/** Store the brand in place of any earlier one, with its record in the same statement group. Always a change. */
|
|
35
|
+
set(images: InstallationBrandImages, audit: AuditEventInput): Promise<InstallationBrandRow>;
|
|
36
|
+
/**
|
|
37
|
+
* Remove the images and keep the row, so `updated_at` says when the brand was cleared. Only a row that still has an
|
|
38
|
+
* image changes, so clearing an absent brand writes neither the row nor its record; null then.
|
|
39
|
+
*/
|
|
40
|
+
clear(audit: AuditEventInput): Promise<InstallationBrandRow | null>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { Scope } from '@fabrika/auth-core';
|
|
2
|
+
import { type SqlDatabase, type SqlStatement } from '@fabrika/platform';
|
|
3
|
+
import { type AuditEventInput, AuditWriter, type SqlChange } from './audit';
|
|
4
|
+
/** What a new credential carries: its secret's hash, its binding and its inline grants. */
|
|
5
|
+
export interface CreateCredentialInput {
|
|
6
|
+
tokenHash: string;
|
|
7
|
+
label?: string | null;
|
|
8
|
+
principalId?: string | null;
|
|
9
|
+
/** The app the credential mints for. NULL = cross-app; illegal for an anonymous credential. */
|
|
10
|
+
app?: string | null;
|
|
11
|
+
issuedBy: string;
|
|
12
|
+
expiresAt?: number | null;
|
|
13
|
+
grants: {
|
|
14
|
+
action: string;
|
|
15
|
+
scopeType?: string | null;
|
|
16
|
+
scopeValue?: string | null;
|
|
17
|
+
}[];
|
|
18
|
+
}
|
|
19
|
+
/** A unified credential row (the `credentials` table). Only the secret's hash is stored. */
|
|
20
|
+
export interface CredentialRow {
|
|
21
|
+
id: string;
|
|
22
|
+
token_hash: string;
|
|
23
|
+
label: string | null;
|
|
24
|
+
/** Bound principal (live perms), or NULL for an anonymous (frozen inline-grant) credential. */
|
|
25
|
+
principal_id: string | null;
|
|
26
|
+
/**
|
|
27
|
+
* The app this credential may mint for. NULL = cross-app, which is legal ONLY for a
|
|
28
|
+
* principal-bound credential (its permissions are still resolved per app through `grants`) and is
|
|
29
|
+
* REFUSED for an anonymous one, whose frozen grants were delegation-checked against exactly one
|
|
30
|
+
* app at issue time. See `resolveCredential`.
|
|
31
|
+
*/
|
|
32
|
+
app: string | null;
|
|
33
|
+
issued_by: string | null;
|
|
34
|
+
expires_at: number | null;
|
|
35
|
+
revoked_at: number | null;
|
|
36
|
+
created_at: number;
|
|
37
|
+
/** The link mount of a share link; NULL on every other credential. */
|
|
38
|
+
link_mount: string | null;
|
|
39
|
+
/** The dimension of the object the link is bound to, as the mount declared it at issue. */
|
|
40
|
+
link_dimension: string | null;
|
|
41
|
+
/** JSON array of the link's containment chain (`{type, value}`, the object first). */
|
|
42
|
+
link_chain: string | null;
|
|
43
|
+
}
|
|
44
|
+
/** One inline grant on a credential (the `credential_grants` table). Matched by `permits`. */
|
|
45
|
+
export interface CredentialGrantRow {
|
|
46
|
+
credential_id: string;
|
|
47
|
+
action: string;
|
|
48
|
+
scope_type: string | null;
|
|
49
|
+
scope_value: string | null;
|
|
50
|
+
}
|
|
51
|
+
export declare class CredentialRepository {
|
|
52
|
+
protected readonly db: SqlDatabase;
|
|
53
|
+
protected readonly writer: AuditWriter;
|
|
54
|
+
constructor(db: SqlDatabase, writer?: AuditWriter);
|
|
55
|
+
/**
|
|
56
|
+
* Insert a credential + its inline grant rows (if any) in one batch, with `audit` when given. Only the hash is
|
|
57
|
+
* stored. `principalId` null = an anonymous (frozen-grant) credential; non-null = principal-bound.
|
|
58
|
+
*/
|
|
59
|
+
createCredential(input: CreateCredentialInput, audit?: (credentialId: string) => AuditEventInput): Promise<string>;
|
|
60
|
+
/**
|
|
61
|
+
* The INSERTs of one credential with a caller-generated id and its inline grants, for a batch with its record. A
|
|
62
|
+
* credential not bound to its issuer (a service key, a standalone key) holds authority of its own, so an external
|
|
63
|
+
* issuer, whose authority is only its live external roles, writes none (ADR-0051): the guard's NOT NULL violation
|
|
64
|
+
* rolls the batch back, behind `issueKey`'s refusal.
|
|
65
|
+
*/
|
|
66
|
+
credentialStatements(id: string, input: CreateCredentialInput): SqlStatement[];
|
|
67
|
+
/**
|
|
68
|
+
* Insert a share link and its grants only while the app's schema still declares exactly what `checked` says was
|
|
69
|
+
* authorized: the mount with its dimension, grants, issuing action and lifetime, the chain's containment, and for
|
|
70
|
+
* a job its actions and the mount it issues — all in one transaction. The first statement locks the mount row with
|
|
71
|
+
* the one row lock SQLite and Postgres share (D1 runs a batch serially anyway); every reconcile deletes and rewrites
|
|
72
|
+
* that row, so one already writing makes this wait and then compare against what it committed, and one that comes
|
|
73
|
+
* later waits for this key. Returns the id, or null when the declaration changed; `audit` is written exactly when
|
|
74
|
+
* the key is.
|
|
75
|
+
*/
|
|
76
|
+
createLinkCredential(input: {
|
|
77
|
+
tokenHash: string;
|
|
78
|
+
label: string;
|
|
79
|
+
app: string;
|
|
80
|
+
issuedBy: string;
|
|
81
|
+
expiresAt: number;
|
|
82
|
+
grants: {
|
|
83
|
+
action: string;
|
|
84
|
+
scopeType: string;
|
|
85
|
+
scopeValue: string;
|
|
86
|
+
}[];
|
|
87
|
+
link: {
|
|
88
|
+
mount: string;
|
|
89
|
+
chain: Scope[];
|
|
90
|
+
};
|
|
91
|
+
checked: CheckedLinkDeclaration;
|
|
92
|
+
}, audit?: (credentialId: string) => AuditEventInput): Promise<string | null>;
|
|
93
|
+
/**
|
|
94
|
+
* Look up a credential by the secret's hash, but ONLY while still valid (not revoked, not
|
|
95
|
+
* expired). An invalid/absent credential returns null. Single-statement so the validity check
|
|
96
|
+
* and the read can't race a concurrent revoke.
|
|
97
|
+
*/
|
|
98
|
+
getActiveCredentialByHash(tokenHash: string): Promise<CredentialRow | null>;
|
|
99
|
+
getCredentialById(id: string): Promise<CredentialRow | null>;
|
|
100
|
+
getCredentialGrants(credentialId: string): Promise<CredentialGrantRow[]>;
|
|
101
|
+
/** Credentials for a principal (admin list / bulk revoke on disable). */
|
|
102
|
+
listCredentialsForPrincipal(principalId: string): Promise<CredentialRow[]>;
|
|
103
|
+
/**
|
|
104
|
+
* The live credential of each of a PAGE of principals, in one statement: an admin list needs each key's expiry, and
|
|
105
|
+
* one query per row would cost 1+N round trips (SEC-22). Newest first, like `listCredentialsForPrincipal`; rotation
|
|
106
|
+
* revokes every older key, so the first live row of a principal is its live credential.
|
|
107
|
+
*/
|
|
108
|
+
listLiveCredentialsFor(principalIds: readonly string[]): Promise<CredentialRow[]>;
|
|
109
|
+
/**
|
|
110
|
+
* Anonymous credentials — the share links (no bound principal). The admin share-links list.
|
|
111
|
+
*
|
|
112
|
+
* One page at a time, keyset-ordered by the UUIDv7 id like every other admin list, and REVOKED
|
|
113
|
+
* rows are excluded unless asked for: the console's default view is "links that still work", and
|
|
114
|
+
* an installation that has been revoking links for a year should not have to scroll through them.
|
|
115
|
+
* `app` filters to one app's links — with the credential now bound to an app there is finally
|
|
116
|
+
* something to filter on (SEC-2).
|
|
117
|
+
*/
|
|
118
|
+
listAnonymousCredentials(filter: {
|
|
119
|
+
app?: string;
|
|
120
|
+
includeRevoked?: boolean;
|
|
121
|
+
before?: string;
|
|
122
|
+
limit: number;
|
|
123
|
+
}): Promise<CredentialRow[]>;
|
|
124
|
+
/**
|
|
125
|
+
* Inline grants for a PAGE of credentials, in one statement. The admin lists used to issue one
|
|
126
|
+
* query per row, so a page cost 1+N round trips against an unbounded N (SEC-22); the page is
|
|
127
|
+
* bounded, so the `IN` list is too.
|
|
128
|
+
*/
|
|
129
|
+
getCredentialGrantsFor(credentialIds: readonly string[]): Promise<CredentialGrantRow[]>;
|
|
130
|
+
/** Revoke a credential by id (idempotent — already-revoked → false). `audit` is written exactly when this call revoked it. */
|
|
131
|
+
revokeCredential(id: string, audit?: AuditEventInput): Promise<boolean>;
|
|
132
|
+
/** Revoke every active credential bound to a principal (key revoke / rotate). Returns the count. */
|
|
133
|
+
revokeCredentialsForPrincipal(principalId: string): Promise<number>;
|
|
134
|
+
/** `revokeCredentialsForPrincipalChange` as a statement, for a batch whose record does not depend on it. */
|
|
135
|
+
revokeCredentialsForPrincipalStatement(principalId: string): SqlStatement;
|
|
136
|
+
/** Revoke every active credential of a principal except `keep`, as a change an audited revocation groups with others. */
|
|
137
|
+
revokeCredentialsForPrincipalChange(principalId: string, keep?: readonly string[]): SqlChange;
|
|
138
|
+
}
|
|
139
|
+
/** What `issueLink` authorized against: the mount as it read it and, for a job, the job's declaration. */
|
|
140
|
+
export interface CheckedLinkDeclaration {
|
|
141
|
+
readonly grants: readonly string[];
|
|
142
|
+
readonly issuedBy: string;
|
|
143
|
+
readonly maxDays: number;
|
|
144
|
+
readonly job?: {
|
|
145
|
+
readonly name: string;
|
|
146
|
+
readonly actions: readonly string[];
|
|
147
|
+
};
|
|
148
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type DeviceAuthorizationConsent, type StartDeviceAuthorizationInput } from '@fabrika/auth-core';
|
|
2
|
+
import { type SqlDatabase } from '@fabrika/platform';
|
|
3
|
+
export interface DeviceAuthorizationRow {
|
|
4
|
+
id: string;
|
|
5
|
+
app: string;
|
|
6
|
+
label: string;
|
|
7
|
+
permissions: string;
|
|
8
|
+
key_lifetime_seconds: number;
|
|
9
|
+
redemption_challenge: string;
|
|
10
|
+
approval_challenge: string;
|
|
11
|
+
expires_at: number;
|
|
12
|
+
next_poll_at: number;
|
|
13
|
+
status: 'pending' | 'approved' | 'issued' | 'denied' | 'cancelled' | 'authority_lost';
|
|
14
|
+
approver_id: string | null;
|
|
15
|
+
consent_id: string | null;
|
|
16
|
+
scope_type: string | null;
|
|
17
|
+
scope_value: string | null;
|
|
18
|
+
key_expires_at: number | null;
|
|
19
|
+
claim_id: string | null;
|
|
20
|
+
credential_id: string | null;
|
|
21
|
+
principal_id: string | null;
|
|
22
|
+
issued_at: number | null;
|
|
23
|
+
created_at: number;
|
|
24
|
+
}
|
|
25
|
+
export interface DeviceReceiptRow extends DeviceAuthorizationRow {
|
|
26
|
+
revoked_at: number | null;
|
|
27
|
+
}
|
|
28
|
+
export declare class DeviceAuthorizationRepository {
|
|
29
|
+
protected readonly db: SqlDatabase;
|
|
30
|
+
constructor(db: SqlDatabase);
|
|
31
|
+
start(input: StartDeviceAuthorizationInput): Promise<{
|
|
32
|
+
attemptId: string;
|
|
33
|
+
expiresAt: number;
|
|
34
|
+
pollIntervalSeconds: 3;
|
|
35
|
+
}>;
|
|
36
|
+
get(id: string): Promise<DeviceReceiptRow | null>;
|
|
37
|
+
poll(id: string, challenge: string): Promise<boolean>;
|
|
38
|
+
approve(id: string, approverId: string, consent: DeviceAuthorizationConsent): Promise<boolean>;
|
|
39
|
+
terminate(id: string, status: 'denied' | 'cancelled' | 'authority_lost'): Promise<void>;
|
|
40
|
+
deny(id: string): Promise<void>;
|
|
41
|
+
issue(input: {
|
|
42
|
+
attemptId: string;
|
|
43
|
+
consentId: string;
|
|
44
|
+
tokenHash: string;
|
|
45
|
+
requestId: string;
|
|
46
|
+
approverLabel: string;
|
|
47
|
+
}): Promise<DeviceAuthorizationRow | null>;
|
|
48
|
+
list(approverId: string, app: string, before: string | undefined, limit: number): Promise<DeviceReceiptRow[]>;
|
|
49
|
+
prune(now: number): Promise<void>;
|
|
50
|
+
}
|