| 1 | package idtest |
| 2 | |
| 3 | // What the door remembers, what Forget and Sign out forget, and the page |
| 4 | // a sign-in kept. |
| 5 | |
| 6 | import ( |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/rastrillo/rastrillo/pow" |
| 15 | ) |
| 16 | |
| 17 | // An Oficina ID sign-in is remembered with the address of the Equipo |
| 18 | // account it signed in as (never the Home address, never one carried over |
| 19 | // from the record it replaces), and the next door offers Oficina ID first |
| 20 | // and the email way, with that address, among Other ways. |
| 21 | func TestDoorRemembersOficinaID(t *testing.T) { |
| 22 | t.Parallel() |
| 23 | f := newFixture(t, true) |
| 24 | f.link(t) |
| 25 | _, earlier := f.emailRecord(t, address) |
| 26 | f.member(t, "someone-else@example.test") |
| 27 | _, other := f.emailRecord(t, "someone-else@example.test") |
| 28 | done := f.oficinaSignin(t, other) |
| 29 | record := cookie(t, done, jarCookie) |
| 30 | if record.Value == earlier.Value || record.Value == other.Value { |
| 31 | t.Fatal("no new record") |
| 32 | } |
| 33 | door := f.request("GET", "/signin", "", nil, record).Body.String() |
| 34 | if !strings.Contains(door, `href="/signin/oficina"`) || strings.Contains(door, "Continue as") { |
| 35 | t.Fatal("the door does not offer Oficina ID first", door) |
| 36 | } |
| 37 | ways := f.request("GET", "/signin?ways=1&way=oficina", "", nil, record).Body.String() |
| 38 | if !strings.Contains(ways, address) || strings.Contains(ways, "someone-else@example.test") || strings.Contains(ways, homeAddress) { |
| 39 | t.Fatal("the remembered address is not the signed-in account's", ways) |
| 40 | } |
| 41 | // Confirming an emailed link leaves the record alone; only the |
| 42 | // sign-in it finishes writes one. |
| 43 | page := f.request("GET", "/signin?way=email", "", nil, record) |
| 44 | form := fields(page.Body.String(), "/auth/begin") |
| 45 | form.Set("address", address) |
| 46 | time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) |
| 47 | begin := f.request("POST", "/auth/begin", f.origin, form, record) |
| 48 | jar := mergeCookies([]*http.Cookie{record}, page.Result().Cookies(), begin.Result().Cookies()) |
| 49 | if confirm := f.request("GET", f.mailedLink(t), "", nil, jar...); confirm.Code != 200 { |
| 50 | t.Fatal("confirm", confirm.Code) |
| 51 | } else if c, _ := set(confirm, jarCookie); c != nil { |
| 52 | t.Fatal("the confirm step touched the remembered way") |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestDoorForget(t *testing.T) { |
| 57 | t.Parallel() |
| 58 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 59 | kept := &http.Cookie{Name: keptCookie, Value: "%2Fwork"} |
| 60 | plain := f.request("POST", "/auth/forget", f.origin, nil, kept) |
| 61 | if plain.Code != http.StatusSeeOther || plain.Header().Get("Location") != "/signin" { |
| 62 | t.Fatal("Use a different email", plain.Code, plain.Header().Get("Location")) |
| 63 | } |
| 64 | if _, clears := set(plain, keptCookie); !clears { |
| 65 | t.Fatal("Forget kept the page a sign-in kept") |
| 66 | } |
| 67 | chained := f.request("POST", "/auth/forget", f.origin, url.Values{"chain": {"oficina"}}) |
| 68 | want := "/signin" |
| 69 | if home { |
| 70 | want = issuer + "/forget?return=https%3A%2F%2Fequipo.example%2Fsigned-out" |
| 71 | } |
| 72 | if chained.Code != http.StatusSeeOther || chained.Header().Get("Location") != want { |
| 73 | t.Fatal("Forget me on this device", chained.Code, chained.Header().Get("Location"), want) |
| 74 | } |
| 75 | if _, clears := set(chained, jarCookie); !clears { |
| 76 | t.Fatal("Forget forgot nothing") |
| 77 | } |
| 78 | if cross := f.request("POST", "/auth/forget", "https://evil.example", url.Values{"chain": {"oficina"}}); cross.Code != http.StatusForbidden || cross.Header().Get("Location") != "" { |
| 79 | t.Fatal("a cross-site Forget", cross.Code, cross.Header().Get("Location")) |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | var forgetForm = regexp.MustCompile(`(?s)<form estilo-signin-forget method="post" action="/auth/forget">(.*?)</form>`) |
| 85 | |
| 86 | // Sign out lands on /signed-out, in the door's words, with Forget me on |
| 87 | // this device (chained to Oficina ID with Home) and, after a Home sign-in, |
| 88 | // Oficina ID's own sign-out. |
| 89 | func TestDoorSignedOut(t *testing.T) { |
| 90 | t.Parallel() |
| 91 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 92 | s := f.local(t, address) |
| 93 | out := f.request("POST", "/signout", f.origin, nil, s, &http.Cookie{Name: keptCookie, Value: "%2Fwork"}) |
| 94 | if out.Code != http.StatusSeeOther || out.Header().Get("Location") != "/signed-out" { |
| 95 | t.Fatal("sign out", out.Code, out.Header().Get("Location")) |
| 96 | } |
| 97 | if _, clears := set(out, keptCookie); !clears || f.sessionAlive(t, s) { |
| 98 | t.Fatal("sign out kept the session or the kept page") |
| 99 | } |
| 100 | page := f.request("GET", "/signed-out", "", nil, out.Result().Cookies()...) |
| 101 | body := page.Body.String() |
| 102 | if page.Code != 200 || title(body) != "Signed out · Equipo" || !strings.Contains(body, "You’re signed out") || !strings.Contains(body, `href="/signin"`) { |
| 103 | t.Fatal("signed-out page", page.Code, title(body)) |
| 104 | } |
| 105 | form := forgetForm.FindStringSubmatch(body) |
| 106 | if form == nil || !strings.Contains(form[1], "Forget me on this device") || strings.Contains(form[1], `name="chain"`) != home { |
| 107 | t.Fatal("Forget on the signed-out page", form) |
| 108 | } |
| 109 | if strings.Contains(body, "Sign out of Oficina ID too") { |
| 110 | t.Fatal("a local sign-out offered Oficina ID's") |
| 111 | } |
| 112 | if home { |
| 113 | f.link(t) |
| 114 | h := cookie(t, f.oficinaSignin(t), sessionCookie) |
| 115 | out := f.request("POST", "/signout", f.origin, nil, h) |
| 116 | landing := f.request("GET", "/signed-out", "", nil, cookie(t, out, "__Host-equipo-id-logout")).Body.String() |
| 117 | if !strings.Contains(landing, `href="`+issuer+`/signout"`) || !strings.Contains(landing, "Sign out of Oficina ID too") || !forgetForm.MatchString(landing) { |
| 118 | t.Fatal("signed out after Oficina ID", landing) |
| 119 | } |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | // The page an email sign-in kept (rastrillo's return memory) is spent by |
| 125 | // every landing that mints a session, and by a plain /signin; a door state |
| 126 | // keeps it. |
| 127 | func TestDoorKeptPage(t *testing.T) { |
| 128 | t.Parallel() |
| 129 | modes(t, func(t *testing.T, f *fixture, home bool) { |
| 130 | page := f.request("GET", "/signin?way=email&return_to=%2Fwork", "", nil) |
| 131 | form := fields(page.Body.String(), "/auth/begin") |
| 132 | if form.Get("return_to") != "/work" { |
| 133 | t.Fatal("the door's form lost the page", form) |
| 134 | } |
| 135 | form.Set("address", address) |
| 136 | time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) |
| 137 | begin := f.request("POST", "/auth/begin", f.origin, form) |
| 138 | kept := cookie(t, begin, keptCookie) |
| 139 | f.mailedLink(t) |
| 140 | if w := f.request("GET", "/signin?sent=1", "", nil, kept); func() bool { _, c := set(w, keptCookie); return c }() { |
| 141 | t.Fatal("Sent spent the kept page") |
| 142 | } |
| 143 | if w := f.request("GET", "/signin", "", nil, kept); func() bool { _, c := set(w, keptCookie); return !c }() { |
| 144 | t.Fatal("a plain /signin kept an abandoned sign-in's page") |
| 145 | } |
| 146 | if home { |
| 147 | f.link(t) |
| 148 | if _, clears := set(f.oficinaSignin(t, kept), keptCookie); !clears { |
| 149 | t.Fatal("an Oficina ID landing kept the email sign-in's page") |
| 150 | } |
| 151 | } |
| 152 | // A /signed-out page is no page to land on. |
| 153 | w := f.request("GET", "/signin?way=email&return_to=%2Fsigned-out", "", nil) |
| 154 | if strings.Contains(w.Body.String(), `name="return_to" value="/signed-out"`) { |
| 155 | t.Fatal("the door would land a sign-in on /signed-out") |
| 156 | } |
| 157 | }) |
| 158 | } |
| 159 | |