oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3// Remembering Oficina ID in MintHomeSession, and forgetting: the signed-out
4// page's Forget, chained to Oficina ID's own.
5
6import (
7 "net/http"
8 "net/http/httptest"
9 "net/url"
10 "regexp"
11 "strings"
12 "testing"
13
14 "amadan.net/rastrillo/rastrillo/lastsignin"
15 "amadan.net/rastrillo/rastrillo/sessions"
16)
17
18// A Home sign-in is remembered as Oficina ID, with the address of the
19// person connect resolved, written in MintHomeSession and only there.
20func TestMintHomeSessionRemembersOficinaID(t *testing.T) {
21 f := newHomeFixture(t)
22 f.local(t, signedInAddress)
23 f.link(t, "opaque-paul")
24
25 // This browser last signed in as bob, by email. Oficina ID signs paul
26 // in: the record is his, and nothing of bob's is carried over.
27 b := f.browser()
28 b.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.com"})
29 redirectsTo(t, b.oficinaSignin(t, "/signin/oficina?return_to=%2Fsheets", "opaque-paul"), "/sheets")
30 got, res := b.remembered(t)
31 want := lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: signedInAddress}
32 if res != lastsignin.Valid || got != want {
33 t.Fatalf("remembered %+v (%v), want %+v", got, res, want)
34 }
35 // And the door offers it next time, with his address only in the chooser.
36 delete(b.jar, f.app.homeSessions.CookieName())
37 card := door(t, b.request(t, "GET", "/signin", nil, false).Body.String())
38 if !strings.Contains(card, oficinaButton) || strings.Contains(card, signedInAddress) {
39 t.Fatalf("the door after an Oficina ID sign-in:\n%s", card)
40 }
41
42 // A local sign-in is remembered by rastrillo itself, as the email link.
43 c := f.browser()
44 c.emailSignin(t, url.Values{"address": {signedInAddress}})
45 if got, res := c.remembered(t); res != lastsignin.Valid || got.Method != lastsignin.MethodMagicLink || got.Address != signedInAddress {
46 t.Fatalf("local sign-in remembered %+v (%v)", got, res)
47 }
48
49 // An unknown subject mints nothing, so remembers nothing.
50 stranger := f.browser()
51 stranger.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.com"})
52 if rec := stranger.oficinaSignin(t, "/signin/oficina", "opaque-nobody"); rec.Code != http.StatusForbidden {
53 t.Fatalf("unknown subject: %d", rec.Code)
54 }
55 if got, res := stranger.remembered(t); res != lastsignin.Valid || got.Method != lastsignin.MethodMagicLink || got.Address != "bob@example.com" {
56 t.Fatalf("an unknown Home subject changed the record: %+v (%v)", got, res)
57 }
58
59 // A linked person who has since lost admission is not signed in, and
60 // changes nothing.
61 f.app.admit = func(string) bool { return false }
62 lost := f.browser()
63 lost.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.com"})
64 lost.oficinaSignin(t, "/signin/oficina", "opaque-paul")
65 if _, ok := lost.jar[f.app.homeSessions.CookieName()]; ok {
66 t.Fatal("a linked person without admission was signed in")
67 }
68 if got, res := lost.remembered(t); res != lastsignin.Valid || got.Method != lastsignin.MethodMagicLink {
69 t.Fatalf("a refused Oficina ID sign-in changed the record: %+v (%v)", got, res)
70 }
71}
72
73// A Home-only member (team mode, "I'm new here") has no address here: the
74// record is Oficina ID with none.
75func TestMintHomeSessionRemembersAHomeOnlyMemberWithoutAnAddress(t *testing.T) {
76 f := newTeamFixture(t)
77 f.dir.Set(teamSubject('b'), "member")
78 f.refreshed(t)
79 fin := f.signInHome(t, teamSubject('b'))
80 if fin.Code != http.StatusSeeOther {
81 t.Fatalf("I'm new here: %d", fin.Code)
82 }
83 got, res := f.remembered(t)
84 if want := (lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina}); res != lastsignin.Valid || got != want {
85 t.Fatalf("remembered %+v (%v), want %+v", got, res, want)
86 }
87}
88
89// Fable on Docs #139, A2: MintHomeSession remembers Oficina ID only for a
90// session the signed-in routes admit, asked of the session just minted.
91func TestRememberOficinaOnlyForAnAdmittedSession(t *testing.T) {
92 f := newHomeFixture(t)
93 jar := f.app.auth.RememberJar().CookieName()
94 for _, c := range []struct {
95 address string
96 want bool
97 }{{signedInAddress, true}, {"nobody@example.com", false}} {
98 if _, err := f.app.memberFor(c.address); err != nil {
99 t.Fatal(err)
100 }
101 token, err := f.app.homeSessions.Mint(sessions.Session{Subject: c.address, Method: "magiclink"})
102 if err != nil {
103 t.Fatal(err)
104 }
105 w := httptest.NewRecorder()
106 http.SetCookie(w, &http.Cookie{Name: f.app.homeSessions.CookieName(), Value: token, Path: "/"})
107 f.app.rememberOficina(w, httptest.NewRequest(http.MethodGet, testOrigin+"/auth/finish", nil))
108 remembered := false
109 for _, ck := range w.Result().Cookies() {
110 remembered = remembered || ck.Name == jar
111 }
112 if remembered != c.want {
113 t.Errorf("%s: remembered %v, want %v", c.address, remembered, c.want)
114 }
115 }
116}
117
118// Opening an emailed link (the confirm step) leaves the remembered way in
119// alone: it neither clears nor rewrites it.
120func TestConfirmingALinkLeavesTheRecordAlone(t *testing.T) {
121 f := newHomeFixture(t)
122 b := f.browser()
123 b.request(t, "POST", "/signin", url.Values{"address": {signedInAddress}}, false)
124 want := lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.com"}
125 b.remember(t, want)
126 jar := f.app.auth.RememberJar().CookieName()
127 u, _ := url.Parse(f.app.testMailer(t).lastLink(t))
128 rec := b.request(t, "GET", u.RequestURI(), nil, false)
129 if rec.Code != http.StatusOK {
130 t.Fatalf("confirm: %d", rec.Code)
131 }
132 for _, c := range rec.Result().Cookies() {
133 if c.Name == jar {
134 t.Fatalf("the confirm step set the remembered cookie: %+v", c)
135 }
136 }
137 if got, res := b.remembered(t); res != lastsignin.Valid || got != want {
138 t.Fatalf("remembered %+v (%v), want %+v", got, res, want)
139 }
140}
141
142// Signed out: "Forget me on this device", a POST to rastrillo's Forget.
143// With Home it then chains to Oficina ID's own Forget, which returns only
144// to this consumer's exact /signed-out. The door's "Use a different email"
145// posts to the same Forget and stays on the door.
146func TestSignedOutForgetChainsToOficinaID(t *testing.T) {
147 f := newHomeFixture(t)
148 b := f.browser()
149 signedOut := b.request(t, "GET", "/signed-out", nil, false)
150 form := regexp.MustCompile(`(?s)<form estilo-signin-forget method="post" action="([^"]+)">(.*?)</form>`).FindStringSubmatch(signedOut.Body.String())
151 if signedOut.Code != http.StatusOK || form == nil || form[1] != signinForgetPath || !strings.Contains(form[2], `name="chain" value="oficina"`) || !strings.Contains(form[2], "Forget me on this device") {
152 t.Fatalf("signed-out page has no chained Forget: %d\n%s", signedOut.Code, signedOut.Body.String())
153 }
154 if !strings.Contains(signedOut.Body.String(), `href="/signin">Sign in</a>`) {
155 t.Fatal("signed-out page lost Sign in")
156 }
157
158 b.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.com"})
159 jar := f.app.auth.RememberJar().CookieName()
160 rec := b.request(t, "POST", signinForgetPath, url.Values{"chain": {"oficina"}}, false)
161 redirectsTo(t, rec, homeTestIssuer+"/forget?return="+url.QueryEscape(testOrigin+"/signed-out"))
162 if _, ok := b.jar[jar]; ok {
163 t.Fatal("Forget kept the remembered way in")
164 }
165 if !strings.Contains(rec.Header().Get("Content-Security-Policy"), "form-action 'self' "+homeTestIssuer) {
166 t.Fatalf("form-action does not allow the chain to %s: %q", homeTestIssuer, rec.Header().Get("Content-Security-Policy"))
167 }
168
169 // The door's "Use a different email": forgets, and opens the email way.
170 b.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "bob@example.com"})
171 redirectsTo(t, b.request(t, "POST", signinForgetPath, url.Values{"way": {"email"}, "return_to": {"/sheets/7"}}, false), "/signin?return_to=%2Fsheets%2F7&way=email")
172 if _, ok := b.jar[jar]; ok {
173 t.Fatal("Use a different email kept the remembered way in")
174 }
175
176 // A cross-site post is refused, and chains nowhere.
177 r := httptest.NewRequest(http.MethodPost, testOrigin+signinForgetPath, strings.NewReader("chain=oficina"))
178 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
179 r.Header.Set("Origin", "https://evil.example")
180 r.Header.Set("Sec-Fetch-Site", "cross-site")
181 refused := do(t, f.app, r)
182 if refused.Code != http.StatusForbidden || refused.Header().Get("Location") != "" {
183 t.Fatalf("cross-site Forget: %d %q", refused.Code, refused.Header().Get("Location"))
184 }
185}
186
187// Standalone, the signed-out page's Forget is Sheets' own, with no chain.
188func TestSignedOutForgetStandalone(t *testing.T) {
189 app, _ := appWithOrigin(t, testOrigin)
190 body := page(t, app, nil, "/signed-out").Body.String()
191 if !strings.Contains(body, `<form estilo-signin-forget method="post" action="/signin/forget">`) || strings.Contains(body, `name="chain"`) {
192 t.Fatalf("standalone signed-out page:\n%s", body)
193 }
194 req := httptest.NewRequest(http.MethodPost, testOrigin+signinForgetPath, strings.NewReader("chain=oficina"))
195 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
196 req.Header.Set("Origin", testOrigin)
197 redirectsTo(t, do(t, app, req), "/signin")
198}
199