| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/home/connect" |
| 11 | "github.com/carlosframework/docs/internal/docs/principalstore" |
| 12 | ) |
| 13 | |
| 14 | // The member choice through Docs' real router (Home spec |
| 15 | // 2026-11-14-oficina-id-native.md §8). |
| 16 | |
| 17 | // "Connect my existing account": the proof is spent, local sign-in resumes |
| 18 | // the link at once, and the member's Oficina ID then opens that account. |
| 19 | func TestTeamChooseConnectLinksTheExistingAccount(t *testing.T) { |
| 20 | f := newTeamRouterFixture(t) |
| 21 | b := f.browser() |
| 22 | nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| 23 | token := b.choosePage(t, b.exchange(t, nonce, teamSubject('a'), time.Now().Add(-time.Minute))) |
| 24 | routerRedirect(t, b.choose("connect", token), "/signin/local") |
| 25 | if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('a')); !errors.Is(e, principalstore.ErrRefused) { |
| 26 | t.Fatal("connect allocated a principal", e) |
| 27 | } |
| 28 | // The spent proof chooses nothing more. |
| 29 | if r := b.choose("new", token); r.Code == 303 { |
| 30 | t.Fatal("a spent proof chose again") |
| 31 | } |
| 32 | // Local sign-in lands on the link's fresh Home check, then the confirm. |
| 33 | b.localSignin(t, "alice@example.com", "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm") |
| 34 | nonce = b.start(t, "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm", "300") |
| 35 | routerRedirect(t, b.exchange(t, nonce, teamSubject('a'), time.Now().Add(-time.Minute)), "/account/home/confirm") |
| 36 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 37 | p, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('a')) |
| 38 | if e != nil || p.ID != f.alice || p.Kind != principalstore.Local { |
| 39 | t.Fatal("the member's Oficina ID does not open the existing account", p, e) |
| 40 | } |
| 41 | // A fresh browser now signs straight in as Alice, with no choice. |
| 42 | other := f.browser() |
| 43 | nonce = other.start(t, "/auth/start?return_to=%2F", "") |
| 44 | routerRedirect(t, other.exchange(t, nonce, teamSubject('a'), time.Now()), "/") |
| 45 | if r := other.do("GET", "/d/"+f.own.Sqid, ""); r.Code != 200 { |
| 46 | t.Fatal("linked member cannot open their own document", r.Code) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // "I'm new here" is refused while the browser holds any app session, and |
| 51 | // the proof survives for a choice made without one. |
| 52 | func TestTeamChooseNewRefusedWithAnAppSession(t *testing.T) { |
| 53 | f := newTeamRouterFixture(t) |
| 54 | b := f.browser() |
| 55 | nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| 56 | token := b.choosePage(t, b.exchange(t, nonce, teamSubject('b'), time.Now())) |
| 57 | proof := b.cookies["__Host-oficina-home-choice"] |
| 58 | b.localSignin(t, "bob@example.com") |
| 59 | b.cookies[proof.Name] = proof |
| 60 | if r := b.choose("new", token); r.Code != 403 { |
| 61 | t.Fatalf("new over a session=%d", r.Code) |
| 62 | } |
| 63 | if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('b')); !errors.Is(e, principalstore.ErrRefused) { |
| 64 | t.Fatal("allocated over a live session", e) |
| 65 | } |
| 66 | fresh := f.browser() |
| 67 | fresh.cookies[proof.Name] = proof |
| 68 | routerRedirect(t, fresh.choose("new", token), "/") |
| 69 | } |
| 70 | |
| 71 | // A cross-origin choose POST is stopped by Docs' own CSRF middleware |
| 72 | // (csrf.Protect, under connect's Middleware) before connect sees it. |
| 73 | func TestTeamChooseRefusesACrossOriginPost(t *testing.T) { |
| 74 | f := newTeamRouterFixture(t) |
| 75 | b := f.browser() |
| 76 | nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| 77 | token := b.choosePage(t, b.exchange(t, nonce, teamSubject('b'), time.Now())) |
| 78 | r := b.request("POST", connect.ChoosePath, url.Values{"choice": {"new"}, "token": {token}}.Encode()) |
| 79 | r.Header.Set("Origin", "https://evil.example") |
| 80 | r.Header.Set("Sec-Fetch-Site", "cross-site") |
| 81 | rec := b.serve(r) |
| 82 | if rec.Code != 403 || !strings.Contains(rec.Body.String(), "cross-origin") { |
| 83 | t.Fatalf("cross-origin choose=%d %q", rec.Code, rec.Body.String()) |
| 84 | } |
| 85 | if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('b')); !errors.Is(e, principalstore.ErrRefused) { |
| 86 | t.Fatal("a cross-origin POST allocated", e) |
| 87 | } |
| 88 | routerRedirect(t, b.choose("new", token), "/") |
| 89 | } |
| 90 | |