| 1 | package webtest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/home/connect/linkstore" |
| 14 | |
| 15 | "amadan.net/oficina/base/internal/store" |
| 16 | ) |
| 17 | |
| 18 | // Moving Base to another BASE_ORIGIN. connect stamps each Oficina ID link |
| 19 | // with Base's origin (its person binding), so a link made at the old origin |
| 20 | // resolves nobody at the new one, and would block linking again. Base |
| 21 | // treats such a link as gone: the member links again, proving who they are |
| 22 | // by email as at first, and the dead link makes way for the new one. |
| 23 | |
| 24 | // oldOrigin is where Base ran before the move. |
| 25 | const oldOrigin = "https://base.old.test" |
| 26 | |
| 27 | // linkAtOldOrigin stores the link connect would have made between email's |
| 28 | // member and subject while Base ran at oldOrigin: the same key, issuer and |
| 29 | // namespace, another origin. It is connect's own carry-over (Adopt), so the |
| 30 | // row is exactly the one linking there wrote. |
| 31 | func (a *testApp) linkAtOldOrigin(email, subject string) { |
| 32 | a.t.Helper() |
| 33 | ctx := context.Background() |
| 34 | old, err := linkstore.New(a.DB, bytes.Repeat([]byte{9}, 32), oldOrigin, homeIssuer, "carlos/base", |
| 35 | func(ctx context.Context, q linkstore.Queryer, n int64) (string, error) { |
| 36 | return store.MemberAddress(ctx, q, n) |
| 37 | }) |
| 38 | if err != nil { |
| 39 | a.t.Fatal(err) |
| 40 | } |
| 41 | m, ok, err := store.MemberByEmail(ctx, a.DB, email) |
| 42 | if err != nil || !ok { |
| 43 | a.t.Fatalf("member %s: %v", email, err) |
| 44 | } |
| 45 | n, err := store.MemberNumber(ctx, a.DB, m.ID) |
| 46 | if err != nil { |
| 47 | a.t.Fatal(err) |
| 48 | } |
| 49 | tx, err := a.DB.BeginTx(ctx, nil) |
| 50 | if err != nil { |
| 51 | a.t.Fatal(err) |
| 52 | } |
| 53 | defer tx.Rollback() |
| 54 | if err := old.Adopt(ctx, tx, homeIssuer, subject, n); err != nil { |
| 55 | a.t.Fatal(err) |
| 56 | } |
| 57 | if err := tx.Commit(); err != nil { |
| 58 | a.t.Fatal(err) |
| 59 | } |
| 60 | // At the old origin it resolves; here it does not. |
| 61 | if _, _, err := old.Resolve(ctx, homeIssuer, subject); err != nil { |
| 62 | a.t.Fatalf("the old link does not resolve at the old origin: %v", err) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // linkRows is how many Oficina ID links name email's member. |
| 67 | func (a *testApp) linkRows(email string) int { |
| 68 | a.t.Helper() |
| 69 | var n int |
| 70 | if err := a.DB.QueryRow(`SELECT count(*) FROM home_identity_links l JOIN member_numbers n ON n.number = l.person_id |
| 71 | JOIN members m ON m.id = n.member_id WHERE m.email = ?`, email).Scan(&n); err != nil { |
| 72 | a.t.Fatal(err) |
| 73 | } |
| 74 | return n |
| 75 | } |
| 76 | |
| 77 | // The move itself: the old link refuses Oficina sign-in, as it must, and |
| 78 | // the member links again the way an unlinked member does (Oficina, then |
| 79 | // email once), after which Oficina ID signs them in. |
| 80 | func TestOficinaLinkFromAnOldOriginLinksAgain(t *testing.T) { |
| 81 | a := newHomeApp(t) |
| 82 | a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| 83 | |
| 84 | unknown := a.signInWithOficina("subject-owner") |
| 85 | if unknown.Code != http.StatusForbidden || !strings.Contains(unknown.Body, "Oficina account not connected") { |
| 86 | t.Fatalf("Oficina sign-in on a link from the old origin: %d\n%s", unknown.Code, unknown.Body) |
| 87 | } |
| 88 | r := a.signIn(ownerAddress) |
| 89 | resume := a.get(r.Location()) |
| 90 | if resume.Code != 303 || !strings.HasPrefix(resume.Location(), "/auth/start") { |
| 91 | t.Fatalf("local sign-in did not resume the link: %d %q", resume.Code, resume.Location()) |
| 92 | } |
| 93 | a.confirmLink(a.atOficina(a.get(resume.Location()), "subject-owner", 30*time.Second), ownerAddress) |
| 94 | if n := a.linkRows(ownerAddress); n != 1 { |
| 95 | t.Fatalf("links: %d", n) |
| 96 | } |
| 97 | b := a.newBrowser() |
| 98 | if fin := b.signInWithOficina("subject-owner"); fin.Code != 303 || fin.Location() != "/" { |
| 99 | t.Fatalf("Oficina sign-in after linking again: %d %q\n%s", fin.Code, fin.Location(), fin.Body) |
| 100 | } |
| 101 | if home := b.get("/"); home.Code != 200 { |
| 102 | t.Fatalf("Home: %d", home.Code) |
| 103 | } |
| 104 | if body := a.get("/settings").Body; strings.Contains(body, `href="/account/home"`) { |
| 105 | t.Errorf("Settings still offers Connect:\n%s", body) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Settings no longer says the dead link is connected: it offers Connect, |
| 110 | // and connecting from there works too. |
| 111 | func TestOficinaLinkFromAnOldOriginConnectsFromSettings(t *testing.T) { |
| 112 | a := newHomeApp(t) |
| 113 | a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| 114 | a.link(ownerAddress, "subject-owner") // fails unless Settings offers Connect and the confirm goes through |
| 115 | if n := a.linkRows(ownerAddress); n != 1 { |
| 116 | t.Fatalf("links: %d", n) |
| 117 | } |
| 118 | if fin := a.newBrowser().signInWithOficina("subject-owner"); fin.Code != 303 || fin.Location() != "/" { |
| 119 | t.Fatalf("Oficina sign-in: %d %q", fin.Code, fin.Location()) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // A link that still resolves is never replaced: another Oficina ID can't |
| 124 | // take a linked member over, even with their email. |
| 125 | func TestOficinaLiveLinkIsNotReplaced(t *testing.T) { |
| 126 | a := newHomeApp(t) |
| 127 | a.link(ownerAddress, "subject-owner") |
| 128 | if body := a.get("/settings").Body; strings.Contains(body, `href="/account/home"`) { |
| 129 | t.Fatalf("Settings offers Connect to a linked member") |
| 130 | } |
| 131 | toIssuer := a.get(a.post("/account/home", nil).Location()) |
| 132 | finish := a.atOficina(toIssuer, "subject-intruder", 30*time.Second) |
| 133 | if finish.Code != 303 || finish.Location() != "/account/home/confirm" { |
| 134 | t.Fatalf("link finish: %d %q", finish.Code, finish.Location()) |
| 135 | } |
| 136 | if r := a.post("/account/home/confirm", nil); r.Code != http.StatusForbidden { |
| 137 | t.Fatalf("confirming a second Oficina ID: %d %q", r.Code, r.Location()) |
| 138 | } |
| 139 | if fin := a.newBrowser().signInWithOficina("subject-owner"); fin.Code != 303 { |
| 140 | t.Errorf("the live link stopped working: %d", fin.Code) |
| 141 | } |
| 142 | if fin := a.newBrowser().signInWithOficina("subject-intruder"); fin.Code != http.StatusForbidden { |
| 143 | t.Errorf("the other Oficina ID signs in: %d", fin.Code) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Linking again clears only the member's own dead link: another member's |
| 148 | // dead link to the same Oficina ID stays, and the confirm is refused. |
| 149 | func TestOficinaOldLinkOfAnotherMemberStays(t *testing.T) { |
| 150 | a := newHomeApp(t) |
| 151 | if _, _, err := store.Invite(context.Background(), a.DB, "ana@acme.test"); err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | a.linkAtOldOrigin("ana@acme.test", "subject-shared") |
| 155 | a.signedInOwner() |
| 156 | toIssuer := a.get(a.post("/account/home", nil).Location()) |
| 157 | a.atOficina(toIssuer, "subject-shared", 30*time.Second) |
| 158 | if r := a.post("/account/home/confirm", nil); r.Code != http.StatusForbidden { |
| 159 | t.Fatalf("the owner took Ana's Oficina ID: %d %q", r.Code, r.Location()) |
| 160 | } |
| 161 | if a.linkRows("ana@acme.test") != 1 || a.linkRows(ownerAddress) != 0 { |
| 162 | t.Fatalf("links: ana %d, owner %d", a.linkRows("ana@acme.test"), a.linkRows(ownerAddress)) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Nothing but the member's own fresh email sign-in clears a dead link: |
| 167 | // not a signed-out POST, nor a POST from another site, nor an old sign-in. |
| 168 | func TestOficinaOldLinkClearedOnlyByTheMember(t *testing.T) { |
| 169 | a := newHomeApp(t) |
| 170 | a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| 171 | before := a.linkRow(ownerAddress) |
| 172 | unchanged := func(what string) { |
| 173 | t.Helper() |
| 174 | if after := a.linkRow(ownerAddress); after != before { |
| 175 | t.Fatalf("%s changed the dead link: %q, was %q", what, after, before) |
| 176 | } |
| 177 | } |
| 178 | if r := a.postFrom(homeIssuer, "/account/home/confirm", url.Values{}); r.Code != http.StatusForbidden { |
| 179 | t.Fatalf("signed-out cross-site confirm: %d", r.Code) |
| 180 | } |
| 181 | unchanged("a signed-out cross-site confirm") |
| 182 | if r := a.post("/account/home/confirm", nil); r.Code == http.StatusOK { |
| 183 | t.Fatalf("signed-out confirm: %d", r.Code) |
| 184 | } |
| 185 | unchanged("a signed-out confirm") |
| 186 | // The owner freshly signed in by email, and a page on another site |
| 187 | // posting the confirm in their browser: refused before anything is |
| 188 | // cleared. |
| 189 | a.signedInOwner() |
| 190 | if r := a.postFrom(homeIssuer, "/account/home/confirm", url.Values{}); r.Code != http.StatusForbidden { |
| 191 | t.Fatalf("signed-in cross-site confirm: %d", r.Code) |
| 192 | } |
| 193 | unchanged("a signed-in cross-site confirm") |
| 194 | } |
| 195 | |
| 196 | // linkRow is the Oficina ID link naming email's member, as stored. |
| 197 | func (a *testApp) linkRow(email string) string { |
| 198 | a.t.Helper() |
| 199 | var subject, binding string |
| 200 | var person int64 |
| 201 | if err := a.DB.QueryRow(`SELECT l.subject, l.person_id, l.person_binding FROM home_identity_links l |
| 202 | JOIN member_numbers n ON n.number = l.person_id JOIN members m ON m.id = n.member_id WHERE m.email = ?`, email). |
| 203 | Scan(&subject, &person, &binding); err != nil { |
| 204 | a.t.Fatalf("link row for %s: %v", email, err) |
| 205 | } |
| 206 | return fmt.Sprintf("%s/%d/%s", subject, person, binding) |
| 207 | } |
| 208 | |
| 209 | // An email sign-in older than connect's five minutes clears nothing. |
| 210 | func TestOficinaOldLinkNeedsAFreshSignIn(t *testing.T) { |
| 211 | a := newHomeApp(t) |
| 212 | a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| 213 | a.signedInOwner() |
| 214 | old := time.Now().Add(-10 * time.Minute).UTC().Format(time.RFC3339) |
| 215 | if _, err := a.DB.Exec(`UPDATE sessions SET created_at = ?, auth_time = ?`, old, old); err != nil { |
| 216 | t.Fatal(err) |
| 217 | } |
| 218 | a.post("/account/home/confirm", nil) |
| 219 | if n := a.linkRows(ownerAddress); n != 1 { |
| 220 | t.Fatalf("an old sign-in cleared the link: %d", n) |
| 221 | } |
| 222 | } |
| 223 | |