| 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 ("slides", set slides_team), merged beside this |
| 7 | // package's own set (slides_team_home). Slides' local people are its |
| 8 | // users table, so a Home-only person is a users row with no address, |
| 9 | // and this package's table says which Home subject it belongs to. |
| 10 | // |
| 11 | // None of it is standing. A Home-only row says who a Home subject is on |
| 12 | // this instance, never whether they are on the team: that is the |
| 13 | // roster's answer, asked again on every request. |
| 14 | package teamstate |
| 15 | |
| 16 | import ( |
| 17 | "context" |
| 18 | "database/sql" |
| 19 | "embed" |
| 20 | "errors" |
| 21 | "strings" |
| 22 | "time" |
| 23 | "unicode/utf8" |
| 24 | |
| 25 | "amadan.net/rastrillo/rastrillo/migrate" |
| 26 | ) |
| 27 | |
| 28 | //go:embed migrations/*.sql |
| 29 | var migrations embed.FS |
| 30 | |
| 31 | // Schema is the Home-only users table and its triggers. They name users |
| 32 | // and home_identity_links, so it is merged after Slides' own schema and |
| 33 | // linkstore's (team_config.go's teamBoot). |
| 34 | var Schema = migrate.MustFromFS(migrations, "slides_team_home") |
| 35 | |
| 36 | var ( |
| 37 | // ErrRefused is a definite "no": no such person, or a subject that |
| 38 | // cannot be one. |
| 39 | ErrRefused = errors.New("slides team: refused") |
| 40 | // ErrUnavailable is "cannot read it now". |
| 41 | ErrUnavailable = errors.New("slides team: unavailable") |
| 42 | ) |
| 43 | |
| 44 | // HomeUser reads the user a Home-only identity was given. It never |
| 45 | // creates one: ErrRefused means there is none. |
| 46 | func HomeUser(ctx context.Context, db *sql.DB, issuer, subject string) (int64, error) { |
| 47 | if !Opaque(subject) { |
| 48 | return 0, ErrRefused |
| 49 | } |
| 50 | var id int64 |
| 51 | e := db.QueryRowContext(ctx, "SELECT user_id FROM slides_team_home_users WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) |
| 52 | if errors.Is(e, sql.ErrNoRows) { |
| 53 | return 0, ErrRefused |
| 54 | } |
| 55 | if e != nil { |
| 56 | return 0, ErrUnavailable |
| 57 | } |
| 58 | return id, nil |
| 59 | } |
| 60 | |
| 61 | // HomeSubject is the reverse: the Home subject a user is known by on this |
| 62 | // instance, through a Home-only row or an explicit account link. |
| 63 | // ErrRefused means neither, which is no team standing, never an error. |
| 64 | func HomeSubject(ctx context.Context, db *sql.DB, issuer string, user int64) (string, error) { |
| 65 | var subject string |
| 66 | e := db.QueryRowContext(ctx, `SELECT subject FROM slides_team_home_users WHERE issuer=? AND user_id=? |
| 67 | UNION ALL SELECT subject FROM home_identity_links WHERE issuer=? AND person_id=? LIMIT 1`, issuer, user, issuer, user).Scan(&subject) |
| 68 | if errors.Is(e, sql.ErrNoRows) { |
| 69 | return "", ErrRefused |
| 70 | } |
| 71 | if e != nil { |
| 72 | return "", ErrUnavailable |
| 73 | } |
| 74 | return subject, nil |
| 75 | } |
| 76 | |
| 77 | // HomeSubjects maps every Home-only user to its subject, for labels on |
| 78 | // pages that name people without asking the roster. |
| 79 | func HomeSubjects(ctx context.Context, db *sql.DB, issuer string) (map[int64]string, error) { |
| 80 | rows, err := db.QueryContext(ctx, "SELECT user_id, subject FROM slides_team_home_users WHERE issuer=?", issuer) |
| 81 | if err != nil { |
| 82 | return nil, ErrUnavailable |
| 83 | } |
| 84 | defer rows.Close() |
| 85 | out := map[int64]string{} |
| 86 | for rows.Next() { |
| 87 | var id int64 |
| 88 | var subject string |
| 89 | if rows.Scan(&id, &subject) != nil { |
| 90 | return nil, ErrUnavailable |
| 91 | } |
| 92 | out[id] = subject |
| 93 | } |
| 94 | if rows.Err() != nil { |
| 95 | return nil, ErrUnavailable |
| 96 | } |
| 97 | return out, nil |
| 98 | } |
| 99 | |
| 100 | // EnsureHomeUser gives a Home subject an addressless user, or returns the |
| 101 | // one it already has; created says which. The caller must have just had |
| 102 | // a current roster answer of Member for this subject: this records |
| 103 | // identity, not standing, and a later request asks the roster again. A |
| 104 | // subject already linked to a local account is refused — that person |
| 105 | // signs in as that account. |
| 106 | func EnsureHomeUser(ctx context.Context, db *sql.DB, issuer, subject, colour string) (id int64, created bool, err error) { |
| 107 | if !Opaque(subject) || issuer == "" { |
| 108 | return 0, false, ErrRefused |
| 109 | } |
| 110 | tx, e := db.BeginTx(ctx, nil) |
| 111 | if e != nil { |
| 112 | return 0, false, ErrUnavailable |
| 113 | } |
| 114 | defer tx.Rollback() |
| 115 | var linked int |
| 116 | if e = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_identity_links WHERE issuer=? AND subject=?", issuer, subject).Scan(&linked); e != nil { |
| 117 | return 0, false, ErrUnavailable |
| 118 | } |
| 119 | if linked != 0 { |
| 120 | return 0, false, ErrRefused |
| 121 | } |
| 122 | e = tx.QueryRowContext(ctx, "SELECT user_id FROM slides_team_home_users WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) |
| 123 | if e == nil { |
| 124 | if tx.Commit() != nil { |
| 125 | return 0, false, ErrUnavailable |
| 126 | } |
| 127 | return id, false, nil |
| 128 | } |
| 129 | if !errors.Is(e, sql.ErrNoRows) { |
| 130 | return 0, false, ErrUnavailable |
| 131 | } |
| 132 | // email NULL, never "": the unique index on users.email admits any |
| 133 | // number of NULLs and exactly one empty string. |
| 134 | res, e := tx.ExecContext(ctx, "INSERT INTO users(email,name,colour,created_at) VALUES(NULL,'',?,?)", colour, time.Now().UTC()) |
| 135 | if e != nil { |
| 136 | return 0, false, ErrUnavailable |
| 137 | } |
| 138 | if id, e = res.LastInsertId(); e != nil || id <= 0 { |
| 139 | return 0, false, ErrUnavailable |
| 140 | } |
| 141 | if _, e = tx.ExecContext(ctx, "INSERT INTO slides_team_home_users(issuer,subject,user_id) VALUES(?,?,?)", issuer, subject, id); e != nil { |
| 142 | return 0, false, ErrUnavailable |
| 143 | } |
| 144 | if tx.Commit() != nil { |
| 145 | return 0, false, ErrUnavailable |
| 146 | } |
| 147 | return id, true, nil |
| 148 | } |
| 149 | |
| 150 | // Opaque is a Home subject's shape: bounded, printable, and never an |
| 151 | // address, so a subject can never be mistaken for one. |
| 152 | func Opaque(s string) bool { |
| 153 | if s == "" || len(s) > 512 || !utf8.ValidString(s) || strings.Contains(s, "@") { |
| 154 | return false |
| 155 | } |
| 156 | for _, r := range s { |
| 157 | if r <= 32 || r == 127 { |
| 158 | return false |
| 159 | } |
| 160 | } |
| 161 | return true |
| 162 | } |
| 163 | |