Files
archie/modules/governance/internal/store/links.go
T

95 lines
3.5 KiB
Go

package store
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/jmoiron/sqlx"
)
// LinkCode is a stored single-use code that links a channel credential to the
// account that issued it.
type LinkCode struct {
IdentityID string `db:"identity_id"`
Issuer string `db:"issuer"`
ExpiresAt time.Time `db:"expires_at"`
UsedAt sql.NullTime `db:"used_at"`
Lifecycle string `db:"lifecycle"`
}
// PutLinkCode stores the hash of a code issued by an account for one channel
// connector, and drops that account's codes that have run out.
func (s *Store) PutLinkCode(ctx context.Context, hash, identityID, issuer string, now, expires time.Time) error {
return s.withTx(ctx, func(tx *sqlx.Tx) error {
if _, err := tx.ExecContext(ctx, `
DELETE FROM governance.link_codes WHERE identity_id = $1 AND (expires_at <= $2 OR used_at IS NOT NULL)`,
identityID, now); err != nil {
return fmt.Errorf("drop spent link codes: %w", err)
}
if _, err := tx.ExecContext(ctx, `
INSERT INTO governance.link_codes (code_hash, identity_id, issuer, expires_at) VALUES ($1, $2, $3, $4)`,
hash, identityID, issuer, expires); err != nil {
return fmt.Errorf("store link code: %w", err)
}
return nil
})
}
// RedeemLinkCode links the channel credential (issuer, subject) to the account
// that issued the code, once. The code row is locked while check judges it, so
// two redemptions of one code can't both pass. check also gets the account the
// credential is already linked to, empty when none; a refusal leaves the code
// unspent.
func (s *Store) RedeemLinkCode(ctx context.Context, hash, subject, name string, now time.Time, check func(code LinkCode, linkedTo string) error) (string, error) {
var account string
err := s.withTx(ctx, func(tx *sqlx.Tx) error {
var code LinkCode
err := tx.GetContext(ctx, &code, `
SELECT c.identity_id, c.issuer, c.expires_at, c.used_at, i.lifecycle
FROM governance.link_codes c JOIN governance.identities i ON i.id = c.identity_id
WHERE c.code_hash = $1
FOR UPDATE OF c`, hash)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
if err != nil {
return fmt.Errorf("read link code: %w", err)
}
var bound string
err = tx.GetContext(ctx, &bound, `
SELECT identity_id FROM governance.identity_subjects WHERE issuer = $1 AND subject = $2 FOR UPDATE`,
code.Issuer, subject)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("read channel credential: %w", err)
}
if err := check(code, bound); err != nil {
return err
}
if _, err := tx.ExecContext(ctx, `UPDATE governance.link_codes SET used_at = $2 WHERE code_hash = $1`, hash, now); err != nil {
return fmt.Errorf("spend link code: %w", err)
}
if _, err := tx.ExecContext(ctx, `
INSERT INTO governance.identity_subjects (issuer, subject, identity_id, bound_at, name)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (issuer, subject) DO UPDATE SET name = EXCLUDED.name`,
code.Issuer, subject, code.IdentityID, now, name); err != nil {
return fmt.Errorf("link channel credential: %w", err)
}
orgID, err := auditSubjectOrg(ctx, tx, "", code.IdentityID)
if err != nil {
return err
}
account = code.IdentityID
return insertIdentityEvent(ctx, tx, IdentityEventRow{
IdentityID: account, EventType: "bind_subject",
FromLifecycle: code.Lifecycle, ToLifecycle: code.Lifecycle,
DisplayName: code.Issuer + ":" + subject, ActorID: account, Source: "channel link",
RequestID: "link:" + hash, At: now, OrgID: orgID,
})
})
return account, err
}