| 1 | package linkstore |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "database/sql" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // Choice is what a member's choice proof holds (Home spec |
| 14 | // 2026-11-14-oficina-id-native.md §8): the subject and authentication time |
| 15 | // of an already verified and consumed assertion whose subject had neither a |
| 16 | // link nor a Home-only person here, and whom the roster called a Member. |
| 17 | // It is bound to one browser by its secret, lives ten minutes, and is |
| 18 | // single-use: TakeChoice deletes it before reading it. |
| 19 | type Choice struct { |
| 20 | Subject string |
| 21 | AuthTime time.Time |
| 22 | } |
| 23 | |
| 24 | type choiceProof struct { |
| 25 | Version int `json:"v"` |
| 26 | Issuer string `json:"issuer"` |
| 27 | Subject string `json:"subject"` |
| 28 | AuthTime int64 `json:"auth"` |
| 29 | Expires int64 `json:"expires"` |
| 30 | } |
| 31 | |
| 32 | const choiceLifetime = 600 |
| 33 | const maxChoices = 10000 |
| 34 | |
| 35 | func (s *Store) choicePurpose(id string) string { |
| 36 | return "identity-choice/v1/" + id + "/" + s.origin |
| 37 | } |
| 38 | |
| 39 | // StartChoice stores a sealed choice proof and returns its browser secret. |
| 40 | // Only the hash of the secret enters the database. At capacity the oldest |
| 41 | // proof is evicted: losing one costs that person a fresh sign-in. |
| 42 | func (s *Store) StartChoice(ctx context.Context, c Choice, now time.Time) (string, error) { |
| 43 | if !validSubject(c.Subject) || c.AuthTime.Unix() <= 0 { |
| 44 | return "", ErrRefused |
| 45 | } |
| 46 | secret, err := newSecret() |
| 47 | if err != nil { |
| 48 | return "", err |
| 49 | } |
| 50 | id := hash(secret) |
| 51 | tx, high, err := s.begin(ctx, now) |
| 52 | if err != nil { |
| 53 | return "", err |
| 54 | } |
| 55 | defer tx.Rollback() |
| 56 | if _, err = tx.ExecContext(ctx, "DELETE FROM home_choice_proofs WHERE expires_at <= ?", high); err != nil { |
| 57 | return "", ErrStorage |
| 58 | } |
| 59 | var count int |
| 60 | if err = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_choice_proofs").Scan(&count); err != nil { |
| 61 | return "", ErrStorage |
| 62 | } |
| 63 | if count >= maxChoices { |
| 64 | 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 { |
| 65 | return "", ErrStorage |
| 66 | } |
| 67 | } |
| 68 | p := choiceProof{Version: 1, Issuer: s.issuer, Subject: c.Subject, AuthTime: c.AuthTime.Unix(), Expires: high + choiceLifetime} |
| 69 | raw, err := json.Marshal(p) |
| 70 | if err != nil { |
| 71 | return "", ErrStorage |
| 72 | } |
| 73 | sealed, err := s.seal.Seal(s.choicePurpose(id), raw) |
| 74 | if err != nil { |
| 75 | return "", ErrStorage |
| 76 | } |
| 77 | if _, err = tx.ExecContext(ctx, "INSERT INTO home_choice_proofs (id_hash, expires_at, payload) VALUES (?, ?, ?)", id, p.Expires, sealed); err != nil { |
| 78 | return "", ErrStorage |
| 79 | } |
| 80 | if err = commit(tx); err != nil { |
| 81 | return "", err |
| 82 | } |
| 83 | return secret, nil |
| 84 | } |
| 85 | |
| 86 | // ChoiceLive reports whether secret names a live proof, consuming nothing: |
| 87 | // the choose page's GET. ErrRefused for none, ErrStorage when unreadable. |
| 88 | func (s *Store) ChoiceLive(ctx context.Context, secret string, now time.Time) error { |
| 89 | id, ok := secretHash(secret) |
| 90 | if !ok { |
| 91 | return ErrRefused |
| 92 | } |
| 93 | var expires int64 |
| 94 | var raw []byte |
| 95 | err := s.db.QueryRowContext(ctx, "SELECT expires_at, payload FROM home_choice_proofs WHERE id_hash = ?", id).Scan(&expires, &raw) |
| 96 | if errors.Is(err, sql.ErrNoRows) { |
| 97 | return ErrRefused |
| 98 | } |
| 99 | if err != nil { |
| 100 | return ErrStorage |
| 101 | } |
| 102 | _, err = s.openChoice(id, expires, raw, now.Unix()) |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | // TakeChoice consumes a proof in its own short transaction, deleting it |
| 107 | // before interpreting it and committing either way, so a proof is used at |
| 108 | // most once even when two choices race: one gets the Choice, the other |
| 109 | // ErrRefused. The caller acts on the Choice with no transaction open. |
| 110 | func (s *Store) TakeChoice(ctx context.Context, secret string, now time.Time) (Choice, error) { |
| 111 | id, ok := secretHash(secret) |
| 112 | if !ok { |
| 113 | return Choice{}, ErrRefused |
| 114 | } |
| 115 | tx, high, err := s.begin(ctx, now) |
| 116 | if err != nil { |
| 117 | return Choice{}, err |
| 118 | } |
| 119 | defer tx.Rollback() |
| 120 | var expires int64 |
| 121 | var raw []byte |
| 122 | err = tx.QueryRowContext(ctx, "DELETE FROM home_choice_proofs WHERE id_hash = ? RETURNING expires_at, payload", id).Scan(&expires, &raw) |
| 123 | if errors.Is(err, sql.ErrNoRows) { |
| 124 | return Choice{}, refuse(tx) |
| 125 | } |
| 126 | if err != nil { |
| 127 | return Choice{}, ErrStorage |
| 128 | } |
| 129 | c, err := s.openChoice(id, expires, raw, high) |
| 130 | if err != nil { |
| 131 | return Choice{}, refuse(tx) |
| 132 | } |
| 133 | if err = commit(tx); err != nil { |
| 134 | return Choice{}, err |
| 135 | } |
| 136 | return c, nil |
| 137 | } |
| 138 | |
| 139 | // DropChoice deletes a proof by its secret, consuming it unused: sign-out. |
| 140 | func (s *Store) DropChoice(ctx context.Context, secret string) error { |
| 141 | id, ok := secretHash(secret) |
| 142 | if !ok { |
| 143 | return ErrRefused |
| 144 | } |
| 145 | if _, err := s.db.ExecContext(ctx, "DELETE FROM home_choice_proofs WHERE id_hash = ?", id); err != nil { |
| 146 | return ErrStorage |
| 147 | } |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | func (s *Store) openChoice(id string, expires int64, raw []byte, now int64) (Choice, error) { |
| 152 | if len(raw) > 4096 || expires <= now { |
| 153 | return Choice{}, ErrRefused |
| 154 | } |
| 155 | opened, err := s.seal.Open(s.choicePurpose(id), raw) |
| 156 | if err != nil { |
| 157 | return Choice{}, ErrRefused |
| 158 | } |
| 159 | var p choiceProof |
| 160 | dec := json.NewDecoder(bytes.NewReader(opened)) |
| 161 | dec.DisallowUnknownFields() |
| 162 | if dec.Decode(&p) != nil || dec.Decode(new(any)) != io.EOF { |
| 163 | return Choice{}, ErrRefused |
| 164 | } |
| 165 | if p.Version != 1 || p.Issuer != s.issuer || p.Expires != expires || !validSubject(p.Subject) || p.AuthTime <= 0 { |
| 166 | return Choice{}, ErrRefused |
| 167 | } |
| 168 | return Choice{Subject: p.Subject, AuthTime: time.Unix(p.AuthTime, 0)}, nil |
| 169 | } |
| 170 | |