package linkstore import ( "bytes" "context" "database/sql" "encoding/json" "errors" "io" "time" ) // Choice is what a member's choice proof holds (Home spec // 2026-11-14-oficina-id-native.md ยง8): the subject and authentication time // of an already verified and consumed assertion whose subject had neither a // link nor a Home-only person here, and whom the roster called a Member. // It is bound to one browser by its secret, lives ten minutes, and is // single-use: TakeChoice deletes it before reading it. type Choice struct { Subject string AuthTime time.Time } type choiceProof struct { Version int `json:"v"` Issuer string `json:"issuer"` Subject string `json:"subject"` AuthTime int64 `json:"auth"` Expires int64 `json:"expires"` } const choiceLifetime = 600 const maxChoices = 10000 func (s *Store) choicePurpose(id string) string { return "identity-choice/v1/" + id + "/" + s.origin } // StartChoice stores a sealed choice proof and returns its browser secret. // Only the hash of the secret enters the database. At capacity the oldest // proof is evicted: losing one costs that person a fresh sign-in. func (s *Store) StartChoice(ctx context.Context, c Choice, now time.Time) (string, error) { if !validSubject(c.Subject) || c.AuthTime.Unix() <= 0 { return "", ErrRefused } secret, err := newSecret() if err != nil { return "", err } id := hash(secret) tx, high, err := s.begin(ctx, now) if err != nil { return "", err } defer tx.Rollback() if _, err = tx.ExecContext(ctx, "DELETE FROM home_choice_proofs WHERE expires_at <= ?", high); err != nil { return "", ErrStorage } var count int if err = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_choice_proofs").Scan(&count); err != nil { return "", ErrStorage } if count >= maxChoices { if _, err = tx.ExecContext(ctx, "DELETE FROM home_choice_proofs WHERE id_hash IN (SELECT id_hash FROM home_choice_proofs ORDER BY expires_at, id_hash LIMIT ?)", count-maxChoices+1); err != nil { return "", ErrStorage } } p := choiceProof{Version: 1, Issuer: s.issuer, Subject: c.Subject, AuthTime: c.AuthTime.Unix(), Expires: high + choiceLifetime} raw, err := json.Marshal(p) if err != nil { return "", ErrStorage } sealed, err := s.seal.Seal(s.choicePurpose(id), raw) if err != nil { return "", ErrStorage } if _, err = tx.ExecContext(ctx, "INSERT INTO home_choice_proofs (id_hash, expires_at, payload) VALUES (?, ?, ?)", id, p.Expires, sealed); err != nil { return "", ErrStorage } if err = commit(tx); err != nil { return "", err } return secret, nil } // ChoiceLive reports whether secret names a live proof, consuming nothing: // the choose page's GET. ErrRefused for none, ErrStorage when unreadable. func (s *Store) ChoiceLive(ctx context.Context, secret string, now time.Time) error { id, ok := secretHash(secret) if !ok { return ErrRefused } var expires int64 var raw []byte err := s.db.QueryRowContext(ctx, "SELECT expires_at, payload FROM home_choice_proofs WHERE id_hash = ?", id).Scan(&expires, &raw) if errors.Is(err, sql.ErrNoRows) { return ErrRefused } if err != nil { return ErrStorage } _, err = s.openChoice(id, expires, raw, now.Unix()) return err } // TakeChoice consumes a proof in its own short transaction, deleting it // before interpreting it and committing either way, so a proof is used at // most once even when two choices race: one gets the Choice, the other // ErrRefused. The caller acts on the Choice with no transaction open. func (s *Store) TakeChoice(ctx context.Context, secret string, now time.Time) (Choice, error) { id, ok := secretHash(secret) if !ok { return Choice{}, ErrRefused } tx, high, err := s.begin(ctx, now) if err != nil { return Choice{}, err } defer tx.Rollback() var expires int64 var raw []byte err = tx.QueryRowContext(ctx, "DELETE FROM home_choice_proofs WHERE id_hash = ? RETURNING expires_at, payload", id).Scan(&expires, &raw) if errors.Is(err, sql.ErrNoRows) { return Choice{}, refuse(tx) } if err != nil { return Choice{}, ErrStorage } c, err := s.openChoice(id, expires, raw, high) if err != nil { return Choice{}, refuse(tx) } if err = commit(tx); err != nil { return Choice{}, err } return c, nil } // DropChoice deletes a proof by its secret, consuming it unused: sign-out. func (s *Store) DropChoice(ctx context.Context, secret string) error { id, ok := secretHash(secret) if !ok { return ErrRefused } if _, err := s.db.ExecContext(ctx, "DELETE FROM home_choice_proofs WHERE id_hash = ?", id); err != nil { return ErrStorage } return nil } func (s *Store) openChoice(id string, expires int64, raw []byte, now int64) (Choice, error) { if len(raw) > 4096 || expires <= now { return Choice{}, ErrRefused } opened, err := s.seal.Open(s.choicePurpose(id), raw) if err != nil { return Choice{}, ErrRefused } var p choiceProof dec := json.NewDecoder(bytes.NewReader(opened)) dec.DisallowUnknownFields() if dec.Decode(&p) != nil || dec.Decode(new(any)) != io.EOF { return Choice{}, ErrRefused } if p.Version != 1 || p.Issuer != s.issuer || p.Expires != expires || !validSubject(p.Subject) || p.AuthTime <= 0 { return Choice{}, ErrRefused } return Choice{Subject: p.Subject, AuthTime: time.Unix(p.AuthTime, 0)}, nil }