package idtest // What the door remembers, what Forget and Sign out forget, and the page // a sign-in kept. import ( "net/http" "net/url" "regexp" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/pow" ) // An Oficina ID sign-in is remembered with the address of the Equipo // account it signed in as (never the Home address, never one carried over // from the record it replaces), and the next door offers Oficina ID first // and the email way, with that address, among Other ways. func TestDoorRemembersOficinaID(t *testing.T) { t.Parallel() f := newFixture(t, true) f.link(t) _, earlier := f.emailRecord(t, address) f.member(t, "someone-else@example.test") _, other := f.emailRecord(t, "someone-else@example.test") done := f.oficinaSignin(t, other) record := cookie(t, done, jarCookie) if record.Value == earlier.Value || record.Value == other.Value { t.Fatal("no new record") } door := f.request("GET", "/signin", "", nil, record).Body.String() if !strings.Contains(door, `href="/signin/oficina"`) || strings.Contains(door, "Continue as") { t.Fatal("the door does not offer Oficina ID first", door) } ways := f.request("GET", "/signin?ways=1&way=oficina", "", nil, record).Body.String() if !strings.Contains(ways, address) || strings.Contains(ways, "someone-else@example.test") || strings.Contains(ways, homeAddress) { t.Fatal("the remembered address is not the signed-in account's", ways) } // Confirming an emailed link leaves the record alone; only the // sign-in it finishes writes one. page := f.request("GET", "/signin?way=email", "", nil, record) form := fields(page.Body.String(), "/auth/begin") form.Set("address", address) time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) begin := f.request("POST", "/auth/begin", f.origin, form, record) jar := mergeCookies([]*http.Cookie{record}, page.Result().Cookies(), begin.Result().Cookies()) if confirm := f.request("GET", f.mailedLink(t), "", nil, jar...); confirm.Code != 200 { t.Fatal("confirm", confirm.Code) } else if c, _ := set(confirm, jarCookie); c != nil { t.Fatal("the confirm step touched the remembered way") } } func TestDoorForget(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { kept := &http.Cookie{Name: keptCookie, Value: "%2Fwork"} plain := f.request("POST", "/auth/forget", f.origin, nil, kept) if plain.Code != http.StatusSeeOther || plain.Header().Get("Location") != "/signin" { t.Fatal("Use a different email", plain.Code, plain.Header().Get("Location")) } if _, clears := set(plain, keptCookie); !clears { t.Fatal("Forget kept the page a sign-in kept") } chained := f.request("POST", "/auth/forget", f.origin, url.Values{"chain": {"oficina"}}) want := "/signin" if home { want = issuer + "/forget?return=https%3A%2F%2Fequipo.example%2Fsigned-out" } if chained.Code != http.StatusSeeOther || chained.Header().Get("Location") != want { t.Fatal("Forget me on this device", chained.Code, chained.Header().Get("Location"), want) } if _, clears := set(chained, jarCookie); !clears { t.Fatal("Forget forgot nothing") } if cross := f.request("POST", "/auth/forget", "https://evil.example", url.Values{"chain": {"oficina"}}); cross.Code != http.StatusForbidden || cross.Header().Get("Location") != "" { t.Fatal("a cross-site Forget", cross.Code, cross.Header().Get("Location")) } }) } var forgetForm = regexp.MustCompile(`(?s)
(.*?)
`) // Sign out lands on /signed-out, in the door's words, with Forget me on // this device (chained to Oficina ID with Home) and, after a Home sign-in, // Oficina ID's own sign-out. func TestDoorSignedOut(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { s := f.local(t, address) out := f.request("POST", "/signout", f.origin, nil, s, &http.Cookie{Name: keptCookie, Value: "%2Fwork"}) if out.Code != http.StatusSeeOther || out.Header().Get("Location") != "/signed-out" { t.Fatal("sign out", out.Code, out.Header().Get("Location")) } if _, clears := set(out, keptCookie); !clears || f.sessionAlive(t, s) { t.Fatal("sign out kept the session or the kept page") } page := f.request("GET", "/signed-out", "", nil, out.Result().Cookies()...) body := page.Body.String() if page.Code != 200 || title(body) != "Signed out · Equipo" || !strings.Contains(body, "You’re signed out") || !strings.Contains(body, `href="/signin"`) { t.Fatal("signed-out page", page.Code, title(body)) } form := forgetForm.FindStringSubmatch(body) if form == nil || !strings.Contains(form[1], "Forget me on this device") || strings.Contains(form[1], `name="chain"`) != home { t.Fatal("Forget on the signed-out page", form) } if strings.Contains(body, "Sign out of Oficina ID too") { t.Fatal("a local sign-out offered Oficina ID's") } if home { f.link(t) h := cookie(t, f.oficinaSignin(t), sessionCookie) out := f.request("POST", "/signout", f.origin, nil, h) landing := f.request("GET", "/signed-out", "", nil, cookie(t, out, "__Host-equipo-id-logout")).Body.String() if !strings.Contains(landing, `href="`+issuer+`/signout"`) || !strings.Contains(landing, "Sign out of Oficina ID too") || !forgetForm.MatchString(landing) { t.Fatal("signed out after Oficina ID", landing) } } }) } // The page an email sign-in kept (rastrillo's return memory) is spent by // every landing that mints a session, and by a plain /signin; a door state // keeps it. func TestDoorKeptPage(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { page := f.request("GET", "/signin?way=email&return_to=%2Fwork", "", nil) form := fields(page.Body.String(), "/auth/begin") if form.Get("return_to") != "/work" { t.Fatal("the door's form lost the page", form) } form.Set("address", address) time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) begin := f.request("POST", "/auth/begin", f.origin, form) kept := cookie(t, begin, keptCookie) f.mailedLink(t) if w := f.request("GET", "/signin?sent=1", "", nil, kept); func() bool { _, c := set(w, keptCookie); return c }() { t.Fatal("Sent spent the kept page") } if w := f.request("GET", "/signin", "", nil, kept); func() bool { _, c := set(w, keptCookie); return !c }() { t.Fatal("a plain /signin kept an abandoned sign-in's page") } if home { f.link(t) if _, clears := set(f.oficinaSignin(t, kept), keptCookie); !clears { t.Fatal("an Oficina ID landing kept the email sign-in's page") } } // A /signed-out page is no page to land on. w := f.request("GET", "/signin?way=email&return_to=%2Fsigned-out", "", nil) if strings.Contains(w.Body.String(), `name="return_to" value="/signed-out"`) { t.Fatal("the door would land a sign-in on /signed-out") } }) }