Files
uptimesebi/internal/migrations/sql/00045_platform_sso.sql
T
Sebastian UnterschützandClaude Opus 5.5 fe5c4fb24c
ci / build (pull_request) Successful in 5m13s
ci / lint (pull_request) Successful in 5m45s
ci / image (pull_request) Successful in 6m20s
ci / test (pull_request) Successful in 11m46s
feat: sign up and sign in with platform identity providers
Platform admins register identity providers for the whole installation:
any OpenID Connect provider, checked by discovery, and GitHub or GitHub
Enterprise through OAuth, which reads the primary email GitHub verified.
Sign-in pages offer them; the sign-up page offers those open to sign-up,
also where email sign-up is closed for lack of a mail server.

Accounts are reached only through identities stored by subject. A new
identity signs up only with an email the provider confirmed, within the
per-address limit and sign-up mode, and never takes over an existing
account, including through an alias of its address: its owner links
the provider in the security settings. The account's TOTP still applies
after the provider. Unlinking refuses to remove the last way to sign in,
and removing a provider refuses while accounts depend on it.

The test browser takes the CSRF token from pages, as the console does.

Closes #114

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-23 01:26:14 +01:00

50 lines
2.4 KiB
SQL

-- +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;