| 1 | // Package teamstate keeps the Home-only people a roster admitted in team |
| 2 | // mode, and the reverse lookup from a person to the Home subject the |
| 3 | // roster knows them by. |
| 4 | // |
| 5 | // The marker and the roster evidence are not here: they are |
| 6 | // home/teammode's State ("calls", set calls_team), merged beside this |
| 7 | // package's own set (calls_team_home). Calls' people are its persons |
| 8 | // table, whose email column is NOT NULL and UNIQUE, so a Home-only |
| 9 | // person is a persons row whose email is a random "home-only:" |
| 10 | // placeholder that holds no address — no "@", never mailed, never shown |
| 11 | // — and this package's table says which Home subject it belongs to. |
| 12 | // |
| 13 | // None of it is standing. A Home-only row says who a Home subject is on |
| 14 | // this instance, never whether they are on the team: that is the |
| 15 | // roster's answer, asked again on every request. |
| 16 | package teamstate |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "crypto/rand" |
| 21 | "database/sql" |
| 22 | "embed" |
| 23 | "encoding/hex" |
| 24 | "errors" |
| 25 | "strings" |
| 26 | "time" |
| 27 | "unicode/utf8" |
| 28 | |
| 29 | "amadan.net/rastrillo/rastrillo/migrate" |
| 30 | ) |
| 31 | |
| 32 | //go:embed migrations/*.sql |
| 33 | var migrations embed.FS |
| 34 | |
| 35 | // Schema is the Home-only persons table and its triggers. They name |
| 36 | // persons and home_identity_links, so it is merged after Calls' own |
| 37 | // schema and linkstore's (team_config.go's teamBoot). |
| 38 | var Schema = migrate.MustFromFS(migrations, "calls_team_home") |
| 39 | |
| 40 | var ( |
| 41 | // ErrRefused is a definite "no": no such person, or a subject that |
| 42 | // cannot be one. |
| 43 | ErrRefused = errors.New("calls team: refused") |
| 44 | // ErrUnavailable is "cannot read it now". |
| 45 | ErrUnavailable = errors.New("calls team: unavailable") |
| 46 | ) |
| 47 | |
| 48 | // PlaceholderPrefix starts every Home-only person's email column. The |
| 49 | // triggers check it, and Addressless recognises it: nothing that sends |
| 50 | // mail or shows an address may use such a value. |
| 51 | const PlaceholderPrefix = "home-only:" |
| 52 | |
| 53 | // Addressless reports whether a persons.email value is a Home-only |
| 54 | // placeholder rather than an address. An anonymous guest's "guest:" value |
| 55 | // is not one: it is never a Home identity. |
| 56 | func Addressless(email string) bool { |
| 57 | return strings.HasPrefix(email, PlaceholderPrefix) |
| 58 | } |
| 59 | |
| 60 | // HomePerson reads the person a Home-only identity was given. It never |
| 61 | // creates one: ErrRefused means there is none. |
| 62 | func HomePerson(ctx context.Context, db *sql.DB, issuer, subject string) (int64, error) { |
| 63 | if !Opaque(subject) { |
| 64 | return 0, ErrRefused |
| 65 | } |
| 66 | var id int64 |
| 67 | e := db.QueryRowContext(ctx, "SELECT person_id FROM calls_team_home_persons WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) |
| 68 | if errors.Is(e, sql.ErrNoRows) { |
| 69 | return 0, ErrRefused |
| 70 | } |
| 71 | if e != nil { |
| 72 | return 0, ErrUnavailable |
| 73 | } |
| 74 | return id, nil |
| 75 | } |
| 76 | |
| 77 | // HomeSubject is the reverse: the Home subject a person is known by on |
| 78 | // this instance, through a Home-only row or an explicit account link. |
| 79 | // ErrRefused means neither, which is no team standing, never an error. |
| 80 | func HomeSubject(ctx context.Context, db *sql.DB, issuer string, person int64) (string, error) { |
| 81 | var subject string |
| 82 | e := db.QueryRowContext(ctx, `SELECT subject FROM calls_team_home_persons WHERE issuer=? AND person_id=? |
| 83 | UNION ALL SELECT subject FROM home_identity_links WHERE issuer=? AND person_id=? LIMIT 1`, issuer, person, issuer, person).Scan(&subject) |
| 84 | if errors.Is(e, sql.ErrNoRows) { |
| 85 | return "", ErrRefused |
| 86 | } |
| 87 | if e != nil { |
| 88 | return "", ErrUnavailable |
| 89 | } |
| 90 | return subject, nil |
| 91 | } |
| 92 | |
| 93 | // PersonSubject is one person the roster can be asked about. |
| 94 | type PersonSubject struct { |
| 95 | PersonID int64 |
| 96 | Subject string |
| 97 | } |
| 98 | |
| 99 | // Subjects is every person on this instance with a Home subject — a |
| 100 | // Home-only row or an account link — for asking the roster about people |
| 101 | // who are not making the request (a knock's push recipients). It says |
| 102 | // who could be on the team, never who is. |
| 103 | func Subjects(ctx context.Context, db *sql.DB, issuer string) ([]PersonSubject, error) { |
| 104 | rows, e := db.QueryContext(ctx, `SELECT person_id, subject FROM calls_team_home_persons WHERE issuer=? |
| 105 | UNION ALL SELECT person_id, subject FROM home_identity_links WHERE issuer=?`, issuer, issuer) |
| 106 | if e != nil { |
| 107 | return nil, ErrUnavailable |
| 108 | } |
| 109 | defer rows.Close() |
| 110 | var out []PersonSubject |
| 111 | for rows.Next() { |
| 112 | var ps PersonSubject |
| 113 | if rows.Scan(&ps.PersonID, &ps.Subject) != nil { |
| 114 | return nil, ErrUnavailable |
| 115 | } |
| 116 | out = append(out, ps) |
| 117 | } |
| 118 | if rows.Err() != nil { |
| 119 | return nil, ErrUnavailable |
| 120 | } |
| 121 | return out, nil |
| 122 | } |
| 123 | |
| 124 | // EnsureHomePerson gives a Home subject an addressless person, or |
| 125 | // returns the one it already has; created says which. The caller must |
| 126 | // have just had a current roster answer of Member for this subject: this |
| 127 | // records identity, not standing, and a later request asks the roster |
| 128 | // again. A subject already linked to a local account is refused — that |
| 129 | // person signs in as that account. |
| 130 | // |
| 131 | // name is the display name the person goes by in a call's roster, which |
| 132 | // for a Home-only person is a label built from the subject, never an |
| 133 | // address. |
| 134 | func EnsureHomePerson(ctx context.Context, db *sql.DB, issuer, subject, name string) (id int64, created bool, err error) { |
| 135 | if !Opaque(subject) || issuer == "" || strings.Contains(name, "@") { |
| 136 | return 0, false, ErrRefused |
| 137 | } |
| 138 | tx, e := db.BeginTx(ctx, nil) |
| 139 | if e != nil { |
| 140 | return 0, false, ErrUnavailable |
| 141 | } |
| 142 | defer tx.Rollback() |
| 143 | var linked int |
| 144 | if e = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_identity_links WHERE issuer=? AND subject=?", issuer, subject).Scan(&linked); e != nil { |
| 145 | return 0, false, ErrUnavailable |
| 146 | } |
| 147 | if linked != 0 { |
| 148 | return 0, false, ErrRefused |
| 149 | } |
| 150 | e = tx.QueryRowContext(ctx, "SELECT person_id FROM calls_team_home_persons WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) |
| 151 | if e == nil { |
| 152 | if tx.Commit() != nil { |
| 153 | return 0, false, ErrUnavailable |
| 154 | } |
| 155 | return id, false, nil |
| 156 | } |
| 157 | if !errors.Is(e, sql.ErrNoRows) { |
| 158 | return 0, false, ErrUnavailable |
| 159 | } |
| 160 | placeholder, e := newPlaceholder() |
| 161 | if e != nil { |
| 162 | return 0, false, ErrUnavailable |
| 163 | } |
| 164 | res, e := tx.ExecContext(ctx, `INSERT INTO persons(email,name,created_at,anonymous) VALUES(?,?,?,0)`, placeholder, name, time.Now().UTC()) |
| 165 | if e != nil { |
| 166 | return 0, false, ErrUnavailable |
| 167 | } |
| 168 | if id, e = res.LastInsertId(); e != nil || id <= 0 { |
| 169 | return 0, false, ErrUnavailable |
| 170 | } |
| 171 | if _, e = tx.ExecContext(ctx, "INSERT INTO calls_team_home_persons(issuer,subject,person_id) VALUES(?,?,?)", issuer, subject, id); e != nil { |
| 172 | return 0, false, ErrUnavailable |
| 173 | } |
| 174 | if tx.Commit() != nil { |
| 175 | return 0, false, ErrUnavailable |
| 176 | } |
| 177 | return id, true, nil |
| 178 | } |
| 179 | |
| 180 | // newPlaceholder is the persons.email a Home-only person holds: the |
| 181 | // prefix and sixteen random bytes, unique (the column is) and never an |
| 182 | // address (no "@"). |
| 183 | func newPlaceholder() (string, error) { |
| 184 | b := make([]byte, 16) |
| 185 | if _, err := rand.Read(b); err != nil { |
| 186 | return "", err |
| 187 | } |
| 188 | return PlaceholderPrefix + hex.EncodeToString(b), nil |
| 189 | } |
| 190 | |
| 191 | // Opaque is a Home subject's shape: bounded, printable, and never an |
| 192 | // address, so a subject can never be mistaken for one. |
| 193 | func Opaque(s string) bool { |
| 194 | if s == "" || len(s) > 512 || !utf8.ValidString(s) || strings.Contains(s, "@") { |
| 195 | return false |
| 196 | } |
| 197 | for _, r := range s { |
| 198 | if r <= 32 || r == 127 { |
| 199 | return false |
| 200 | } |
| 201 | } |
| 202 | return true |
| 203 | } |
| 204 | |