-- +goose Up -- Identity providers of the whole installation, such as Google or GitHub, -- for signing up and signing in; organizations' own IdPs stay in -- sso_providers. CREATE TABLE platform_identity_providers ( id uuid PRIMARY KEY, kind text NOT NULL CHECK (kind IN ('oidc', 'github')), name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100), -- The OIDC issuer, or the GitHub server (https://github.com or a GitHub -- Enterprise host). issuer text NOT NULL CHECK (issuer ~ '^https?://[^\s]+$' AND length(issuer) <= 500), client_id text NOT NULL CHECK (length(client_id) BETWEEN 1 AND 500), -- Sealed with the application keyring, bound to the provider id. client_secret bytea, -- Whether people without an account may sign up through it. allow_signup boolean NOT NULL DEFAULT true, enabled boolean NOT NULL DEFAULT true, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); -- Accounts are linked by subject, never by email. CREATE TABLE platform_identities ( provider_id uuid NOT NULL REFERENCES platform_identity_providers (id) ON DELETE CASCADE, subject text NOT NULL CHECK (length(subject) BETWEEN 1 AND 255), user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, email citext, created_at timestamptz NOT NULL DEFAULT now(), last_login_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (provider_id, subject) ); CREATE INDEX platform_identities_user_idx ON platform_identities (user_id); ALTER TABLE platform_identity_providers ENABLE ROW LEVEL SECURITY; ALTER TABLE platform_identity_providers FORCE ROW LEVEL SECURITY; -- Sign-in pages list them to everyone; only the system scope changes them. CREATE POLICY platform_identity_providers_read ON platform_identity_providers FOR SELECT USING (true); CREATE POLICY platform_identity_providers_write ON platform_identity_providers FOR ALL USING (app_scope_system()) WITH CHECK (app_scope_system()); ALTER TABLE platform_identities ENABLE ROW LEVEL SECURITY; ALTER TABLE platform_identities FORCE ROW LEVEL SECURITY; CREATE POLICY platform_identities_own ON platform_identities FOR ALL USING (app_scope_system() OR user_id = app_user_id()) WITH CHECK (app_scope_system() OR user_id = app_user_id()); -- +goose Down DROP TABLE platform_identities; DROP TABLE platform_identity_providers;