@x12i/keyrox-store-pg 0.1.0 → 0.1.1

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.
Files changed (74) hide show
  1. package/dist/database.d.ts.map +1 -1
  2. package/dist/database.js +2 -0
  3. package/dist/database.js.map +1 -1
  4. package/dist/directory-access-sql.d.ts +12 -0
  5. package/dist/directory-access-sql.d.ts.map +1 -0
  6. package/dist/directory-access-sql.js +253 -0
  7. package/dist/directory-access-sql.js.map +1 -0
  8. package/dist/directory-sql.d.ts +4 -2
  9. package/dist/directory-sql.d.ts.map +1 -1
  10. package/dist/directory-sql.js +808 -30
  11. package/dist/directory-sql.js.map +1 -1
  12. package/dist/hosted-human-sessions.d.ts +5 -0
  13. package/dist/hosted-human-sessions.d.ts.map +1 -0
  14. package/dist/hosted-human-sessions.js +105 -0
  15. package/dist/hosted-human-sessions.js.map +1 -0
  16. package/dist/migrate.d.ts.map +1 -1
  17. package/dist/migrate.js +24 -1
  18. package/dist/migrate.js.map +1 -1
  19. package/dist/notification-deliveries.d.ts +5 -0
  20. package/dist/notification-deliveries.d.ts.map +1 -0
  21. package/dist/notification-deliveries.js +81 -0
  22. package/dist/notification-deliveries.js.map +1 -0
  23. package/dist/notification-webhook-sql.d.ts +4 -0
  24. package/dist/notification-webhook-sql.d.ts.map +1 -0
  25. package/dist/notification-webhook-sql.js +109 -0
  26. package/dist/notification-webhook-sql.js.map +1 -0
  27. package/dist/rows.d.ts +19 -1
  28. package/dist/rows.d.ts.map +1 -1
  29. package/dist/rows.js +153 -3
  30. package/dist/rows.js.map +1 -1
  31. package/dist/sql-store.d.ts.map +1 -1
  32. package/dist/sql-store.js +796 -78
  33. package/dist/sql-store.js.map +1 -1
  34. package/migrations/007_account_sso.sql +40 -0
  35. package/migrations/008_app_sign_in.sql +26 -0
  36. package/migrations/009_account_sso_next.sql +4 -0
  37. package/migrations/010_account_sso_profile.sql +14 -0
  38. package/migrations/011_hosted_sso_profiles.sql +26 -0
  39. package/migrations/012_app_signup_roles.sql +2 -0
  40. package/migrations/013_product_role_catalog.sql +14 -0
  41. package/migrations/014_application_staff_access.sql +21 -0
  42. package/migrations/015_audit_service_actor.sql +7 -0
  43. package/migrations/016_app_waitlist_questions.sql +2 -0
  44. package/migrations/017_app_signup_policy.sql +9 -0
  45. package/migrations/018_managed_app_sign_in.sql +8 -0
  46. package/migrations/019_hosted_sso_email_proofs.sql +12 -0
  47. package/migrations/020_cli_login_codes.sql +15 -0
  48. package/migrations/021_hosted_email_codes.sql +18 -0
  49. package/migrations/022_sso_waitlist_callback.sql +3 -0
  50. package/migrations/023_app_email_code_sign_in.sql +1 -0
  51. package/migrations/024_hosted_auth_copy.sql +2 -0
  52. package/migrations/025_app_directory_workspaces.sql +55 -0
  53. package/migrations/027_role_features_and_configuration.sql +17 -0
  54. package/migrations/028_sso_only_signup_audience.sql +4 -0
  55. package/migrations/029_people_only_directory_model.sql +3 -0
  56. package/migrations/030_typed_questionnaires.sql +3 -0
  57. package/migrations/031_app_notifications.sql +22 -0
  58. package/migrations/032_agent_grant_duration.sql +10 -0
  59. package/migrations/033_application_lifecycle.sql +67 -0
  60. package/migrations/034_notification_deliveries.sql +25 -0
  61. package/migrations/035_app_notification_webhook_jobs.sql +28 -0
  62. package/migrations/036_account_notification_recovery.sql +11 -0
  63. package/migrations/037_waitlist_confirmation.sql +7 -0
  64. package/migrations/039_hosted_human_sessions.sql +45 -0
  65. package/migrations/040_hosted_human_clients.sql +35 -0
  66. package/migrations/041_app_workspace_invitations.sql +9 -0
  67. package/migrations/042_directory_email_proof.sql +16 -0
  68. package/migrations/043_sso_exchange_redirect_uri.sql +1 -0
  69. package/migrations/044_repair_app_sign_in_connections.sql +30 -0
  70. package/migrations/045_directory_integrity.sql +140 -0
  71. package/migrations/046_person_email_across_organizations.sql +7 -0
  72. package/migrations/047_owner_access.sql +35 -0
  73. package/migrations/048_invitation_links.sql +28 -0
  74. package/package.json +3 -3
@@ -0,0 +1,4 @@
1
+ ALTER TABLE applications DROP CONSTRAINT applications_signup_audience_check;
2
+
3
+ ALTER TABLE applications ADD CONSTRAINT applications_signup_audience_check
4
+ CHECK (signup_audience IN ('any-email', 'business-or-sso', 'sso-only'));
@@ -0,0 +1,3 @@
1
+ ALTER TABLE applications DROP CONSTRAINT applications_directory_model_check;
2
+ ALTER TABLE applications ADD CONSTRAINT applications_directory_model_check
3
+ CHECK (directory_model IN ('organizations', 'workspaces', 'organizations-and-workspaces', 'people-only'));
@@ -0,0 +1,3 @@
1
+ ALTER TABLE applications
2
+ ADD COLUMN waitlist_question_definitions jsonb CHECK (jsonb_typeof(waitlist_question_definitions) = 'array'),
3
+ ADD COLUMN waitlist_question_layout text NOT NULL DEFAULT 'form' CHECK (waitlist_question_layout IN ('form', 'wizard'));
@@ -0,0 +1,22 @@
1
+ CREATE TABLE app_notification_settings (
2
+ environment_id text PRIMARY KEY REFERENCES app_environments (id),
3
+ app_id text NOT NULL REFERENCES applications (id),
4
+ org_id text NOT NULL REFERENCES orgs (id),
5
+ delivery text NOT NULL CHECK (delivery IN ('keyrox', 'both', 'webhook')),
6
+ templates jsonb NOT NULL,
7
+ updated_at timestamptz NOT NULL
8
+ );
9
+
10
+ CREATE TABLE app_notification_webhooks (
11
+ id text PRIMARY KEY,
12
+ environment_id text NOT NULL REFERENCES app_environments (id),
13
+ app_id text NOT NULL REFERENCES applications (id),
14
+ org_id text NOT NULL REFERENCES orgs (id),
15
+ url text NOT NULL,
16
+ secret_enc text NOT NULL,
17
+ enabled boolean NOT NULL,
18
+ created_at timestamptz NOT NULL,
19
+ updated_at timestamptz NOT NULL
20
+ );
21
+
22
+ CREATE INDEX app_notification_webhooks_environment_idx ON app_notification_webhooks (environment_id, created_at);
@@ -0,0 +1,10 @@
1
+ -- Upgrade the former default while keeping any tenant limit an owner explicitly set.
2
+ UPDATE orgs
3
+ SET grant_max_days = 730, policy_version = policy_version + 1
4
+ WHERE grant_max_days = 90
5
+ AND NOT EXISTS (
6
+ SELECT 1 FROM audit_events
7
+ WHERE audit_events.org_id = orgs.id
8
+ AND audit_events.action = 'org.policy.update'
9
+ AND audit_events.detail LIKE '%grantMaxDays%'
10
+ );
@@ -0,0 +1,67 @@
1
+ ALTER TABLE applications ADD COLUMN trashed_at timestamptz;
2
+
3
+ CREATE INDEX applications_trash_idx ON applications (org_id, trashed_at DESC, id DESC)
4
+ WHERE trashed_at IS NOT NULL;
5
+
6
+ CREATE TABLE application_logo_purge (
7
+ app_id text PRIMARY KEY
8
+ );
9
+
10
+ ALTER TABLE app_environments DROP CONSTRAINT app_environments_app_id_fkey,
11
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE;
12
+ ALTER TABLE app_tokens DROP CONSTRAINT app_tokens_app_id_fkey, DROP CONSTRAINT app_tokens_environment_fk,
13
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE,
14
+ ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE;
15
+ ALTER TABLE app_sign_in DROP CONSTRAINT app_sign_in_app_id_fkey,
16
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE;
17
+ ALTER TABLE environment_redirects DROP CONSTRAINT environment_redirects_environment_id_fkey,
18
+ ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE;
19
+ ALTER TABLE sso_logins DROP CONSTRAINT sso_logins_app_id_fkey, DROP CONSTRAINT sso_logins_environment_id_fkey,
20
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE,
21
+ ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE;
22
+ ALTER TABLE audiences DROP CONSTRAINT audiences_app_id_fkey,
23
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE;
24
+ ALTER TABLE gateway_routes DROP CONSTRAINT gateway_routes_audience_id_fkey,
25
+ ADD FOREIGN KEY (audience_id) REFERENCES audiences (id) ON DELETE CASCADE;
26
+ ALTER TABLE app_identities DROP CONSTRAINT app_identities_app_id_fkey, DROP CONSTRAINT app_identities_environment_id_fkey,
27
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE,
28
+ ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE;
29
+ ALTER TABLE app_identity_memberships DROP CONSTRAINT app_identity_memberships_identity_id_fkey,
30
+ ADD FOREIGN KEY (identity_id) REFERENCES app_identities (id) ON DELETE CASCADE;
31
+ ALTER TABLE app_group_members DROP CONSTRAINT app_group_members_group_id_fkey,
32
+ ADD FOREIGN KEY (group_id) REFERENCES app_groups (id) ON DELETE CASCADE;
33
+ ALTER TABLE hosted_sso_profiles DROP CONSTRAINT hosted_sso_profiles_app_id_fkey, DROP CONSTRAINT hosted_sso_profiles_environment_id_fkey,
34
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE,
35
+ ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE;
36
+ ALTER TABLE hosted_sso_identities DROP CONSTRAINT hosted_sso_identities_environment_id_fkey, DROP CONSTRAINT hosted_sso_identities_identity_id_fkey,
37
+ ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE,
38
+ ADD FOREIGN KEY (identity_id) REFERENCES app_identities (id) ON DELETE CASCADE;
39
+ ALTER TABLE app_staff_access DROP CONSTRAINT app_staff_access_app_id_org_id_fkey,
40
+ ADD FOREIGN KEY (app_id, org_id) REFERENCES applications (id, org_id) ON DELETE CASCADE;
41
+ ALTER TABLE app_workspaces DROP CONSTRAINT app_workspaces_app_id_environment_id_fkey, DROP CONSTRAINT app_workspaces_environment_id_organization_id_fkey,
42
+ ADD FOREIGN KEY (app_id, environment_id) REFERENCES app_environments (app_id, id) ON DELETE CASCADE,
43
+ ADD FOREIGN KEY (environment_id, organization_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE;
44
+ ALTER TABLE app_workspace_memberships DROP CONSTRAINT app_workspace_memberships_environment_id_workspace_id_fkey,
45
+ DROP CONSTRAINT app_workspace_memberships_environment_id_identity_id_fkey,
46
+ ADD FOREIGN KEY (environment_id, workspace_id) REFERENCES app_workspaces (environment_id, id) ON DELETE CASCADE,
47
+ ADD FOREIGN KEY (environment_id, identity_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE;
48
+ ALTER TABLE app_onboarding_responses DROP CONSTRAINT app_onboarding_responses_environment_id_identity_id_fkey,
49
+ DROP CONSTRAINT app_onboarding_responses_environment_id_workspace_id_fkey,
50
+ ADD FOREIGN KEY (environment_id, identity_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE,
51
+ ADD FOREIGN KEY (environment_id, workspace_id) REFERENCES app_workspaces (environment_id, id) ON DELETE CASCADE;
52
+ ALTER TABLE app_notification_settings DROP CONSTRAINT app_notification_settings_app_id_fkey,
53
+ DROP CONSTRAINT app_notification_settings_environment_id_fkey,
54
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE,
55
+ ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE;
56
+ ALTER TABLE app_notification_webhooks DROP CONSTRAINT app_notification_webhooks_app_id_fkey,
57
+ DROP CONSTRAINT app_notification_webhooks_environment_id_fkey,
58
+ ADD FOREIGN KEY (app_id) REFERENCES applications (id) ON DELETE CASCADE,
59
+ ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE;
60
+
61
+ -- Keep legacy rows readable and enforce their environment reference on new writes.
62
+ ALTER TABLE app_permissions ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE NOT VALID;
63
+ ALTER TABLE app_product_roles ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE NOT VALID;
64
+ ALTER TABLE app_groups ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE NOT VALID;
65
+ ALTER TABLE app_role_bindings ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE NOT VALID;
66
+ ALTER TABLE app_features ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE NOT VALID;
67
+ ALTER TABLE app_waitlist ADD FOREIGN KEY (environment_id) REFERENCES app_environments (id) ON DELETE CASCADE NOT VALID;
@@ -0,0 +1,25 @@
1
+ CREATE TABLE notification_deliveries (
2
+ message_id text PRIMARY KEY,
3
+ organization_id text NOT NULL,
4
+ environment_id text,
5
+ environment_key_version integer,
6
+ type text NOT NULL,
7
+ status text NOT NULL CHECK (status IN ('pending','queued','sending','provider_accepted','delivered','failed','bounced','complained','suppressed','cancelled','expired')),
8
+ payload_enc text,
9
+ created_at timestamptz NOT NULL,
10
+ updated_at timestamptz NOT NULL,
11
+ expires_at timestamptz NOT NULL,
12
+ accepted_at timestamptz,
13
+ failure_code text,
14
+ attempts integer NOT NULL DEFAULT 0,
15
+ next_attempt_at timestamptz,
16
+ lease_id text,
17
+ lease_until timestamptz,
18
+ CHECK ((environment_id IS NULL) = (environment_key_version IS NULL)),
19
+ CHECK ((lease_id IS NULL) = (lease_until IS NULL))
20
+ );
21
+
22
+ CREATE INDEX notification_deliveries_due ON notification_deliveries (next_attempt_at, created_at) WHERE next_attempt_at IS NOT NULL;
23
+ CREATE INDEX notification_deliveries_scope ON notification_deliveries (organization_id, environment_id, created_at DESC, message_id DESC);
24
+ CREATE INDEX notification_deliveries_cleanup ON notification_deliveries (updated_at) WHERE next_attempt_at IS NULL;
25
+
@@ -0,0 +1,28 @@
1
+ CREATE TABLE app_notification_webhook_jobs (
2
+ id text PRIMARY KEY,
3
+ event_id text NOT NULL,
4
+ webhook_id text NOT NULL,
5
+ organization_id text NOT NULL,
6
+ app_id text NOT NULL,
7
+ environment_id text NOT NULL REFERENCES app_environments (id) ON DELETE CASCADE,
8
+ environment_key_version integer NOT NULL,
9
+ type text NOT NULL,
10
+ body_enc text,
11
+ keyrox_delivery boolean NOT NULL,
12
+ status text NOT NULL CHECK (status IN ('queued', 'accepted', 'failed', 'expired', 'cancelled')),
13
+ attempt_count integer NOT NULL DEFAULT 0,
14
+ last_http_status integer,
15
+ failure_code text,
16
+ created_at timestamptz NOT NULL,
17
+ updated_at timestamptz NOT NULL,
18
+ expires_at timestamptz NOT NULL,
19
+ next_attempt_at timestamptz,
20
+ lease_token text,
21
+ leased_until timestamptz
22
+ );
23
+
24
+ CREATE INDEX app_notification_webhook_jobs_due_idx ON app_notification_webhook_jobs (next_attempt_at, id) WHERE status = 'queued';
25
+ CREATE INDEX app_notification_webhook_jobs_event_idx ON app_notification_webhook_jobs (event_id) WHERE status = 'queued';
26
+ CREATE INDEX app_notification_webhook_jobs_activity_idx ON app_notification_webhook_jobs (environment_id, organization_id, created_at DESC, id DESC);
27
+ CREATE INDEX app_notification_webhook_jobs_content_expiry_idx ON app_notification_webhook_jobs (expires_at) WHERE body_enc IS NOT NULL;
28
+ CREATE INDEX app_notification_webhook_jobs_retention_idx ON app_notification_webhook_jobs (updated_at) WHERE body_enc IS NULL;
@@ -0,0 +1,11 @@
1
+ ALTER TABLE invites ADD COLUMN revoked_at timestamptz;
2
+ ALTER TABLE invites ADD COLUMN last_mail_requested_at timestamptz;
3
+
4
+ CREATE INDEX invites_open_by_org ON invites (org_id, created_at DESC, id COLLATE "C" DESC)
5
+ WHERE accepted_at IS NULL AND revoked_at IS NULL;
6
+
7
+ CREATE INDEX invites_open_by_recipient ON invites (org_id, lower(email), expires_at)
8
+ WHERE accepted_at IS NULL AND revoked_at IS NULL;
9
+
10
+ CREATE INDEX email_challenges_by_account_and_purpose
11
+ ON email_challenges (user_id, purpose, created_at DESC);
@@ -0,0 +1,7 @@
1
+ ALTER TABLE platform_waitlist ADD COLUMN confirmation_pending boolean NOT NULL DEFAULT false;
2
+ ALTER TABLE platform_waitlist ADD COLUMN confirmation_claimed_at timestamptz;
3
+
4
+ ALTER TABLE app_waitlist ADD COLUMN confirmation_pending boolean NOT NULL DEFAULT false;
5
+ ALTER TABLE app_waitlist ADD COLUMN confirmation_claimed_at timestamptz;
6
+
7
+ CREATE INDEX directory_challenges_recovery_idx ON directory_challenges (environment_id, identity_id, purpose, created_at DESC);
@@ -0,0 +1,45 @@
1
+ CREATE TABLE hosted_human_sessions (
2
+ id text PRIMARY KEY,
3
+ client_id text NOT NULL,
4
+ app_id text NOT NULL,
5
+ environment_id text NOT NULL,
6
+ subject_id text NOT NULL,
7
+ organization_id text,
8
+ workspace_id text,
9
+ audience text NOT NULL,
10
+ created_at timestamptz NOT NULL,
11
+ expires_at timestamptz NOT NULL CHECK (expires_at > created_at),
12
+ revoked_at timestamptz CHECK (revoked_at >= created_at),
13
+ CHECK (organization_id IS NOT NULL OR workspace_id IS NOT NULL),
14
+ FOREIGN KEY (app_id, environment_id) REFERENCES app_environments (app_id, id) ON DELETE CASCADE,
15
+ FOREIGN KEY (environment_id, subject_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE,
16
+ FOREIGN KEY (environment_id, organization_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE,
17
+ FOREIGN KEY (environment_id, workspace_id) REFERENCES app_workspaces (environment_id, id) ON DELETE CASCADE
18
+ );
19
+
20
+ CREATE INDEX hosted_human_sessions_environment_idx ON hosted_human_sessions (environment_id);
21
+ CREATE INDEX hosted_human_sessions_expiry_idx ON hosted_human_sessions (expires_at);
22
+
23
+ CREATE TABLE hosted_human_refresh_tokens (
24
+ token_hash text PRIMARY KEY,
25
+ session_id text NOT NULL REFERENCES hosted_human_sessions(id) ON DELETE CASCADE,
26
+ used_at timestamptz
27
+ );
28
+
29
+ CREATE INDEX hosted_human_refresh_tokens_session_idx ON hosted_human_refresh_tokens (session_id);
30
+ CREATE UNIQUE INDEX hosted_human_refresh_tokens_active_idx ON hosted_human_refresh_tokens (session_id) WHERE used_at IS NULL;
31
+
32
+ CREATE TABLE hosted_human_selections (
33
+ token_hash text PRIMARY KEY,
34
+ client_id text NOT NULL,
35
+ app_id text NOT NULL,
36
+ environment_id text NOT NULL,
37
+ subject_id text NOT NULL,
38
+ created_at timestamptz NOT NULL,
39
+ expires_at timestamptz NOT NULL CHECK (expires_at > created_at),
40
+ FOREIGN KEY (app_id, environment_id) REFERENCES app_environments (app_id, id) ON DELETE CASCADE,
41
+ FOREIGN KEY (environment_id, subject_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE
42
+ );
43
+
44
+ CREATE INDEX hosted_human_selections_environment_idx ON hosted_human_selections (environment_id);
45
+ CREATE INDEX hosted_human_selections_expiry_idx ON hosted_human_selections (expires_at);
@@ -0,0 +1,35 @@
1
+ CREATE TABLE hosted_human_clients (
2
+ id text PRIMARY KEY,
3
+ app_id text NOT NULL REFERENCES applications (id) ON DELETE CASCADE,
4
+ environment_id text NOT NULL REFERENCES app_environments (id) ON DELETE CASCADE,
5
+ org_id text NOT NULL,
6
+ name text NOT NULL,
7
+ redirect_uri text NOT NULL,
8
+ audience text NOT NULL,
9
+ secret_hash text NOT NULL CHECK (secret_hash ~ '^[0-9a-f]{64}$'),
10
+ created_at timestamptz NOT NULL,
11
+ revoked_at timestamptz
12
+ );
13
+
14
+ CREATE INDEX hosted_human_clients_scope_idx ON hosted_human_clients (app_id, environment_id);
15
+
16
+ CREATE TABLE hosted_human_codes (
17
+ code_hash text PRIMARY KEY CHECK (code_hash ~ '^[0-9a-f]{64}$'),
18
+ client_id text NOT NULL REFERENCES hosted_human_clients (id) ON DELETE CASCADE,
19
+ app_id text NOT NULL REFERENCES applications (id) ON DELETE CASCADE,
20
+ environment_id text NOT NULL REFERENCES app_environments (id) ON DELETE CASCADE,
21
+ org_id text NOT NULL,
22
+ redirect_uri text NOT NULL,
23
+ code_challenge text NOT NULL CHECK (code_challenge ~ '^[A-Za-z0-9_-]{43}$'),
24
+ identity_id text NOT NULL REFERENCES app_identities (id) ON DELETE CASCADE,
25
+ provider text NOT NULL,
26
+ created_at timestamptz NOT NULL,
27
+ expires_at timestamptz NOT NULL CHECK (expires_at > created_at)
28
+ );
29
+
30
+ CREATE INDEX hosted_human_codes_expiry_idx ON hosted_human_codes (expires_at);
31
+ CREATE INDEX hosted_human_codes_app_idx ON hosted_human_codes (app_id);
32
+ CREATE INDEX hosted_human_codes_client_idx ON hosted_human_codes (client_id);
33
+
34
+ ALTER TABLE sso_logins ADD COLUMN human_binding jsonb;
35
+ ALTER TABLE hosted_sso_profiles ADD COLUMN human_binding jsonb;
@@ -0,0 +1,9 @@
1
+ ALTER TABLE app_waitlist
2
+ ADD COLUMN invitation_workspace_id text,
3
+ ADD COLUMN invitation_role_id text,
4
+ ADD COLUMN pending_workspace_identity_id text,
5
+ ADD CHECK ((invitation_workspace_id IS NULL) = (invitation_role_id IS NULL)),
6
+ ADD CHECK (pending_workspace_identity_id IS NULL OR (status = 'invited' AND invitation_workspace_id IS NOT NULL));
7
+
8
+ ALTER TABLE app_workspace_memberships
9
+ ADD COLUMN role_id text REFERENCES app_product_roles (id) ON DELETE SET NULL;
@@ -0,0 +1,16 @@
1
+ ALTER TABLE app_identities
2
+ ADD COLUMN email_proof_at timestamptz,
3
+ ADD CHECK (email_proof_at IS NULL OR (kind = 'user' AND email IS NOT NULL AND email_verified_at IS NOT NULL));
4
+
5
+ UPDATE app_identities AS identity
6
+ SET email_proof_at = identity.email_verified_at
7
+ WHERE identity.kind = 'user' AND identity.email IS NOT NULL AND identity.email_verified_at IS NOT NULL
8
+ AND EXISTS (
9
+ SELECT 1 FROM directory_challenges AS challenge
10
+ WHERE challenge.identity_id = identity.id
11
+ AND challenge.environment_id = identity.environment_id
12
+ AND lower(challenge.email) = lower(identity.email)
13
+ AND challenge.purpose = 'verify'
14
+ AND challenge.used_at = identity.email_verified_at
15
+ AND challenge.used_at < challenge.expires_at
16
+ );
@@ -0,0 +1 @@
1
+ ALTER TABLE sso_exchanges ADD COLUMN redirect_uri text;
@@ -0,0 +1,30 @@
1
+ -- Some databases recorded migration 008 without the application provider table.
2
+ -- Keep existing provider connections in place and only restore legacy links when the table is missing.
3
+ CREATE TEMP TABLE keyrox_app_sign_in_repair ON COMMIT DROP AS
4
+ SELECT to_regclass('app_sign_in_connections') IS NULL AS table_was_missing;
5
+
6
+ CREATE TABLE IF NOT EXISTS app_sign_in_connections (
7
+ id text PRIMARY KEY,
8
+ app_id text NOT NULL REFERENCES applications (id) ON DELETE CASCADE,
9
+ provider text NOT NULL,
10
+ client_id text NOT NULL,
11
+ client_secret_enc text NOT NULL,
12
+ directory_id text,
13
+ issuer text,
14
+ created_at timestamptz NOT NULL,
15
+ legacy_connection_id text,
16
+ UNIQUE (app_id, provider)
17
+ );
18
+
19
+ INSERT INTO app_sign_in_connections (
20
+ id, app_id, provider, client_id, client_secret_enc, directory_id, issuer, created_at, legacy_connection_id
21
+ )
22
+ SELECT
23
+ concat('sic_migrated_', length(connection.id), ':', connection.id, ':', application.id),
24
+ application.id, connection.provider, connection.client_id, connection.client_secret_enc,
25
+ connection.directory_id, connection.issuer, connection.created_at, connection.id
26
+ FROM sign_in_connections AS connection
27
+ JOIN applications AS application ON application.org_id = connection.org_id
28
+ JOIN app_sign_in AS settings ON settings.app_id = application.id AND connection.provider = ANY(settings.providers)
29
+ CROSS JOIN pg_temp.keyrox_app_sign_in_repair AS repair
30
+ WHERE repair.table_was_missing;
@@ -0,0 +1,140 @@
1
+ -- Check directory relationships that were previously recorded without validating existing rows.
2
+ ALTER TABLE app_permissions VALIDATE CONSTRAINT app_permissions_environment_id_fkey;
3
+ ALTER TABLE app_product_roles VALIDATE CONSTRAINT app_product_roles_environment_id_fkey;
4
+ ALTER TABLE app_groups VALIDATE CONSTRAINT app_groups_environment_id_fkey;
5
+ ALTER TABLE app_role_bindings VALIDATE CONSTRAINT app_role_bindings_environment_id_fkey;
6
+ ALTER TABLE app_features VALIDATE CONSTRAINT app_features_environment_id_fkey;
7
+ ALTER TABLE app_waitlist VALIDATE CONSTRAINT app_waitlist_environment_id_fkey;
8
+
9
+ ALTER TABLE app_identities
10
+ ADD COLUMN organization_key text GENERATED ALWAYS AS (CASE WHEN kind = 'organization' THEN id END) STORED;
11
+ ALTER TABLE app_identities
12
+ ADD CONSTRAINT app_identities_environment_organization_key UNIQUE (environment_id, organization_key);
13
+
14
+ ALTER TABLE app_identity_memberships
15
+ ADD CONSTRAINT app_identity_memberships_relation_check CHECK (relation IN ('owner', 'member'));
16
+ ALTER TABLE app_identity_memberships
17
+ ADD CONSTRAINT app_identity_memberships_environment_identity_fkey
18
+ FOREIGN KEY (environment_id, identity_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE;
19
+ ALTER TABLE app_identity_memberships
20
+ ADD CONSTRAINT app_identity_memberships_environment_organization_fkey
21
+ FOREIGN KEY (environment_id, organization_id) REFERENCES app_identities (environment_id, organization_key) ON DELETE CASCADE;
22
+
23
+ -- A directory model that includes organizations gives every person an organization.
24
+ -- A model that includes workspaces gives every person a workspace under that organization when one is required.
25
+ WITH missing AS (
26
+ SELECT
27
+ 'idn_' || rtrim(translate(encode(decode(replace(gen_random_uuid()::text, '-', ''), 'hex'), 'base64'), '+/', '-_'), '=') AS organization_id,
28
+ 'dms_' || rtrim(translate(encode(decode(replace(gen_random_uuid()::text, '-', ''), 'hex'), 'base64'), '+/', '-_'), '=') AS membership_id,
29
+ person.app_id,
30
+ person.environment_id,
31
+ person.org_id,
32
+ person.id AS identity_id,
33
+ person.display_name
34
+ FROM app_identities AS person
35
+ JOIN applications AS application ON application.id = person.app_id AND application.trashed_at IS NULL
36
+ WHERE person.kind IN ('user', 'account')
37
+ AND application.directory_model IN ('organizations', 'organizations-and-workspaces')
38
+ AND length(btrim(person.display_name)) > 0
39
+ AND NOT EXISTS (
40
+ SELECT 1 FROM app_identity_memberships AS membership
41
+ WHERE membership.environment_id = person.environment_id AND membership.identity_id = person.id
42
+ )
43
+ ),
44
+ created AS (
45
+ INSERT INTO app_identities (
46
+ id, app_id, environment_id, org_id, kind, email, password_hash, display_name, status,
47
+ email_verified_at, metadata, created_at, updated_at, email_proof_at
48
+ )
49
+ SELECT organization_id, app_id, environment_id, org_id, 'organization', NULL, NULL, display_name, 'active',
50
+ NULL, '{}'::jsonb, now(), now(), NULL
51
+ FROM missing
52
+ RETURNING id
53
+ )
54
+ INSERT INTO app_identity_memberships (id, environment_id, identity_id, organization_id, relation, created_at)
55
+ SELECT membership_id, environment_id, identity_id, organization_id, 'owner', now()
56
+ FROM missing
57
+ WHERE EXISTS (SELECT 1 FROM created);
58
+
59
+ WITH missing AS (
60
+ SELECT
61
+ 'idn_' || rtrim(translate(encode(decode(replace(gen_random_uuid()::text, '-', ''), 'hex'), 'base64'), '+/', '-_'), '=') AS organization_id,
62
+ workspace.id AS workspace_id,
63
+ workspace.environment_id,
64
+ workspace.app_id,
65
+ application.org_id,
66
+ workspace.name AS display_name
67
+ FROM app_workspaces AS workspace
68
+ JOIN applications AS application ON application.id = workspace.app_id AND application.trashed_at IS NULL
69
+ WHERE application.directory_model = 'organizations-and-workspaces' AND workspace.organization_id IS NULL
70
+ ),
71
+ created AS (
72
+ INSERT INTO app_identities (
73
+ id, app_id, environment_id, org_id, kind, email, password_hash, display_name, status,
74
+ email_verified_at, metadata, created_at, updated_at, email_proof_at
75
+ )
76
+ SELECT organization_id, app_id, environment_id, org_id, 'organization', NULL, NULL, display_name, 'active',
77
+ NULL, '{}'::jsonb, now(), now(), NULL
78
+ FROM missing
79
+ RETURNING id
80
+ ),
81
+ owners AS (
82
+ INSERT INTO app_identity_memberships (id, environment_id, identity_id, organization_id, relation, created_at)
83
+ SELECT
84
+ 'dms_' || rtrim(translate(encode(decode(replace(gen_random_uuid()::text, '-', ''), 'hex'), 'base64'), '+/', '-_'), '='),
85
+ missing.environment_id, owner.identity_id, missing.organization_id, 'member', now()
86
+ FROM missing
87
+ JOIN app_workspace_memberships AS owner
88
+ ON owner.environment_id = missing.environment_id AND owner.workspace_id = missing.workspace_id AND owner.relation = 'owner'
89
+ WHERE NOT EXISTS (
90
+ SELECT 1 FROM app_identity_memberships AS membership
91
+ WHERE membership.environment_id = missing.environment_id
92
+ AND membership.identity_id = owner.identity_id
93
+ AND membership.organization_id = missing.organization_id
94
+ )
95
+ RETURNING id
96
+ )
97
+ UPDATE app_workspaces AS workspace
98
+ SET organization_id = missing.organization_id, updated_at = now()
99
+ FROM missing
100
+ WHERE workspace.id = missing.workspace_id AND workspace.environment_id = missing.environment_id
101
+ AND (SELECT count(*) FROM created) >= 0
102
+ AND (SELECT count(*) FROM owners) >= 0;
103
+
104
+ WITH missing AS (
105
+ SELECT
106
+ 'wsp_' || rtrim(translate(encode(decode(replace(gen_random_uuid()::text, '-', ''), 'hex'), 'base64'), '+/', '-_'), '=') AS workspace_id,
107
+ 'wsm_' || rtrim(translate(encode(decode(replace(gen_random_uuid()::text, '-', ''), 'hex'), 'base64'), '+/', '-_'), '=') AS membership_id,
108
+ person.app_id,
109
+ person.environment_id,
110
+ parent.organization_id,
111
+ person.id AS identity_id,
112
+ person.display_name
113
+ FROM app_identities AS person
114
+ JOIN applications AS application ON application.id = person.app_id AND application.trashed_at IS NULL
115
+ LEFT JOIN LATERAL (
116
+ SELECT membership.organization_id
117
+ FROM app_identity_memberships AS membership
118
+ WHERE membership.environment_id = person.environment_id AND membership.identity_id = person.id
119
+ ORDER BY membership.created_at, membership.id
120
+ LIMIT 1
121
+ ) AS parent ON application.directory_model = 'organizations-and-workspaces'
122
+ WHERE person.kind IN ('user', 'account')
123
+ AND application.directory_model IN ('workspaces', 'organizations-and-workspaces')
124
+ AND length(btrim(person.display_name)) > 0
125
+ AND NOT EXISTS (
126
+ SELECT 1 FROM app_workspace_memberships AS membership
127
+ WHERE membership.environment_id = person.environment_id AND membership.identity_id = person.id
128
+ )
129
+ AND (application.directory_model = 'workspaces' OR parent.organization_id IS NOT NULL)
130
+ ),
131
+ created AS (
132
+ INSERT INTO app_workspaces (id, app_id, environment_id, organization_id, name, status, created_at, updated_at)
133
+ SELECT workspace_id, app_id, environment_id, organization_id, display_name, 'active', now(), now()
134
+ FROM missing
135
+ RETURNING id
136
+ )
137
+ INSERT INTO app_workspace_memberships (id, environment_id, workspace_id, identity_id, relation, created_at)
138
+ SELECT membership_id, environment_id, workspace_id, identity_id, 'owner', now()
139
+ FROM missing
140
+ WHERE EXISTS (SELECT 1 FROM created);
@@ -0,0 +1,7 @@
1
+ -- An organization contact address is not a sign-in identity. One person can belong to
2
+ -- many organizations, and those organizations may keep that same contact email.
3
+ DROP INDEX IF EXISTS app_identities_email_idx;
4
+
5
+ CREATE UNIQUE INDEX app_identities_signin_email_idx
6
+ ON app_identities (environment_id, lower(email))
7
+ WHERE email IS NOT NULL AND kind <> 'organization';
@@ -0,0 +1,35 @@
1
+ ALTER TABLE app_product_roles ADD COLUMN owner_access boolean NOT NULL DEFAULT false;
2
+
3
+ CREATE UNIQUE INDEX app_product_roles_owner_access_idx ON app_product_roles (environment_id) WHERE owner_access;
4
+
5
+ CREATE TABLE app_access_checks (
6
+ id text PRIMARY KEY,
7
+ environment_id text NOT NULL,
8
+ client_id text NOT NULL,
9
+ identity_id text NOT NULL,
10
+ workspace_id text,
11
+ organization_id text,
12
+ role_id text NOT NULL,
13
+ reference text NOT NULL,
14
+ audit_id text NOT NULL,
15
+ created_at timestamptz NOT NULL,
16
+ FOREIGN KEY (environment_id, identity_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE,
17
+ FOREIGN KEY (environment_id, workspace_id) REFERENCES app_workspaces (environment_id, id) ON DELETE CASCADE,
18
+ FOREIGN KEY (environment_id, organization_id) REFERENCES app_identities (environment_id, id) ON DELETE CASCADE
19
+ );
20
+
21
+ CREATE INDEX app_access_checks_scope_idx ON app_access_checks (environment_id, identity_id);
22
+
23
+ DO $$
24
+ DECLARE
25
+ constraint_name text;
26
+ BEGIN
27
+ SELECT con.conname INTO constraint_name
28
+ FROM pg_constraint con
29
+ JOIN pg_class rel ON rel.oid = con.conrelid
30
+ WHERE rel.relname = 'hosted_human_sessions' AND con.contype = 'c'
31
+ AND pg_get_constraintdef(con.oid) ILIKE '%organization_id IS NOT NULL%workspace_id IS NOT NULL%';
32
+ IF constraint_name IS NOT NULL THEN
33
+ EXECUTE format('ALTER TABLE hosted_human_sessions DROP CONSTRAINT %I', constraint_name);
34
+ END IF;
35
+ END $$;
@@ -0,0 +1,28 @@
1
+ ALTER TABLE app_waitlist
2
+ ADD COLUMN invitation_organization_id text,
3
+ ADD COLUMN invitation_delivery text,
4
+ ADD COLUMN invitation_audit_id text,
5
+ ADD COLUMN revoked_at timestamptz;
6
+
7
+ ALTER TABLE sso_logins
8
+ ADD COLUMN invitation_token_hash text;
9
+
10
+ DO $$
11
+ DECLARE
12
+ constraint_name text;
13
+ BEGIN
14
+ SELECT con.conname INTO constraint_name
15
+ FROM pg_constraint con
16
+ JOIN pg_class rel ON rel.oid = con.conrelid
17
+ WHERE rel.relname = 'app_waitlist' AND con.contype = 'c'
18
+ AND pg_get_constraintdef(con.oid) ILIKE '%invitation_workspace_id IS NULL%invitation_role_id IS NULL%';
19
+ IF constraint_name IS NOT NULL THEN
20
+ EXECUTE format('ALTER TABLE app_waitlist DROP CONSTRAINT %I', constraint_name);
21
+ END IF;
22
+ END $$;
23
+
24
+ ALTER TABLE app_waitlist
25
+ ADD CHECK (invitation_delivery IS NULL OR invitation_delivery IN ('email', 'link')),
26
+ ADD CHECK (invitation_organization_id IS NULL OR invitation_workspace_id IS NULL),
27
+ ADD CHECK (invitation_workspace_id IS NULL OR invitation_role_id IS NOT NULL),
28
+ ADD CHECK (invitation_delivery IS DISTINCT FROM 'link' OR invitation_role_id IS NOT NULL);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x12i/keyrox-store-pg",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "PostgreSQL repository for Keyrox accounts, grants, and tokens",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -19,8 +19,8 @@
19
19
  "lint": "echo ok"
20
20
  },
21
21
  "dependencies": {
22
- "@x12i/keyrox-store": "0.1.0",
23
- "@x12i/keyrox-types": "0.1.0",
22
+ "@x12i/keyrox-store": "0.2.1",
23
+ "@x12i/keyrox-types": "0.2.1",
24
24
  "pg": "^8.16.3"
25
25
  },
26
26
  "devDependencies": {