@logto/schemas 1.28.0 → 1.30.0

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 (129) hide show
  1. package/alterations/1.29.0-1748832174-add-webauthn-related-origins.ts +20 -0
  2. package/alterations/1.29.0-1749005587-user-sso-identities-table-add-updated-at-column.ts +31 -0
  3. package/alterations/1.29.0-1749026308-add-oidc-session-extension-table.ts +41 -0
  4. package/alterations/1.29.0-1749523818-add-custom-profile-fields.ts +58 -0
  5. package/alterations/1.29.0-1749724664-drop-sie-order-constraint-from-custom-profile-fields.ts +20 -0
  6. package/alterations/1.29.0-1750663091-change-user-password-encrypted-length.ts +18 -0
  7. package/alterations/1.29.0-1750744518-add-secrets-table.ts +50 -0
  8. package/alterations/1.29.0-1750744539-add-secret-connector-relations-table.ts +109 -0
  9. package/alterations/1.30.0-1750744685-add-triggers-to-delete-secrets-on-social-identities-deletion.ts +81 -0
  10. package/alterations/1.30.0-1750748516-add-enable-token-storage-column-to-connectors-table.ts +20 -0
  11. package/alterations/1.30.0-1751255436-split-secret-connector-relatioins-table.ts +359 -0
  12. package/alterations/1.30.0-1751337183-add-require-mfa-on-sign-in-to-users.ts +20 -0
  13. package/alterations/1.30.0-1751400000-move-require-mfa-on-sign-in-to-logto-config.ts +21 -0
  14. package/alterations/1.30.0-1751529530-add-enable-token-storage-column-to-sso-connectors-table.ts +20 -0
  15. package/alterations/1.30.0-1752630302-alterate-enable-column-default-value-in-account-centers-table.ts +20 -0
  16. package/alterations/1.30.0-1753669579-add-organization-user-relations-foreign-key.ts +46 -0
  17. package/alterations-js/1.29.0-1748832174-add-webauthn-related-origins.js +16 -0
  18. package/alterations-js/1.29.0-1749005587-user-sso-identities-table-add-updated-at-column.js +25 -0
  19. package/alterations-js/1.29.0-1749026308-add-oidc-session-extension-table.js +33 -0
  20. package/alterations-js/1.29.0-1749523818-add-custom-profile-fields.js +52 -0
  21. package/alterations-js/1.29.0-1749724664-drop-sie-order-constraint-from-custom-profile-fields.js +16 -0
  22. package/alterations-js/1.29.0-1750663091-change-user-password-encrypted-length.js +14 -0
  23. package/alterations-js/1.29.0-1750744518-add-secrets-table.js +42 -0
  24. package/alterations-js/1.29.0-1750744539-add-secret-connector-relations-table.js +99 -0
  25. package/alterations-js/1.30.0-1750744685-add-triggers-to-delete-secrets-on-social-identities-deletion.js +76 -0
  26. package/alterations-js/1.30.0-1750748516-add-enable-token-storage-column-to-connectors-table.js +16 -0
  27. package/alterations-js/1.30.0-1751255436-split-secret-connector-relatioins-table.js +338 -0
  28. package/alterations-js/1.30.0-1751337183-add-require-mfa-on-sign-in-to-users.js +16 -0
  29. package/alterations-js/1.30.0-1751400000-move-require-mfa-on-sign-in-to-logto-config.js +17 -0
  30. package/alterations-js/1.30.0-1751529530-add-enable-token-storage-column-to-sso-connectors-table.js +16 -0
  31. package/alterations-js/1.30.0-1752630302-alterate-enable-column-default-value-in-account-centers-table.js +16 -0
  32. package/alterations-js/1.30.0-1753669579-add-organization-user-relations-foreign-key.js +38 -0
  33. package/lib/consts/oidc.d.ts +9 -1
  34. package/lib/consts/oidc.js +5 -0
  35. package/lib/db-entries/account-center.d.ts +4 -2
  36. package/lib/db-entries/account-center.js +5 -1
  37. package/lib/db-entries/connector.d.ts +5 -1
  38. package/lib/db-entries/connector.js +4 -0
  39. package/lib/db-entries/custom-profile-field.d.ts +32 -0
  40. package/lib/db-entries/custom-profile-field.js +58 -0
  41. package/lib/db-entries/index.d.ts +5 -0
  42. package/lib/db-entries/index.js +5 -0
  43. package/lib/db-entries/oidc-session-extension.d.ts +24 -0
  44. package/lib/db-entries/oidc-session-extension.js +42 -0
  45. package/lib/db-entries/secret-enterprise-sso-connector-relation.d.ts +28 -0
  46. package/lib/db-entries/secret-enterprise-sso-connector-relation.js +37 -0
  47. package/lib/db-entries/secret-social-connector-relation.d.ts +28 -0
  48. package/lib/db-entries/secret-social-connector-relation.js +37 -0
  49. package/lib/db-entries/secret.d.ts +44 -0
  50. package/lib/db-entries/secret.js +62 -0
  51. package/lib/db-entries/sso-connector.d.ts +5 -1
  52. package/lib/db-entries/sso-connector.js +4 -0
  53. package/lib/db-entries/user-sso-identity.d.ts +5 -1
  54. package/lib/db-entries/user-sso-identity.js +4 -0
  55. package/lib/db-entries/user.js +2 -2
  56. package/lib/foundations/jsonb-types/account-centers.d.ts +5 -0
  57. package/lib/foundations/jsonb-types/account-centers.js +2 -0
  58. package/lib/foundations/jsonb-types/custom-profile-fields.d.ts +441 -0
  59. package/lib/foundations/jsonb-types/custom-profile-fields.js +44 -0
  60. package/lib/foundations/jsonb-types/index.d.ts +2 -1
  61. package/lib/foundations/jsonb-types/index.js +2 -1
  62. package/lib/foundations/jsonb-types/secrets.d.ts +11 -0
  63. package/lib/foundations/jsonb-types/secrets.js +15 -0
  64. package/lib/foundations/jsonb-types/sign-in-experience.d.ts +3 -1
  65. package/lib/foundations/jsonb-types/sign-in-experience.js +2 -0
  66. package/lib/foundations/jsonb-types/users.d.ts +126 -0
  67. package/lib/foundations/jsonb-types/users.js +22 -10
  68. package/lib/types/connector.d.ts +39 -0
  69. package/lib/types/connector.js +1 -0
  70. package/lib/types/consent.d.ts +44 -0
  71. package/lib/types/custom-profile-fields.d.ts +2587 -0
  72. package/lib/types/custom-profile-fields.js +159 -0
  73. package/lib/types/index.d.ts +4 -0
  74. package/lib/types/index.js +4 -0
  75. package/lib/types/interactions.d.ts +181 -1
  76. package/lib/types/interactions.js +49 -1
  77. package/lib/types/log/interaction.d.ts +2 -1
  78. package/lib/types/logto-config/index.d.ts +1139 -18
  79. package/lib/types/logto-config/jwt-customizer.d.ts +2529 -32
  80. package/lib/types/logto-config/jwt-customizer.js +55 -1
  81. package/lib/types/logto-config/oidc-provider.d.ts +6 -6
  82. package/lib/types/mfa.d.ts +10 -10
  83. package/lib/types/secrets.d.ts +436 -0
  84. package/lib/types/secrets.js +73 -0
  85. package/lib/types/sign-in-experience.d.ts +21 -3
  86. package/lib/types/sign-in-experience.js +3 -1
  87. package/lib/types/sso-connector.d.ts +28 -2
  88. package/lib/types/sso-connector.js +3 -0
  89. package/lib/types/tenant.d.ts +1 -0
  90. package/lib/types/tenant.js +1 -0
  91. package/lib/types/user-logto-config.d.ts +45 -0
  92. package/lib/types/user-logto-config.js +18 -0
  93. package/lib/types/user.d.ts +626 -0
  94. package/lib/types/user.js +17 -1
  95. package/lib/types/verification-records/backup-code-verification.d.ts +47 -0
  96. package/lib/types/verification-records/backup-code-verification.js +12 -0
  97. package/lib/types/verification-records/code-verification.d.ts +89 -0
  98. package/lib/types/verification-records/code-verification.js +22 -0
  99. package/lib/types/verification-records/enterprise-sso-verification.d.ts +213 -0
  100. package/lib/types/verification-records/enterprise-sso-verification.js +15 -0
  101. package/lib/types/verification-records/index.d.ts +16 -0
  102. package/lib/types/verification-records/index.js +16 -0
  103. package/lib/types/verification-records/new-password-identity-verification.d.ts +85 -0
  104. package/lib/types/verification-records/new-password-identity-verification.js +20 -0
  105. package/lib/types/verification-records/one-time-token-verification.d.ts +55 -0
  106. package/lib/types/verification-records/one-time-token-verification.js +13 -0
  107. package/lib/types/verification-records/password-verification.d.ts +40 -0
  108. package/lib/types/verification-records/password-verification.js +9 -0
  109. package/lib/types/verification-records/social-verification.d.ts +270 -0
  110. package/lib/types/verification-records/social-verification.js +16 -0
  111. package/lib/types/verification-records/totp-verification.d.ts +47 -0
  112. package/lib/types/verification-records/totp-verification.js +12 -0
  113. package/lib/types/verification-records/web-authn-verification.d.ts +124 -0
  114. package/lib/types/verification-records/web-authn-verification.js +17 -0
  115. package/package.json +6 -6
  116. package/tables/account_centers.sql +2 -1
  117. package/tables/connectors.sql +4 -0
  118. package/tables/custom_profile_fields.sql +31 -0
  119. package/tables/oidc_model_instances.sql +2 -0
  120. package/tables/oidc_session_extensions.sql +18 -0
  121. package/tables/organization_user_relations.sql +4 -1
  122. package/tables/secret_enterprise_sso_connector_relations.sql +60 -0
  123. package/tables/secret_social_connector_relations.sql +75 -0
  124. package/tables/secrets.sql +26 -0
  125. package/tables/sso_connectors.sql +2 -0
  126. package/tables/user_sso_identities.sql +8 -0
  127. package/tables/users.sql +3 -2
  128. /package/lib/{foundations/jsonb-types/verification-records.d.ts → types/verification-records/verification-type.d.ts} +0 -0
  129. /package/lib/{foundations/jsonb-types/verification-records.js → types/verification-records/verification-type.js} +0 -0
@@ -0,0 +1,20 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ const alteration: AlterationScript = {
6
+ up: async (pool) => {
7
+ await pool.query(sql`
8
+ alter table account_centers
9
+ add column webauthn_related_origins jsonb not null default '[]'::jsonb;
10
+ `);
11
+ },
12
+ down: async (pool) => {
13
+ await pool.query(sql`
14
+ alter table account_centers
15
+ drop column webauthn_related_origins;
16
+ `);
17
+ },
18
+ };
19
+
20
+ export default alteration;
@@ -0,0 +1,31 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ const alteration: AlterationScript = {
6
+ up: async (pool) => {
7
+ await pool.query(sql`
8
+ alter table user_sso_identities
9
+ add column updated_at timestamptz not null default(now());
10
+ `);
11
+
12
+ await pool.query(sql`
13
+ create trigger set_updated_at
14
+ before update on user_sso_identities
15
+ for each row
16
+ execute procedure set_updated_at();
17
+ `);
18
+ },
19
+ down: async (pool) => {
20
+ await pool.query(sql`
21
+ drop trigger set_updated_at on user_sso_identities;
22
+ `);
23
+
24
+ await pool.query(sql`
25
+ alter table user_sso_identities
26
+ drop column updated_at;
27
+ `);
28
+ },
29
+ };
30
+
31
+ export default alteration;
@@ -0,0 +1,41 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js';
6
+
7
+ const alteration: AlterationScript = {
8
+ up: async (pool) => {
9
+ await pool.query(sql`
10
+ create table oidc_session_extensions (
11
+ tenant_id varchar(21) not null
12
+ references tenants (id) on update cascade on delete cascade,
13
+ session_uid varchar(128) not null,
14
+ account_id varchar(12) not null
15
+ references users (id) on update cascade on delete cascade,
16
+ last_submission jsonb /* @use JsonObject */ not null default '{}'::jsonb,
17
+ created_at timestamptz not null default(now()),
18
+ updated_at timestamptz not null default(now()),
19
+ primary key (tenant_id, session_uid)
20
+ );
21
+ `);
22
+
23
+ await pool.query(sql`
24
+ create trigger set_updated_at
25
+ before update on oidc_session_extensions
26
+ for each row
27
+ execute procedure set_updated_at();
28
+ `);
29
+
30
+ await applyTableRls(pool, 'oidc_session_extensions');
31
+ },
32
+ down: async (pool) => {
33
+ await dropTableRls(pool, 'oidc_session_extensions');
34
+
35
+ await pool.query(sql`
36
+ drop table oidc_session_extensions;
37
+ `);
38
+ },
39
+ };
40
+
41
+ export default alteration;
@@ -0,0 +1,58 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js';
6
+
7
+ const alteration: AlterationScript = {
8
+ up: async (pool) => {
9
+ await pool.query(sql`
10
+ create table custom_profile_fields (
11
+ tenant_id varchar(21) not null
12
+ references tenants (id) on update cascade on delete cascade,
13
+ id varchar(21) not null,
14
+ name varchar(128) not null,
15
+ type varchar(128) not null /* @use CustomProfileFieldType */,
16
+ label varchar(128) not null default '',
17
+ description varchar(256),
18
+ required boolean not null default false,
19
+ config jsonb /* @use CustomProfileFieldConfig */ not null default '{}'::jsonb,
20
+ created_at timestamptz not null default(now()),
21
+ sie_order int2 not null default 0,
22
+ primary key (id),
23
+ constraint custom_profile_fields__name
24
+ unique (tenant_id, name),
25
+ constraint custom_profile_fields__sie_order
26
+ unique (tenant_id, sie_order)
27
+ );
28
+
29
+ create or replace function custom_profile_fields__increment_sie_order() returns trigger as
30
+ $$ begin
31
+ new.sie_order = (
32
+ select coalesce(max(sie_order), 0)
33
+ from custom_profile_fields
34
+ where tenant_id = (
35
+ select id from tenants where db_user = current_user
36
+ )
37
+ ) + 1;
38
+ return new;
39
+ end; $$ language plpgsql;
40
+
41
+ create trigger custom_profile_fields__increment_sie_order before insert on custom_profile_fields
42
+ for each row execute procedure custom_profile_fields__increment_sie_order();
43
+ `);
44
+
45
+ await applyTableRls(pool, 'custom_profile_fields');
46
+ },
47
+ down: async (pool) => {
48
+ await dropTableRls(pool, 'custom_profile_fields');
49
+ await pool.query(sql`
50
+ drop trigger custom_profile_fields__increment_sie_order on custom_profile_fields;
51
+ drop function custom_profile_fields__increment_sie_order;
52
+ drop table if exists custom_profile_fields;
53
+ drop type if exists custom_profile_field_type;
54
+ `);
55
+ },
56
+ };
57
+
58
+ export default alteration;
@@ -0,0 +1,20 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ const alteration: AlterationScript = {
6
+ up: async (pool) => {
7
+ await pool.query(sql`
8
+ alter table custom_profile_fields
9
+ drop constraint custom_profile_fields__sie_order
10
+ `);
11
+ },
12
+ down: async (pool) => {
13
+ await pool.query(sql`
14
+ alter table custom_profile_fields
15
+ add constraint custom_profile_fields__sie_order unique (tenant_id, sie_order)
16
+ `);
17
+ },
18
+ };
19
+
20
+ export default alteration;
@@ -0,0 +1,18 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ const alteration: AlterationScript = {
6
+ up: async (pool) => {
7
+ await pool.query(sql`
8
+ alter table users alter column password_encrypted set data type varchar(256);
9
+ `);
10
+ },
11
+ down: async (pool) => {
12
+ await pool.query(sql`
13
+ alter table users alter column password_encrypted set data type varchar(128);
14
+ `);
15
+ },
16
+ };
17
+
18
+ export default alteration;
@@ -0,0 +1,50 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js';
6
+
7
+ const alteration: AlterationScript = {
8
+ up: async (pool) => {
9
+ await pool.query(sql`
10
+ create table secrets (
11
+ tenant_id varchar(21) not null
12
+ references tenants (id) on update cascade on delete cascade,
13
+ id varchar(21) not null primary key,
14
+ user_id varchar(21) not null
15
+ references users (id) on update cascade on delete cascade,
16
+ type varchar(256) /* @user SecretType */ not null,
17
+ /** Encrypted data encryption key (DEK) for the secret. */
18
+ encrypted_dek bytea not null,
19
+ /** Initialization vector for the secret encryption. */
20
+ iv bytea not null,
21
+ /** Authentication tag for the secret encryption. */
22
+ auth_tag bytea not null,
23
+ /** The encrypted secret data. e.g. { access_token, refresh_token }*/
24
+ ciphertext bytea not null,
25
+ /** The metadata associated with the secret. */
26
+ metadata jsonb not null default '{}'::jsonb,
27
+ created_at timestamptz not null default(now()),
28
+ updated_at timestamptz not null default(now())
29
+ );
30
+ `);
31
+
32
+ await pool.query(sql`
33
+ create trigger set_updated_at
34
+ before update on secrets
35
+ for each row
36
+ execute procedure set_updated_at();
37
+ `);
38
+
39
+ await applyTableRls(pool, 'secrets');
40
+ },
41
+ down: async (pool) => {
42
+ await dropTableRls(pool, 'secrets');
43
+
44
+ await pool.query(sql`
45
+ drop table secrets;
46
+ `);
47
+ },
48
+ };
49
+
50
+ export default alteration;
@@ -0,0 +1,109 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js';
6
+
7
+ const alteration: AlterationScript = {
8
+ up: async (pool) => {
9
+ await pool.query(sql`
10
+ create table secret_connector_relations (
11
+ tenant_id varchar(21) not null
12
+ references tenants (id) on update cascade on delete cascade,
13
+ secret_id varchar(21) not null
14
+ references secrets (id) on update cascade on delete cascade,
15
+ /** Social connector ID foreign reference. Only present for secrets that store social connector tokens. Note: avoid directly cascading deletes here, need to delete the secrets first.*/
16
+ connector_id varchar(128)
17
+ references connectors (id) on update cascade,
18
+ /** SSO connector ID foreign reference. Only present for secrets that store SSO connector tokens. Note: avoid directly cascading deletes here, need to delete the secrets first.*/
19
+ sso_connector_id varchar(128)
20
+ references sso_connectors (id) on update cascade,
21
+ /** The target of the social connector. e.g. 'github', 'google', etc. */
22
+ social_connector_target varchar(256),
23
+ /** User social identity ID foreign reference. Only present for secrets that store social identity tokens. */
24
+ social_identity_id varchar(128),
25
+ /** User sso connector issuer. Only present for secrets that store SSO connector tokens. */
26
+ sso_connector_issuer varchar(256),
27
+ /** User SSO identity ID. Only present for secrets that store SSO identity tokens. */
28
+ sso_identity_id varchar(128),
29
+ primary key (tenant_id, secret_id),
30
+ /** Ensures that each social identity is associated with only one secret. */
31
+ constraint secret_connector_relations__target__social_identity_id
32
+ unique (tenant_id, social_connector_target, social_identity_id),
33
+ /** Ensures that each SSO identity is associated with only one secret. */
34
+ foreign key (tenant_id, sso_connector_issuer, sso_identity_id)
35
+ references user_sso_identities (tenant_id, issuer, identity_id) on update cascade,
36
+ /** Ensure that each secret is associated with a social connector or SSO connector, but not both at the same time. */
37
+ constraint secret_connector_relations__connector_id__sso_connector_id
38
+ check (
39
+ (
40
+ connector_id is not null and social_connector_target is not null and social_identity_id is not null and
41
+ sso_connector_id is null and sso_identity_id is null
42
+ ) or (
43
+ connector_id is null and social_connector_target is null and social_identity_id is null and
44
+ sso_connector_id is not null and sso_identity_id is not null
45
+ )
46
+ )
47
+ );
48
+ `);
49
+
50
+ /** Trigger function to delete secrets when the social connector is deleted. */
51
+ await pool.query(sql`
52
+ create function delete_secrets_on_social_connector_delete()
53
+ returns trigger as $$
54
+ begin
55
+ delete from secrets
56
+ where id in (
57
+ select secret_id from secret_connector_relations
58
+ where tenant_id = old.tenant_id and connector_id = old.id
59
+ );
60
+ return old;
61
+ end;
62
+ $$ language plpgsql;
63
+
64
+ create trigger delete_secrets_before_social_connector_delete
65
+ before delete on connectors
66
+ for each row
67
+ execute procedure delete_secrets_on_social_connector_delete();
68
+ `);
69
+
70
+ /** Trigger function to delete secrets when the SSO connector is deleted. */
71
+ await pool.query(sql`
72
+ create function delete_secrets_on_sso_connector_delete()
73
+ returns trigger as $$
74
+ begin
75
+ delete from secrets
76
+ where id in (
77
+ select secret_id from secret_connector_relations
78
+ where tenant_id = old.tenant_id and sso_connector_id = old.id
79
+ );
80
+ return old;
81
+ end;
82
+ $$ language plpgsql;
83
+
84
+ create trigger delete_secrets_before_sso_connector_delete
85
+ before delete on sso_connectors
86
+ for each row
87
+ execute procedure delete_secrets_on_sso_connector_delete();
88
+ `);
89
+
90
+ await applyTableRls(pool, 'secret_connector_relations');
91
+ },
92
+ down: async (pool) => {
93
+ await pool.query(sql`
94
+ drop trigger if exists delete_secrets_before_social_connector_delete on connectors;
95
+ drop function if exists delete_secrets_on_social_connector_delete;
96
+
97
+ drop trigger if exists delete_secrets_before_sso_connector_delete on sso_connectors;
98
+ drop function if exists delete_secrets_on_sso_connector_delete;
99
+ `);
100
+
101
+ await dropTableRls(pool, 'secret_connector_relations');
102
+
103
+ await pool.query(sql`
104
+ drop table secret_connector_relations;
105
+ `);
106
+ },
107
+ };
108
+
109
+ export default alteration;
@@ -0,0 +1,81 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ const alteration: AlterationScript = {
6
+ up: async (pool) => {
7
+ /** Trigger function to delete secret when the SSO identity is deleted. */
8
+ await pool.query(sql`
9
+ create function delete_secret_on_sso_identity_delete()
10
+ returns trigger as $$
11
+ begin
12
+ delete from secrets
13
+ where id in (
14
+ select secret_id from secret_connector_relations
15
+ where tenant_id = old.tenant_id
16
+ and sso_connector_issuer = old.issuer
17
+ and sso_identity_id = old.identity_id
18
+ )
19
+ -- we also need to ensure that the secret is associated with the correct user
20
+ and user_id = old.user_id;
21
+ return old;
22
+ end;
23
+ $$ language plpgsql;
24
+
25
+ create trigger delete_secret_before_sso_identity_delete
26
+ before delete on user_sso_identities
27
+ for each row
28
+ execute procedure delete_secret_on_sso_identity_delete();
29
+ `);
30
+
31
+ /** Trigger function to delete associated secrets when social identities are deleted. */
32
+ await pool.query(sql`
33
+ create function delete_secrets_on_social_identity_delete()
34
+ returns trigger as $$
35
+ declare
36
+ target text;
37
+ old_identity jsonb;
38
+ new_identity jsonb;
39
+ begin
40
+ -- Loop over old identities to detect deletions or modifications
41
+ for target in select jsonb_object_keys(old.identities)
42
+ loop
43
+ old_identity := old.identities -> target;
44
+ new_identity := new.identities -> target;
45
+
46
+ -- If the identity was deleted or modified, delete the associated secret
47
+ if new_identity is null or (new_identity->>'userId') is distinct from (old_identity->>'userId') then
48
+ -- Identity was removed or changed, delete the corresponding secrets
49
+ delete from secrets
50
+ using secret_connector_relations
51
+ where secrets.id = secret_connector_relations.secret_id
52
+ -- Ensure we are deleting the correct social identity
53
+ and secret_connector_relations.social_connector_target = target
54
+ and secret_connector_relations.social_identity_id = old_identity->>'userId'
55
+ -- Ensure we delete the correct user's secret
56
+ and secrets.user_id = old.id;
57
+ end if;
58
+ end loop;
59
+
60
+ return new;
61
+ end;
62
+ $$ language plpgsql;
63
+
64
+ create trigger delete_secrets_before_social_identity_delete
65
+ before update of identities on users
66
+ for each row
67
+ execute procedure delete_secrets_on_social_identity_delete();
68
+ `);
69
+ },
70
+ down: async (pool) => {
71
+ await pool.query(sql`
72
+ drop trigger if exists delete_secret_before_sso_identity_delete on user_sso_identities;
73
+ drop function if exists delete_secret_on_sso_identity_delete();
74
+
75
+ drop trigger if exists delete_secrets_before_social_identity_delete on users;
76
+ drop function if exists delete_secrets_on_social_identity_delete();
77
+ `);
78
+ },
79
+ };
80
+
81
+ export default alteration;
@@ -0,0 +1,20 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ const alteration: AlterationScript = {
6
+ up: async (pool) => {
7
+ await pool.query(sql`
8
+ alter table connectors
9
+ add column enable_token_storage boolean not null default FALSE;
10
+ `);
11
+ },
12
+ down: async (pool) => {
13
+ await pool.query(sql`
14
+ alter table connectors
15
+ drop column if exists enable_token_storage;
16
+ `);
17
+ },
18
+ };
19
+
20
+ export default alteration;