@fabrika/iam 0.0.1 → 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 +69 -8
- 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,13 @@
|
|
|
1
|
+
-- The application's own people (`members` and `directory` of the contract's vocabulary). Postgres dialect;
|
|
2
|
+
-- `../migrations/0018_app_members.sql` states the same outcome.
|
|
3
|
+
--
|
|
4
|
+
-- The schema reconcile writes the row in its own batch; an application that declares no `members` has none. A holder
|
|
5
|
+
-- of `manage`, globally at the application, grants and revokes the application's global roles through IAM's
|
|
6
|
+
-- administration API within its own actions; a holder of `list`, globally, reads every principal holding one of the
|
|
7
|
+
-- application's roles (`listAppMembers`). `directory` `labels` shows labels to `list` holders too.
|
|
8
|
+
CREATE TABLE app_members (
|
|
9
|
+
app TEXT PRIMARY KEY,
|
|
10
|
+
manage TEXT NOT NULL,
|
|
11
|
+
list TEXT NOT NULL,
|
|
12
|
+
directory TEXT CHECK (directory IS NULL OR directory = 'labels')
|
|
13
|
+
);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
-- An application's HTML body of an IAM e-mail, beside its subject and text. Postgres dialect;
|
|
2
|
+
-- `../migrations/0019_app_mail_html.sql` states the same outcome.
|
|
3
|
+
--
|
|
4
|
+
-- The closed HTML subset of `mailHtmlTokens` (`@fabrika/auth-core`): IAM checks it at the reconcile and again at render,
|
|
5
|
+
-- renders it from its tokens with every value escaped and the action link put in, and falls back to its catalog for a
|
|
6
|
+
-- row it refuses. NULL on every row before this file and on one that declares no HTML: IAM builds the HTML part itself.
|
|
7
|
+
ALTER TABLE app_mail_templates ADD COLUMN html TEXT;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
-- A public procedure is a workload of its own (`public`, named by its endpoint id). Postgres dialect;
|
|
2
|
+
-- `../migrations/0020_public_workloads.sql` states the same outcome. Every existing row keeps its kind.
|
|
3
|
+
ALTER TABLE workload_principals DROP CONSTRAINT workload_principals_kind_check;
|
|
4
|
+
|
|
5
|
+
ALTER TABLE workload_principals ADD CONSTRAINT workload_principals_kind_check CHECK (kind IN ('job', 'webhook', 'public', 'app'));
|
|
6
|
+
|
|
7
|
+
ALTER TABLE app_workloads DROP CONSTRAINT app_workloads_kind_check;
|
|
8
|
+
|
|
9
|
+
ALTER TABLE app_workloads ADD CONSTRAINT app_workloads_kind_check CHECK (kind IN ('job', 'webhook', 'public', 'app'));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
-- The installation's client logo on fabrika's own pages (ADR-0066). Postgres dialect;
|
|
2
|
+
-- `../migrations/0021_installation_brand.sql` states the same outcome.
|
|
3
|
+
--
|
|
4
|
+
-- An installation serves one client, so this is one row or none: `id` is always 1. An installation administrator sets
|
|
5
|
+
-- it through IAM's administration API, which checks each image with `brandImageProblem` (`@fabrika/proxy-core/brand`)
|
|
6
|
+
-- and IAM checks it again at every read, so the image types are not checked here. A clear keeps the row with no
|
|
7
|
+
-- images, so `updated_at` still says when the brand last changed. No row, or a row without a light image, is no brand:
|
|
8
|
+
-- the pages draw the fabrika mark, as every installation did before this file.
|
|
9
|
+
CREATE TABLE installation_brand (
|
|
10
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
11
|
+
light_type TEXT,
|
|
12
|
+
light_base64 TEXT,
|
|
13
|
+
dark_type TEXT,
|
|
14
|
+
dark_base64 TEXT,
|
|
15
|
+
updated_at INTEGER NOT NULL,
|
|
16
|
+
CHECK ((light_type IS NULL) = (light_base64 IS NULL)),
|
|
17
|
+
CHECK ((dark_type IS NULL) = (dark_base64 IS NULL)),
|
|
18
|
+
CHECK (dark_type IS NULL OR light_type IS NOT NULL)
|
|
19
|
+
);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
-- The installation's minimum password length (ADR-0020). Postgres dialect;
|
|
2
|
+
-- `../migrations/0022_password_policy.sql` states the same outcome.
|
|
3
|
+
--
|
|
4
|
+
-- One minimum for every principal of the installation, so this is one row or none: `id` is always 1. An installation
|
|
5
|
+
-- administrator sets it through IAM's administration API, which also holds it to the runtime's floor (15 where the
|
|
6
|
+
-- verifiers are cheaper than OWASP's PBKDF2 cost); the CHECK is only the widest range any runtime accepts. No row is the
|
|
7
|
+
-- default of 15, which every installation applied before this file.
|
|
8
|
+
CREATE TABLE password_policy (
|
|
9
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
10
|
+
min_length INTEGER NOT NULL CHECK (min_length BETWEEN 4 AND 256),
|
|
11
|
+
updated_at INTEGER NOT NULL
|
|
12
|
+
);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
-- Terminals (ADR-0067): a browser an administrator enrolled, on which an application asserts which person is at it.
|
|
2
|
+
-- Postgres dialect; `../migrations/0023_terminals.sql` states the same outcome. Every existing session keeps its method.
|
|
3
|
+
--
|
|
4
|
+
-- A terminal is a `service` principal with this side table, as a workload is. Only the hash of its cookie is stored.
|
|
5
|
+
-- `session_ttl` is how long a person's session on it lasts, in seconds, at most one hour.
|
|
6
|
+
CREATE TABLE terminals (
|
|
7
|
+
principal_id TEXT PRIMARY KEY REFERENCES principals(id) ON DELETE CASCADE,
|
|
8
|
+
label TEXT NOT NULL,
|
|
9
|
+
enrolled_by TEXT REFERENCES principals(id) ON DELETE SET NULL,
|
|
10
|
+
token_hash TEXT NOT NULL UNIQUE,
|
|
11
|
+
session_ttl INTEGER NOT NULL CHECK (session_ttl > 0 AND session_ttl <= 3600),
|
|
12
|
+
created_at INTEGER NOT NULL,
|
|
13
|
+
last_seen_at INTEGER,
|
|
14
|
+
revoked_at INTEGER
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
-- The applications that may assert a person on a terminal, each with its ceiling: a JSON array of the action patterns
|
|
18
|
+
-- of the person's own permissions that survive there. An application without a row asserts nothing on the terminal.
|
|
19
|
+
CREATE TABLE terminal_asserters (
|
|
20
|
+
terminal_id TEXT NOT NULL REFERENCES terminals(principal_id) ON DELETE CASCADE,
|
|
21
|
+
app TEXT NOT NULL,
|
|
22
|
+
ceiling TEXT NOT NULL,
|
|
23
|
+
PRIMARY KEY (terminal_id, app)
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
-- One identification: IAM sent the terminal's browser to `app` to identify the person, carrying the proxy-started
|
|
27
|
+
-- handoff. Single use (`consumed_at`), and only its id's hash is stored.
|
|
28
|
+
CREATE TABLE terminal_identifications (
|
|
29
|
+
id_hash TEXT PRIMARY KEY,
|
|
30
|
+
terminal_id TEXT NOT NULL REFERENCES terminals(principal_id) ON DELETE CASCADE,
|
|
31
|
+
app TEXT NOT NULL,
|
|
32
|
+
return_url TEXT NOT NULL,
|
|
33
|
+
handoff_state TEXT NOT NULL,
|
|
34
|
+
handoff_challenge TEXT NOT NULL,
|
|
35
|
+
created_at INTEGER NOT NULL,
|
|
36
|
+
expires_at INTEGER NOT NULL,
|
|
37
|
+
consumed_at INTEGER
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
CREATE INDEX idx_terminal_identifications_terminal ON terminal_identifications(terminal_id);
|
|
41
|
+
CREATE INDEX idx_terminal_identifications_expires ON terminal_identifications(expires_at);
|
|
42
|
+
|
|
43
|
+
-- A login on a terminal carries its terminal and the application that asserted the person.
|
|
44
|
+
ALTER TABLE sessions ADD COLUMN terminal_id TEXT REFERENCES terminals(principal_id) ON DELETE CASCADE;
|
|
45
|
+
|
|
46
|
+
ALTER TABLE sessions ADD COLUMN asserted_by TEXT;
|
|
47
|
+
|
|
48
|
+
ALTER TABLE sessions DROP CONSTRAINT sessions_authentication_method_check;
|
|
49
|
+
|
|
50
|
+
ALTER TABLE sessions ADD CONSTRAINT sessions_authentication_method_check
|
|
51
|
+
CHECK (authentication_method IN ('oidc', 'password', 'local_dev', 'terminal_assertion'));
|
|
52
|
+
|
|
53
|
+
ALTER TABLE sessions DROP CONSTRAINT sessions_method_subject_check;
|
|
54
|
+
|
|
55
|
+
ALTER TABLE sessions ADD CONSTRAINT sessions_method_subject_check CHECK (
|
|
56
|
+
(authentication_method IN ('oidc', 'local_dev') AND idp_sub IS NOT NULL)
|
|
57
|
+
OR (authentication_method IN ('password', 'terminal_assertion') AND idp_sub IS NULL)
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
ALTER TABLE sessions ADD CONSTRAINT sessions_terminal_check CHECK (
|
|
61
|
+
(authentication_method = 'terminal_assertion' AND terminal_id IS NOT NULL AND asserted_by IS NOT NULL)
|
|
62
|
+
OR (authentication_method <> 'terminal_assertion' AND terminal_id IS NULL AND asserted_by IS NULL)
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
CREATE INDEX idx_sessions_terminal ON sessions(terminal_id) WHERE terminal_id IS NOT NULL;
|
|
66
|
+
|
|
67
|
+
-- Every `auth_log` row of a terminal session names the terminal and the asserting application, read from the session.
|
|
68
|
+
ALTER TABLE auth_log ADD COLUMN terminal_id TEXT;
|
|
69
|
+
|
|
70
|
+
ALTER TABLE auth_log ADD COLUMN asserted_by TEXT;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
-- The identification page of an application that identifies people at enrolled terminals (`terminal` of the
|
|
2
|
+
-- contract's vocabulary, ADR-0067). Postgres dialect; `../migrations/0024_app_terminals.sql` states the same outcome.
|
|
3
|
+
--
|
|
4
|
+
-- The schema reconcile writes the row in its own batch; an application that declares no terminal has none, and IAM
|
|
5
|
+
-- never sends a terminal's browser to it. `identify` is an absolute path on the application's own origin, which IAM
|
|
6
|
+
-- resolves against the sign-in's return origin.
|
|
7
|
+
CREATE TABLE app_terminals (
|
|
8
|
+
app TEXT PRIMARY KEY,
|
|
9
|
+
identify TEXT NOT NULL CHECK (identify LIKE '/%')
|
|
10
|
+
);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
-- The completion of a terminal identification (ADR-0067 §3). Postgres dialect;
|
|
2
|
+
-- `../migrations/0025_terminal_completion.sql` states the same outcome.
|
|
3
|
+
--
|
|
4
|
+
-- An assertion no longer hands the application a code. It records the login it opened in `session_id`, and the
|
|
5
|
+
-- terminal's own browser completes the identification at IAM (`completed_at`), which only then issues the code.
|
|
6
|
+
-- `session_id` has no foreign key: the assertion's batch writes it before the login row, and the completion reads the
|
|
7
|
+
-- login through the session validity rule, which answers nothing for a login that is gone.
|
|
8
|
+
ALTER TABLE terminal_identifications ADD COLUMN session_id TEXT;
|
|
9
|
+
|
|
10
|
+
ALTER TABLE terminal_identifications ADD COLUMN completed_at INTEGER;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
-- The generation of a terminal (ADR-0067 §5). Postgres dialect; `../migrations/0026_terminal_generation.sql` states
|
|
2
|
+
-- the same outcome.
|
|
3
|
+
--
|
|
4
|
+
-- A release or a revocation of a terminal ends every session and identification of its current generation. A release
|
|
5
|
+
-- increments `terminals.generation`; an identification records the generation it was opened in, a login on the
|
|
6
|
+
-- terminal the generation of its identification, and a session is valid only while that is still the terminal's. So an
|
|
7
|
+
-- assertion that raced a release opens a login that is already invalid, whichever transaction commits first.
|
|
8
|
+
ALTER TABLE terminals ADD COLUMN generation INTEGER NOT NULL DEFAULT 0;
|
|
9
|
+
|
|
10
|
+
ALTER TABLE terminal_identifications ADD COLUMN generation INTEGER NOT NULL DEFAULT 0;
|
|
11
|
+
|
|
12
|
+
ALTER TABLE sessions ADD COLUMN terminal_generation INTEGER;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
-- A contract application's authority is exact actions of its approved catalog (the authorization model's Layer 1):
|
|
2
|
+
-- from this release IAM resolves no wildcard at a contract application, so a pattern can no longer track the catalog
|
|
3
|
+
-- beyond the approved revision. Writes have refused one since 0003; this file is the preflight for rows older than
|
|
4
|
+
-- that. Postgres dialect; `../migrations/0027_application_exact_actions.sql` states the same outcome.
|
|
5
|
+
--
|
|
6
|
+
-- No outcome would be correct for such a row: dropping it loses authority, expanding it guesses which revision it
|
|
7
|
+
-- meant. So the file REFUSES while one exists — an inline grant or a role of the app that names a pattern, a grant of
|
|
8
|
+
-- the built-in `admin`, or a key's frozen pattern — and the deploy stops with the old release serving. Resolve each
|
|
9
|
+
-- row with exact actions (or remove it), then deploy again. Expired rows count too.
|
|
10
|
+
CREATE TABLE IF NOT EXISTS iam_preflight_exact_actions (
|
|
11
|
+
app TEXT,
|
|
12
|
+
CONSTRAINT "contract app holds a wildcard: give it exact actions first"
|
|
13
|
+
CHECK (app IS NULL)
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
INSERT INTO iam_preflight_exact_actions (app)
|
|
17
|
+
SELECT g.app FROM grants g JOIN contract_apps c ON c.app = g.app
|
|
18
|
+
WHERE g.permissions LIKE '%*%' OR g.role_key = 'admin';
|
|
19
|
+
|
|
20
|
+
INSERT INTO iam_preflight_exact_actions (app)
|
|
21
|
+
SELECT r.app FROM roles r JOIN contract_apps c ON c.app = r.app
|
|
22
|
+
WHERE r.permissions LIKE '%*%';
|
|
23
|
+
|
|
24
|
+
INSERT INTO iam_preflight_exact_actions (app)
|
|
25
|
+
SELECT k.app FROM credential_grants cg JOIN credentials k ON k.id = cg.credential_id JOIN contract_apps c ON c.app = k.app
|
|
26
|
+
WHERE cg.action LIKE '%*%';
|
|
27
|
+
|
|
28
|
+
DROP TABLE iam_preflight_exact_actions;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
-- The `jti` of every accepted `private_key_jwt` client assertion. Postgres twin of
|
|
2
|
+
-- ../migrations/0028_oauth_client_assertions.sql — see that file for the model; the outcome is identical.
|
|
3
|
+
CREATE TABLE oauth_client_assertions (
|
|
4
|
+
client_id TEXT NOT NULL,
|
|
5
|
+
jti TEXT NOT NULL,
|
|
6
|
+
expires_at INTEGER NOT NULL,
|
|
7
|
+
PRIMARY KEY (client_id, jti)
|
|
8
|
+
);
|
|
9
|
+
CREATE INDEX idx_oauth_client_assertions_expires ON oauth_client_assertions(expires_at);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
-- A membership role may hold actions through a relation (`via`). Postgres twin of
|
|
2
|
+
-- ../migrations/0030_membership_role_via.sql — see that file for the model; the outcome is identical. 0029 stated the
|
|
3
|
+
-- check on the column without a name, and Postgres named it `roles_check1` (after 0012's `roles_check`); every
|
|
4
|
+
-- installation applied the same files in the same order, so the name is the same everywhere. The new one is named.
|
|
5
|
+
ALTER TABLE roles DROP CONSTRAINT roles_check1;
|
|
6
|
+
ALTER TABLE roles ADD CONSTRAINT roles_via_check
|
|
7
|
+
CHECK (via IS NULL OR (external = 0 AND origin = 'app'));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
-- The language a person reads IAM in, beside the application's (`app_presentation.locale`). Postgres dialect;
|
|
2
|
+
-- `../migrations/0031_person_locale.sql` states the same outcome.
|
|
3
|
+
--
|
|
4
|
+
-- An application may name the invited person's language on an invitation (`InvitationOptions.locale`): IAM mails the
|
|
5
|
+
-- invitation in it, and its acceptance keeps it as the principal's. A password action keeps the language its mail was
|
|
6
|
+
-- written in, so the page its link opens speaks the same one. None of the three is checked here: IAM parses each at
|
|
7
|
+
-- read and treats a value it does not speak as not stated. NULL on every row before this file and on one that names
|
|
8
|
+
-- none, which keeps the application's language as before.
|
|
9
|
+
ALTER TABLE membership_invitations ADD COLUMN locale TEXT;
|
|
10
|
+
ALTER TABLE principals ADD COLUMN locale TEXT;
|
|
11
|
+
ALTER TABLE password_action_tokens ADD COLUMN locale TEXT;
|
package/package.json
CHANGED
|
@@ -1,10 +1,71 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
"name": "@fabrika/iam",
|
|
3
|
+
"version": "0.9.0-dev.26",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/contember/fabrika-platform.git",
|
|
7
|
+
"directory": "packages/iam"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"!src/**/__tests__/**",
|
|
15
|
+
"migrations",
|
|
16
|
+
"migrations-postgres",
|
|
17
|
+
"fabrika.config.ts",
|
|
18
|
+
"types"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"bun": "./src/index.ts",
|
|
24
|
+
"types": "./types/src/index.d.ts",
|
|
25
|
+
"default": "./src/index.ts"
|
|
26
|
+
},
|
|
27
|
+
"./service": {
|
|
28
|
+
"bun": "./src/service.ts",
|
|
29
|
+
"types": "./types/src/service.d.ts",
|
|
30
|
+
"default": "./src/service.ts"
|
|
31
|
+
},
|
|
32
|
+
"./node/runtime": {
|
|
33
|
+
"bun": "./src/node/runtime.ts",
|
|
34
|
+
"types": "./types/src/node/runtime.d.ts",
|
|
35
|
+
"default": "./src/node/runtime.ts"
|
|
36
|
+
},
|
|
37
|
+
"./node/server": {
|
|
38
|
+
"bun": "./src/node/server.ts",
|
|
39
|
+
"types": "./types/src/node/server.d.ts",
|
|
40
|
+
"default": "./src/node/server.ts"
|
|
41
|
+
},
|
|
42
|
+
"./fabrika.config": {
|
|
43
|
+
"bun": "./fabrika.config.ts",
|
|
44
|
+
"types": "./types/fabrika.config.d.ts",
|
|
45
|
+
"default": "./fabrika.config.ts"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"typecheck": "tsrs --build tsconfig.test.json",
|
|
50
|
+
"test": "bun test",
|
|
51
|
+
"dev": "bunx lopata dev --env local --port 18191 --listen 0.0.0.0",
|
|
52
|
+
"oblaka": "bunx oblaka oblaka.ts",
|
|
53
|
+
"deploy:cloudflare": "fabrika app deploy --provider=cloudflare",
|
|
54
|
+
"serve": "bun src/node/server.ts",
|
|
55
|
+
"migrate:postgres": "bun src/node/migrate.ts",
|
|
56
|
+
"prune:auth-log": "bun src/node/prune.ts"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@fabrika/app": "0.9.0-dev.26",
|
|
60
|
+
"@fabrika/auth": "0.9.0-dev.26",
|
|
61
|
+
"@fabrika/auth-core": "0.9.0-dev.26",
|
|
62
|
+
"@fabrika/email": "0.9.0-dev.26",
|
|
63
|
+
"@fabrika/iam-contract": "0.9.0-dev.26",
|
|
64
|
+
"@fabrika/platform": "0.9.0-dev.26",
|
|
65
|
+
"@fabrika/platform-node": "0.9.0-dev.26",
|
|
66
|
+
"@fabrika/provider-cloudflare": "0.9.0-dev.26",
|
|
67
|
+
"@fabrika/proxy-core": "0.9.0-dev.26",
|
|
68
|
+
"jose": "^5.9.0",
|
|
69
|
+
"zod": "^3.25.76"
|
|
70
|
+
}
|
|
10
71
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { auditLabelOf } from '../db'
|
|
2
|
+
import { prop } from '../json'
|
|
3
|
+
import type { Services } from '../services'
|
|
4
|
+
import type { ResolvedAdmin } from './router'
|
|
5
|
+
|
|
6
|
+
/** The audit action of one privileged request: an installation admin used `/admin/*`, a read as much as a change. */
|
|
7
|
+
export const ADMIN_ACCESS_ACTION = 'iam.admin.access'
|
|
8
|
+
|
|
9
|
+
/** One `/admin/*` operation as it is recorded: the method, the route template, and for `/admin/rpc` the procedure. */
|
|
10
|
+
export interface AdminAccess {
|
|
11
|
+
readonly method: string
|
|
12
|
+
readonly route: string
|
|
13
|
+
/** The procedure an `/admin/rpc` body names, when it is a well-formed name; null otherwise. */
|
|
14
|
+
readonly procedure?: string | null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const PROCEDURE_NAME = /^[A-Za-z][A-Za-z0-9]*(\.[A-Za-z][A-Za-z0-9]*){0,3}$/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Write the access row before the operation runs, so a request is recorded whatever it then does. The row's `app` is
|
|
21
|
+
* the audience the admin's credential was verified for — the console gateway, or IAM itself for a `px_` key. A request
|
|
22
|
+
* whose access cannot be recorded does not run.
|
|
23
|
+
*/
|
|
24
|
+
export async function recordAdminAccess(services: Services, admin: ResolvedAdmin, requestId: string, access: AdminAccess): Promise<void> {
|
|
25
|
+
await services.repositories.audit.writeAuditEvent({
|
|
26
|
+
requestId,
|
|
27
|
+
principalId: admin.id,
|
|
28
|
+
principalLabel: auditLabelOf(admin),
|
|
29
|
+
app: admin.audience,
|
|
30
|
+
action: ADMIN_ACCESS_ACTION,
|
|
31
|
+
resourceType: 'admin_route',
|
|
32
|
+
resourceId: access.procedure == null ? access.route : `${access.route}#${access.procedure}`,
|
|
33
|
+
metadata: { method: access.method, route: access.route, ...(access.procedure === undefined ? {} : { procedure: access.procedure }) },
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The procedure an `/admin/rpc` request names, read from a copy of its body; null when the body names none it may. */
|
|
38
|
+
export async function rpcProcedureOf(request: Request): Promise<string | null> {
|
|
39
|
+
let body: unknown
|
|
40
|
+
try {
|
|
41
|
+
body = await request.clone().json()
|
|
42
|
+
} catch {
|
|
43
|
+
return null
|
|
44
|
+
}
|
|
45
|
+
const method = prop(body, 'method')
|
|
46
|
+
return typeof method === 'string' && method.length <= 64 && PROCEDURE_NAME.test(method) ? method : null
|
|
47
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { API_KEY_PREFIX } from '@fabrika/auth-core'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type ApiKeyDetail,
|
|
5
|
+
type ApiKeyDto,
|
|
6
|
+
type ApiKeyIdInput,
|
|
7
|
+
type CursorList,
|
|
8
|
+
type ListApiKeysInput,
|
|
9
|
+
type OkResponse,
|
|
10
|
+
type ProvisionApiKeyRequest,
|
|
11
|
+
type ProvisionApiKeyResponse,
|
|
12
|
+
type RotateApiKeyInput,
|
|
13
|
+
type RotateApiKeyResponse,
|
|
14
|
+
} from '@fabrika/iam-contract'
|
|
15
|
+
import { uuidv7 } from '@fabrika/platform'
|
|
16
|
+
import { z } from 'zod'
|
|
17
|
+
import { contractGrantProblem } from '../contract-rules'
|
|
18
|
+
import { type CredentialRow, type GrantRow, type PrincipalRow, principalStatus } from '../db'
|
|
19
|
+
import { generateToken, hashToken } from '../secret'
|
|
20
|
+
import { type AdminContext, adminEvent, fail } from './context'
|
|
21
|
+
import { appField, parseRoleOrInline, parseScope, RoleKnownCache, toGrantDtos } from './grant-support'
|
|
22
|
+
import { cursorOf, groupBy, normalizeLimit } from './pagination'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One page of service principals with their grants and live credentials. THREE queries per page, whatever the page
|
|
26
|
+
* holds: the roster read, then one grants read and one credentials read for the page's ids. It used to be 1+N over an unbounded
|
|
27
|
+
* roster, with no pagination at all (SEC-22).
|
|
28
|
+
*/
|
|
29
|
+
export async function listApiKeysUseCase(c: AdminContext, input: ListApiKeysInput): Promise<CursorList<ApiKeyDetail>> {
|
|
30
|
+
const limit = normalizeLimit(input.limit)
|
|
31
|
+
const principals = await c.services.repositories.principals.listPrincipals({
|
|
32
|
+
type: 'service',
|
|
33
|
+
withoutWorkloads: true,
|
|
34
|
+
withoutTerminals: true,
|
|
35
|
+
...(input.before ? { before: input.before } : {}),
|
|
36
|
+
limit,
|
|
37
|
+
})
|
|
38
|
+
const ids = principals.map((p) => p.id)
|
|
39
|
+
const [grantRows, credentialRows] = await Promise.all([
|
|
40
|
+
c.services.repositories.grants.listGrantsFor(ids),
|
|
41
|
+
c.services.repositories.credentials.listLiveCredentialsFor(ids),
|
|
42
|
+
])
|
|
43
|
+
const grants = groupBy(grantRows, (row) => row.principal_id)
|
|
44
|
+
const live = new Map<string, CredentialRow>()
|
|
45
|
+
for (const row of credentialRows) {
|
|
46
|
+
if (row.principal_id !== null && !live.has(row.principal_id)) live.set(row.principal_id, row)
|
|
47
|
+
}
|
|
48
|
+
const cache = new RoleKnownCache(c.services)
|
|
49
|
+
const items: ApiKeyDetail[] = []
|
|
50
|
+
for (const principal of principals) {
|
|
51
|
+
const dto = await toApiKeyDto(c, principal, grants.get(principal.id) ?? [], cache)
|
|
52
|
+
items.push({ ...dto, credential: credentialDto(live.get(principal.id)) })
|
|
53
|
+
}
|
|
54
|
+
return { items, nextCursor: cursorOf(items, limit, (item) => item.principalId) }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The service principal behind an API key, or null. A workload's key and a terminal are service principals too, and
|
|
59
|
+
* neither is an API key: a terminal holds no key at all, and rotating one would hand it a `px_` credential.
|
|
60
|
+
*/
|
|
61
|
+
async function apiKeyPrincipal(c: AdminContext, principalId: string): Promise<PrincipalRow | null> {
|
|
62
|
+
const { principals, grants, terminals } = c.services.repositories
|
|
63
|
+
const principal = await principals.getPrincipalById(principalId)
|
|
64
|
+
if (principal?.type !== 'service' || (await grants.isWorkloadPrincipal(principalId)) || (await terminals.getById(principalId)) !== null) {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
return principal
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** One key with its grants and its live credential, for the console's key page. */
|
|
71
|
+
export async function getApiKeyUseCase(c: AdminContext, input: ApiKeyIdInput): Promise<ApiKeyDetail> {
|
|
72
|
+
const { principalId } = input
|
|
73
|
+
const { grants, credentials } = c.services.repositories
|
|
74
|
+
const principal = await apiKeyPrincipal(c, principalId)
|
|
75
|
+
if (principal === null) {
|
|
76
|
+
return fail(404, 'service principal not found')
|
|
77
|
+
}
|
|
78
|
+
// `listCredentialsForPrincipal` is newest-first and rotation revokes every older key, so this is the live one.
|
|
79
|
+
const live = (await credentials.listCredentialsForPrincipal(principalId)).find((row) => row.revoked_at === null)
|
|
80
|
+
const dto = await toApiKeyDto(c, principal, await grants.listGrants(principalId), new RoleKnownCache(c.services))
|
|
81
|
+
return { ...dto, credential: credentialDto(live) }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function credentialDto(live: CredentialRow | undefined): ApiKeyDetail['credential'] {
|
|
85
|
+
return live === undefined ? null : { app: live.app, expiresAt: live.expires_at }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function toApiKeyDto(c: AdminContext, principal: PrincipalRow, grants: GrantRow[], cache: RoleKnownCache): Promise<ApiKeyDto> {
|
|
89
|
+
return {
|
|
90
|
+
principalId: principal.id,
|
|
91
|
+
label: principal.label,
|
|
92
|
+
status: principalStatus(principal),
|
|
93
|
+
grants: await toGrantDtos(grants, c.services, cache),
|
|
94
|
+
createdAt: principal.created_at,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function provisionApiKeyUseCase(c: AdminContext, input: ProvisionApiKeyRequest): Promise<ProvisionApiKeyResponse> {
|
|
99
|
+
const { label, type } = input
|
|
100
|
+
if (label.trim() === '' || type !== 'service') return fail(400, 'label and type=service required')
|
|
101
|
+
const appResult = await appField(c, input)
|
|
102
|
+
if (!appResult.ok) {
|
|
103
|
+
return fail(400, 'unknown app')
|
|
104
|
+
}
|
|
105
|
+
const app = appResult.app
|
|
106
|
+
const roleOrInline = await parseRoleOrInline(c, input, app)
|
|
107
|
+
if (!roleOrInline.ok) {
|
|
108
|
+
return fail(400, roleOrInline.message)
|
|
109
|
+
}
|
|
110
|
+
const scope = parseScope(input)
|
|
111
|
+
if (!scope.ok) {
|
|
112
|
+
return fail(400, 'scopeType and scopeValue must be both set or both omitted')
|
|
113
|
+
}
|
|
114
|
+
const contractProblem = await contractGrantProblem(c.services.repositories.appSchema, app, { ...roleOrInline, scopeType: scope.scopeType })
|
|
115
|
+
if (contractProblem !== null) {
|
|
116
|
+
return fail(400, contractProblem)
|
|
117
|
+
}
|
|
118
|
+
const expiresAt = Number.isFinite(input.expiresAt) ? input.expiresAt ?? null : null
|
|
119
|
+
const nowSeconds = Math.floor(Date.now() / 1000)
|
|
120
|
+
if (expiresAt !== null && expiresAt <= nowSeconds) {
|
|
121
|
+
return fail(400, 'expiresAt must be in the future')
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Create the native service principal (external_id NULL — resolved by its `px_` key, not by a
|
|
125
|
+
// CF client_id), its grant, the bound credential and their records, in one transaction. Shown once.
|
|
126
|
+
const principalId = uuidv7()
|
|
127
|
+
const grantId = uuidv7()
|
|
128
|
+
const apiKey = `${API_KEY_PREFIX}${generateToken()}`
|
|
129
|
+
const { principals, grants, credentials, audit } = c.services.repositories
|
|
130
|
+
await audit.commit([
|
|
131
|
+
principals.createServiceStatement(principalId, label),
|
|
132
|
+
grants.grantStatement(grantId, {
|
|
133
|
+
principalId,
|
|
134
|
+
app,
|
|
135
|
+
roleKey: roleOrInline.roleKey,
|
|
136
|
+
permissions: roleOrInline.permissions,
|
|
137
|
+
scopeType: scope.scopeType,
|
|
138
|
+
scopeValue: scope.scopeValue,
|
|
139
|
+
grantedBy: c.admin.id,
|
|
140
|
+
expiresAt,
|
|
141
|
+
}),
|
|
142
|
+
...credentials.credentialStatements(uuidv7(), {
|
|
143
|
+
tokenHash: await hashToken(apiKey),
|
|
144
|
+
label,
|
|
145
|
+
principalId,
|
|
146
|
+
// The credential is bound to the SAME app as the grant it was provisioned for. `null` here is
|
|
147
|
+
// the operator's explicit "every app" choice, which is legal for a principal-bound credential
|
|
148
|
+
// because its permissions still resolve per app through that cross-app grant (SEC-2).
|
|
149
|
+
app,
|
|
150
|
+
issuedBy: c.admin.id,
|
|
151
|
+
expiresAt,
|
|
152
|
+
grants: [],
|
|
153
|
+
}),
|
|
154
|
+
audit.eventStatement(adminEvent(c, {
|
|
155
|
+
action: 'iam.apikey.create',
|
|
156
|
+
resourceType: 'principal',
|
|
157
|
+
resourceId: principalId,
|
|
158
|
+
metadata: { label, roleKey: roleOrInline.roleKey, permissions: roleOrInline.permissions, app },
|
|
159
|
+
})),
|
|
160
|
+
audit.eventStatement(adminEvent(c, {
|
|
161
|
+
action: 'iam.grant.create',
|
|
162
|
+
resourceType: 'grant',
|
|
163
|
+
resourceId: grantId,
|
|
164
|
+
metadata: {
|
|
165
|
+
principalId,
|
|
166
|
+
roleKey: roleOrInline.roleKey,
|
|
167
|
+
permissions: roleOrInline.permissions,
|
|
168
|
+
scopeType: scope.scopeType,
|
|
169
|
+
scopeValue: scope.scopeValue,
|
|
170
|
+
app,
|
|
171
|
+
},
|
|
172
|
+
})),
|
|
173
|
+
])
|
|
174
|
+
|
|
175
|
+
const response: ProvisionApiKeyResponse = { principalId, apiKey }
|
|
176
|
+
return response
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export async function revokeApiKeyUseCase(c: AdminContext, input: { principalId: string }): Promise<OkResponse> {
|
|
180
|
+
const { principalId } = input
|
|
181
|
+
const principal = await apiKeyPrincipal(c, principalId)
|
|
182
|
+
if (principal === null) {
|
|
183
|
+
return fail(404, 'service principal not found')
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const { principals, grants, credentials, audit } = c.services.repositories
|
|
187
|
+
// One transaction, recorded only when it deleted a grant, disabled the principal or revoked a key.
|
|
188
|
+
await audit.writer.run(
|
|
189
|
+
[
|
|
190
|
+
grants.deleteGrantsForPrincipalChange(principalId),
|
|
191
|
+
principals.disablePrincipalChange(principalId),
|
|
192
|
+
credentials.revokeCredentialsForPrincipalChange(principalId),
|
|
193
|
+
],
|
|
194
|
+
adminEvent(c, { action: 'iam.apikey.revoke', resourceType: 'principal', resourceId: principalId, metadata: { label: principal.label } }),
|
|
195
|
+
)
|
|
196
|
+
return { ok: true }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Rotate: same principal, same grants, new secret. Effective immediately.
|
|
201
|
+
*
|
|
202
|
+
* Two things the old implementation got wrong. It DROPPED the credential's expiry — a key provisioned
|
|
203
|
+
* to die in 30 days came back immortal, and an operator rotating a key on schedule was quietly
|
|
204
|
+
* removing its bound (CORR-5); the replacement carries the old expiry forward unless the caller states
|
|
205
|
+
* a new one, and a stated one must be in the future exactly as provisioning requires. And it rotated a
|
|
206
|
+
* DISABLED principal's key, handing out a working secret for an account somebody had just revoked —
|
|
207
|
+
* the same guard `passwordUser` already applies.
|
|
208
|
+
*/
|
|
209
|
+
export async function rotateApiKeyUseCase(c: AdminContext, input: RotateApiKeyInput): Promise<RotateApiKeyResponse> {
|
|
210
|
+
const { principalId } = input
|
|
211
|
+
const principal = await apiKeyPrincipal(c, principalId)
|
|
212
|
+
if (principal === null) {
|
|
213
|
+
return fail(404, 'service principal not found')
|
|
214
|
+
}
|
|
215
|
+
if (principal.disabled_at !== null) {
|
|
216
|
+
return fail(409, 'principal is disabled')
|
|
217
|
+
}
|
|
218
|
+
const nowSeconds = Math.floor(Date.now() / 1000)
|
|
219
|
+
if (input.expiresAt !== undefined && input.expiresAt !== null && input.expiresAt <= nowSeconds) {
|
|
220
|
+
return fail(400, 'expiresAt must be in the future')
|
|
221
|
+
}
|
|
222
|
+
// The credential being replaced decides what the replacement inherits: its expiry and its app
|
|
223
|
+
// binding. `listCredentialsForPrincipal` is newest-first, so this is the live one.
|
|
224
|
+
const existing = (await c.services.repositories.credentials.listCredentialsForPrincipal(principalId)).find((row) => row.revoked_at === null)
|
|
225
|
+
const expiresAt = input.expiresAt !== undefined ? input.expiresAt : existing?.expires_at ?? null
|
|
226
|
+
const apiKey = `${API_KEY_PREFIX}${generateToken()}`
|
|
227
|
+
const { credentials, audit } = c.services.repositories
|
|
228
|
+
// The old keys go, the new one comes and the record is written in one transaction: a failure leaves the old key.
|
|
229
|
+
await audit.commit([
|
|
230
|
+
credentials.revokeCredentialsForPrincipalStatement(principalId),
|
|
231
|
+
...credentials.credentialStatements(uuidv7(), {
|
|
232
|
+
tokenHash: await hashToken(apiKey),
|
|
233
|
+
label: principal.label,
|
|
234
|
+
principalId,
|
|
235
|
+
app: existing?.app ?? null,
|
|
236
|
+
issuedBy: c.admin.id,
|
|
237
|
+
expiresAt,
|
|
238
|
+
grants: [],
|
|
239
|
+
}),
|
|
240
|
+
audit.eventStatement(
|
|
241
|
+
adminEvent(c, {
|
|
242
|
+
action: 'iam.apikey.rotate',
|
|
243
|
+
resourceType: 'principal',
|
|
244
|
+
resourceId: principalId,
|
|
245
|
+
metadata: { label: principal.label, expiresAt },
|
|
246
|
+
}),
|
|
247
|
+
),
|
|
248
|
+
])
|
|
249
|
+
const response: RotateApiKeyResponse = { principalId, apiKey }
|
|
250
|
+
return response
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Bootstrap REST omits malformed optional values; admin RPC rejects them instead.
|
|
254
|
+
const provisionApiKeyRequestSchema = z.object({
|
|
255
|
+
label: z.string(),
|
|
256
|
+
type: z.literal('service'),
|
|
257
|
+
roleKey: z.string().optional().catch(undefined),
|
|
258
|
+
permissions: z.array(z.string()).optional().catch(undefined),
|
|
259
|
+
scopeType: z.string().nullable().optional().catch(undefined),
|
|
260
|
+
scopeValue: z.string().nullable().optional().catch(undefined),
|
|
261
|
+
app: z.string().nullable().optional().catch(undefined),
|
|
262
|
+
expiresAt: z.number().finite().optional().catch(undefined),
|
|
263
|
+
}).transform(({ label, type, roleKey, permissions, scopeType, scopeValue, app, expiresAt }) => {
|
|
264
|
+
return {
|
|
265
|
+
label,
|
|
266
|
+
type,
|
|
267
|
+
...(roleKey !== undefined ? { roleKey } : {}),
|
|
268
|
+
...(permissions !== undefined ? { permissions } : {}),
|
|
269
|
+
...(scopeType !== undefined ? { scopeType } : {}),
|
|
270
|
+
...(scopeValue !== undefined ? { scopeValue } : {}),
|
|
271
|
+
...(app !== undefined ? { app } : {}),
|
|
272
|
+
...(expiresAt !== undefined ? { expiresAt } : {}),
|
|
273
|
+
}
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
export function parseProvisionApiKeyRequest(body: unknown): ProvisionApiKeyRequest {
|
|
277
|
+
const parsed = provisionApiKeyRequestSchema.safeParse(body)
|
|
278
|
+
if (!parsed.success) return fail(400, 'label and type=service required')
|
|
279
|
+
return parsed.data
|
|
280
|
+
}
|