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
This commit is contained in:
1 parent
1ad68e2e87
commit
fe5c4fb24c
26 files changed
+2710
-351
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user