oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3// Remembering Oficina ID, Forget (chained to Oficina ID's own), and the
4// signed-out page.
5
6import (
7 "net/http"
8 "net/http/httptest"
9 "net/url"
10 "strings"
11 "testing"
12 "time"
13
14 "amadan.net/oficina/home/handoff"
15 "amadan.net/rastrillo/rastrillo/lastsignin"
16 "amadan.net/rastrillo/rastrillo/sessions"
17)
18
19// A finished Oficina ID sign-in is remembered as Oficina ID, with the
20// address of the person connect resolved for it, and nothing carried over
21// from the record it replaces.
22func TestOficinaIDIsRememberedWithThePersonsAddress(t *testing.T) {
23 b, u := newHomeBrowser(t)
24 homeLinked(t, b.a, u)
25 br := b.browser()
26 // A record of somebody else's, which must not survive.
27 br.remember(lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "someone.else@example.com"})
28 fin := br.oficinaSignin("/signin/oficina?return_to=%2Fd%2F7", "home-person")
29 redirectsTo(t, fin, "/d/7")
30 rec, res := br.remembered()
31 if res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated || rec.Provider != lastsignin.ProviderOficina || rec.Address != u.Email {
32 t.Fatalf("remembered %+v (%v), want Oficina ID as %s", rec, res, u.Email)
33 }
34 // The door now offers Continue with Oficina ID, uncaptioned.
35 delete(br.jar, b.a.sessions.CookieName())
36 if card := door(t, br.get("/signin").Body.String()); !strings.Contains(card, oficinaButton) || strings.Contains(card, u.Email) {
37 t.Fatalf("the remembered door:\n%s", card)
38 }
39}
40
41// An Oficina ID sign-in connect cannot turn into a session here (a
42// subject with no link: "unknown") remembers nothing.
43func TestAnUnknownOficinaIDIsNotRemembered(t *testing.T) {
44 b, _ := newHomeBrowser(t)
45 br := b.browser()
46 br.remember(lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "owner@example.com"})
47 fin := br.oficinaSignin("/signin/oficina", "unlinked-home-person")
48 if fin.Code != http.StatusForbidden {
49 t.Fatalf("unknown arrival: %d", fin.Code)
50 }
51 if rec, res := br.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodMagicLink {
52 t.Fatalf("an unknown arrival changed the record: %+v (%v)", rec, res)
53 }
54}
55
56// A Home-only member (team mode, "I'm new here") is remembered as Oficina
57// ID with no address: there is none to remember.
58func TestAHomeOnlyMemberIsRememberedWithNoAddress(t *testing.T) {
59 f := newTeamFixture(t)
60 w := httptest.NewRecorder()
61 f.a.auth.RememberJar().Remember(w, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "someone.else@example.com"})
62 f.member(teamSubject('a'), "member")
63 // member signs in from an empty jar; put the old record in front of a
64 // second sign-in to show nothing carries over.
65 for _, c := range w.Result().Cookies() {
66 f.jar[c.Name] = c
67 }
68 delete(f.jar, f.a.sessions.CookieName())
69 if fin := f.signInHome(teamSubject('a')); fin.Code != http.StatusSeeOther {
70 t.Fatalf("second sign-in: %d", fin.Code)
71 }
72 rec, res := readRemembered(f.a, f.jar)
73 if res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated || rec.Address != "" {
74 t.Fatalf("remembered %+v (%v), want Oficina ID with no address", rec, res)
75 }
76}
77
78// rememberOficina writes the record only for a session the signed-in
79// routes admit, asked of the session MintHomeSession has just set (Fable
80// on Docs #139, A2): a refused one would offer Oficina ID again, round a
81// loop of sign-in, refusal and door.
82func TestOficinaIDIsRememberedOnlyForAnAdmittedSession(t *testing.T) {
83 b, u := newHomeBrowser(t)
84 for _, c := range []struct {
85 subject string
86 want bool
87 }{{u.Email, true}, {"gone@example.com", false}} {
88 w := httptest.NewRecorder()
89 r := httptest.NewRequest(http.MethodGet, homeTestOrigin+handoff.FinishPath, nil)
90 if err := b.a.homeSessions.SignIn(w, r, sessions.Session{Subject: c.subject, Method: "magiclink", AuthTime: time.Now()}); err != nil {
91 t.Fatal(err)
92 }
93 b.a.rememberOficina(w, r)
94 remembered := false
95 for _, ck := range w.Result().Cookies() {
96 jar := map[string]*http.Cookie{ck.Name: ck}
97 if _, res := readRemembered(b.a, jar); res == lastsignin.Valid {
98 remembered = true
99 }
100 }
101 if remembered != c.want {
102 t.Errorf("%s: remembered = %v, want %v", c.subject, remembered, c.want)
103 }
104 }
105}
106
107// The signed-out page offers Forget me on this device. With Home its form
108// asks to go on to Oficina ID's own Forget; Forget then forgets this
109// browser here, and the return memory, and sends it to
110// {issuer}/forget?return=<origin>/signed-out. Without the chain (the
111// door's Use a different email) it stays on the door. A cross-site post is
112// passed on as auth refuses it.
113func TestForgetChainsToOficinaID(t *testing.T) {
114 b, _ := newHomeBrowser(t)
115 page := b.get("/signed-out")
116 body := page.Body.String()
117 if page.Code != http.StatusOK || !strings.Contains(body, `action="/signin/forget"`) || !strings.Contains(body, `name="chain" value="oficina"`) || !strings.Contains(body, "Forget me on this device") {
118 t.Fatalf("signed-out page: %d\n%s", page.Code, body)
119 }
120 br := b.browser()
121 br.remember(lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "owner@example.com"})
122 br.get("/signin?return_to=%2Fd%2F7")
123 rec := br.do(http.MethodPost, signinForgetPath, url.Values{"chain": {"oficina"}}, "")
124 redirectsTo(t, rec, "https://home.example.com/forget?return="+url.QueryEscape(homeTestOrigin+"/signed-out"))
125 if _, res := br.remembered(); res == lastsignin.Valid {
126 t.Error("Forget kept the remembered way")
127 }
128 if !clears(rec, returnCookie) || br.returnPath() != "" {
129 t.Error("Forget kept the return memory")
130 }
131 // The door's Use a different email: no chain, back on the door.
132 plain := b.browser().do(http.MethodPost, signinForgetPath, url.Values{"return_to": {"/d/7"}, "way": {"email"}}, "")
133 redirectsTo(t, plain, "/signin?return_to=%2Fd%2F7&way=email")
134 // Cross-site: auth's own refusal, passed on.
135 if cross := b.browser().do(http.MethodPost, signinForgetPath, url.Values{"chain": {"oficina"}}, "https://evil.example"); cross.Code != http.StatusForbidden {
136 t.Errorf("cross-site Forget: %d %q", cross.Code, cross.Header().Get("Location"))
137 }
138}
139
140// Standalone, the signed-out page's Forget has no chain, and Forget stays
141// here even if one is posted.
142func TestForgetStandaloneHasNoChain(t *testing.T) {
143 b, _ := newStandaloneBrowser(t)
144 body := b.get("/signed-out").Body.String()
145 if !strings.Contains(body, `action="/signin/forget"`) || strings.Contains(body, `name="chain"`) {
146 t.Fatalf("standalone signed-out page:\n%s", body)
147 }
148 redirectsTo(t, b.do(http.MethodPost, signinForgetPath, url.Values{"chain": {"oficina"}}, ""), "/signin")
149}
150
151// Sign out forgets the return memory too, in both modes: it was this
152// person's page, not the next one's. With Home, the hop to Oficina ID's
153// sign-out is unchanged.
154func TestSignOutForgetsTheReturnMemory(t *testing.T) {
155 for _, mk := range []func(*testing.T) (*homeBrowser, User){newHomeBrowser, newStandaloneBrowser} {
156 b, u := mk(t)
157 b.get("/signin?return_to=%2Fd%2F7")
158 if b.returnPath() != "/d/7" {
159 t.Fatal("the door did not remember the page")
160 }
161 if b.a.home != nil {
162 b.set(homeSession(t, b.a, u.Email, "magiclink"))
163 } else {
164 b.set(standaloneSession(t, b.a, u.Email))
165 }
166 rec := b.do(http.MethodPost, "/signout", nil, "")
167 if rec.Code != http.StatusSeeOther && rec.Code != http.StatusTemporaryRedirect {
168 t.Fatalf("sign out: %d", rec.Code)
169 }
170 if !clears(rec, returnCookie) {
171 t.Errorf("home=%v: sign out kept the return memory", b.a.home != nil)
172 }
173 want := "/signed-out"
174 if b.a.home != nil {
175 want = "https://home.example.com/signout"
176 }
177 if rec.Header().Get("Location") != want {
178 t.Errorf("sign out went to %q, want %q", rec.Header().Get("Location"), want)
179 }
180 }
181}
182
183// Confirming an emailed link (the GET that draws the confirm step) leaves
184// the remembered way alone: the link may have been sent to anyone.
185func TestTheConfirmStepLeavesTheRecordAlone(t *testing.T) {
186 b, u := newHomeBrowser(t)
187 b.remember(lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: u.Email})
188 b.a.cfg.Mailer.(*homeTestMail).body = ""
189 b.do(http.MethodPost, signinPath, url.Values{"address": {u.Email}}, "")
190 b.a.FlushMail()
191 link := strings.Fields(b.a.cfg.Mailer.(*homeTestMail).body)
192 var verify string
193 for _, word := range link {
194 if strings.Contains(word, "/auth/verify?token=") {
195 verify = word
196 }
197 }
198 parsed, err := url.Parse(verify)
199 if err != nil || verify == "" {
200 t.Fatalf("no link: %q", b.a.cfg.Mailer.(*homeTestMail).body)
201 }
202 before, _ := b.remembered()
203 open := b.get(parsed.RequestURI())
204 if open.Code != http.StatusOK || len(open.Result().Cookies()) != 0 {
205 t.Fatalf("confirm step: %d, cookies %v", open.Code, open.Result().Cookies())
206 }
207 if after, res := b.remembered(); res != lastsignin.Valid || after != before {
208 t.Fatalf("the confirm step changed the record: %+v -> %+v", before, after)
209 }
210 if card := door(t, open.Body.String()); strings.Contains(card, oficinaButton) || strings.Contains(card, u.Email) {
211 t.Fatalf("the confirm step shows this browser's record:\n%s", card)
212 }
213}
214