feat(webui): add a member by provider subject or email

POST /api/orgs/{id}/members binds a person by subject, or by email as a
placeholder. The first sign-in whose provider vouches for that email
claims it: the State Store moves the binding onto the provider's subject
in one statement, so a second account with the same address finds
nothing to claim and later sign-ins rest on the subject alone.
This commit is contained in:
sam committed 2026-10-11 03:54:49 +11:00
1 parent 9e1a40ce99
commit 37cbf2ef47
17 files changed
+2085 -1505

No files matched your search

@@ -187,6 +187,38 @@ func (s *Store) ResolveSubject(ctx context.Context, subject identity.Subject) (i
return identityFromRow(i), nil
}
// ClaimSubject moves a placeholder binding (a person added by email) onto
// the subject the provider asserts. The placeholder is gone afterwards, so a
// second account showing the same address finds nothing to claim.
func (s *Store) ClaimSubject(ctx context.Context, placeholder, subject identity.Subject) (identity.Identity, error) {
if err := subject.Validate(); err != nil {
return identity.Identity{}, err
}
now := time.Now().UTC()
var claimed identity.Identity
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := postgresdb.New(tx)
id, err := q.ClaimIdentitySubject(ctx, postgresdb.ClaimIdentitySubjectParams{
Issuer: placeholder.Issuer, Placeholder: placeholder.Subject, Subject: subject.Subject, BoundAt: now,
})
if errors.Is(err, pgx.ErrNoRows) {
return identity.ErrNotFound
}
if err != nil {
return err
}
row, err := q.GetIdentity(ctx, id)
if err != nil {
return err
}
claimed = identityFromRow(row)
return insertIdentityEvent(ctx, q, claimed.ID, identity.Event{
IdentityID: claimed.ID, Type: "claim_subject", From: claimed.Lifecycle, To: claimed.Lifecycle, DisplayName: subject.Subject,
}, identity.Audit{ActorID: claimed.ID, Source: "sign-in", RequestID: placeholder.Subject}, now)
})
return claimed, err
}
// BindSubject binds an identity to the provider subject that asserts it,
// moving any prior binding for that subject.
func (s *Store) BindSubject(ctx context.Context, id identity.IdentityID, subject identity.Subject, audit identity.Audit) error {
@@ -10,6 +10,33 @@ import (
"time"
)
const claimIdentitySubject = `-- name: ClaimIdentitySubject :one
UPDATE identity_subjects SET subject = $1, bound_at = $2
WHERE issuer = $3 AND subject = $4
RETURNING identity_id
`
type ClaimIdentitySubjectParams struct {
Subject string
BoundAt time.Time
Issuer string
Placeholder string
}
// Moves a placeholder binding onto the subject the provider asserts, in one
// statement, so only the first claimant gets it.
func (q *Queries) ClaimIdentitySubject(ctx context.Context, arg ClaimIdentitySubjectParams) (string, error) {
row := q.db.QueryRow(ctx, claimIdentitySubject,
arg.Subject,
arg.BoundAt,
arg.Issuer,
arg.Placeholder,
)
var identity_id string
err := row.Scan(&identity_id)
return identity_id, err
}
const deleteIdentitySubject = `-- name: DeleteIdentitySubject :execrows
DELETE FROM identity_subjects WHERE identity_id = $1 AND issuer = $2 AND subject = $3
`
@@ -39,6 +39,13 @@ SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.upd
FROM identities i JOIN identity_subjects b ON b.identity_id = i.id
WHERE b.issuer = $1 AND b.subject = $2;
-- name: ClaimIdentitySubject :one
-- Moves a placeholder binding onto the subject the provider asserts, in one
-- statement, so only the first claimant gets it.
UPDATE identity_subjects SET subject = @subject, bound_at = @bound_at
WHERE issuer = @issuer AND subject = @placeholder
RETURNING identity_id;
-- name: UpsertIdentitySubject :exec
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at)
VALUES ($1, $2, $3, $4)