| 1 | package calls |
| 2 | |
| 3 | // What only Calls has, and what the door must leave as it was: guests who |
| 4 | // join a call by name, the join cookie's email-only sign-in, and the native |
| 5 | // apps' own sessions. |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "net/url" |
| 12 | "regexp" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/home/connect" |
| 18 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 19 | "amadan.net/rastrillo/rastrillo/sessions" |
| 20 | ) |
| 21 | |
| 22 | // doorMeeting makes a meeting on f's instance and returns its sqid and its |
| 23 | // invitation's token, needing sign-in or not. |
| 24 | func doorMeeting(t *testing.T, f *homeRouterFixture, requireSignin bool) (string, string) { |
| 25 | t.Helper() |
| 26 | m, token, err := f.app.store.createMeeting(context.Background(), "Planning", nil, "person:1") |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if requireSignin { |
| 31 | if _, err := f.app.db.Writer().Exec("UPDATE meetings SET require_signin = 1 WHERE id = ?", m.ID); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | } |
| 35 | return m.Sqid, token |
| 36 | } |
| 37 | |
| 38 | // doorMeetingModes runs fn with Home, without, and in team mode, each with |
| 39 | // a way to make a meeting (by a member, then from an empty jar) and |
| 40 | // return its sqid and invitation token. |
| 41 | func doorMeetingModes(t *testing.T, fn func(t *testing.T, f *homeRouterFixture, meeting func(requireSignin bool) (string, string))) { |
| 42 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 43 | fn(t, f, func(requireSignin bool) (string, string) { return doorMeeting(t, f, requireSignin) }) |
| 44 | }) |
| 45 | t.Run("team", func(t *testing.T) { |
| 46 | tf := newTeamFixture(t) |
| 47 | fn(t, tf.homeRouterFixture, func(requireSignin bool) (string, string) { |
| 48 | tf.member(teamSubject('h'), "member") |
| 49 | sqid, token := tf.meeting("Planning") |
| 50 | if requireSignin { |
| 51 | if _, err := tf.app.db.Writer().Exec("UPDATE meetings SET require_signin = 1 WHERE sqid = ?", sqid); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | } |
| 55 | tf.cookies = map[string]*http.Cookie{} |
| 56 | return sqid, token |
| 57 | }) |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | // Paul's ruling: Calls guests join by name by default, Oficina ID only on |
| 62 | // explicit choice. The guest's way in is the invitation, untouched by the |
| 63 | // door: the name form, the guest session, the holding screen. And a guest |
| 64 | // who then opens the door is drawn it as anybody signed out is: never sent |
| 65 | // to a page that 404s for a guest, never refused, never signed out. |
| 66 | func TestAGuestJoiningByNameIsUnaffected(t *testing.T) { |
| 67 | doorMeetingModes(t, func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string)) { |
| 68 | sqid, token := meeting(false) |
| 69 | invite := f.get("/j/" + token) |
| 70 | if invite.Code != http.StatusOK || !strings.Contains(invite.Body.String(), `name="name"`) { |
| 71 | t.Fatalf("the invitation = %d, want the name form", invite.Code) |
| 72 | } |
| 73 | join := f.do(http.MethodPost, "/j/"+token+"/guest", url.Values{"name": {"Grace"}}.Encode()) |
| 74 | redirectsTo(t, join, "/m/"+sqid+"/join") |
| 75 | guest := f.sessionCookie() |
| 76 | if guest == nil { |
| 77 | t.Fatal("no guest session") |
| 78 | } |
| 79 | if w := f.get("/m/" + sqid + "/join"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "Planning") { |
| 80 | t.Fatalf("the holding screen for a guest = %d", w.Code) |
| 81 | } |
| 82 | // The door, for a guest. |
| 83 | rec := f.get("/signin") |
| 84 | card := door(t, rec.Body.String()) |
| 85 | if rec.Code != http.StatusOK || strings.Contains(card, signinRefusal) || strings.Contains(card, signinRemoved) { |
| 86 | t.Fatalf("the door for a guest: %d\n%s", rec.Code, card) |
| 87 | } |
| 88 | if clears(rec, f.app.sessionCookie()) || !f.liveSession(guest.Value) { |
| 89 | t.Fatal("the door ended a guest's session") |
| 90 | } |
| 91 | if w := f.get("/m/" + sqid + "/join"); w.Code != http.StatusOK { |
| 92 | t.Fatalf("after the door, the guest's holding screen = %d", w.Code) |
| 93 | } |
| 94 | if ad := f.app.admission(f.jarRequest(), true); ad.signedIn { |
| 95 | t.Fatal("the door's probe judges a guest's session") |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | // An outsider through the join cookie (an invitation to a meeting that |
| 101 | // needs an emailed sign-in) signs in by email, as ever, and lands in the |
| 102 | // meeting, the pending join taken at /continue. The door opens on the |
| 103 | // email field: Oficina ID is not its default there. |
| 104 | func TestAnEmailSigninThroughTheJoinCookie(t *testing.T) { |
| 105 | doorMeetingModes(t, func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string)) { |
| 106 | sqid, token := meeting(true) |
| 107 | redirectsTo(t, f.get("/j/"+token), "/signin") |
| 108 | rec := f.get("/signin") |
| 109 | card := door(t, rec.Body.String()) |
| 110 | if rec.Code != http.StatusOK || strings.Contains(card, oficinaButton) || !strings.Contains(card, emailField) { |
| 111 | t.Fatalf("the door with a join cookie: %d\n%s", rec.Code, card) |
| 112 | } |
| 113 | if got := f.emailSignin(t, url.Values{"address": {"outsider@example.test"}}); got != "/m/"+sqid+"/join" { |
| 114 | t.Fatalf("the outsider landed on %q, want the meeting", got) |
| 115 | } |
| 116 | if w := f.get("/m/" + sqid + "/join"); w.Code != http.StatusOK { |
| 117 | t.Fatalf("the outsider's holding screen = %d", w.Code) |
| 118 | } |
| 119 | }) |
| 120 | } |
| 121 | |
| 122 | // Paul's ruling (2026-11-22, #46): while an invitation waits, the plain |
| 123 | // door is the email way, whatever the browser used last, and Oficina ID is |
| 124 | // a row under Other ways. A step-up still never offers it. With Home, in |
| 125 | // both modes. |
| 126 | func TestTheJoinCookieDoorIsEmailFirstWithOficinaIDUnderOtherWays(t *testing.T) { |
| 127 | homeMeetingModes(t, func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string)) { |
| 128 | _, token := meeting(true) |
| 129 | f.get("/j/" + token) |
| 130 | card := door(t, f.get("/signin").Body.String()) |
| 131 | if strings.Contains(card, oficinaButton) || !strings.Contains(card, emailField) || !strings.Contains(card, otherWays) { |
| 132 | t.Fatalf("the join cookie's plain door:\n%s", card) |
| 133 | } |
| 134 | chooser := door(t, f.get(waysHref(t, card)).Body.String()) |
| 135 | if !strings.Contains(chooser, "Oficina ID") || !strings.Contains(chooser, `href="/signin?way=oficina"`) { |
| 136 | t.Fatalf("the join cookie's chooser has no Oficina ID row:\n%s", chooser) |
| 137 | } |
| 138 | if way := door(t, f.get("/signin?way=oficina").Body.String()); !strings.Contains(way, oficinaButton) { |
| 139 | t.Fatalf("the Oficina ID row's page:\n%s", way) |
| 140 | } |
| 141 | for _, step := range []string{"/signin?reauth=1", "/signin?reauth=1&ways=1&way=email"} { |
| 142 | if card := door(t, f.get(step).Body.String()); strings.Contains(card, "Oficina ID") { |
| 143 | t.Fatalf("a step-up with a join cookie offers Oficina ID (%s):\n%s", step, card) |
| 144 | } |
| 145 | } |
| 146 | // Remembered Oficina ID does not make it the default either: the |
| 147 | // email way, with the remembered address. |
| 148 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: "alice@example.test"}) |
| 149 | card = door(t, f.get("/signin").Body.String()) |
| 150 | if strings.Contains(card, oficinaButton) || !strings.Contains(card, "Continue as alice@example.test") { |
| 151 | t.Fatalf("the join cookie's door with Oficina ID remembered:\n%s", card) |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | |
| 156 | // Without the join cookie, the same browser's door opens on Oficina ID |
| 157 | // again (with Home): the cookie, not the browser, decides. |
| 158 | func TestTheJoinCookieAloneMakesEmailTheDefault(t *testing.T) { |
| 159 | f := newHomeRouterFixture(t) |
| 160 | _, token := doorMeeting(t, f, true) |
| 161 | f.get("/j/" + token) |
| 162 | if card := door(t, f.get("/signin").Body.String()); strings.Contains(card, oficinaButton) { |
| 163 | t.Fatal("the join cookie's door opens on Oficina ID") |
| 164 | } |
| 165 | delete(f.cookies, joinCookieName) |
| 166 | if card := door(t, f.get("/signin").Body.String()); !strings.Contains(card, oficinaButton) { |
| 167 | t.Fatalf("without the join cookie the door does not open on Oficina ID:\n%s", card) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // An Oficina ID sign-in chosen under Other ways, with an invitation |
| 172 | // waiting, lands on the invitation, as an email sign-in does through |
| 173 | // /continue: the exchange lands on /continue, never on a page the request |
| 174 | // named, and /continue follows the cookie's own link. |
| 175 | func TestAnOficinaSigninFromTheJoinCookieLandsOnTheInvitation(t *testing.T) { |
| 176 | t.Run("home", func(t *testing.T) { |
| 177 | f := newHomeRouterFixture(t) |
| 178 | f.link(t) |
| 179 | homeSignout(t, f.do(http.MethodPost, "/signout", "")) |
| 180 | sqid, token := doorMeeting(t, f, true) |
| 181 | redirectsTo(t, f.get("/j/"+token), "/signin") |
| 182 | // A page kept by an earlier attempt, which the invitation spends. |
| 183 | w := httptest.NewRecorder() |
| 184 | f.app.rememberReturn(w, "/settings") |
| 185 | f.cookies[f.app.returnCookieName()] = w.Result().Cookies()[0] |
| 186 | start := homeRedirect(t, f.get("/signin/oficina?return_to=%2Fsettings")) |
| 187 | if !strings.Contains(start, "return_to=%2Fcontinue") { |
| 188 | t.Fatalf("the exchange starts for %q, want /continue", start) |
| 189 | } |
| 190 | landing := homeRedirect(t, f.exchange(t, start, "opaque-home-alice")) |
| 191 | if landing != "/continue" { |
| 192 | t.Fatalf("the exchange landed on %q", landing) |
| 193 | } |
| 194 | cont := f.get(landing) |
| 195 | redirectsTo(t, cont, "/m/"+sqid+"/join") |
| 196 | if f.cookies[joinCookieName] != nil { |
| 197 | t.Fatal("the join cookie outlived the sign-in") |
| 198 | } |
| 199 | if !clears(cont, f.app.returnCookieName()) || f.returnPath() != "" { |
| 200 | t.Fatal("/continue followed the invitation and kept the return page") |
| 201 | } |
| 202 | // A second /continue lands on the list, not the stale page. |
| 203 | redirectsTo(t, f.get("/continue"), "/") |
| 204 | }) |
| 205 | t.Run("team", func(t *testing.T) { |
| 206 | tf := newTeamFixture(t) |
| 207 | tf.member(teamSubject('h'), "member") |
| 208 | sqid, token := tf.meeting("Planning") |
| 209 | if _, err := tf.app.db.Writer().Exec("UPDATE meetings SET require_signin = 1 WHERE sqid = ?", sqid); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | joiner := teamSubject('j') |
| 213 | tf.dir.Set(joiner, "member") |
| 214 | tf.refreshed() |
| 215 | tf.cookies = map[string]*http.Cookie{} |
| 216 | redirectsTo(t, tf.get("/j/"+token), "/signin") |
| 217 | start := homeRedirect(t, tf.get("/signin/oficina")) |
| 218 | fin := tf.exchange(t, start, joiner) |
| 219 | if fin.Code == http.StatusSeeOther && fin.Header().Get("Location") == connect.ChoosePath { |
| 220 | fin = tf.choose("new") |
| 221 | } |
| 222 | landing := homeRedirect(t, fin) |
| 223 | if landing != "/continue" { |
| 224 | t.Fatalf("the exchange landed on %q", landing) |
| 225 | } |
| 226 | redirectsTo(t, tf.get(landing), "/m/"+sqid+"/join") |
| 227 | if w := tf.get("/m/" + sqid + "/join"); w.Code != http.StatusOK { |
| 228 | t.Fatalf("the member's holding screen = %d", w.Code) |
| 229 | } |
| 230 | }) |
| 231 | } |
| 232 | |
| 233 | // homeMeetingModes is doorMeetingModes with Home only: with Home and in |
| 234 | // team mode. |
| 235 | func homeMeetingModes(t *testing.T, fn func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string))) { |
| 236 | doorMeetingModes(t, func(t *testing.T, f *homeRouterFixture, meeting func(bool) (string, string)) { |
| 237 | if f.app.home == nil { |
| 238 | t.Skip("no Oficina ID without Home") |
| 239 | } |
| 240 | fn(t, f, meeting) |
| 241 | }) |
| 242 | } |
| 243 | |
| 244 | // waysHref is the door's "Other ways to sign in" href, unescaped. |
| 245 | func waysHref(t *testing.T, card string) string { |
| 246 | t.Helper() |
| 247 | m := regexp.MustCompile(`href="(/signin\?[^"]*ways=1[^"]*)"`).FindStringSubmatch(card) |
| 248 | if m == nil { |
| 249 | t.Fatalf("no Other ways link:\n%s", card) |
| 250 | } |
| 251 | return m[1] |
| 252 | } |
| 253 | |
| 254 | // A native app's session (native_auth.go: a session of its own, a copy of |
| 255 | // the browser's) is never refused or ended by the web door. Presented at |
| 256 | // the door it is a signed-in session, sent on; and the door refusing the |
| 257 | // browser's own session ends that one alone. |
| 258 | func TestTheDoorNeverEndsANativeSession(t *testing.T) { |
| 259 | f := newHomeRouterFixture(t) |
| 260 | f.link(t) |
| 261 | browser := f.sessionCookie().Value |
| 262 | s, ok := f.app.nativeAuth.sessions.From(f.jarRequest()) |
| 263 | if !ok { |
| 264 | t.Fatal("no session") |
| 265 | } |
| 266 | native, err := f.app.nativeAuth.sessions.Mint(sessions.Session{Subject: s.Subject, Method: s.Method, AuthTime: s.AuthTime, At: time.Now()}) |
| 267 | if err != nil { |
| 268 | t.Fatal(err) |
| 269 | } |
| 270 | app := f.fresh() |
| 271 | app.cookies[f.app.sessionCookie()] = &http.Cookie{Name: f.app.sessionCookie(), Value: native} |
| 272 | redirectsTo(t, app.get("/signin"), "/") |
| 273 | if !f.liveSession(native) { |
| 274 | t.Fatal("the door ended the native session it was shown") |
| 275 | } |
| 276 | // Removed: the browser's session is refused at the door and ended; the |
| 277 | // native one is not the door's to end (its own next request is |
| 278 | // requirePerson's, as every request is). |
| 279 | f.app.cfg.allow = allowlist{} |
| 280 | if rec := f.get("/signin"); !strings.Contains(rec.Body.String(), signinRemoved) { |
| 281 | t.Fatal("no removal at the door") |
| 282 | } |
| 283 | if f.liveSession(browser) { |
| 284 | t.Fatal("the browser's refused session survived") |
| 285 | } |
| 286 | if !f.liveSession(native) { |
| 287 | t.Fatal("the web door ended the native app's session") |
| 288 | } |
| 289 | } |
| 290 | |