| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "slices" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | // homeOnlyHooks is a team-mode app in miniature: a Home-only person table, |
| 18 | // a roster decision and an allocation decision a test sets (admit, refuse, |
| 19 | // or cannot decide). It counts how often each hook is asked, so a test can |
| 20 | // prove when connect does not ask at all. |
| 21 | type homeOnlyHooks struct { |
| 22 | db *sql.DB |
| 23 | admission error // Admit: nil is a Member; ErrNoPerson a non-member; anything else unavailable |
| 24 | decision error // Unlinked: nil allocates; anything else is returned as is |
| 25 | label string |
| 26 | lookupErr error |
| 27 | admitted int |
| 28 | unlinked int |
| 29 | homeOnly int |
| 30 | remember int |
| 31 | // mu guards the counters when a test races requests. |
| 32 | mu sync.Mutex |
| 33 | } |
| 34 | |
| 35 | func (h *homeOnlyHooks) tally(n *int) { |
| 36 | h.mu.Lock() |
| 37 | *n++ |
| 38 | h.mu.Unlock() |
| 39 | } |
| 40 | |
| 41 | func (h *homeOnlyHooks) install(f *bridgeFixture, c *Config) { |
| 42 | h.db = f.db |
| 43 | if _, err := f.db.Exec("CREATE TABLE home_only(id INTEGER PRIMARY KEY AUTOINCREMENT, issuer TEXT NOT NULL, subject TEXT NOT NULL, UNIQUE(issuer,subject)); INSERT INTO sqlite_sequence(name,seq) VALUES('home_only',1000)"); err != nil { |
| 44 | panic(err) |
| 45 | } |
| 46 | c.HomeOnly = func(ctx context.Context, issuer, subject string) (Person, error) { |
| 47 | h.tally(&h.homeOnly) |
| 48 | if h.lookupErr != nil { |
| 49 | return Person{}, h.lookupErr |
| 50 | } |
| 51 | var p Person |
| 52 | err := h.db.QueryRowContext(ctx, "SELECT id FROM home_only WHERE issuer=? AND subject=?", issuer, subject).Scan(&p.ID) |
| 53 | if errors.Is(err, sql.ErrNoRows) { |
| 54 | return Person{}, ErrNoPerson |
| 55 | } |
| 56 | p.Label = h.label |
| 57 | return p, err |
| 58 | } |
| 59 | c.Admit = func(ctx context.Context, issuer, subject string) error { |
| 60 | h.tally(&h.admitted) |
| 61 | return h.admission |
| 62 | } |
| 63 | c.Unlinked = func(ctx context.Context, issuer, subject string) (Person, error) { |
| 64 | h.tally(&h.unlinked) |
| 65 | if h.decision != nil { |
| 66 | return Person{}, h.decision |
| 67 | } |
| 68 | res, err := h.db.ExecContext(ctx, "INSERT INTO home_only(issuer,subject) VALUES(?,?)", issuer, subject) |
| 69 | if err != nil { |
| 70 | return Person{}, err |
| 71 | } |
| 72 | id, err := res.LastInsertId() |
| 73 | return Person{ID: id, Label: h.label}, err |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func newHomeOnlyFixture(t *testing.T) (*bridgeFixture, *homeOnlyHooks) { |
| 78 | t.Helper() |
| 79 | h := &homeOnlyHooks{} |
| 80 | f := newBridgeFixtureWith(t, h.install) |
| 81 | f.b.rememberUnknown = func(http.ResponseWriter) { h.remember++ } |
| 82 | return f, h |
| 83 | } |
| 84 | |
| 85 | func (h *homeOnlyHooks) count(t *testing.T) int { |
| 86 | t.Helper() |
| 87 | var n int |
| 88 | if err := h.db.QueryRow("SELECT COUNT(*) FROM home_only").Scan(&n); err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | return n |
| 92 | } |
| 93 | |
| 94 | // finishAs runs one ordinary exchange for subject with cookies present. |
| 95 | func (f *bridgeFixture) finishAs(t *testing.T, subject string, cookies []*http.Cookie) *httptest.ResponseRecorder { |
| 96 | t.Helper() |
| 97 | pending, nonce := f.start(t, cookies, "/next") |
| 98 | inbox := f.callback(t, subject, nonce, time.Now().Add(-time.Minute)) |
| 99 | w := httptest.NewRecorder() |
| 100 | f.b.Finish(w, bridgeRequest(http.MethodGet, "/auth/finish", append(append([]*http.Cookie{}, cookies...), pending, inbox))) |
| 101 | return w |
| 102 | } |
| 103 | |
| 104 | // choose follows a finish that answered "choose": the page, then the POST. |
| 105 | // It returns the page's response and the POST's. |
| 106 | func (f *bridgeFixture) choose(t *testing.T, finish *httptest.ResponseRecorder, choice string, cookies []*http.Cookie) (*httptest.ResponseRecorder, *httptest.ResponseRecorder) { |
| 107 | t.Helper() |
| 108 | if finish.Code != 303 || finish.Header().Get("Location") != ChoosePath { |
| 109 | t.Fatalf("finish=%d %q, want the choose page", finish.Code, finish.Header().Get("Location")) |
| 110 | } |
| 111 | proof := bridgeCookie(t, finish, homeChoiceCookie) |
| 112 | cookies = append(append([]*http.Cookie{}, cookies...), proof) |
| 113 | page := httptest.NewRecorder() |
| 114 | f.b.ChoosePage(page, bridgeRequest(http.MethodGet, ChoosePath, cookies)) |
| 115 | if page.Code != 200 || page.Body.String() != "choose" { |
| 116 | t.Fatalf("choose page=%d %q", page.Code, page.Body.String()) |
| 117 | } |
| 118 | return page, f.choosePost(t, choice, f.b.choiceToken(proof.Value), cookies) |
| 119 | } |
| 120 | |
| 121 | func (f *bridgeFixture) choosePost(t *testing.T, choice, token string, cookies []*http.Cookie) *httptest.ResponseRecorder { |
| 122 | t.Helper() |
| 123 | form := url.Values{"choice": {choice}, "token": {token}} |
| 124 | r := httptest.NewRequest(http.MethodPost, bridgeOrigin+ChoosePath, strings.NewReader(form.Encode())) |
| 125 | r.Header.Set("Origin", bridgeOrigin) |
| 126 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 127 | for _, c := range cookies { |
| 128 | r.AddCookie(c) |
| 129 | } |
| 130 | w := httptest.NewRecorder() |
| 131 | f.b.ChoosePost(w, r) |
| 132 | return w |
| 133 | } |
| 134 | |
| 135 | // signInNew is a Member's first sign-in: finish, then "I'm new here". It |
| 136 | // returns the session cookie. |
| 137 | func (f *bridgeFixture) signInNew(t *testing.T, subject string) *http.Cookie { |
| 138 | t.Helper() |
| 139 | _, w := f.choose(t, f.finishAs(t, subject, nil), "new", nil) |
| 140 | if w.Code != 303 || w.Header().Get("Location") != "/" { |
| 141 | t.Fatalf("choose new=%d %q body=%s", w.Code, w.Header().Get("Location"), w.Body.String()) |
| 142 | } |
| 143 | return bridgeCookie(t, w, f.b.sessionCookie) |
| 144 | } |
| 145 | |
| 146 | func TestTeamHooksAreATrio(t *testing.T) { |
| 147 | hook := func(context.Context, string, string) (Person, error) { return Person{}, ErrNoPerson } |
| 148 | admit := func(context.Context, string, string) error { return ErrNoPerson } |
| 149 | newBridgeFixtureWith(t, func(_ *bridgeFixture, c *Config) { |
| 150 | for name, adjust := range map[string]func(*Config){ |
| 151 | "unlinked only": func(c *Config) { c.Unlinked = hook }, |
| 152 | "home-only only": func(c *Config) { c.HomeOnly = hook }, |
| 153 | "admit only": func(c *Config) { c.Admit = admit }, |
| 154 | "no admit": func(c *Config) { c.Unlinked, c.HomeOnly = hook, hook }, |
| 155 | "no unlinked": func(c *Config) { c.Admit, c.HomeOnly = admit, hook }, |
| 156 | "no home-only only": func(c *Config) { c.Admit, c.Unlinked = admit, hook }, |
| 157 | } { |
| 158 | one := *c |
| 159 | adjust(&one) |
| 160 | if _, err := New(one); err == nil { |
| 161 | t.Errorf("%s: New accepted an incomplete set of team hooks", name) |
| 162 | } |
| 163 | } |
| 164 | all := *c |
| 165 | all.Admit, all.Unlinked, all.HomeOnly = admit, hook, hook |
| 166 | if _, err := New(all); err != nil { |
| 167 | t.Errorf("New refused all three hooks: %v", err) |
| 168 | } |
| 169 | }) |
| 170 | } |
| 171 | |
| 172 | // A Member with no link is asked, not allocated: the finish leaves a proof |
| 173 | // and the choose page, and "I'm new here" makes the Home-only person. |
| 174 | func TestMemberChoosesNewAndSignsInAsAHomeOnlyPerson(t *testing.T) { |
| 175 | f, h := newHomeOnlyFixture(t) |
| 176 | finish := f.finishAs(t, "pr_member", nil) |
| 177 | bridgeNoSession(t, f.b, finish) |
| 178 | if h.admitted != 1 || h.unlinked != 0 || h.count(t) != 0 || h.remember != 0 { |
| 179 | t.Fatalf("finish admitted=%d unlinked=%d people=%d remember=%d", h.admitted, h.unlinked, h.count(t), h.remember) |
| 180 | } |
| 181 | _, w := f.choose(t, finish, "new", nil) |
| 182 | if w.Code != 303 || w.Header().Get("Location") != "/" { |
| 183 | t.Fatalf("choose new=%d %q", w.Code, w.Header().Get("Location")) |
| 184 | } |
| 185 | if h.unlinked != 1 || h.count(t) != 1 || h.admitted != 2 { |
| 186 | t.Fatalf("unlinked=%d people=%d admitted=%d", h.unlinked, h.count(t), h.admitted) |
| 187 | } |
| 188 | cookie := bridgeCookie(t, w, f.b.sessionCookie) |
| 189 | r := bridgeRequest(http.MethodGet, "/", []*http.Cookie{cookie}) |
| 190 | s, _ := f.sessions.From(r) |
| 191 | if s.Method != HomeMethod || s.Subject != "pr_member" { |
| 192 | t.Fatalf("session %+v", s) |
| 193 | } |
| 194 | p, err := f.b.ResolveSession(r) |
| 195 | if err != nil || p.ID <= 1000 || p.Label != "" { |
| 196 | t.Fatalf("ResolveSession %+v %v", p, err) |
| 197 | } |
| 198 | // A second sign-in is the same person, straight through: no roster |
| 199 | // decision, no choice, no allocation. |
| 200 | again := f.finishAs(t, "pr_member", nil) |
| 201 | if again.Code != 303 || again.Header().Get("Location") != "/next" || h.unlinked != 1 || h.admitted != 2 || h.count(t) != 1 { |
| 202 | t.Fatalf("second sign-in=%d %q unlinked=%d admitted=%d", again.Code, again.Header().Get("Location"), h.unlinked, h.admitted) |
| 203 | } |
| 204 | if p2, err := f.b.Resolve(context.Background(), "pr_member"); err != nil || p2.ID != p.ID { |
| 205 | t.Fatal("Resolve does not find the Home-only person", err) |
| 206 | } |
| 207 | own := f.finishAs(t, "pr_member", []*http.Cookie{cookie}) |
| 208 | if own.Code != 303 || h.unlinked != 1 { |
| 209 | t.Fatalf("same person over own session=%d", own.Code) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // "Connect my existing account" spends the proof and goes to local sign-in |
| 214 | // with the link-resuming marker; the spent proof cannot then say "new". |
| 215 | func TestChooseConnectConsumesTheProof(t *testing.T) { |
| 216 | f, h := newHomeOnlyFixture(t) |
| 217 | finish := f.finishAs(t, "pr_member", nil) |
| 218 | proof := bridgeCookie(t, finish, homeChoiceCookie) |
| 219 | _, w := f.choose(t, finish, "connect", nil) |
| 220 | if w.Code != 303 || w.Header().Get("Location") != "/signin/local" || h.remember != 1 { |
| 221 | t.Fatalf("connect=%d %q remember=%d", w.Code, w.Header().Get("Location"), h.remember) |
| 222 | } |
| 223 | bridgeNoSession(t, f.b, w) |
| 224 | if again := f.choosePost(t, "new", f.b.choiceToken(proof.Value), []*http.Cookie{proof}); again.Code != 403 { |
| 225 | t.Fatalf("spent proof chose new: %d", again.Code) |
| 226 | } |
| 227 | if h.unlinked != 0 || h.count(t) != 0 { |
| 228 | t.Fatal("connect allocated a person") |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // The POST that allocates refuses while any app session is present, and |
| 233 | // leaves the proof for a choice made without one. |
| 234 | func TestChooseNewRefusedWithAnAppSession(t *testing.T) { |
| 235 | f, h := newHomeOnlyFixture(t) |
| 236 | finish := f.finishAs(t, "pr_member", nil) |
| 237 | proof := bridgeCookie(t, finish, homeChoiceCookie) |
| 238 | local := f.local(t, "guest@example.com", "magiclink", time.Now().Add(-time.Minute)) |
| 239 | w := f.choosePost(t, "new", f.b.choiceToken(proof.Value), []*http.Cookie{proof, local}) |
| 240 | if w.Code != 403 || h.unlinked != 0 || h.count(t) != 0 { |
| 241 | t.Fatalf("new over a session=%d unlinked=%d", w.Code, h.unlinked) |
| 242 | } |
| 243 | bridgeNoSession(t, f.b, w) |
| 244 | if ok := f.choosePost(t, "new", f.b.choiceToken(proof.Value), []*http.Cookie{proof}); ok.Code != 303 { |
| 245 | t.Fatalf("proof did not survive the refusal: %d", ok.Code) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // The form token is bound to its proof cookie, and the POST to this origin. |
| 250 | func TestChooseFormIsBoundToItsProof(t *testing.T) { |
| 251 | f, h := newHomeOnlyFixture(t) |
| 252 | mine := bridgeCookie(t, f.finishAs(t, "pr_member", nil), homeChoiceCookie) |
| 253 | other := bridgeCookie(t, f.finishAs(t, "pr_other", nil), homeChoiceCookie) |
| 254 | for name, w := range map[string]*httptest.ResponseRecorder{ |
| 255 | "another proof's token": f.choosePost(t, "new", f.b.choiceToken(other.Value), []*http.Cookie{mine}), |
| 256 | "no token": f.choosePost(t, "new", "", []*http.Cookie{mine}), |
| 257 | "no proof": f.choosePost(t, "new", f.b.choiceToken(mine.Value), nil), |
| 258 | "unknown choice": f.choosePost(t, "both", f.b.choiceToken(mine.Value), []*http.Cookie{mine}), |
| 259 | } { |
| 260 | if w.Code == 303 { |
| 261 | t.Errorf("%s: accepted", name) |
| 262 | } |
| 263 | bridgeNoSession(t, f.b, w) |
| 264 | } |
| 265 | for _, origin := range []string{"", "null", "https://evil.example", bridgeIssuer} { |
| 266 | form := url.Values{"choice": {"new"}, "token": {f.b.choiceToken(mine.Value)}} |
| 267 | r := httptest.NewRequest(http.MethodPost, bridgeOrigin+ChoosePath, strings.NewReader(form.Encode())) |
| 268 | if origin != "" { |
| 269 | r.Header.Set("Origin", origin) |
| 270 | } |
| 271 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 272 | r.AddCookie(mine) |
| 273 | w := httptest.NewRecorder() |
| 274 | f.b.ChoosePost(w, r) |
| 275 | if w.Code != 403 { |
| 276 | t.Errorf("Origin %q: %d", origin, w.Code) |
| 277 | } |
| 278 | } |
| 279 | if h.unlinked != 0 || h.count(t) != 0 { |
| 280 | t.Fatal("a forged choice allocated") |
| 281 | } |
| 282 | // The proof survived every refusal. |
| 283 | if w := f.choosePost(t, "new", f.b.choiceToken(mine.Value), []*http.Cookie{mine}); w.Code != 303 { |
| 284 | t.Fatalf("genuine choice=%d", w.Code) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | func TestChoosePageNeedsALiveProof(t *testing.T) { |
| 289 | f, _ := newHomeOnlyFixture(t) |
| 290 | for name, cookies := range map[string][]*http.Cookie{ |
| 291 | "none": nil, |
| 292 | "malformed": {{Name: homeChoiceCookie, Value: "bad"}}, |
| 293 | "unknown": {{Name: homeChoiceCookie, Value: strings.Repeat("A", 43)}}, |
| 294 | } { |
| 295 | w := httptest.NewRecorder() |
| 296 | f.b.ChoosePage(w, bridgeRequest(http.MethodGet, ChoosePath, cookies)) |
| 297 | if w.Code != 403 || w.Body.String() != "failed" { |
| 298 | t.Errorf("%s: %d %q", name, w.Code, w.Body.String()) |
| 299 | } |
| 300 | } |
| 301 | proof := bridgeCookie(t, f.finishAs(t, "pr_member", nil), homeChoiceCookie) |
| 302 | f.b.now = func() time.Time { return time.Now().Add(11 * time.Minute) } |
| 303 | w := httptest.NewRecorder() |
| 304 | f.b.ChoosePage(w, bridgeRequest(http.MethodGet, ChoosePath, []*http.Cookie{proof})) |
| 305 | if w.Code != 403 { |
| 306 | t.Fatalf("expired proof page=%d", w.Code) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // Without the hooks there is no choice to make. |
| 311 | func TestChooseWithoutHooksIsNotFound(t *testing.T) { |
| 312 | f := newBridgeFixture(t) |
| 313 | w := httptest.NewRecorder() |
| 314 | f.b.ChoosePage(w, bridgeRequest(http.MethodGet, ChoosePath, nil)) |
| 315 | if w.Code != 404 { |
| 316 | t.Fatalf("page=%d", w.Code) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Two "I'm new here" POSTs racing on one proof make one person. |
| 321 | func TestChooseRaceMakesOnePerson(t *testing.T) { |
| 322 | f, h := newHomeOnlyFixture(t) |
| 323 | proof := bridgeCookie(t, f.finishAs(t, "pr_member", nil), homeChoiceCookie) |
| 324 | token := f.b.choiceToken(proof.Value) |
| 325 | codes := make(chan int, 4) |
| 326 | var wg sync.WaitGroup |
| 327 | for range 4 { |
| 328 | wg.Go(func() { codes <- f.choosePost(t, "new", token, []*http.Cookie{proof}).Code }) |
| 329 | } |
| 330 | wg.Wait() |
| 331 | close(codes) |
| 332 | minted := 0 |
| 333 | for c := range codes { |
| 334 | if c == 303 { |
| 335 | minted++ |
| 336 | } |
| 337 | } |
| 338 | if minted != 1 || h.count(t) != 1 { |
| 339 | t.Fatalf("minted=%d people=%d", minted, h.count(t)) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Unlinked refusing after the roster said Member: resolve again. Nobody |
| 344 | // found is unknown, an error is a 503, and a person made meanwhile (a |
| 345 | // racing choice) is signed in. |
| 346 | func TestChooseNewReResolvesAfterUnlinkedRefuses(t *testing.T) { |
| 347 | for _, c := range []struct { |
| 348 | name string |
| 349 | decision error |
| 350 | existing bool |
| 351 | code int |
| 352 | body string |
| 353 | }{ |
| 354 | {"refused, nobody", ErrNoPerson, false, 403, "unknown"}, |
| 355 | {"error, nobody", errors.New("disk"), false, 503, "failed"}, |
| 356 | {"refused, made meanwhile", ErrNoPerson, true, 303, ""}, |
| 357 | {"error, made meanwhile", errors.New("disk"), true, 303, ""}, |
| 358 | } { |
| 359 | t.Run(c.name, func(t *testing.T) { |
| 360 | f, h := newHomeOnlyFixture(t) |
| 361 | finish := f.finishAs(t, "pr_member", nil) |
| 362 | proof := bridgeCookie(t, finish, homeChoiceCookie) |
| 363 | h.decision = c.decision |
| 364 | if c.existing { |
| 365 | // The person appears between the re-check and Unlinked. |
| 366 | inner := f.b.unlinked |
| 367 | f.b.unlinked = func(ctx context.Context, issuer, subject string) (Person, error) { |
| 368 | if _, err := h.db.Exec("INSERT INTO home_only(issuer,subject) VALUES(?,?)", issuer, subject); err != nil { |
| 369 | t.Fatal(err) |
| 370 | } |
| 371 | return inner(ctx, issuer, subject) |
| 372 | } |
| 373 | } |
| 374 | w := f.choosePost(t, "new", f.b.choiceToken(proof.Value), []*http.Cookie{proof}) |
| 375 | if w.Code != c.code || (c.body != "" && w.Body.String() != c.body) { |
| 376 | t.Fatalf("%d %q", w.Code, w.Body.String()) |
| 377 | } |
| 378 | if c.code != 303 { |
| 379 | bridgeNoSession(t, f.b, w) |
| 380 | } |
| 381 | }) |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | func TestNonMemberIsConnectsUnknown(t *testing.T) { |
| 386 | f, h := newHomeOnlyFixture(t) |
| 387 | h.admission = ErrNoPerson |
| 388 | w := f.finishAs(t, "pr_stranger", nil) |
| 389 | if w.Code != 403 || w.Body.String() != "unknown" || h.remember != 1 { |
| 390 | t.Fatalf("refused=%d %q remember=%d", w.Code, w.Body.String(), h.remember) |
| 391 | } |
| 392 | bridgeNoSession(t, f.b, w) |
| 393 | for _, c := range w.Result().Cookies() { |
| 394 | if c.Name == homeChoiceCookie { |
| 395 | t.Fatal("a non-member got a choice proof") |
| 396 | } |
| 397 | } |
| 398 | if h.count(t) != 0 || h.unlinked != 0 { |
| 399 | t.Fatal("refusal allocated a person") |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // The answer to a non-member is the answer an instance without the hooks |
| 404 | // gives, status, body and headers alike; only the freshly sealed marker |
| 405 | // cookie's value differs. |
| 406 | func TestNonMemberAnswerIsByteIdenticalToNoHooks(t *testing.T) { |
| 407 | plain := newBridgeFixture(t) |
| 408 | plain.routes(t) |
| 409 | team, h := newHomeOnlyFixture(t) |
| 410 | team.routes(t) |
| 411 | h.admission = ErrNoPerson |
| 412 | a := plain.finishAs(t, "pr_stranger", nil) |
| 413 | b := team.finishAs(t, "pr_stranger", nil) |
| 414 | if a.Code != b.Code || a.Body.String() != b.Body.String() { |
| 415 | t.Fatalf("status/body differ: %d %q vs %d %q", a.Code, a.Body.String(), b.Code, b.Body.String()) |
| 416 | } |
| 417 | if shape(a.Header()) != shape(b.Header()) { |
| 418 | t.Fatalf("headers differ:\n%s\n%s", shape(a.Header()), shape(b.Header())) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // shape is a header set with cookie values elided. |
| 423 | func shape(h http.Header) string { |
| 424 | var lines []string |
| 425 | for k, vs := range h { |
| 426 | for _, v := range vs { |
| 427 | if k == "Set-Cookie" { |
| 428 | name, rest, _ := strings.Cut(v, "=") |
| 429 | _, attrs, _ := strings.Cut(rest, ";") |
| 430 | v = name + "=…;" + attrs |
| 431 | } |
| 432 | lines = append(lines, k+": "+v) |
| 433 | } |
| 434 | } |
| 435 | slices.Sort(lines) |
| 436 | return strings.Join(lines, "\n") |
| 437 | } |
| 438 | |
| 439 | func TestRosterUnavailableIsRetryable(t *testing.T) { |
| 440 | f, h := newHomeOnlyFixture(t) |
| 441 | h.admission = errors.New("roster unknown") |
| 442 | w := f.finishAs(t, "pr_member", nil) |
| 443 | if w.Code != 503 || w.Body.String() != "failed" || h.remember != 0 { |
| 444 | t.Fatalf("undecided=%d %q remember=%d", w.Code, w.Body.String(), h.remember) |
| 445 | } |
| 446 | bridgeNoSession(t, f.b, w) |
| 447 | if h.count(t) != 0 || h.unlinked != 0 { |
| 448 | t.Fatal("undecided allocated a person") |
| 449 | } |
| 450 | h.admission = nil |
| 451 | h.lookupErr = errors.New("disk") |
| 452 | w = f.finishAs(t, "pr_member", nil) |
| 453 | if w.Code != 503 || h.admitted != 1 { |
| 454 | t.Fatalf("unreadable Home-only store=%d admitted=%d", w.Code, h.admitted) |
| 455 | } |
| 456 | bridgeNoSession(t, f.b, w) |
| 457 | } |
| 458 | |
| 459 | // An addressless person is the contract: a hook handing back a label (an |
| 460 | // address) is refused, never minted. |
| 461 | func TestHomeOnlyPersonWithAnAddressIsRefused(t *testing.T) { |
| 462 | f, h := newHomeOnlyFixture(t) |
| 463 | h.label = "someone@example.com" |
| 464 | _, w := f.choose(t, f.finishAs(t, "pr_member", nil), "new", nil) |
| 465 | if w.Code != 403 || w.Body.String() != "unknown" { |
| 466 | t.Fatalf("labelled Home-only person=%d %q", w.Code, w.Body.String()) |
| 467 | } |
| 468 | bridgeNoSession(t, f.b, w) |
| 469 | if _, err := f.b.Resolve(context.Background(), "pr_member"); err == nil { |
| 470 | t.Fatal("Resolve accepted a Home-only person with an address") |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // Somebody already signed in: an unknown arrival is refused before the |
| 475 | // roster is asked and before any proof is issued. |
| 476 | func TestAdmitNeverAskedOverALiveSession(t *testing.T) { |
| 477 | f, h := newHomeOnlyFixture(t) |
| 478 | local := f.local(t, "x@example.com", "magiclink", time.Now().Add(-time.Minute)) |
| 479 | w := f.finishAs(t, "pr_member", []*http.Cookie{local}) |
| 480 | if w.Code != 403 || w.Body.String() != "unknown" || h.remember != 1 || h.admitted != 0 || h.unlinked != 0 || h.count(t) != 0 { |
| 481 | t.Fatalf("arrival over a live session=%d %q admitted=%d unlinked=%d", w.Code, w.Body.String(), h.admitted, h.unlinked) |
| 482 | } |
| 483 | for _, c := range w.Result().Cookies() { |
| 484 | if c.Name == homeChoiceCookie { |
| 485 | t.Fatal("a proof was issued over a live session") |
| 486 | } |
| 487 | } |
| 488 | bridgeNoSession(t, f.b, w) |
| 489 | } |
| 490 | |
| 491 | // A guest's local account never blocks a new member's first sign-in: |
| 492 | // the roster decides, not whether local accounts exist. |
| 493 | func TestGuestLocalAccountNeverBlocksANewMember(t *testing.T) { |
| 494 | f, h := newHomeOnlyFixture(t) |
| 495 | f.local(t, "guest@example.com", "magiclink", time.Now().Add(-time.Minute)) |
| 496 | f.signInNew(t, "pr_member") |
| 497 | if h.count(t) != 1 { |
| 498 | t.Fatal("member was not admitted beside a guest's local account") |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // Connect's same-person rule holds for Home-only people: a known Home-only |
| 503 | // member arriving over somebody else's live session is 403, and that |
| 504 | // session survives. |
| 505 | func TestKnownHomeOnlyArrivalOverAnotherSessionIsRefused(t *testing.T) { |
| 506 | f, h := newHomeOnlyFixture(t) |
| 507 | f.signInNew(t, "pr_member") |
| 508 | local := f.local(t, "x@example.com", "magiclink", time.Now().Add(-time.Minute)) |
| 509 | w := f.finishAs(t, "pr_member", []*http.Cookie{local}) |
| 510 | if w.Code != 403 || w.Body.String() != "failed" { |
| 511 | t.Fatalf("known arrival over another session=%d %q", w.Code, w.Body.String()) |
| 512 | } |
| 513 | bridgeNoSession(t, f.b, w) |
| 514 | if s, ok := f.sessions.From(bridgeRequest(http.MethodGet, "/", []*http.Cookie{local})); !ok || s.Subject != "x@example.com" { |
| 515 | t.Fatal("the live local session changed") |
| 516 | } |
| 517 | if h.unlinked != 1 { |
| 518 | t.Fatal("Unlinked asked again") |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // A linked subject is the linked account: no hook decides anything, and |
| 523 | // the link flow never reaches them. |
| 524 | func TestLinkedSubjectNeverReachesTheHooks(t *testing.T) { |
| 525 | f, h := newHomeOnlyFixture(t) |
| 526 | local := f.local(t, "one@example.com", "magiclink", time.Now().Add(-time.Minute)) |
| 527 | f.link(t, local, "pr_linked") |
| 528 | if h.unlinked != 0 || h.admitted != 0 { |
| 529 | t.Fatal("the link flow asked a team hook") |
| 530 | } |
| 531 | before := h.homeOnly |
| 532 | w := f.finishAs(t, "pr_linked", nil) |
| 533 | if w.Code != 303 || h.unlinked != 0 || h.admitted != 0 || h.homeOnly != before || h.count(t) != 0 { |
| 534 | t.Fatalf("linked sign-in=%d unlinked=%d admitted=%d homeOnly=%d", w.Code, h.unlinked, h.admitted, h.homeOnly-before) |
| 535 | } |
| 536 | p, err := f.b.ResolveSession(bridgeRequest(http.MethodGet, "/", []*http.Cookie{bridgeCookie(t, w, f.b.sessionCookie)})) |
| 537 | if err != nil || p.Label != "one@example.com" { |
| 538 | t.Fatal("linked identity changed", err) |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // Malformed cookies are still connect's refusals with the hooks set: the |
| 543 | // shape checks are shared, and none reaches a team hook. |
| 544 | func TestTeamHooksKeepConnectsCookieChecks(t *testing.T) { |
| 545 | f, h := newHomeOnlyFixture(t) |
| 546 | pending, nonce := f.start(t, nil, "/") |
| 547 | inbox := f.callback(t, "pr_member", nonce, time.Now().Add(-time.Minute)) |
| 548 | bad := strings.Repeat("A", 43) |
| 549 | for name, cookies := range map[string][]*http.Cookie{ |
| 550 | "unreadable session": {{Name: f.b.sessionCookie, Value: bad}}, |
| 551 | "duplicate session": {{Name: f.b.sessionCookie, Value: bad}, {Name: f.b.sessionCookie, Value: bad}}, |
| 552 | "malformed init": {{Name: homeInitCookie, Value: "bad"}}, |
| 553 | "malformed proof": {{Name: homeProofCookie, Value: "bad"}}, |
| 554 | } { |
| 555 | w := httptest.NewRecorder() |
| 556 | f.b.Finish(w, bridgeRequest(http.MethodGet, "/auth/finish", append(cookies, pending, inbox))) |
| 557 | if w.Code == 303 { |
| 558 | t.Fatalf("%s: answered %q", name, w.Header().Get("Location")) |
| 559 | } |
| 560 | bridgeNoSession(t, f.b, w) |
| 561 | } |
| 562 | if h.unlinked != 0 || h.admitted != 0 || h.count(t) != 0 { |
| 563 | t.Fatal("a malformed cookie reached a team hook") |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | // Signing out ends a pending choice: the proof row goes, not just the cookie. |
| 568 | func TestSignoutDropsAPendingChoice(t *testing.T) { |
| 569 | f, h := newHomeOnlyFixture(t) |
| 570 | routes := f.routes(t) |
| 571 | proof := bridgeCookie(t, f.finishAs(t, "pr_member", nil), homeChoiceCookie) |
| 572 | r := httptest.NewRequest(http.MethodPost, bridgeOrigin+"/signout", nil) |
| 573 | r.AddCookie(proof) |
| 574 | routes.Signout(httptest.NewRecorder(), r) |
| 575 | // The browser kept a copy of the cookie: it chooses nothing. |
| 576 | if w := f.choosePost(t, "new", f.b.choiceToken(proof.Value), []*http.Cookie{proof}); w.Code != 403 { |
| 577 | t.Fatalf("choice after sign-out=%d", w.Code) |
| 578 | } |
| 579 | if h.unlinked != 0 || h.count(t) != 0 { |
| 580 | t.Fatal("a signed-out choice allocated") |
| 581 | } |
| 582 | } |
| 583 | |