| 1 | package calendar |
| 2 | |
| 3 | // What the door remembers, and forgets. |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/home/connect" |
| 13 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 14 | "amadan.net/rastrillo/rastrillo/sessions" |
| 15 | ) |
| 16 | |
| 17 | // An Oficina ID sign-in is remembered as Oficina ID with the address of the |
| 18 | // person it signed in as, replacing whatever was remembered before; nothing |
| 19 | // carries over. |
| 20 | func TestOficinaIDIsRememberedWithTheResolvedAddress(t *testing.T) { |
| 21 | f := newHomeRouterFixture(t) |
| 22 | f.linkAlice(t, "opaque-home-alice") |
| 23 | f.cookies = map[string]*http.Cookie{} |
| 24 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "somebody.else@example.test"}) |
| 25 | if got := homeRedirect(t, f.oficinaSignin(t, connect.SigninOficinaPath, "opaque-home-alice")); got != "/" { |
| 26 | t.Fatalf("landed on %q", got) |
| 27 | } |
| 28 | rec, res := f.remembered() |
| 29 | if res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated || rec.Provider != lastsignin.ProviderOficina || rec.Address != "alice@example.test" { |
| 30 | t.Fatalf("remembered %+v (%v), want Oficina ID as alice", rec, res) |
| 31 | } |
| 32 | // Signed out, the next door offers it back, never captioned with the |
| 33 | // address. |
| 34 | delete(f.cookies, f.app.sessionCookie()) |
| 35 | card := door(t, f.get("/signin").Body.String()) |
| 36 | if !strings.Contains(card, oficinaButton) || strings.Contains(card, "alice@example.test") { |
| 37 | t.Errorf("the remembered door:\n%s", card) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // An unknown subject signs nobody in, and nothing is remembered. |
| 42 | func TestNothingIsRememberedForAnUnknownSubject(t *testing.T) { |
| 43 | f := newHomeRouterFixture(t) |
| 44 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 45 | if w := f.oficinaSignin(t, connect.SigninOficinaPath, "unknown-home"); w.Code != http.StatusForbidden { |
| 46 | t.Fatalf("unknown subject: %d", w.Code) |
| 47 | } |
| 48 | if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodMagicLink { |
| 49 | t.Errorf("an unknown subject changed the record: %+v %v", rec, res) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Outside team mode connect mints no session for a linked person the |
| 54 | // allowlist no longer holds (its lookup refuses them as unknown), so |
| 55 | // nothing is remembered and nobody is signed in. |
| 56 | func TestARefusedLinkedPersonIsNotRemembered(t *testing.T) { |
| 57 | f := newHomeRouterFixture(t) |
| 58 | f.linkAlice(t, "opaque-home-alice") |
| 59 | f.cookies = map[string]*http.Cookie{} |
| 60 | delete(f.app.home.admitted.addrs, "alice@example.test") |
| 61 | if w := f.oficinaSignin(t, connect.SigninOficinaPath, "opaque-home-alice"); w.Code != http.StatusForbidden { |
| 62 | t.Fatalf("a refused linked person's exchange = %d, want connect's 403", w.Code) |
| 63 | } |
| 64 | if rec, res := f.remembered(); res == lastsignin.Valid { |
| 65 | t.Fatalf("a refused Oficina ID sign-in was remembered: %+v", rec) |
| 66 | } |
| 67 | if f.sessionCookie() != nil { |
| 68 | t.Fatal("a refused linked person got a session") |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // rememberOficina asks strictly, of the session MintHomeSession has just |
| 73 | // set on the response (admittedAs), never of one the request carried |
| 74 | // (Fable on Docs #139, A2): connect mints for any person it resolves, and |
| 75 | // a record for a refused one would offer Oficina ID again, round a loop of |
| 76 | // sign-in, refusal and door. |
| 77 | func TestOficinaIDIsRememberedOnlyForAnAdmittedSession(t *testing.T) { |
| 78 | f := newHomeRouterFixture(t) |
| 79 | mint := func(carried, minted string) bool { |
| 80 | t.Helper() |
| 81 | g := f.fresh() |
| 82 | if carried != "" { |
| 83 | g.session(t, carried, "magiclink", time.Now()) |
| 84 | } |
| 85 | r := g.jarRequest() |
| 86 | w := httptest.NewRecorder() |
| 87 | if err := f.app.sessions.SignIn(w, r, sessions.Session{Subject: minted, Method: "magiclink", AuthTime: time.Now()}); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | f.app.rememberOficina(w, r) |
| 91 | for _, c := range w.Result().Cookies() { |
| 92 | if c.Name != f.app.sessionCookie() { |
| 93 | return true |
| 94 | } |
| 95 | } |
| 96 | return false |
| 97 | } |
| 98 | if mint("", "nobody@example.test") { |
| 99 | t.Error("rememberOficina remembered a session the routes refuse") |
| 100 | } |
| 101 | if mint("alice@example.test", "nobody@example.test") { |
| 102 | t.Error("rememberOficina judged the request's old session, not the one just minted") |
| 103 | } |
| 104 | if !mint("", "alice@example.test") || !mint("nobody@example.test", "alice@example.test") { |
| 105 | t.Error("rememberOficina did not remember an admitted session") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Opening an emailed link's confirm step leaves the remembered way alone: |
| 110 | // only the sign-in it finishes writes one. |
| 111 | func TestTheConfirmStepLeavesTheRecordAlone(t *testing.T) { |
| 112 | f := newHomeRouterFixture(t) |
| 113 | homeRedirect(t, f.do(http.MethodPost, "/signin", "address=alice%40example.test")) |
| 114 | f.app.signinMail.wait() |
| 115 | target, _ := linkPath(magicLinkPattern.FindString(f.app.mailRecorder.last())) |
| 116 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"}) |
| 117 | if w := f.get(target); w.Code != http.StatusOK { |
| 118 | t.Fatalf("confirm: %d", w.Code) |
| 119 | } |
| 120 | if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated { |
| 121 | t.Errorf("the confirm step changed the record: %+v %v", rec, res) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Signed out, with Home: "Forget me on this device" posts chain=oficina to |
| 126 | // Forget, which forgets here and goes on to Oficina ID's own Forget, |
| 127 | // returning to this signed-out page. Forget refuses a cross-site post. The |
| 128 | // door's own "Use a different email" stays on the door. |
| 129 | func TestSignedOutForgetsAndChainsToOficinaID(t *testing.T) { |
| 130 | f := newHomeRouterFixture(t) |
| 131 | w := f.get("/signed-out") |
| 132 | body := w.Body.String() |
| 133 | for _, want := range []string{"You’re signed out", "Forget me on this device", `action="/signin/forget"`, `name="chain" value="oficina"`, `href="/signin"`} { |
| 134 | if !strings.Contains(body, want) { |
| 135 | t.Errorf("/signed-out lacks %q", want) |
| 136 | } |
| 137 | } |
| 138 | if titleOf(t, body) != "Signed out · Calendar" || w.Header().Get("Cache-Control") != "no-store" || strings.Contains(body, "Home") { |
| 139 | t.Errorf("/signed-out: title %q, Cache-Control %q", titleOf(t, body), w.Header().Get("Cache-Control")) |
| 140 | } |
| 141 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 142 | out := f.do(http.MethodPost, signinForgetPath, "chain=oficina") |
| 143 | redirectsTo(t, out, homeTestIssuer+"/forget?return="+"https%3A%2F%2Fcalendar.example.test%2Fsigned-out") |
| 144 | if _, res := f.remembered(); res == lastsignin.Valid { |
| 145 | t.Error("Forget kept the record") |
| 146 | } |
| 147 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 148 | if loc := f.do(http.MethodPost, signinForgetPath, "").Header().Get("Location"); !strings.HasPrefix(loc, "/signin") { |
| 149 | t.Errorf("the door's Use a different email went to %q", loc) |
| 150 | } |
| 151 | // Cross-site: refused, and nothing forgotten. |
| 152 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 153 | r := httptest.NewRequest(http.MethodPost, homeTestOrigin+signinForgetPath, strings.NewReader("chain=oficina")) |
| 154 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 155 | r.Header.Set("Origin", "https://evil.example") |
| 156 | r.Header.Set("Sec-Fetch-Site", "cross-site") |
| 157 | for _, c := range f.cookies { |
| 158 | r.AddCookie(c) |
| 159 | } |
| 160 | cross := httptest.NewRecorder() |
| 161 | f.app.mux.ServeHTTP(cross, r) |
| 162 | if cross.Code != http.StatusForbidden || strings.Contains(cross.Header().Get("Location"), "/forget") || len(cross.Result().Cookies()) != 0 { |
| 163 | t.Errorf("cross-site Forget: %d %q %v", cross.Code, cross.Header().Get("Location"), cross.Result().Cookies()) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Standalone, the signed-out page forgets here only: no chain, nowhere else. |
| 168 | func TestSignedOutForgetsStandalone(t *testing.T) { |
| 169 | f := newStandaloneDoor(t) |
| 170 | body := f.get("/signed-out").Body.String() |
| 171 | if !strings.Contains(body, "Forget me on this device") || strings.Contains(body, `name="chain"`) { |
| 172 | t.Errorf("standalone /signed-out:\n%s", body) |
| 173 | } |
| 174 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 175 | w := f.do(http.MethodPost, signinForgetPath, "chain=oficina") |
| 176 | if loc := w.Header().Get("Location"); w.Code != http.StatusSeeOther || strings.Contains(loc, "forget") || !strings.HasPrefix(loc, "/signin") { |
| 177 | t.Errorf("standalone Forget: %d %q", w.Code, loc) |
| 178 | } |
| 179 | if _, res := f.remembered(); res == lastsignin.Valid { |
| 180 | t.Error("standalone Forget kept the record") |
| 181 | } |
| 182 | // Sign out lands on the signed-out page. |
| 183 | f.session(t, "alice@example.test", "magiclink", time.Now()) |
| 184 | redirectsTo(t, f.do(http.MethodPost, "/signout", ""), "/signed-out") |
| 185 | } |
| 186 | |