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

+7
View File
@@ -103,14 +103,21 @@ func (v *Verifier) Verify(ctx context.Context, rawToken string) (identity.Creden
ClientID string `json:"client_id"`
Scope string `json:"scope"`
Scopes []string `json:"scp"`
Email string `json:"email"`
Verified bool `json:"email_verified"`
}
_ = token.Claims(&claims)
subject, err := subjectFor(token.Issuer, claims.Subject, claims.ClientID)
if err != nil {
return identity.Credential{}, err
}
email := ""
if claims.Verified {
email = strings.TrimSpace(claims.Email)
}
return identity.Credential{
Subject: subject,
Email: email,
Scopes: scopesFor(claims.Scope, claims.Scopes),
Expires: token.Expiry,
}, nil
@@ -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)
@@ -80,6 +80,19 @@ func (s *server) ResolveIdentitySubject(ctx context.Context, request *pb.Resolve
return &pb.ResolveIdentitySubjectResponse{Identity: identityProto(value)}, nil
}
func (s *server) ClaimIdentitySubject(ctx context.Context, request *pb.ClaimIdentitySubjectRequest) (*pb.ClaimIdentitySubjectResponse, error) {
if s.deps.SubjectBindings == nil {
return nil, status.Error(codes.Unavailable, "identity subject bindings unavailable")
}
value, err := s.deps.SubjectBindings.ClaimSubject(ctx,
identity.Subject{Issuer: request.Issuer, Subject: request.Placeholder},
identity.Subject{Issuer: request.Issuer, Subject: request.Subject})
if err != nil {
return nil, identityStatus(err)
}
return &pb.ClaimIdentitySubjectResponse{Identity: identityProto(value)}, nil
}
func (s *server) BindIdentitySubject(ctx context.Context, request *pb.BindIdentitySubjectRequest) (*pb.BindIdentitySubjectResponse, error) {
if s.deps.SubjectBindings == nil {
return nil, status.Error(codes.Unavailable, "identity subject bindings unavailable")
@@ -103,6 +116,14 @@ func (c *Client) ResolveSubject(ctx context.Context, subject identity.Subject) (
return identityValue(response.Identity), nil
}
func (c *Client) ClaimSubject(ctx context.Context, placeholder, subject identity.Subject) (identity.Identity, error) {
response, err := c.client.ClaimIdentitySubject(ctx, &pb.ClaimIdentitySubjectRequest{Issuer: subject.Issuer, Placeholder: placeholder.Subject, Subject: subject.Subject})
if err != nil {
return identity.Identity{}, identityClientError(err)
}
return identityValue(response.Identity), nil
}
func (c *Client) BindSubject(ctx context.Context, id identity.IdentityID, subject identity.Subject, audit identity.Audit) error {
_, err := c.client.BindIdentitySubject(ctx, &pb.BindIdentitySubjectRequest{Id: string(id), Issuer: subject.Issuer, Subject: subject.Subject, Audit: auditProto(audit)})
return identityClientError(err)