Identities are keyed by provider and subject. Both tables are tenant scoped; org_id_by_slug resolves a slug before sign-in without opening a broader scope. Refs #4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
71 lines
3.0 KiB
PL/PgSQL
71 lines
3.0 KiB
PL/PgSQL
-- +goose Up
|
|
CREATE TABLE sso_providers (
|
|
id uuid PRIMARY KEY,
|
|
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
|
|
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100),
|
|
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,
|
|
scopes text[] NOT NULL DEFAULT '{openid,email,profile}',
|
|
-- Lower-case domains; empty means any. Applies to auto-provisioning only.
|
|
allowed_domains text[] NOT NULL DEFAULT '{}',
|
|
auto_provision boolean NOT NULL DEFAULT false,
|
|
enabled boolean NOT NULL DEFAULT true,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX sso_providers_org_idx ON sso_providers (org_id);
|
|
|
|
-- Accounts are linked by issuer subject, never by email: an org admin
|
|
-- controls their IdP and could otherwise assert any address.
|
|
CREATE TABLE user_identities (
|
|
provider_id uuid NOT NULL REFERENCES sso_providers (id) ON DELETE CASCADE,
|
|
subject text NOT NULL CHECK (length(subject) BETWEEN 1 AND 255),
|
|
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
|
|
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 user_identities_user_idx ON user_identities (user_id);
|
|
|
|
ALTER TABLE sso_providers ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE sso_providers FORCE ROW LEVEL SECURITY;
|
|
CREATE POLICY sso_providers_tenant ON sso_providers FOR ALL
|
|
USING (app_scope_system() OR org_id = app_org_id())
|
|
WITH CHECK (app_scope_system() OR org_id = app_org_id());
|
|
|
|
ALTER TABLE user_identities ENABLE ROW LEVEL SECURITY;
|
|
ALTER TABLE user_identities FORCE ROW LEVEL SECURITY;
|
|
CREATE POLICY user_identities_tenant ON user_identities FOR ALL
|
|
USING (app_scope_system() OR org_id = app_org_id() OR user_id = app_user_id())
|
|
WITH CHECK (app_scope_system() OR org_id = app_org_id());
|
|
|
|
-- SSO starts before anyone is signed in. This reveals only whether a slug
|
|
-- exists, which the public /sso/<slug> URL does anyway. FORCE ROW LEVEL
|
|
-- SECURITY applies to the table owner too, so SECURITY DEFINER would not
|
|
-- help; the system scope is raised for this single lookup and restored.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION org_id_by_slug(s citext) RETURNS uuid
|
|
LANGUAGE plpgsql VOLATILE AS $$
|
|
DECLARE
|
|
prev text := coalesce(current_setting('app.scope', true), '');
|
|
result uuid;
|
|
BEGIN
|
|
PERFORM set_config('app.scope', 'system', true);
|
|
SELECT id INTO result FROM orgs WHERE slug = s;
|
|
PERFORM set_config('app.scope', prev, true);
|
|
RETURN result;
|
|
END
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- +goose Down
|
|
DROP FUNCTION org_id_by_slug(citext);
|
|
DROP TABLE user_identities;
|
|
DROP TABLE sso_providers;
|