| 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 ("memoria", set memoria_team), merged beside this |
| 7 | // package's own set (memoria_team_home). Memoria's local people are its |
| 8 | // memoria_home_local_identities rows, the IDs connect's links name, so a |
| 9 | // Home-only person is one of those rows holding no address — an empty |
| 10 | // sealed blob and a lookup no address fingerprint can equal — and this |
| 11 | // package's table says which Home subject it belongs to. Sharing the row |
| 12 | // is what keeps the two kinds of person in one ID space: connect's |
| 13 | // same-person rule compares IDs, and a Home-only ID that could equal a |
| 14 | // local account's would let one arrive over the other's session. |
| 15 | // |
| 16 | // None of it is standing. A Home-only row says who a Home subject is on |
| 17 | // this instance, never whether they are on the team: that is the |
| 18 | // roster's answer, asked again on every request. |
| 19 | package teamstate |
| 20 | |
| 21 | import ( |
| 22 | "context" |
| 23 | "database/sql" |
| 24 | "embed" |
| 25 | "errors" |
| 26 | "strings" |
| 27 | "unicode/utf8" |
| 28 | |
| 29 | "github.com/google/uuid" |
| 30 | |
| 31 | "amadan.net/rastrillo/rastrillo/migrate" |
| 32 | ) |
| 33 | |
| 34 | //go:embed migrations/*.sql |
| 35 | var migrations embed.FS |
| 36 | |
| 37 | // Schema is the Home-only members table and its triggers. They name |
| 38 | // memoria_home_local_identities and home_identity_links, so it is merged |
| 39 | // after the Home identity and link store sets (team_config.go's teamBoot). |
| 40 | var Schema = migrate.MustFromFS(migrations, "memoria_team_home") |
| 41 | |
| 42 | // homeOnlyLookup prefixes a Home-only person's address_lookup. A real |
| 43 | // lookup is a hex HMAC (seal.Fingerprint), which never contains ':', so no |
| 44 | // address can be looked up to one of these rows; the table's trigger |
| 45 | // insists on the prefix and an empty sealed address. |
| 46 | const homeOnlyLookup = "home-only:" |
| 47 | |
| 48 | var ( |
| 49 | // ErrRefused is a definite "no": no such person, or a subject that |
| 50 | // cannot be one. |
| 51 | ErrRefused = errors.New("memoria team: refused") |
| 52 | // ErrUnavailable is "cannot read it now". |
| 53 | ErrUnavailable = errors.New("memoria team: unavailable") |
| 54 | ) |
| 55 | |
| 56 | // HomeMember reads the person a Home-only identity was given. It never |
| 57 | // creates one: ErrRefused means there is none. |
| 58 | func HomeMember(ctx context.Context, db *sql.DB, issuer, subject string) (int64, error) { |
| 59 | if !Opaque(subject) { |
| 60 | return 0, ErrRefused |
| 61 | } |
| 62 | var id int64 |
| 63 | e := db.QueryRowContext(ctx, "SELECT person_id FROM memoria_team_home_members WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) |
| 64 | if errors.Is(e, sql.ErrNoRows) { |
| 65 | return 0, ErrRefused |
| 66 | } |
| 67 | if e != nil { |
| 68 | return 0, ErrUnavailable |
| 69 | } |
| 70 | return id, nil |
| 71 | } |
| 72 | |
| 73 | // LinkedSubject is the Home subject a local account is linked to, through |
| 74 | // connect's link table. ErrRefused means no link, which is no team |
| 75 | // standing, never an error. |
| 76 | func LinkedSubject(ctx context.Context, db *sql.DB, issuer string, person int64) (string, error) { |
| 77 | var subject string |
| 78 | e := db.QueryRowContext(ctx, "SELECT subject FROM home_identity_links WHERE issuer=? AND person_id=?", issuer, person).Scan(&subject) |
| 79 | if errors.Is(e, sql.ErrNoRows) { |
| 80 | return "", ErrRefused |
| 81 | } |
| 82 | if e != nil { |
| 83 | return "", ErrUnavailable |
| 84 | } |
| 85 | return subject, nil |
| 86 | } |
| 87 | |
| 88 | // EnsureHomeMember gives a Home subject an addressless person, or returns |
| 89 | // the one it already has. The caller must have just had a current roster |
| 90 | // answer of Member for this subject: this records identity, not standing, |
| 91 | // and a later request asks the roster again. A subject already linked to |
| 92 | // a local account is refused — that person signs in as that account. |
| 93 | func EnsureHomeMember(ctx context.Context, db *sql.DB, issuer, subject string) (int64, error) { |
| 94 | if !Opaque(subject) || issuer == "" { |
| 95 | return 0, ErrRefused |
| 96 | } |
| 97 | tx, e := db.BeginTx(ctx, nil) |
| 98 | if e != nil { |
| 99 | return 0, ErrUnavailable |
| 100 | } |
| 101 | defer tx.Rollback() |
| 102 | var linked int |
| 103 | if e = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_identity_links WHERE issuer=? AND subject=?", issuer, subject).Scan(&linked); e != nil { |
| 104 | return 0, ErrUnavailable |
| 105 | } |
| 106 | if linked != 0 { |
| 107 | return 0, ErrRefused |
| 108 | } |
| 109 | var id int64 |
| 110 | e = tx.QueryRowContext(ctx, "SELECT person_id FROM memoria_team_home_members WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) |
| 111 | if e == nil { |
| 112 | if tx.Commit() != nil { |
| 113 | return 0, ErrUnavailable |
| 114 | } |
| 115 | return id, nil |
| 116 | } |
| 117 | if !errors.Is(e, sql.ErrNoRows) { |
| 118 | return 0, ErrUnavailable |
| 119 | } |
| 120 | // The person row: no address, sealed or otherwise, and a lookup that |
| 121 | // is random rather than derived from the subject, so nothing about |
| 122 | // the person is written twice. |
| 123 | res, e := tx.ExecContext(ctx, "INSERT INTO memoria_home_local_identities(address_lookup,address_sealed) VALUES(?,?)", homeOnlyLookup+uuid.NewString(), []byte{}) |
| 124 | if e != nil { |
| 125 | return 0, ErrUnavailable |
| 126 | } |
| 127 | if id, e = res.LastInsertId(); e != nil || id <= 0 { |
| 128 | return 0, ErrUnavailable |
| 129 | } |
| 130 | if _, e = tx.ExecContext(ctx, "INSERT INTO memoria_team_home_members(issuer,subject,person_id) VALUES(?,?,?)", issuer, subject, id); e != nil { |
| 131 | return 0, ErrUnavailable |
| 132 | } |
| 133 | if tx.Commit() != nil { |
| 134 | return 0, ErrUnavailable |
| 135 | } |
| 136 | return id, nil |
| 137 | } |
| 138 | |
| 139 | // Opaque is a Home subject's shape: bounded, printable, and never an |
| 140 | // address, so a subject can never be mistaken for one. |
| 141 | func Opaque(s string) bool { |
| 142 | if s == "" || len(s) > 512 || !utf8.ValidString(s) || strings.Contains(s, "@") { |
| 143 | return false |
| 144 | } |
| 145 | for _, r := range s { |
| 146 | if r <= 32 || r == 127 { |
| 147 | return false |
| 148 | } |
| 149 | } |
| 150 | return true |
| 151 | } |
| 152 | |