feat: sign up and sign in with platform identity providers
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

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
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-23 01:26:14 +01:00
1 parent 1ad68e2e87
commit fe5c4fb24c
26 files changed
+2710 -351

No files matched your search

+22
View File
@@ -292,6 +292,28 @@ type Org struct {
LimitOwnRegionsCount *bool
}
type PlatformIdentity struct {
ProviderID uuid.UUID
Subject string
UserID uuid.UUID
Email *string
CreatedAt time.Time
LastLoginAt time.Time
}
type PlatformIdentityProvider struct {
ID uuid.UUID
Kind string
Name string
Issuer string
ClientID string
ClientSecret []byte
AllowSignup bool
Enabled bool
CreatedAt time.Time
UpdatedAt time.Time
}
type PlatformSetting struct {
Key string
Value []byte
+318
View File
@@ -0,0 +1,318 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: platform_sso.sql
package store
import (
"context"
"time"
"github.com/google/uuid"
)
const countUserSignIns = `-- name: CountUserSignIns :one
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
`
// Ways the account can sign in besides platform identities.
func (q *Queries) CountUserSignIns(ctx context.Context, id uuid.UUID) (int32, error) {
row := q.db.QueryRow(ctx, countUserSignIns, id)
var ways int32
err := row.Scan(&ways)
return ways, err
}
const createPlatformIDP = `-- 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 id, kind, name, issuer, client_id, client_secret, allow_signup, enabled, created_at, updated_at
`
type CreatePlatformIDPParams struct {
ID uuid.UUID
Kind string
Name string
Issuer string
ClientID string
ClientSecret []byte
AllowSignup bool
Enabled bool
}
func (q *Queries) CreatePlatformIDP(ctx context.Context, arg CreatePlatformIDPParams) (PlatformIdentityProvider, error) {
row := q.db.QueryRow(ctx, createPlatformIDP,
arg.ID,
arg.Kind,
arg.Name,
arg.Issuer,
arg.ClientID,
arg.ClientSecret,
arg.AllowSignup,
arg.Enabled,
)
var i PlatformIdentityProvider
err := row.Scan(
&i.ID,
&i.Kind,
&i.Name,
&i.Issuer,
&i.ClientID,
&i.ClientSecret,
&i.AllowSignup,
&i.Enabled,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const createPlatformIdentity = `-- name: CreatePlatformIdentity :exec
INSERT INTO platform_identities (provider_id, subject, user_id, email) VALUES ($1, $2, $3, $4)
`
type CreatePlatformIdentityParams struct {
ProviderID uuid.UUID
Subject string
UserID uuid.UUID
Email *string
}
func (q *Queries) CreatePlatformIdentity(ctx context.Context, arg CreatePlatformIdentityParams) error {
_, err := q.db.Exec(ctx, createPlatformIdentity,
arg.ProviderID,
arg.Subject,
arg.UserID,
arg.Email,
)
return err
}
const deletePlatformIDP = `-- name: DeletePlatformIDP :execrows
DELETE FROM platform_identity_providers WHERE id = $1
`
func (q *Queries) DeletePlatformIDP(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, deletePlatformIDP, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const deletePlatformIdentity = `-- name: DeletePlatformIdentity :execrows
DELETE FROM platform_identities WHERE user_id = $1 AND provider_id = $2
`
type DeletePlatformIdentityParams struct {
UserID uuid.UUID
ProviderID uuid.UUID
}
func (q *Queries) DeletePlatformIdentity(ctx context.Context, arg DeletePlatformIdentityParams) (int64, error) {
result, err := q.db.Exec(ctx, deletePlatformIdentity, arg.UserID, arg.ProviderID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getPlatformIDP = `-- name: GetPlatformIDP :one
SELECT id, kind, name, issuer, client_id, client_secret, allow_signup, enabled, created_at, updated_at FROM platform_identity_providers WHERE id = $1
`
func (q *Queries) GetPlatformIDP(ctx context.Context, id uuid.UUID) (PlatformIdentityProvider, error) {
row := q.db.QueryRow(ctx, getPlatformIDP, id)
var i PlatformIdentityProvider
err := row.Scan(
&i.ID,
&i.Kind,
&i.Name,
&i.Issuer,
&i.ClientID,
&i.ClientSecret,
&i.AllowSignup,
&i.Enabled,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const getPlatformIdentity = `-- name: GetPlatformIdentity :one
SELECT provider_id, subject, user_id, email, created_at, last_login_at FROM platform_identities WHERE provider_id = $1 AND subject = $2
`
type GetPlatformIdentityParams struct {
ProviderID uuid.UUID
Subject string
}
func (q *Queries) GetPlatformIdentity(ctx context.Context, arg GetPlatformIdentityParams) (PlatformIdentity, error) {
row := q.db.QueryRow(ctx, getPlatformIdentity, arg.ProviderID, arg.Subject)
var i PlatformIdentity
err := row.Scan(
&i.ProviderID,
&i.Subject,
&i.UserID,
&i.Email,
&i.CreatedAt,
&i.LastLoginAt,
)
return i, err
}
const listPlatformIDPs = `-- name: ListPlatformIDPs :many
SELECT id, kind, name, issuer, client_id, client_secret, allow_signup, enabled, created_at, updated_at FROM platform_identity_providers ORDER BY name
`
func (q *Queries) ListPlatformIDPs(ctx context.Context) ([]PlatformIdentityProvider, error) {
rows, err := q.db.Query(ctx, listPlatformIDPs)
if err != nil {
return nil, err
}
defer rows.Close()
items := []PlatformIdentityProvider{}
for rows.Next() {
var i PlatformIdentityProvider
if err := rows.Scan(
&i.ID,
&i.Kind,
&i.Name,
&i.Issuer,
&i.ClientID,
&i.ClientSecret,
&i.AllowSignup,
&i.Enabled,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listUserPlatformIdentities = `-- 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
`
type ListUserPlatformIdentitiesRow struct {
ProviderID uuid.UUID
Email *string
CreatedAt time.Time
ProviderName string
Kind string
}
func (q *Queries) ListUserPlatformIdentities(ctx context.Context, userID uuid.UUID) ([]ListUserPlatformIdentitiesRow, error) {
rows, err := q.db.Query(ctx, listUserPlatformIdentities, userID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ListUserPlatformIdentitiesRow{}
for rows.Next() {
var i ListUserPlatformIdentitiesRow
if err := rows.Scan(
&i.ProviderID,
&i.Email,
&i.CreatedAt,
&i.ProviderName,
&i.Kind,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const soleSignInUsers = `-- name: SoleSignInUsers :one
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)
`
// Accounts that can sign in only through this provider.
func (q *Queries) SoleSignInUsers(ctx context.Context, providerID uuid.UUID) (int64, error) {
row := q.db.QueryRow(ctx, soleSignInUsers, providerID)
var count int64
err := row.Scan(&count)
return count, err
}
const touchPlatformIdentity = `-- name: TouchPlatformIdentity :exec
UPDATE platform_identities SET last_login_at = now(), email = coalesce($3, email)
WHERE provider_id = $1 AND subject = $2
`
type TouchPlatformIdentityParams struct {
ProviderID uuid.UUID
Subject string
Email *string
}
func (q *Queries) TouchPlatformIdentity(ctx context.Context, arg TouchPlatformIdentityParams) error {
_, err := q.db.Exec(ctx, touchPlatformIdentity, arg.ProviderID, arg.Subject, arg.Email)
return err
}
const updatePlatformIDP = `-- name: UpdatePlatformIDP :one
UPDATE platform_identity_providers
SET name = $2, issuer = $3, client_id = $4, client_secret = coalesce($7, client_secret),
allow_signup = $5, enabled = $6, updated_at = now()
WHERE id = $1
RETURNING id, kind, name, issuer, client_id, client_secret, allow_signup, enabled, created_at, updated_at
`
type UpdatePlatformIDPParams struct {
ID uuid.UUID
Name string
Issuer string
ClientID string
AllowSignup bool
Enabled bool
ClientSecret []byte
}
func (q *Queries) UpdatePlatformIDP(ctx context.Context, arg UpdatePlatformIDPParams) (PlatformIdentityProvider, error) {
row := q.db.QueryRow(ctx, updatePlatformIDP,
arg.ID,
arg.Name,
arg.Issuer,
arg.ClientID,
arg.AllowSignup,
arg.Enabled,
arg.ClientSecret,
)
var i PlatformIdentityProvider
err := row.Scan(
&i.ID,
&i.Kind,
&i.Name,
&i.Issuer,
&i.ClientID,
&i.ClientSecret,
&i.AllowSignup,
&i.Enabled,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
+14
View File
@@ -51,6 +51,8 @@ type Querier interface {
CountRoleAssignments(ctx context.Context, roleID uuid.UUID) (int64, error)
CountStatusPages(ctx context.Context, orgID uuid.UUID) (int64, error)
CountUnusedRecoveryCodes(ctx context.Context, userID uuid.UUID) (int64, error)
// Ways the account can sign in besides platform identities.
CountUserSignIns(ctx context.Context, id uuid.UUID) (int32, error)
CreateAPIToken(ctx context.Context, arg CreateAPITokenParams) (ApiToken, error)
CreateAgent(ctx context.Context, arg CreateAgentParams) (Agent, error)
CreateAlertGroup(ctx context.Context, arg CreateAlertGroupParams) (AlertGroup, error)
@@ -63,6 +65,8 @@ type Querier interface {
CreateMaintenance(ctx context.Context, arg CreateMaintenanceParams) (MaintenanceWindow, error)
CreateMonitor(ctx context.Context, arg CreateMonitorParams) (Monitor, error)
CreateOrg(ctx context.Context, arg CreateOrgParams) (Org, error)
CreatePlatformIDP(ctx context.Context, arg CreatePlatformIDPParams) (PlatformIdentityProvider, error)
CreatePlatformIdentity(ctx context.Context, arg CreatePlatformIdentityParams) error
CreateRegion(ctx context.Context, arg CreateRegionParams) (Region, error)
CreateRole(ctx context.Context, arg CreateRoleParams) (Role, error)
CreateSSOProvider(ctx context.Context, arg CreateSSOProviderParams) (SsoProvider, error)
@@ -90,6 +94,8 @@ type Querier interface {
DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error
DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error)
DeleteOrgStorage(ctx context.Context, orgID uuid.UUID) error
DeletePlatformIDP(ctx context.Context, id uuid.UUID) (int64, error)
DeletePlatformIdentity(ctx context.Context, arg DeletePlatformIdentityParams) (int64, error)
DeleteRecoveryCodes(ctx context.Context, userID uuid.UUID) error
DeleteRegion(ctx context.Context, id uuid.UUID) (int64, error)
DeleteRole(ctx context.Context, id uuid.UUID) (int64, error)
@@ -139,6 +145,8 @@ type Querier interface {
GetMonitor(ctx context.Context, id uuid.UUID) (Monitor, error)
GetOrg(ctx context.Context, id uuid.UUID) (Org, error)
GetOrgBySlug(ctx context.Context, slug string) (Org, error)
GetPlatformIDP(ctx context.Context, id uuid.UUID) (PlatformIdentityProvider, error)
GetPlatformIdentity(ctx context.Context, arg GetPlatformIdentityParams) (PlatformIdentity, error)
GetPlatformSetting(ctx context.Context, key string) ([]byte, error)
GetRegion(ctx context.Context, id uuid.UUID) (Region, error)
GetRole(ctx context.Context, id uuid.UUID) (Role, error)
@@ -205,6 +213,7 @@ type Querier interface {
ListOrgMembersPage(ctx context.Context, arg ListOrgMembersPageParams) ([]ListOrgMembersPageRow, error)
ListOrgs(ctx context.Context) ([]Org, error)
ListPendingInvitations(ctx context.Context) ([]ListPendingInvitationsRow, error)
ListPlatformIDPs(ctx context.Context) ([]PlatformIdentityProvider, error)
ListPlatformRegions(ctx context.Context) ([]Region, error)
// A monitor's results newest first, a page from a keyset cursor (see
// paging.go) whose string key is region and checker.
@@ -215,6 +224,7 @@ type Querier interface {
ListStatusPages(ctx context.Context) ([]StatusPage, error)
ListUserIdentitiesInOrg(ctx context.Context, userID uuid.UUID) ([]ListUserIdentitiesInOrgRow, error)
ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUserOrgsRow, error)
ListUserPlatformIdentities(ctx context.Context, userID uuid.UUID) ([]ListUserPlatformIdentitiesRow, error)
// Row-level security limits this to platform regions and the scope's org;
// the org's own regions come first.
ListVisibleRegions(ctx context.Context) ([]Region, error)
@@ -304,6 +314,8 @@ type Querier interface {
SetUserOrgLimit(ctx context.Context, arg SetUserOrgLimitParams) (SetUserOrgLimitRow, error)
// Other instances of the same agent seen in the last day.
SiblingInstances(ctx context.Context, agentID uuid.UUID) ([]SiblingInstancesRow, error)
// Accounts that can sign in only through this provider.
SoleSignInUsers(ctx context.Context, providerID uuid.UUID) (int64, error)
StatusPageMonitors(ctx context.Context, pageID uuid.UUID) ([]StatusPageMonitorsRow, error)
// Deletes key and, as certmagic expects, everything below it.
StorageDelete(ctx context.Context, arg StorageDeleteParams) (int64, error)
@@ -318,12 +330,14 @@ type Querier interface {
// Writes at most once a minute per instance; agents poll far more often.
TouchAgentInstance(ctx context.Context, arg TouchAgentInstanceParams) error
TouchIdentity(ctx context.Context, arg TouchIdentityParams) error
TouchPlatformIdentity(ctx context.Context, arg TouchPlatformIdentityParams) error
UpdateAlertGroup(ctx context.Context, arg UpdateAlertGroupParams) (AlertGroup, error)
UpdateChannel(ctx context.Context, arg UpdateChannelParams) (NotificationChannel, error)
UpdateGroup(ctx context.Context, arg UpdateGroupParams) (MonitorGroup, error)
UpdateMaintenance(ctx context.Context, arg UpdateMaintenanceParams) (MaintenanceWindow, error)
UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (Monitor, error)
UpdateOrgPolicy(ctx context.Context, arg UpdateOrgPolicyParams) (Org, error)
UpdatePlatformIDP(ctx context.Context, arg UpdatePlatformIDPParams) (PlatformIdentityProvider, error)
UpdateRole(ctx context.Context, arg UpdateRoleParams) (Role, error)
UpdateSSOProvider(ctx context.Context, arg UpdateSSOProviderParams) (SsoProvider, error)
UpdateStatusPage(ctx context.Context, arg UpdateStatusPageParams) (StatusPage, error)
+53
View File
@@ -0,0 +1,53 @@
-- 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);