// Package teamstate keeps the Home-only people a roster admitted in team // mode, and the reverse lookup from a person to the Home subject the // roster knows them by. // // The marker and the roster evidence are not here: they are // home/teammode's State ("calls", set calls_team), merged beside this // package's own set (calls_team_home). Calls' people are its persons // table, whose email column is NOT NULL and UNIQUE, so a Home-only // person is a persons row whose email is a random "home-only:" // placeholder that holds no address — no "@", never mailed, never shown // — and this package's table says which Home subject it belongs to. // // None of it is standing. A Home-only row says who a Home subject is on // this instance, never whether they are on the team: that is the // roster's answer, asked again on every request. package teamstate import ( "context" "crypto/rand" "database/sql" "embed" "encoding/hex" "errors" "strings" "time" "unicode/utf8" "amadan.net/rastrillo/rastrillo/migrate" ) //go:embed migrations/*.sql var migrations embed.FS // Schema is the Home-only persons table and its triggers. They name // persons and home_identity_links, so it is merged after Calls' own // schema and linkstore's (team_config.go's teamBoot). var Schema = migrate.MustFromFS(migrations, "calls_team_home") var ( // ErrRefused is a definite "no": no such person, or a subject that // cannot be one. ErrRefused = errors.New("calls team: refused") // ErrUnavailable is "cannot read it now". ErrUnavailable = errors.New("calls team: unavailable") ) // PlaceholderPrefix starts every Home-only person's email column. The // triggers check it, and Addressless recognises it: nothing that sends // mail or shows an address may use such a value. const PlaceholderPrefix = "home-only:" // Addressless reports whether a persons.email value is a Home-only // placeholder rather than an address. An anonymous guest's "guest:" value // is not one: it is never a Home identity. func Addressless(email string) bool { return strings.HasPrefix(email, PlaceholderPrefix) } // HomePerson reads the person a Home-only identity was given. It never // creates one: ErrRefused means there is none. func HomePerson(ctx context.Context, db *sql.DB, issuer, subject string) (int64, error) { if !Opaque(subject) { return 0, ErrRefused } var id int64 e := db.QueryRowContext(ctx, "SELECT person_id FROM calls_team_home_persons WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) if errors.Is(e, sql.ErrNoRows) { return 0, ErrRefused } if e != nil { return 0, ErrUnavailable } return id, nil } // HomeSubject is the reverse: the Home subject a person is known by on // this instance, through a Home-only row or an explicit account link. // ErrRefused means neither, which is no team standing, never an error. func HomeSubject(ctx context.Context, db *sql.DB, issuer string, person int64) (string, error) { var subject string e := db.QueryRowContext(ctx, `SELECT subject FROM calls_team_home_persons WHERE issuer=? AND person_id=? UNION ALL SELECT subject FROM home_identity_links WHERE issuer=? AND person_id=? LIMIT 1`, issuer, person, issuer, person).Scan(&subject) if errors.Is(e, sql.ErrNoRows) { return "", ErrRefused } if e != nil { return "", ErrUnavailable } return subject, nil } // PersonSubject is one person the roster can be asked about. type PersonSubject struct { PersonID int64 Subject string } // Subjects is every person on this instance with a Home subject — a // Home-only row or an account link — for asking the roster about people // who are not making the request (a knock's push recipients). It says // who could be on the team, never who is. func Subjects(ctx context.Context, db *sql.DB, issuer string) ([]PersonSubject, error) { rows, e := db.QueryContext(ctx, `SELECT person_id, subject FROM calls_team_home_persons WHERE issuer=? UNION ALL SELECT person_id, subject FROM home_identity_links WHERE issuer=?`, issuer, issuer) if e != nil { return nil, ErrUnavailable } defer rows.Close() var out []PersonSubject for rows.Next() { var ps PersonSubject if rows.Scan(&ps.PersonID, &ps.Subject) != nil { return nil, ErrUnavailable } out = append(out, ps) } if rows.Err() != nil { return nil, ErrUnavailable } return out, nil } // EnsureHomePerson gives a Home subject an addressless person, or // returns the one it already has; created says which. The caller must // have just had a current roster answer of Member for this subject: this // records identity, not standing, and a later request asks the roster // again. A subject already linked to a local account is refused — that // person signs in as that account. // // name is the display name the person goes by in a call's roster, which // for a Home-only person is a label built from the subject, never an // address. func EnsureHomePerson(ctx context.Context, db *sql.DB, issuer, subject, name string) (id int64, created bool, err error) { if !Opaque(subject) || issuer == "" || strings.Contains(name, "@") { return 0, false, ErrRefused } tx, e := db.BeginTx(ctx, nil) if e != nil { return 0, false, ErrUnavailable } defer tx.Rollback() var linked int if e = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_identity_links WHERE issuer=? AND subject=?", issuer, subject).Scan(&linked); e != nil { return 0, false, ErrUnavailable } if linked != 0 { return 0, false, ErrRefused } e = tx.QueryRowContext(ctx, "SELECT person_id FROM calls_team_home_persons WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) if e == nil { if tx.Commit() != nil { return 0, false, ErrUnavailable } return id, false, nil } if !errors.Is(e, sql.ErrNoRows) { return 0, false, ErrUnavailable } placeholder, e := newPlaceholder() if e != nil { return 0, false, ErrUnavailable } res, e := tx.ExecContext(ctx, `INSERT INTO persons(email,name,created_at,anonymous) VALUES(?,?,?,0)`, placeholder, name, time.Now().UTC()) if e != nil { return 0, false, ErrUnavailable } if id, e = res.LastInsertId(); e != nil || id <= 0 { return 0, false, ErrUnavailable } if _, e = tx.ExecContext(ctx, "INSERT INTO calls_team_home_persons(issuer,subject,person_id) VALUES(?,?,?)", issuer, subject, id); e != nil { return 0, false, ErrUnavailable } if tx.Commit() != nil { return 0, false, ErrUnavailable } return id, true, nil } // newPlaceholder is the persons.email a Home-only person holds: the // prefix and sixteen random bytes, unique (the column is) and never an // address (no "@"). func newPlaceholder() (string, error) { b := make([]byte, 16) if _, err := rand.Read(b); err != nil { return "", err } return PlaceholderPrefix + hex.EncodeToString(b), nil } // Opaque is a Home subject's shape: bounded, printable, and never an // address, so a subject can never be mistaken for one. func Opaque(s string) bool { if s == "" || len(s) > 512 || !utf8.ValidString(s) || strings.Contains(s, "@") { return false } for _, r := range s { if r <= 32 || r == 127 { return false } } return true }