oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package webtest
2
3import (
4 "context"
5 "net/http"
6 "net/url"
7 "regexp"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/home/connect"
13 "amadan.net/rastrillo/rastrillo/assertion"
14
15 "amadan.net/oficina/base/internal/install"
16 "amadan.net/oficina/base/internal/store"
17)
18
19// With no BASE_HOME_* settings Base is exactly what it was: its own sign-in,
20// no Oficina routes, sign-out back to /signin.
21func TestOficinaOffKeepsBaseAsItWas(t *testing.T) {
22 a := newApp(t)
23 page := a.get("/signin")
24 if page.Code != 200 || strings.Contains(page.Body, "Oficina") || strings.Contains(page.Body, "/auth/start") {
25 t.Fatalf("signin page mentions Oficina: %d\n%s", page.Code, page.Body)
26 }
27 for _, p := range []string{"/auth/start", "/auth/finish", "/signin/local", "/signed-out", "/account/home"} {
28 if r := a.get(p); r.Code != http.StatusNotFound {
29 t.Errorf("GET %s: %d, want 404", p, r.Code)
30 }
31 }
32 a.signedInOwner()
33 if body := a.get("/settings").Body; strings.Contains(body, "/account/home") || strings.Contains(body, "Oficina") {
34 t.Errorf("settings offers Oficina:\n%s", body)
35 }
36 if r := a.post("/auth/signout", nil); r.Code != 303 || r.Location() != "/signin" {
37 t.Errorf("signout: %d %q", r.Code, r.Location())
38 }
39}
40
41func TestOficinaSigninPageLeadsWithOficina(t *testing.T) {
42 a := newHomeApp(t)
43 page := a.get("/signin")
44 if page.Code != 200 {
45 t.Fatalf("signin: %d %q", page.Code, page.Location())
46 }
47 if got := a.oficinaButton(page); got != "/auth/start" {
48 t.Errorf("Sign in with Oficina goes to %q", got)
49 }
50 oficina := strings.Index(page.Body, "Sign in with Oficina")
51 email := strings.Index(page.Body, `action="/auth/begin"`)
52 if oficina < 0 || email < 0 || oficina > email {
53 t.Errorf("Oficina (%d) does not lead the email form (%d):\n%s", oficina, email, page.Body)
54 }
55 // The way back after signing in rides along to Oficina.
56 if got := a.oficinaButton(a.get("/signin?return_to=%2Fpeople")); got != "/auth/start?return_to=%2Fpeople" {
57 t.Errorf("with return_to: %q", got)
58 }
59 csp := page.Header.Get("Content-Security-Policy")
60 if !strings.Contains(csp, "form-action 'self' "+homeIssuer) || strings.Contains(csp, "unsafe-inline") {
61 t.Errorf("CSP: %q", csp)
62 }
63 // The local door is still there, with email and passkey, and no Oficina
64 // button: it is where linking sends people to prove the local account.
65 local := a.get("/signin/local")
66 if local.Code != 200 || !strings.Contains(local.Body, `action="/auth/begin"`) || !strings.Contains(local.Body, "data-rst-passkey") || strings.Contains(local.Body, "/auth/start") {
67 t.Errorf("local sign-in page: %d\n%s", local.Code, local.Body)
68 }
69 // Someone already signed in is sent on, not shown the door again.
70 a.signedInOwner()
71 if r := a.get("/signin?return_to=%2Fpeople"); r.Code != 303 || r.Location() != "/people" {
72 t.Errorf("signed-in /signin: %d %q", r.Code, r.Location())
73 }
74}
75
76// An Oficina sign-in that Base has no link for creates nobody. It asks the
77// person to sign in the local way once, and that sign-in goes straight on to
78// connecting the two accounts. After that, Oficina alone signs them in.
79func TestOficinaUnlinkedPersonLinksAfterOneLocalSignIn(t *testing.T) {
80 a := newHomeApp(t)
81 members := func() int {
82 var n int
83 if err := a.DB.QueryRow(`SELECT count(*) FROM members`).Scan(&n); err != nil {
84 t.Fatal(err)
85 }
86 return n
87 }
88 before := members()
89
90 unknown := a.signInWithOficina("subject-owner")
91 if unknown.Code != http.StatusForbidden || !strings.Contains(unknown.Body, "Oficina account not connected") || !strings.Contains(unknown.Body, `href="/signin/local"`) {
92 t.Fatalf("unlinked arrival: %d\n%s", unknown.Code, unknown.Body)
93 }
94 if !strings.Contains(unknown.Body, `class="card glass"`) {
95 t.Errorf("not in Base's look:\n%s", unknown.Body)
96 }
97 if r := a.get("/"); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") {
98 t.Fatalf("signed in without a link: %d %q", r.Code, r.Location())
99 }
100
101 r := a.signIn(ownerAddress)
102 if r.Code != 303 || r.Location() != connect.LocalCompletePath {
103 t.Fatalf("local sign-in: %d %q", r.Code, r.Location())
104 }
105 resume := a.get(r.Location())
106 if resume.Code != 303 || !strings.HasPrefix(resume.Location(), "/auth/start") {
107 t.Fatalf("local sign-in did not resume the link: %d %q", resume.Code, resume.Location())
108 }
109 toIssuer := a.get(resume.Location())
110 if u, _ := url.Parse(toIssuer.Location()); u == nil || u.Query().Get("max_age") != "300" {
111 t.Fatalf("link did not ask Oficina for a fresh sign-in: %q", toIssuer.Location())
112 }
113 a.confirmLink(a.atOficina(toIssuer, "subject-owner", 30*time.Second), ownerAddress)
114
115 if body := a.get("/settings").Body; strings.Contains(body, `href="/account/home"`) {
116 t.Errorf("settings still offers Connect after linking:\n%s", body)
117 }
118 b := a.newBrowser()
119 if fin := b.signInWithOficina("subject-owner"); fin.Code != 303 || fin.Location() != "/" {
120 t.Fatalf("linked Oficina sign-in: %d %q\n%s", fin.Code, fin.Location(), fin.Body)
121 }
122 if home := b.get("/"); home.Code != 200 {
123 t.Fatalf("GET / after Oficina sign-in: %d", home.Code)
124 }
125 if n := members(); n != before {
126 t.Errorf("members: %d, was %d", n, before)
127 }
128}
129
130func TestOficinaLinkedPersonReachesHome(t *testing.T) {
131 a := newHomeApp(t)
132 a.link(ownerAddress, "subject-owner")
133 a.setInstall("scratchpad", "installed", "")
134
135 b := a.newBrowser()
136 fin := b.signInWithOficina("subject-owner")
137 if fin.Code != 303 || fin.Location() != "/" {
138 t.Fatalf("finish: %d %q\n%s", fin.Code, fin.Location(), fin.Body)
139 }
140 if home := b.get("/"); home.Code != 200 || !strings.Contains(home.Body, "Scratchpad") {
141 t.Fatalf("Home: %d\n%s", home.Code, home.Body)
142 }
143 settings := b.get("/settings")
144 if settings.Code != 200 || !strings.Contains(settings.Body, ownerAddress) {
145 t.Fatalf("settings: %d\n%s", settings.Code, settings.Body)
146 }
147 if strings.Contains(settings.Body, `href="/account/home"`) || strings.Contains(settings.Body, "data-passkey-add") {
148 t.Errorf("settings for an Oficina session offers Connect or a new passkey:\n%s", settings.Body)
149 }
150
151 // Base still vouches for its own members to installed apps, as the same
152 // member whichever way they signed in.
153 n, _ := assertion.RandomID()
154 page := b.get("/authorize?" + url.Values{"consumer": {"scratchpad"}, "nonce": {n}}.Encode())
155 appOrigin := "https://acme-scratchpad.bab.oncarlos.com"
156 v := formFields(t, page.Body, appOrigin+"/auth/assertion")
157 kid, pub := install.KeySet(a.home.cfg.AssertionKey)
158 verifier, err := assertion.NewVerifier(homeOrigin, appOrigin, map[string][]byte{kid: pub})
159 if err != nil {
160 t.Fatal(err)
161 }
162 claims, err := verifier.Verify(v.Get("assertion"), time.Now())
163 if err != nil {
164 t.Fatalf("verify: %v", err)
165 }
166 owner, _, _ := store.MemberByEmail(context.Background(), a.DB, ownerAddress)
167 if claims.Subject != owner.ID || claims.Nonce != n {
168 t.Errorf("claims: %+v, want subject %s", claims, owner.ID)
169 }
170
171 // The way back survives the trip to Oficina.
172 c := a.newBrowser()
173 toIssuer := c.get(c.oficinaButton(c.get("/signin?return_to=%2Fpeople")))
174 if fin := c.atOficina(toIssuer, "subject-owner", time.Hour); fin.Code != 303 || fin.Location() != "/people" {
175 t.Errorf("finish with return_to: %d %q", fin.Code, fin.Location())
176 }
177}
178
179// The roster decides, on every request: an Oficina link to someone who is no
180// longer a member opens nothing.
181func TestOficinaRemovedMemberIsRefused(t *testing.T) {
182 a := newHomeApp(t)
183 ctx := context.Background()
184 if _, _, err := store.Invite(ctx, a.DB, "ana@acme.test"); err != nil {
185 t.Fatal(err)
186 }
187 a.link("ana@acme.test", "subject-ana")
188 b := a.newBrowser()
189 if fin := b.signInWithOficina("subject-ana"); fin.Code != 303 {
190 t.Fatalf("ana's Oficina sign-in: %d", fin.Code)
191 }
192 if r := b.get("/"); r.Code != 200 {
193 t.Fatalf("ana's Home: %d", r.Code)
194 }
195 ana, _, _ := store.MemberByEmail(ctx, a.DB, "ana@acme.test")
196 if err := store.RemoveMember(ctx, a.DB, ana.ID); err != nil {
197 t.Fatal(err)
198 }
199 for _, p := range []string{"/", "/settings", "/people"} {
200 if r := b.get(p); r.Code != http.StatusForbidden {
201 t.Errorf("removed member's Oficina session, GET %s: %d", p, r.Code)
202 }
203 }
204 c := a.newBrowser()
205 if fin := c.signInWithOficina("subject-ana"); fin.Code != http.StatusForbidden {
206 t.Errorf("removed member signed in again: %d %q", fin.Code, fin.Location())
207 }
208 if r := c.get("/"); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") {
209 t.Errorf("removed member reached Home: %d %q", r.Code, r.Location())
210 }
211}
212
213// Sign out ends Base's session, then hands the same POST to Oficina's
214// /signout so the Oficina session ends too.
215func TestOficinaSignOutGoesThroughOficina(t *testing.T) {
216 a := newHomeApp(t)
217 a.link(ownerAddress, "subject-owner")
218 b := a.newBrowser()
219 b.signInWithOficina("subject-owner")
220 settings := b.get("/settings")
221 if got := settings.Header.Get("Referrer-Policy"); got != "strict-origin" {
222 t.Errorf("settings Referrer-Policy %q, want strict-origin", got)
223 }
224 if !strings.Contains(settings.Body, `action="/auth/signout"`) {
225 t.Fatalf("no sign-out form:\n%s", settings.Body)
226 }
227 out := b.post("/auth/signout", nil)
228 if out.Code != http.StatusTemporaryRedirect || out.Location() != homeIssuer+"/signout" {
229 t.Fatalf("signout: %d %q", out.Code, out.Location())
230 }
231 if r := b.get("/"); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") {
232 t.Errorf("still signed in: %d %q", r.Code, r.Location())
233 }
234 // Oficina sends the browser back here.
235 page := b.get("/signed-out")
236 if page.Code != 200 || !strings.Contains(page.Body, "You’re signed out") || !strings.Contains(page.Body, `href="/signin"`) {
237 t.Errorf("signed-out page: %d\n%s", page.Code, page.Body)
238 }
239 // A local session signs out the same way.
240 if r := a.post("/auth/signout", nil); r.Code != http.StatusTemporaryRedirect || r.Location() != homeIssuer+"/signout" {
241 t.Errorf("local signout: %d %q", r.Code, r.Location())
242 }
243}
244
245// POST /auth/assertion is the one route another site may post to; the
246// bridge checks the signed assertion itself. Every other POST still needs
247// Base's own origin.
248func TestOficinaOnlyTheAssertionCallbackSkipsCSRF(t *testing.T) {
249 a := newHomeApp(t)
250 a.signedInOwner()
251 for _, p := range []string{"/auth/begin", "/auth/verify", "/auth/forget", "/auth/signout", "/people", "/account/home",
252 "/account/home/confirm", "/store/scratchpad/install", "/store/scratchpad/uninstall", "/passkey/discover/begin", "/passkey/register/begin"} {
253 if r := a.postFrom(homeIssuer, p, url.Values{"x": {"1"}}); r.Code != http.StatusForbidden {
254 t.Errorf("POST %s from Oficina's origin: %d, want 403", p, r.Code)
255 }
256 }
257 if r := a.get("/"); r.Code != 200 {
258 t.Fatalf("the refused POSTs changed the session: %d", r.Code)
259 }
260 // The callback itself takes Oficina's post (atOficina checks the 303).
261 b := a.newBrowser()
262 b.signInWithOficina("anyone")
263}
264
265// An Oficina session signs a member in, but a passkey belongs to the
266// member's own sign-in: one added from an Oficina session would be filed
267// under the Oficina subject and could never sign anyone in.
268func TestOficinaSessionCannotAddPasskey(t *testing.T) {
269 a := newHomeApp(t)
270 a.link(ownerAddress, "subject-owner")
271 b := a.newBrowser()
272 b.signInWithOficina("subject-owner")
273 req, _ := http.NewRequest(http.MethodPost, homeOrigin+"/passkey/register/begin", strings.NewReader("{}"))
274 req.Header.Set("Content-Type", "application/json")
275 req.Header.Set("Origin", homeOrigin)
276 if r := b.do(req); r.Code != http.StatusForbidden {
277 t.Errorf("register from an Oficina session: %d", r.Code)
278 }
279}
280
281// Settings offers "Connect your Oficina account" only while there is no link.
282func TestOficinaConnectOfferedOnlyUntilLinked(t *testing.T) {
283 a := newHomeApp(t)
284 a.signedInOwner()
285 body := a.get("/settings").Body
286 if !regexp.MustCompile(`<a[^>]*href="/account/home"[^>]*>Connect your Oficina account</a>`).MatchString(body) {
287 t.Fatalf("settings lacks the Connect link:\n%s", body)
288 }
289 b := a.newBrowser()
290 b.link(ownerAddress, "subject-owner")
291 if body := a.get("/settings").Body; strings.Contains(body, "Connect your Oficina account") {
292 t.Errorf("still offered after linking from another browser:\n%s", body)
293 }
294}
295
296// A removed member's Oficina account is free again: invited back, the same
297// person links the same Oficina account to their new membership, which has a
298// new number.
299func TestOficinaRemovedMemberCanBeReinvitedAndRelinked(t *testing.T) {
300 a := newHomeApp(t)
301 ctx := context.Background()
302 old, _, err := store.Invite(ctx, a.DB, "ana@acme.test")
303 if err != nil {
304 t.Fatal(err)
305 }
306 a.link("ana@acme.test", "subject-ana")
307 oldNumber, _ := store.MemberNumber(ctx, a.DB, old.ID)
308 if err := store.RemoveMember(ctx, a.DB, old.ID); err != nil {
309 t.Fatal(err)
310 }
311 again, _, err := store.Invite(ctx, a.DB, "ana@acme.test")
312 if err != nil {
313 t.Fatal(err)
314 }
315 if n, _ := store.MemberNumber(ctx, a.DB, again.ID); n == oldNumber {
316 t.Fatalf("re-invited member reuses number %d", n)
317 }
318 a.newBrowser().link("ana@acme.test", "subject-ana")
319 b := a.newBrowser()
320 if fin := b.signInWithOficina("subject-ana"); fin.Code != 303 || fin.Location() != "/" {
321 t.Fatalf("Oficina sign-in after re-linking: %d %q", fin.Code, fin.Location())
322 }
323 if r := b.get("/settings"); r.Code != 200 || !strings.Contains(r.Body, "ana@acme.test") {
324 t.Fatalf("settings as the new member: %d", r.Code)
325 }
326}
327
328// While a link waits on a local sign-in, a passkey would sign the person in
329// without connecting anything (connect accepts only an emailed link or
330// Keymail), so the local page hides it and says to use email.
331func TestOficinaLocalSigninDuringLinkHidesPasskey(t *testing.T) {
332 a := newHomeApp(t)
333 const line = "To connect your Oficina account, sign in with your email."
334 if page := a.get("/signin/local"); strings.Contains(page.Body, line) || !strings.Contains(page.Body, "data-rst-passkey") {
335 t.Fatalf("no link pending, but the page changed:\n%s", page.Body)
336 }
337 if r := a.signInWithOficina("subject-owner"); r.Code != http.StatusForbidden {
338 t.Fatalf("unlinked arrival: %d", r.Code)
339 }
340 page := a.get("/signin/local")
341 if page.Code != 200 || strings.Contains(page.Body, "data-rst-passkey") || !strings.Contains(page.Body, line) || !strings.Contains(page.Body, `action="/auth/begin"`) {
342 t.Fatalf("local sign-in while linking: %d\n%s", page.Code, page.Body)
343 }
344 // Once the local sign-in has picked the link up, the page is as before.
345 r := a.signIn(ownerAddress)
346 a.get(r.Location())
347 if page := a.get("/signin/local"); strings.Contains(page.Body, line) {
348 t.Errorf("still says to use email after the link began:\n%s", page.Body)
349 }
350}
351