feat(identity): personal API tokens, created and revoked from the dashboard, act as their owner

This commit is contained in:
sam committed 2026-10-04 18:10:28 +11:00
1 parent 4d1d874676
commit 4dac44f98a
21 files changed
+2167 -1205

No files matched your search

@@ -230,6 +230,56 @@ func (s *Store) BindSubject(ctx context.Context, id identity.IdentityID, subject
return tx.Commit(ctx)
}
var _ identity.PersonalTokenStore = (*Store)(nil)
// SubjectsOf lists an identity's bindings under one issuer, newest first.
func (s *Store) SubjectsOf(ctx context.Context, id identity.IdentityID, issuer string) ([]identity.PersonalToken, error) {
rows, err := postgresdb.New(s.pool).ListIdentitySubjects(ctx, postgresdb.ListIdentitySubjectsParams{IdentityID: string(id), Issuer: issuer})
if err != nil {
return nil, err
}
out := make([]identity.PersonalToken, 0, len(rows))
for _, row := range rows {
out = append(out, identity.PersonalToken{ID: row.Subject, CreatedAt: row.BoundAt})
}
return out, nil
}
// UnbindSubject removes one of an identity's bindings; a binding the
// identity does not hold is ErrNotFound, so no caller can remove another's.
func (s *Store) UnbindSubject(ctx context.Context, id identity.IdentityID, subject identity.Subject, audit identity.Audit) error {
if audit.ActorID == "" || audit.Source == "" || audit.RequestID == "" {
return fmt.Errorf("%w: audit actor, source, and request ID are required", identity.ErrInvalid)
}
now := audit.At.UTC()
if now.IsZero() {
now = time.Now().UTC()
}
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
q := postgresdb.New(tx)
current, err := q.GetIdentity(ctx, string(id))
if errors.Is(err, pgx.ErrNoRows) {
return identity.ErrNotFound
}
if err != nil {
return err
}
deleted, err := q.DeleteIdentitySubject(ctx, postgresdb.DeleteIdentitySubjectParams{
IdentityID: string(id), Issuer: subject.Issuer, Subject: subject.Subject,
})
if err != nil {
return err
}
if deleted == 0 {
return identity.ErrNotFound
}
cur := identityFromRow(current)
return insertIdentityEvent(ctx, q, id, identity.Event{
IdentityID: id, Type: "unbind_subject", From: cur.Lifecycle, To: cur.Lifecycle, DisplayName: subject.Subject,
}, audit, now)
})
}
// BootstrapIdentities seeds the system identity and the legacy names, and
// moves tasks carrying a legacy name to its stable id. Invalid names are
// errors.
@@ -10,6 +10,24 @@ import (
"time"
)
const deleteIdentitySubject = `-- name: DeleteIdentitySubject :execrows
DELETE FROM identity_subjects WHERE identity_id = $1 AND issuer = $2 AND subject = $3
`
type DeleteIdentitySubjectParams struct {
IdentityID string
Issuer string
Subject string
}
func (q *Queries) DeleteIdentitySubject(ctx context.Context, arg DeleteIdentitySubjectParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteIdentitySubject, arg.IdentityID, arg.Issuer, arg.Subject)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getIdentity = `-- name: GetIdentity :one
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
FROM identities WHERE id = $1
@@ -156,6 +174,42 @@ func (q *Queries) ListIdentities(ctx context.Context) ([]Identity, error) {
return items, nil
}
const listIdentitySubjects = `-- name: ListIdentitySubjects :many
SELECT subject, bound_at FROM identity_subjects
WHERE identity_id = $1 AND issuer = $2
ORDER BY bound_at DESC
`
type ListIdentitySubjectsParams struct {
IdentityID string
Issuer string
}
type ListIdentitySubjectsRow struct {
Subject string
BoundAt time.Time
}
func (q *Queries) ListIdentitySubjects(ctx context.Context, arg ListIdentitySubjectsParams) ([]ListIdentitySubjectsRow, error) {
rows, err := q.db.Query(ctx, listIdentitySubjects, arg.IdentityID, arg.Issuer)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListIdentitySubjectsRow
for rows.Next() {
var i ListIdentitySubjectsRow
if err := rows.Scan(&i.Subject, &i.BoundAt); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const resolveIdentityAlias = `-- name: ResolveIdentityAlias :one
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
FROM identities i JOIN identity_aliases a ON a.identity_id = i.id
@@ -46,3 +46,11 @@ ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id,
-- name: UpdateTaskIdentity :exec
UPDATE tasks SET identity = $1 WHERE identity = $2;
-- name: ListIdentitySubjects :many
SELECT subject, bound_at FROM identity_subjects
WHERE identity_id = $1 AND issuer = $2
ORDER BY bound_at DESC;
-- name: DeleteIdentitySubject :execrows
DELETE FROM identity_subjects WHERE identity_id = $1 AND issuer = $2 AND subject = $3;