@logto/schemas 1.16.0 → 1.18.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 (139) hide show
  1. package/alterations/1.17.0-1715826336-add-default-user-role-config.ts +18 -0
  2. package/alterations/1.17.0-1715829731-rename-data-hook-schema-update-event.ts +120 -0
  3. package/alterations/1.17.0-1716278409-remove-internal-role-database-policies.ts +37 -0
  4. package/alterations/1.17.0-1716291265-create-pre-configured-m-api-role.ts +92 -0
  5. package/alterations/1.17.0-1717148078-remove-service-log-reference.ts +19 -0
  6. package/alterations/1.18.0-1717567857-social-sign-in-linking.ts +18 -0
  7. package/alterations/1.18.0-1717597875-add-organization-email-domains-table.ts +31 -0
  8. package/alterations/1.18.0-1717818597-organization-mfa-requirement.ts +18 -0
  9. package/alterations/1.18.0-1718340884-rename-org-email-domains-and-add-jit-roles-table.ts +56 -0
  10. package/alterations/1.18.0-1718594164-add-agree-to-terms-policy.ts +40 -0
  11. package/alterations/1.18.0-1718785576-organization-application-relations.ts +37 -0
  12. package/alterations/1.18.0-1718786576-organization-jit-sso-connectors.ts +31 -0
  13. package/alterations/1.18.0-1718807616-organization-role-application-relations.ts +34 -0
  14. package/alterations/1.18.0-1718865814-add-subject-tokens.ts +36 -0
  15. package/alterations/1.18.0-1719014832-organization-role-types.ts +35 -0
  16. package/alterations/1.18.0-1719221205-fix-functions.ts +25 -0
  17. package/alterations/1.18.0-1719312694-custom-ui-assets.ts +18 -0
  18. package/alterations/utils/1716643968-id-generation.ts +46 -0
  19. package/alterations-js/1.17.0-1715826336-add-default-user-role-config.d.ts +3 -0
  20. package/alterations-js/1.17.0-1715826336-add-default-user-role-config.js +14 -0
  21. package/alterations-js/1.17.0-1715829731-rename-data-hook-schema-update-event.d.ts +3 -0
  22. package/alterations-js/1.17.0-1715829731-rename-data-hook-schema-update-event.js +96 -0
  23. package/alterations-js/1.17.0-1716278409-remove-internal-role-database-policies.d.ts +3 -0
  24. package/alterations-js/1.17.0-1716278409-remove-internal-role-database-policies.js +33 -0
  25. package/alterations-js/1.17.0-1716291265-create-pre-configured-m-api-role.d.ts +7 -0
  26. package/alterations-js/1.17.0-1716291265-create-pre-configured-m-api-role.js +77 -0
  27. package/alterations-js/1.17.0-1717148078-remove-service-log-reference.d.ts +3 -0
  28. package/alterations-js/1.17.0-1717148078-remove-service-log-reference.js +15 -0
  29. package/alterations-js/1.18.0-1717567857-social-sign-in-linking.d.ts +3 -0
  30. package/alterations-js/1.18.0-1717567857-social-sign-in-linking.js +14 -0
  31. package/alterations-js/1.18.0-1717597875-add-organization-email-domains-table.d.ts +3 -0
  32. package/alterations-js/1.18.0-1717597875-add-organization-email-domains-table.js +26 -0
  33. package/alterations-js/1.18.0-1717818597-organization-mfa-requirement.d.ts +3 -0
  34. package/alterations-js/1.18.0-1717818597-organization-mfa-requirement.js +14 -0
  35. package/alterations-js/1.18.0-1718340884-rename-org-email-domains-and-add-jit-roles-table.d.ts +3 -0
  36. package/alterations-js/1.18.0-1718340884-rename-org-email-domains-and-add-jit-roles-table.js +51 -0
  37. package/alterations-js/1.18.0-1718594164-add-agree-to-terms-policy.d.ts +3 -0
  38. package/alterations-js/1.18.0-1718594164-add-agree-to-terms-policy.js +34 -0
  39. package/alterations-js/1.18.0-1718785576-organization-application-relations.d.ts +3 -0
  40. package/alterations-js/1.18.0-1718785576-organization-application-relations.js +32 -0
  41. package/alterations-js/1.18.0-1718786576-organization-jit-sso-connectors.d.ts +3 -0
  42. package/alterations-js/1.18.0-1718786576-organization-jit-sso-connectors.js +26 -0
  43. package/alterations-js/1.18.0-1718807616-organization-role-application-relations.d.ts +3 -0
  44. package/alterations-js/1.18.0-1718807616-organization-role-application-relations.js +29 -0
  45. package/alterations-js/1.18.0-1718865814-add-subject-tokens.d.ts +3 -0
  46. package/alterations-js/1.18.0-1718865814-add-subject-tokens.js +31 -0
  47. package/alterations-js/1.18.0-1719014832-organization-role-types.d.ts +3 -0
  48. package/alterations-js/1.18.0-1719014832-organization-role-types.js +31 -0
  49. package/alterations-js/1.18.0-1719221205-fix-functions.d.ts +7 -0
  50. package/alterations-js/1.18.0-1719221205-fix-functions.js +20 -0
  51. package/alterations-js/1.18.0-1719312694-custom-ui-assets.d.ts +3 -0
  52. package/alterations-js/1.18.0-1719312694-custom-ui-assets.js +14 -0
  53. package/alterations-js/utils/1716643968-id-generation.d.ts +19 -0
  54. package/alterations-js/utils/1716643968-id-generation.js +26 -0
  55. package/lib/consts/subscriptions.d.ts +9 -6
  56. package/lib/consts/subscriptions.js +8 -5
  57. package/lib/db-entries/custom-types.d.ts +5 -0
  58. package/lib/db-entries/custom-types.js +6 -0
  59. package/lib/db-entries/index.d.ts +6 -0
  60. package/lib/db-entries/index.js +6 -0
  61. package/lib/db-entries/organization-application-relation.d.ts +20 -0
  62. package/lib/db-entries/organization-application-relation.js +29 -0
  63. package/lib/db-entries/organization-jit-email-domain.d.ts +24 -0
  64. package/lib/db-entries/organization-jit-email-domain.js +29 -0
  65. package/lib/db-entries/organization-jit-role.d.ts +24 -0
  66. package/lib/db-entries/organization-jit-role.js +29 -0
  67. package/lib/db-entries/organization-jit-sso-connector.d.ts +22 -0
  68. package/lib/db-entries/organization-jit-sso-connector.js +29 -0
  69. package/lib/db-entries/organization-role-application-relation.d.ts +22 -0
  70. package/lib/db-entries/organization-role-application-relation.js +33 -0
  71. package/lib/db-entries/organization-role.d.ts +6 -1
  72. package/lib/db-entries/organization-role.js +5 -0
  73. package/lib/db-entries/organization.d.ts +5 -1
  74. package/lib/db-entries/organization.js +4 -0
  75. package/lib/db-entries/role.d.ts +5 -1
  76. package/lib/db-entries/role.js +4 -0
  77. package/lib/db-entries/sign-in-experience.d.ts +11 -3
  78. package/lib/db-entries/sign-in-experience.js +14 -2
  79. package/lib/db-entries/subject-token.d.ts +28 -0
  80. package/lib/db-entries/subject-token.js +50 -0
  81. package/lib/foundations/jsonb-types/hooks.d.ts +18 -20
  82. package/lib/foundations/jsonb-types/hooks.js +21 -21
  83. package/lib/foundations/jsonb-types/oidc-module.d.ts +33 -1
  84. package/lib/foundations/jsonb-types/oidc-module.js +2 -0
  85. package/lib/foundations/jsonb-types/sign-in-experience.d.ts +14 -0
  86. package/lib/foundations/jsonb-types/sign-in-experience.js +3 -0
  87. package/lib/models/tenants.d.ts +0 -21
  88. package/lib/models/tenants.js +0 -3
  89. package/lib/seeds/cloud-api.js +1 -0
  90. package/lib/seeds/management-api.d.ts +4 -0
  91. package/lib/seeds/management-api.js +10 -0
  92. package/lib/seeds/sign-in-experience.js +1 -0
  93. package/lib/types/application.d.ts +7 -47
  94. package/lib/types/connector.d.ts +724 -190
  95. package/lib/types/consent.d.ts +1 -9
  96. package/lib/types/hook.d.ts +67 -6
  97. package/lib/types/index.d.ts +2 -0
  98. package/lib/types/index.js +2 -0
  99. package/lib/types/interactions.d.ts +141 -6
  100. package/lib/types/interactions.js +62 -8
  101. package/lib/types/log/token.d.ts +2 -1
  102. package/lib/types/log/token.js +1 -0
  103. package/lib/types/logto-config/index.d.ts +10 -0
  104. package/lib/types/logto-config/jwt-customizer.d.ts +174 -137
  105. package/lib/types/logto-config/jwt-customizer.js +2 -1
  106. package/lib/types/mapi-proxy.js +1 -0
  107. package/lib/types/oidc-config.d.ts +2 -1
  108. package/lib/types/oidc-config.js +1 -0
  109. package/lib/types/organization.d.ts +12 -3
  110. package/lib/types/organization.js +4 -1
  111. package/lib/types/role.d.ts +6 -1
  112. package/lib/types/role.js +6 -1
  113. package/lib/types/sign-in-experience.d.ts +1064 -0
  114. package/lib/types/sign-in-experience.js +21 -0
  115. package/lib/types/subject-token.d.ts +12 -0
  116. package/lib/types/subject-token.js +5 -0
  117. package/lib/types/system.d.ts +4 -4
  118. package/lib/types/tenant-organization.d.ts +1 -0
  119. package/lib/types/tenant-organization.js +3 -0
  120. package/lib/types/user.d.ts +5 -1
  121. package/lib/types/user.js +5 -1
  122. package/lib/utils/role.d.ts +2 -0
  123. package/lib/utils/role.js +2 -0
  124. package/lib/utils/zod.d.ts +1 -1
  125. package/package.json +8 -7
  126. package/tables/_after_all.sql +0 -27
  127. package/tables/applications.sql +5 -0
  128. package/tables/organization_application_relations.sql +14 -0
  129. package/tables/organization_jit_email_domains.sql +13 -0
  130. package/tables/organization_jit_roles.sql +14 -0
  131. package/tables/organization_jit_sso_connectors.sql +13 -0
  132. package/tables/organization_role_application_relations.sql +18 -0
  133. package/tables/organization_role_user_relations.sql +3 -1
  134. package/tables/organization_roles.sql +8 -1
  135. package/tables/organizations.sql +2 -0
  136. package/tables/roles.sql +2 -0
  137. package/tables/service_logs.sql +1 -2
  138. package/tables/sign_in_experiences.sql +5 -0
  139. package/tables/subject_tokens.sql +16 -0
@@ -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 roles add column is_default boolean not null default false;
9
+ `);
10
+ },
11
+ down: async (pool) => {
12
+ await pool.query(sql`
13
+ alter table roles drop column is_default;
14
+ `);
15
+ },
16
+ };
17
+
18
+ export default alteration;
@@ -0,0 +1,120 @@
1
+ import { sql } from '@silverhand/slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ enum DataHookSchema {
6
+ User = 'User',
7
+ Role = 'Role',
8
+ Scope = 'Scope',
9
+ Organization = 'Organization',
10
+ OrganizationRole = 'OrganizationRole',
11
+ OrganizationScope = 'OrganizationScope',
12
+ }
13
+
14
+ type OldSchemaUpdateEvent = `${DataHookSchema}.${'Updated'}`;
15
+ type NewSchemaUpdateEvent = `${DataHookSchema}.Data.Updated`;
16
+
17
+ const oldSchemaUpdateEvents = Object.freeze([
18
+ 'User.Updated',
19
+ 'Role.Updated',
20
+ 'Scope.Updated',
21
+ 'Organization.Updated',
22
+ 'OrganizationRole.Updated',
23
+ 'OrganizationScope.Updated',
24
+ ] satisfies OldSchemaUpdateEvent[]);
25
+
26
+ const newSchemaUpdateEvents = Object.freeze([
27
+ 'User.Data.Updated',
28
+ 'Role.Data.Updated',
29
+ 'Scope.Data.Updated',
30
+ 'Organization.Data.Updated',
31
+ 'OrganizationRole.Data.Updated',
32
+ 'OrganizationScope.Data.Updated',
33
+ ] as const satisfies NewSchemaUpdateEvent[]);
34
+
35
+ const updateMap: { [key in OldSchemaUpdateEvent]: NewSchemaUpdateEvent } = {
36
+ 'User.Updated': 'User.Data.Updated',
37
+ 'Role.Updated': 'Role.Data.Updated',
38
+ 'Scope.Updated': 'Scope.Data.Updated',
39
+ 'Organization.Updated': 'Organization.Data.Updated',
40
+ 'OrganizationRole.Updated': 'OrganizationRole.Data.Updated',
41
+ 'OrganizationScope.Updated': 'OrganizationScope.Data.Updated',
42
+ };
43
+
44
+ const reverseMap: { [key in NewSchemaUpdateEvent]: OldSchemaUpdateEvent } = {
45
+ 'User.Data.Updated': 'User.Updated',
46
+ 'Role.Data.Updated': 'Role.Updated',
47
+ 'Scope.Data.Updated': 'Scope.Updated',
48
+ 'Organization.Data.Updated': 'Organization.Updated',
49
+ 'OrganizationRole.Data.Updated': 'OrganizationRole.Updated',
50
+ 'OrganizationScope.Data.Updated': 'OrganizationScope.Updated',
51
+ };
52
+
53
+ // This alteration script filters all the hook's events jsonb column to replace all the old schema update events with the new schema update events.
54
+
55
+ const isOldSchemaUpdateEvent = (event: string): event is OldSchemaUpdateEvent =>
56
+ // eslint-disable-next-line no-restricted-syntax
57
+ oldSchemaUpdateEvents.includes(event as OldSchemaUpdateEvent);
58
+
59
+ const isNewSchemaUpdateEvent = (event: string): event is NewSchemaUpdateEvent =>
60
+ // eslint-disable-next-line no-restricted-syntax
61
+ newSchemaUpdateEvents.includes(event as NewSchemaUpdateEvent);
62
+
63
+ const alteration: AlterationScript = {
64
+ up: async (pool) => {
65
+ const { rows: hooks } = await pool.query<{ id: string; events: string[] }>(sql`
66
+ select id, events
67
+ from hooks
68
+ `);
69
+
70
+ const hooksToBeUpdate = hooks.filter(({ events }) => {
71
+ return oldSchemaUpdateEvents.some((oldEvent) => events.includes(oldEvent));
72
+ });
73
+
74
+ await Promise.all(
75
+ hooksToBeUpdate.map(async ({ id, events }) => {
76
+ const updateEvents = events.reduce<string[]>((accumulator, event) => {
77
+ if (isOldSchemaUpdateEvent(event)) {
78
+ return [...accumulator, updateMap[event]];
79
+ }
80
+ return [...accumulator, event];
81
+ }, []);
82
+
83
+ await pool.query(sql`
84
+ update hooks
85
+ set events = ${JSON.stringify(updateEvents)}
86
+ where id = ${id};
87
+ `);
88
+ })
89
+ );
90
+ },
91
+ down: async (pool) => {
92
+ const { rows: hooks } = await pool.query<{ id: string; events: string[] }>(sql`
93
+ select id, events
94
+ from hooks
95
+ `);
96
+
97
+ const hooksToBeUpdate = hooks.filter(({ events }) => {
98
+ return newSchemaUpdateEvents.some((newEvent) => events.includes(newEvent));
99
+ });
100
+
101
+ await Promise.all(
102
+ hooksToBeUpdate.map(async ({ id, events }) => {
103
+ const updateEvents = events.reduce<string[]>((accumulator, event) => {
104
+ if (isNewSchemaUpdateEvent(event)) {
105
+ return [...accumulator, reverseMap[event]];
106
+ }
107
+ return [...accumulator, event];
108
+ }, []);
109
+
110
+ await pool.query(sql`
111
+ update hooks
112
+ set events = ${JSON.stringify(updateEvents)}
113
+ where id = ${id};
114
+ `);
115
+ })
116
+ );
117
+ },
118
+ };
119
+
120
+ export default alteration;
@@ -0,0 +1,37 @@
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
+ drop policy if exists roles_select on roles;
9
+ drop policy if exists roles_modification on roles;
10
+ create policy roles_modification on roles using (true);
11
+
12
+ drop policy if exists roles_scopes_select on roles_scopes;
13
+ drop policy if exists roles_scopes_modification on roles_scopes;
14
+ create policy roles_scopes_modification on roles_scopes using (true);
15
+ `);
16
+ },
17
+ down: async (pool) => {
18
+ await pool.query(sql`
19
+ create policy roles_select on roles
20
+ for select using (true);
21
+
22
+ drop policy roles_modification on roles;
23
+ create policy roles_modification on roles
24
+ using (not starts_with(name, '#internal:'));
25
+
26
+ -- Restrict role - scope modification
27
+ create policy roles_scopes_select on roles_scopes
28
+ for select using (true);
29
+
30
+ drop policy roles_scopes_modification on roles_scopes;
31
+ create policy roles_scopes_modification on roles_scopes
32
+ using (not starts_with((select roles.name from roles where roles.id = role_id), '#internal:'));
33
+ `);
34
+ },
35
+ };
36
+
37
+ export default alteration;
@@ -0,0 +1,92 @@
1
+ import { yes } from '@silverhand/essentials';
2
+ import { sql } from '@silverhand/slonik';
3
+
4
+ import type { AlterationScript } from '../lib/types/alteration.js';
5
+
6
+ import { generateStandardId } from './utils/1716643968-id-generation.js';
7
+
8
+ const isCi = yes(process.env.CI);
9
+
10
+ const defaultTenantId = 'default';
11
+ const defaultTenantManagementApiIndicator = `https://${defaultTenantId}.logto.app/api`;
12
+ const roleName = 'Logto Management API access';
13
+ const roleDescription = 'This default role grants access to the Logto management API.';
14
+ enum RoleType {
15
+ MachineToMachine = 'MachineToMachine',
16
+ }
17
+
18
+ enum PredefinedScope {
19
+ All = 'all',
20
+ }
21
+
22
+ /**
23
+ * This script is to create a pre-configured Management API M2M role for new users.
24
+ * This script is **only for CI**, since we won't create this role for existing users, so this script is not applicable for existing db data.
25
+ */
26
+ const alteration: AlterationScript = {
27
+ up: async (pool) => {
28
+ if (!isCi) {
29
+ console.info(
30
+ "Skipping the alteration script `next-1716291265-create-pre-configured-m-api-role.ts` since it's should not be applied to existing db data."
31
+ );
32
+ return;
33
+ }
34
+
35
+ /**
36
+ * Only affect the `default` tenant, since this is the only tenant in the OSS version and the initial tenant in the cloud version.
37
+ * So we only need to care about this role for the `default` tenant.
38
+ */
39
+
40
+ const roleId = generateStandardId();
41
+
42
+ await pool.query(sql`
43
+ insert into roles (id, tenant_id, name, description, type)
44
+ values (
45
+ ${roleId},
46
+ ${defaultTenantId},
47
+ ${roleName},
48
+ ${roleDescription},
49
+ ${RoleType.MachineToMachine}
50
+ );
51
+ `);
52
+
53
+ // Assign Logto Management API permission `all` to the Logto Management API M2M role
54
+ await pool.query(sql`
55
+ insert into roles_scopes (id, role_id, scope_id, tenant_id)
56
+ values (
57
+ ${generateStandardId()},
58
+ ${roleId},
59
+ (
60
+ select scopes.id
61
+ from scopes
62
+ join resources on
63
+ scopes.tenant_id = resources.tenant_id and
64
+ scopes.resource_id = resources.id
65
+ where resources.indicator = ${defaultTenantManagementApiIndicator}
66
+ and scopes.name = ${PredefinedScope.All}
67
+ and scopes.tenant_id = ${defaultTenantId}
68
+ ),
69
+ ${defaultTenantId}
70
+ )
71
+ `);
72
+ },
73
+ down: async (pool) => {
74
+ if (!isCi) {
75
+ console.info(
76
+ "Skipping the down script `next-1716291265-create-pre-configured-m-api-role.ts` since it's should not be applied to production db."
77
+ );
78
+ return;
79
+ }
80
+
81
+ // Delete the created role
82
+ await pool.query(sql`
83
+ delete from roles
84
+ where tenant_id = ${defaultTenantId}
85
+ and name = ${roleName}
86
+ and description = ${roleDescription}
87
+ and type = ${RoleType.MachineToMachine}
88
+ `);
89
+ },
90
+ };
91
+
92
+ export default alteration;
@@ -0,0 +1,19 @@
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 service_logs drop constraint service_logs_tenant_id_fkey;
9
+ `);
10
+ },
11
+ down: async (pool) => {
12
+ await pool.query(sql`
13
+ alter table service_logs add constraint service_logs_tenant_id_fkey
14
+ foreign key (tenant_id) references tenants(id) on update cascade on delete cascade;
15
+ `);
16
+ },
17
+ };
18
+
19
+ 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 sign_in_experiences add column social_sign_in jsonb not null default '{}'::jsonb;
9
+ `);
10
+ },
11
+ down: async (pool) => {
12
+ await pool.query(sql`
13
+ alter table sign_in_experiences drop column social_sign_in;
14
+ `);
15
+ },
16
+ };
17
+
18
+ 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
+ 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 organization_email_domains (
11
+ tenant_id varchar(21) not null
12
+ references tenants (id) on update cascade on delete cascade,
13
+ /** The ID of the organization. */
14
+ organization_id varchar(21) not null
15
+ references organizations (id) on update cascade on delete cascade,
16
+ /** The email domain that will be automatically provisioned. */
17
+ email_domain varchar(128) not null,
18
+ primary key (tenant_id, organization_id, email_domain)
19
+ );
20
+ `);
21
+ await applyTableRls(pool, 'organization_email_domains');
22
+ },
23
+ down: async (pool) => {
24
+ await dropTableRls(pool, 'organization_email_domains');
25
+ await pool.query(sql`
26
+ drop table organization_email_domains
27
+ `);
28
+ },
29
+ };
30
+
31
+ 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 organizations add column is_mfa_required boolean not null default false;
9
+ `);
10
+ },
11
+ down: async (pool) => {
12
+ await pool.query(sql`
13
+ alter table organizations drop column is_mfa_required;
14
+ `);
15
+ },
16
+ };
17
+
18
+ export default alteration;
@@ -0,0 +1,56 @@
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
+ alter table organization_email_domains rename to organization_jit_email_domains;
11
+ alter table organization_jit_email_domains
12
+ rename constraint organization_email_domains_organization_id_fkey to organization_jit_email_domains_organization_id_fkey;
13
+ alter table organization_jit_email_domains
14
+ rename constraint organization_email_domains_pkey to organization_jit_email_domains_pkey;
15
+ alter table organization_jit_email_domains
16
+ rename constraint organization_email_domains_tenant_id_fkey to organization_jit_email_domains_tenant_id_fkey;
17
+ alter policy organization_email_domains_modification
18
+ on organization_jit_email_domains rename to organization_jit_email_domains_modification;
19
+ alter policy organization_email_domains_tenant_id
20
+ on organization_jit_email_domains rename to organization_jit_email_domains_tenant_id;
21
+ create table organization_jit_roles (
22
+ tenant_id varchar(21) not null
23
+ references tenants (id) on update cascade on delete cascade,
24
+ /** The ID of the organization. */
25
+ organization_id varchar(21) not null
26
+ references organizations (id) on update cascade on delete cascade,
27
+ /** The organization role ID that will be automatically provisioned. */
28
+ organization_role_id varchar(21) not null
29
+ references organization_roles (id) on update cascade on delete cascade,
30
+ primary key (tenant_id, organization_id, organization_role_id)
31
+ );
32
+ `);
33
+ await applyTableRls(pool, 'organization_jit_roles');
34
+ },
35
+ down: async (pool) => {
36
+ await dropTableRls(pool, 'organization_jit_roles');
37
+ await pool.query(sql`
38
+ drop table organization_jit_roles
39
+ `);
40
+ await pool.query(sql`
41
+ alter table organization_jit_email_domains rename to organization_email_domains;
42
+ alter table organization_email_domains
43
+ rename constraint organization_jit_email_domains_organization_id_fkey to organization_email_domains_organization_id_fkey;
44
+ alter table organization_email_domains
45
+ rename constraint organization_jit_email_domains_pkey to organization_email_domains_pkey;
46
+ alter table organization_email_domains
47
+ rename constraint organization_jit_email_domains_tenant_id_fkey to organization_email_domains_tenant_id_fkey;
48
+ alter policy organization_jit_email_domains_modification
49
+ on organization_email_domains rename to organization_email_domains_modification;
50
+ alter policy organization_jit_email_domains_tenant_id
51
+ on organization_email_domains rename to organization_email_domains_tenant_id;
52
+ `);
53
+ },
54
+ };
55
+
56
+ export default alteration;
@@ -0,0 +1,40 @@
1
+ import { yes } from '@silverhand/essentials';
2
+ import { sql } from '@silverhand/slonik';
3
+
4
+ import type { AlterationScript } from '../lib/types/alteration.js';
5
+
6
+ const isCi = yes(process.env.CI);
7
+
8
+ const alteration: AlterationScript = {
9
+ up: async (pool) => {
10
+ // Create type
11
+ await pool.query(sql`
12
+ create type agree_to_terms_policy as enum ('Automatic', 'ManualRegistrationOnly', 'Manual');
13
+ `);
14
+
15
+ if (isCi) {
16
+ // Direct set default to 'Automatic' to align with the sql table definition when running CI
17
+ await pool.query(sql`
18
+ alter table sign_in_experiences add column agree_to_terms_policy agree_to_terms_policy not null default 'Automatic';
19
+ `);
20
+ } else {
21
+ // For compatibility with existing data, default to 'ManualRegistrationOnly'
22
+ await pool.query(sql`
23
+ alter table sign_in_experiences add column agree_to_terms_policy agree_to_terms_policy not null default 'ManualRegistrationOnly';
24
+ `);
25
+
26
+ // For new data, default to 'Automatic'
27
+ await pool.query(sql`
28
+ alter table sign_in_experiences alter column agree_to_terms_policy set default 'Automatic';
29
+ `);
30
+ }
31
+ },
32
+ down: async (pool) => {
33
+ await pool.query(sql`
34
+ alter table sign_in_experiences drop column agree_to_terms_policy;
35
+ drop type agree_to_terms_policy;
36
+ `);
37
+ },
38
+ };
39
+
40
+ export default alteration;
@@ -0,0 +1,37 @@
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 function check_application_type(application_id varchar(21), target_type application_type) returns boolean as
11
+ $$ begin
12
+ return (select type from applications where id = application_id) = target_type;
13
+ end; $$ language plpgsql;
14
+ create table organization_application_relations (
15
+ tenant_id varchar(21) not null
16
+ references tenants (id) on update cascade on delete cascade,
17
+ organization_id varchar(21) not null
18
+ references organizations (id) on update cascade on delete cascade,
19
+ application_id varchar(21) not null
20
+ references applications (id) on update cascade on delete cascade,
21
+ primary key (tenant_id, organization_id, application_id),
22
+ constraint application_type
23
+ check (check_application_type(application_id, 'MachineToMachine'))
24
+ );
25
+ `);
26
+ await applyTableRls(pool, 'organization_application_relations');
27
+ },
28
+ down: async (pool) => {
29
+ await dropTableRls(pool, 'organization_application_relations');
30
+ await pool.query(sql`
31
+ drop table organization_application_relations;
32
+ drop function check_application_type;
33
+ `);
34
+ },
35
+ };
36
+
37
+ 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
+ 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 organization_jit_sso_connectors (
11
+ tenant_id varchar(21) not null
12
+ references tenants (id) on update cascade on delete cascade,
13
+ /** The ID of the organization. */
14
+ organization_id varchar(21) not null
15
+ references organizations (id) on update cascade on delete cascade,
16
+ sso_connector_id varchar(128) not null
17
+ references sso_connectors (id) on update cascade on delete cascade,
18
+ primary key (tenant_id, organization_id, sso_connector_id)
19
+ );
20
+ `);
21
+ await applyTableRls(pool, 'organization_jit_sso_connectors');
22
+ },
23
+ down: async (pool) => {
24
+ await dropTableRls(pool, 'organization_jit_sso_connectors');
25
+ await pool.query(sql`
26
+ drop table organization_jit_sso_connectors;
27
+ `);
28
+ },
29
+ };
30
+
31
+ export default alteration;
@@ -0,0 +1,34 @@
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 organization_role_application_relations (
11
+ tenant_id varchar(21) not null
12
+ references tenants (id) on update cascade on delete cascade,
13
+ organization_id varchar(21) not null,
14
+ organization_role_id varchar(21) not null
15
+ references organization_roles (id) on update cascade on delete cascade,
16
+ application_id varchar(21) not null,
17
+ primary key (tenant_id, organization_id, organization_role_id, application_id),
18
+ /** Application's roles in an organization should be synchronized with the application's membership in the organization. */
19
+ foreign key (tenant_id, organization_id, application_id)
20
+ references organization_application_relations (tenant_id, organization_id, application_id)
21
+ on update cascade on delete cascade
22
+ );
23
+ `);
24
+ await applyTableRls(pool, 'organization_role_application_relations');
25
+ },
26
+ down: async (pool) => {
27
+ await dropTableRls(pool, 'organization_role_application_relations');
28
+ await pool.query(sql`
29
+ drop table organization_role_application_relations;
30
+ `);
31
+ },
32
+ };
33
+
34
+ export default alteration;
@@ -0,0 +1,36 @@
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 subject_tokens (
11
+ tenant_id varchar(21) not null
12
+ references tenants (id) on update cascade on delete cascade,
13
+ id varchar(25) not null,
14
+ context jsonb /* @use JsonObject */ not null default '{}'::jsonb,
15
+ expires_at timestamptz not null,
16
+ consumed_at timestamptz,
17
+ user_id varchar(21) not null
18
+ references users (id) on update cascade on delete cascade,
19
+ created_at timestamptz not null default(now()),
20
+ creator_id varchar(32) not null, /* It is intented to not reference to user or application table */
21
+ primary key (id)
22
+ );
23
+
24
+ create index subject_token__id on subject_tokens (tenant_id, id);
25
+ `);
26
+ await applyTableRls(pool, 'subject_tokens');
27
+ },
28
+ down: async (pool) => {
29
+ await dropTableRls(pool, 'subject_tokens');
30
+ await pool.query(sql`
31
+ drop table subject_tokens
32
+ `);
33
+ },
34
+ };
35
+
36
+ export default alteration;
@@ -0,0 +1,35 @@
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 organization_roles
9
+ add column type role_type not null default 'User';
10
+ create function check_organization_role_type(role_id varchar(21), target_type role_type) returns boolean as
11
+ $$ begin
12
+ return (select type from organization_roles where id = role_id) = target_type;
13
+ end; $$ language plpgsql;
14
+ alter table organization_role_user_relations
15
+ add constraint organization_role_user_relations__role_type
16
+ check (check_organization_role_type(organization_role_id, 'User'));
17
+ alter table organization_role_application_relations
18
+ add constraint organization_role_application_relations__role_type
19
+ check (check_organization_role_type(organization_role_id, 'MachineToMachine'));
20
+ `);
21
+ },
22
+ down: async (pool) => {
23
+ await pool.query(sql`
24
+ alter table organization_role_application_relations
25
+ drop constraint organization_role_application_relations__role_type;
26
+ alter table organization_role_user_relations
27
+ drop constraint organization_role_user_relations__role_type;
28
+ alter table organization_roles
29
+ drop column type;
30
+ drop function check_organization_role_type;
31
+ `);
32
+ },
33
+ };
34
+
35
+ export default alteration;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * In Logto Cloud, we have multiple schemas and the default search behavior will be problematic.
3
+ * This alteration script will fix it by setting the search path to public for the functions.
4
+ */
5
+
6
+ import { sql } from '@silverhand/slonik';
7
+
8
+ import type { AlterationScript } from '../lib/types/alteration.js';
9
+
10
+ const alteration: AlterationScript = {
11
+ up: async (pool) => {
12
+ await pool.query(sql`
13
+ alter function check_application_type set search_path = public;
14
+ alter function check_organization_role_type set search_path = public;
15
+ `);
16
+ },
17
+ down: async (pool) => {
18
+ await pool.query(sql`
19
+ alter function check_application_type reset search_path;
20
+ alter function check_organization_role_type reset search_path;
21
+ `);
22
+ },
23
+ };
24
+
25
+ export default alteration;