Files
uptimesebi/internal/store/queries/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

54 lines
2.2 KiB
SQL

-- name: CreatePlatformIDP :one
INSERT INTO platform_identity_providers (id, kind, name, issuer, client_id, client_secret, allow_signup, enabled)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;
-- name: UpdatePlatformIDP :one
UPDATE platform_identity_providers
SET name = $2, issuer = $3, client_id = $4, client_secret = coalesce(sqlc.narg(client_secret), client_secret),
allow_signup = $5, enabled = $6, updated_at = now()
WHERE id = $1
RETURNING *;
-- name: GetPlatformIDP :one
SELECT * FROM platform_identity_providers WHERE id = $1;
-- name: ListPlatformIDPs :many
SELECT * FROM platform_identity_providers ORDER BY name;
-- name: DeletePlatformIDP :execrows
DELETE FROM platform_identity_providers WHERE id = $1;
-- name: GetPlatformIdentity :one
SELECT * FROM platform_identities WHERE provider_id = $1 AND subject = $2;
-- name: CreatePlatformIdentity :exec
INSERT INTO platform_identities (provider_id, subject, user_id, email) VALUES ($1, $2, $3, $4);
-- name: TouchPlatformIdentity :exec
UPDATE platform_identities SET last_login_at = now(), email = coalesce(sqlc.narg(email), email)
WHERE provider_id = $1 AND subject = $2;
-- name: ListUserPlatformIdentities :many
SELECT i.provider_id, i.email, i.created_at, p.name AS provider_name, p.kind
FROM platform_identities i JOIN platform_identity_providers p ON p.id = i.provider_id
WHERE i.user_id = $1
ORDER BY p.name;
-- name: DeletePlatformIdentity :execrows
DELETE FROM platform_identities WHERE user_id = $1 AND provider_id = $2;
-- name: CountUserSignIns :one
-- Ways the account can sign in besides platform identities.
SELECT (password_hash IS NOT NULL)::integer
+ (SELECT count(*) FROM user_identities ui WHERE ui.user_id = users.id)::integer
+ (SELECT count(*) FROM platform_identities pi WHERE pi.user_id = users.id)::integer AS ways
FROM users WHERE id = $1;
-- name: SoleSignInUsers :one
-- Accounts that can sign in only through this provider.
SELECT count(*) FROM platform_identities pi JOIN users u ON u.id = pi.user_id
WHERE pi.provider_id = $1 AND u.password_hash IS NULL
AND NOT EXISTS (SELECT 1 FROM user_identities ui WHERE ui.user_id = u.id)
AND NOT EXISTS (SELECT 1 FROM platform_identities o WHERE o.user_id = u.id AND o.provider_id <> pi.provider_id);