-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core,schemas,console): refactor log key types and sso-connec…
…tor authn-url api name (#4798) * refactor(core,schemas,console): refactor log key types and sso-connector authn-url api name refactor log key types and sso-connector authn-url api name * feat(schemas): add user sso identities table (#4801) * feat(schemas): add user sso identities table add user sso identities table * fix(schemas): fix alterations fix alterations * refactor(schemas): use unique constrain use unique constrain
- Loading branch information
Showing
8 changed files
with
110 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/schemas/alterations/next-1698820410-add-user-sso-identities-table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { type CommonQueryMethods, sql } from 'slonik'; | ||
|
||
import type { AlterationScript } from '../lib/types/alteration.js'; | ||
|
||
const getId = (value: string) => sql.identifier([value]); | ||
|
||
const getDatabaseName = async (pool: CommonQueryMethods) => { | ||
const { currentDatabase } = await pool.one<{ currentDatabase: string }>(sql` | ||
select current_database(); | ||
`); | ||
|
||
return currentDatabase.replaceAll('-', '_'); | ||
}; | ||
|
||
/** The alteration script for adding `sso_identities` column to the users table. */ | ||
const alteration: AlterationScript = { | ||
up: async (pool) => { | ||
const database = await getDatabaseName(pool); | ||
const baseRoleId = getId(`logto_tenant_${database}`); | ||
|
||
await pool.query(sql` | ||
create table user_sso_identities ( | ||
tenant_id varchar(21) not null | ||
references tenants (id) on update cascade on delete cascade, | ||
id varchar(21) not null, | ||
user_id varchar(12) not null | ||
references users (id) on update cascade on delete cascade, | ||
issuer varchar(256) not null, | ||
identity_id varchar(128) not null, | ||
detail jsonb not null default '{}'::jsonb, | ||
created_at timestamp not null default(now()), | ||
primary key (id), | ||
constraint user_sso_identities__issuer__identity_id | ||
unique (tenant_id, issuer, identity_id) | ||
); | ||
create trigger set_tenant_id before insert on user_sso_identities | ||
for each row execute procedure set_tenant_id(); | ||
alter table user_sso_identities enable row level security; | ||
create policy user_sso_identities_tenant_id on user_sso_identities | ||
as restrictive | ||
using (tenant_id = (select id from tenants where db_user = current_user)); | ||
create policy user_sso_identities_modification on user_sso_identities | ||
using (true); | ||
grant select, insert, update, delete on user_sso_identities to ${baseRoleId}; | ||
`); | ||
}, | ||
down: async (pool) => { | ||
await pool.query(sql` | ||
drop policy user_sso_identities_modification on user_sso_identities; | ||
drop policy user_sso_identities_tenant_id on user_sso_identities; | ||
drop table user_sso_identities; | ||
`); | ||
}, | ||
}; | ||
|
||
export default alteration; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* init_order = 2 */ | ||
|
||
create table user_sso_identities ( | ||
tenant_id varchar(21) not null | ||
references tenants (id) on update cascade on delete cascade, | ||
id varchar(21) not null, | ||
user_id varchar(12) not null references users (id) on update cascade on delete cascade, | ||
/** Unique provider identifier. Issuer of the OIDC connectors, entityId of the SAML providers */ | ||
issuer varchar(256) not null, | ||
/** Provider user identity id*/ | ||
identity_id varchar(128) not null, | ||
detail jsonb /* @use JsonObject */ not null default '{}'::jsonb, | ||
created_at timestamp not null default(now()), | ||
primary key (id), | ||
constraint user_sso_identities__issuer__identity_id | ||
unique (tenant_id, issuer, identity_id) | ||
); |