| 1 | package memoria |
| 2 | |
| 3 | // The door's refusals: a verified address Admit refuses (auth's Refused), |
| 4 | // a session the signed-in routes no longer admit (the admission probe), |
| 5 | // and a gate that cannot decide (a 5xx, never a refusal). |
| 6 | |
| 7 | import ( |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/home/connect" |
| 15 | "amadan.net/rastrillo/rastrillo/lastsignin" |
| 16 | "amadan.net/rastrillo/rastrillo/sessions" |
| 17 | ) |
| 18 | |
| 19 | // Paul's wording (approved for Docs and Sheets, 2026-11-21/22, the same |
| 20 | // sentence with the app's name), and Memoria's removal, unchanged. |
| 21 | func TestTheRefusalIsPaulsWording(t *testing.T) { |
| 22 | if signinRefusal != "That email can’t sign in to Memoria. Use a different email, or ask whoever runs this team to add it." { |
| 23 | t.Fatalf("refusal = %q", signinRefusal) |
| 24 | } |
| 25 | if signinRemoved != "Your access has been removed, so you’ve been signed out. Ask whoever runs this team if that’s wrong." { |
| 26 | t.Fatalf("removal = %q", signinRemoved) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // auth's Refused hook draws the door with the refusal in the app-problem |
| 31 | // slot and the email field, in a 403, on the plain door only. |
| 32 | func TestRefusedDrawsTheDoorWithTheRefusal(t *testing.T) { |
| 33 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 34 | w := httptest.NewRecorder() |
| 35 | f.app.refused(w, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/auth/callback?state=x", nil), rauthIdentity("someone@else.test")) |
| 36 | card := door(t, w.Body.String()) |
| 37 | if w.Code != http.StatusForbidden || !strings.Contains(card, appProblem) || !strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) { |
| 38 | t.Fatalf("refused: %d\n%s", w.Code, card) |
| 39 | } |
| 40 | if w.Header().Get("Cache-Control") != "no-store" { |
| 41 | t.Errorf("the refusal's Cache-Control = %q", w.Header().Get("Cache-Control")) |
| 42 | } |
| 43 | // Nobody can summon it by typing a query. |
| 44 | for _, path := range []string{"/signin?err=refused", "/signin?ways=1&err=refused"} { |
| 45 | if body := f.fresh().get(path).Body.String(); strings.Contains(body, signinRefusal) { |
| 46 | t.Errorf("GET %s draws the refusal", path) |
| 47 | } |
| 48 | } |
| 49 | // And on the chooser it draws none: Other ways stays reachable. |
| 50 | w = httptest.NewRecorder() |
| 51 | f.app.refused(w, httptest.NewRequest(http.MethodGet, homeTestOrigin+"/auth/callback?ways=1", nil), rauthIdentity("someone@else.test")) |
| 52 | if strings.Contains(w.Body.String(), signinRefusal) { |
| 53 | t.Error("the chooser draws the refusal") |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | // An owner taken off MEMORIA_OWNERS, at the door with a live Home session: |
| 59 | // the removal is drawn on the plain door, the session is ended on the |
| 60 | // door's own response, a remembered Oficina ID is forgotten, and the next |
| 61 | // /signin is an ordinary door, not a loop. |
| 62 | func TestARemovedOwnersSessionAtTheDoor(t *testing.T) { |
| 63 | for _, path := range []string{"/signin", "/signin/local"} { |
| 64 | t.Run(path, func(t *testing.T) { |
| 65 | f := newHomeRouterFixture(t) |
| 66 | f.link(t) |
| 67 | if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodFederated { |
| 68 | t.Fatalf("after the Oficina sign-in the jar holds %+v (%v)", rec, res) |
| 69 | } |
| 70 | session := f.sessionCookie().Value |
| 71 | f.app.SetOwners([]string{"someone-else@example.test"}) |
| 72 | rec := f.get(path) |
| 73 | card := door(t, rec.Body.String()) |
| 74 | if rec.Code != http.StatusOK || !strings.Contains(card, signinRemoved) || !strings.Contains(card, appProblem) { |
| 75 | t.Fatalf("GET %s: %d\n%s", path, rec.Code, card) |
| 76 | } |
| 77 | if !clears(rec, f.app.sessionCookie()) || f.liveSession(session) { |
| 78 | t.Fatal("the door kept the removed owner's session") |
| 79 | } |
| 80 | if _, res := f.remembered(); res == lastsignin.Valid { |
| 81 | t.Fatal("the door kept the remembered Oficina ID") |
| 82 | } |
| 83 | // The door is drawn signed out, even for the session it has |
| 84 | // just refused. |
| 85 | if strings.Contains(rec.Body.String(), "estilo-account") { |
| 86 | t.Error("the refusal's door draws the account menu") |
| 87 | } |
| 88 | again := f.get("/signin") |
| 89 | if again.Code != http.StatusOK || strings.Contains(again.Body.String(), signinRemoved) { |
| 90 | t.Fatalf("the next /signin: %d", again.Code) |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Other ways stays reachable for a refused session: the chooser draws no |
| 97 | // refusal and lists its ways, while the session is still ended; and with |
| 98 | // the remembered Oficina ID forgotten, the plain door offers Oficina ID |
| 99 | // afresh (under another account, say). |
| 100 | func TestARefusedSessionCanReachOtherWays(t *testing.T) { |
| 101 | f := newHomeRouterFixture(t) |
| 102 | f.link(t) |
| 103 | f.app.SetOwners([]string{"someone-else@example.test"}) |
| 104 | rec := f.get("/signin?ways=1") |
| 105 | card := door(t, rec.Body.String()) |
| 106 | if rec.Code != http.StatusOK || strings.Contains(card, signinRemoved) || strings.Contains(card, signinRefusal) || !strings.Contains(card, "Email") || !strings.Contains(card, "Use a different email") { |
| 107 | t.Fatalf("the chooser for a refused session: %d\n%s", rec.Code, card) |
| 108 | } |
| 109 | if f.sessionCookie() != nil { |
| 110 | t.Fatal("the chooser kept a refused session") |
| 111 | } |
| 112 | if card := door(t, f.get("/signin").Body.String()); !strings.Contains(card, oficinaButton) { |
| 113 | t.Fatalf("after the refusal the door does not offer Oficina ID:\n%s", card) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Continue with Oficina ID for a refused session ends it and starts the |
| 118 | // exchange, rather than sending it back to the page that refuses it. |
| 119 | func TestARefusedSessionAtSigninOficinaStartsTheExchange(t *testing.T) { |
| 120 | f := newHomeRouterFixture(t) |
| 121 | f.link(t) |
| 122 | f.app.SetOwners([]string{"someone-else@example.test"}) |
| 123 | rec := f.get("/signin/oficina") |
| 124 | if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/auth/start") { |
| 125 | t.Fatalf("GET /signin/oficina: %d %q", rec.Code, rec.Header().Get("Location")) |
| 126 | } |
| 127 | if f.sessionCookie() != nil { |
| 128 | t.Fatal("the removed owner's session survived") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // A remembered email way is no loop, so a refusal keeps it. |
| 133 | func TestARefusalKeepsARememberedEmailWay(t *testing.T) { |
| 134 | f := newHomeRouterFixture(t) |
| 135 | f.link(t) |
| 136 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: "alice@example.test"}) |
| 137 | f.app.SetOwners([]string{"someone-else@example.test"}) |
| 138 | f.get("/signin") |
| 139 | if rec, res := f.remembered(); res != lastsignin.Valid || rec.Method != lastsignin.MethodMagicLink { |
| 140 | t.Fatalf("the refusal dropped the email way: %+v (%v)", rec, res) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Standalone, an owner taken off the list at the door: the removal, the |
| 145 | // session ended, and no loop; a session the signed-in routes refuse |
| 146 | // without ending as removed (a Home sign-in on an instance with no Home) |
| 147 | // gets the refusal. |
| 148 | func TestStandaloneRefusedSessions(t *testing.T) { |
| 149 | f := newStandaloneDoor(t) |
| 150 | f.session(t, "alice@example.test", "magiclink", time.Now()) |
| 151 | session := f.sessionCookie().Value |
| 152 | f.app.SetOwners([]string{"someone-else@example.test"}) |
| 153 | rec := f.get("/signin") |
| 154 | card := door(t, rec.Body.String()) |
| 155 | if rec.Code != http.StatusOK || !strings.Contains(card, signinRemoved) || !strings.Contains(card, emailField) { |
| 156 | t.Fatalf("standalone removed owner: %d\n%s", rec.Code, card) |
| 157 | } |
| 158 | if !clears(rec, f.app.sessionCookie()) || f.liveSession(session) { |
| 159 | t.Fatal("the standalone door kept a removed owner's session") |
| 160 | } |
| 161 | |
| 162 | f = newStandaloneDoor(t) |
| 163 | f.session(t, "opaque-home-person", connect.HomeMethod, time.Now()) |
| 164 | session = f.sessionCookie().Value |
| 165 | rec = f.get("/signin") |
| 166 | card = door(t, rec.Body.String()) |
| 167 | if rec.Code != http.StatusOK || !strings.Contains(card, signinRefusal) || !strings.Contains(card, emailField) { |
| 168 | t.Fatalf("standalone refused session: %d\n%s", rec.Code, card) |
| 169 | } |
| 170 | if !clears(rec, f.app.sessionCookie()) || f.liveSession(session) { |
| 171 | t.Fatal("the standalone door kept a refused session") |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // The removal is Memoria's own sentence, on the plain door, in both modes. |
| 176 | func TestTheRemovalIsOnThePlainDoorOnly(t *testing.T) { |
| 177 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 178 | if card := door(t, f.get("/signin?err=removed").Body.String()); !strings.Contains(card, signinRemoved) || strings.Contains(card, "Something went wrong") { |
| 179 | t.Fatalf("?err=removed:\n%s", card) |
| 180 | } |
| 181 | if card := door(t, f.get("/signin?err=removed&ways=1").Body.String()); strings.Contains(card, signinRemoved) { |
| 182 | t.Fatalf("the chooser draws the removal:\n%s", card) |
| 183 | } |
| 184 | }) |
| 185 | } |
| 186 | |
| 187 | // The refusal is the plain door's only: a refused session at the chooser |
| 188 | // (?ways=) or a way's own page (?way=) is drawn no refusal, and is still |
| 189 | // ended. |
| 190 | func TestARefusalIsNotDrawnOnTheChooser(t *testing.T) { |
| 191 | for _, path := range []string{"/signin?ways=1", "/signin?way=email"} { |
| 192 | f := newStandaloneDoor(t) |
| 193 | f.session(t, "opaque-home-person", connect.HomeMethod, time.Now()) |
| 194 | rec := f.get(path) |
| 195 | if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), signinRefusal) { |
| 196 | t.Fatalf("GET %s for a refused session: %d, refusal drawn", path, rec.Code) |
| 197 | } |
| 198 | if f.sessionCookie() != nil { |
| 199 | t.Fatalf("GET %s kept a refused session", path) |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // A gate that cannot decide is no refusal. Home identity storage that |
| 205 | // cannot be read (the local identity table gone) is a no-store 503 on a |
| 206 | // page (it was a 404, which the probe would have read as a refusal), and |
| 207 | // at the door: no refusal, the session and the remembered Oficina ID kept, |
| 208 | // nothing set on /signin. The strict probe MintHomeSession asks refuses |
| 209 | // it, so nothing would be remembered on one. |
| 210 | func TestAnUndecidedGateIsNoRefusal(t *testing.T) { |
| 211 | f := newHomeRouterFixture(t) |
| 212 | f.link(t) |
| 213 | session := f.sessionCookie().Value |
| 214 | if _, err := f.app.db.Writer().Exec("ALTER TABLE memoria_home_local_identities RENAME TO identities_away"); err != nil { |
| 215 | t.Fatal(err) |
| 216 | } |
| 217 | t.Cleanup(func() { |
| 218 | _, _ = f.app.db.Writer().Exec("ALTER TABLE identities_away RENAME TO memoria_home_local_identities") |
| 219 | }) |
| 220 | page := httptest.NewRequest(http.MethodGet, homeTestOrigin+"/", nil) |
| 221 | page.Header.Set("Accept", "text/html") |
| 222 | for _, c := range f.cookies { |
| 223 | page.AddCookie(c) |
| 224 | } |
| 225 | if rec := f.send(page); rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Cache-Control") != "no-store" { |
| 226 | t.Fatalf("a page with identity storage down = %d %q %q, want a no-store 503", rec.Code, rec.Header().Get("Cache-Control"), rec.Header().Get("Location")) |
| 227 | } |
| 228 | // The native apps' API too, as plain text, the same retryable no-store |
| 229 | // 503 (a deliberate change: outside team mode this was a 404). |
| 230 | for _, c := range []struct{ method, path, body string }{ |
| 231 | {http.MethodGet, "/api/native/session", ""}, |
| 232 | {http.MethodPost, "/api/native/search", `{"query":"plan"}`}, |
| 233 | } { |
| 234 | r := httptest.NewRequest(c.method, homeTestOrigin+c.path, strings.NewReader(c.body)) |
| 235 | r.Header.Set("Content-Type", "application/json") |
| 236 | r.Header.Set("Origin", homeTestOrigin) |
| 237 | for _, ck := range f.cookies { |
| 238 | r.AddCookie(ck) |
| 239 | } |
| 240 | if rec := f.send(r); rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Cache-Control") != "no-store" || rec.Header().Get("Retry-After") == "" { |
| 241 | t.Fatalf("%s %s with identity storage down = %d %q, Retry-After %q; want a retryable no-store 503", c.method, c.path, rec.Code, rec.Header().Get("Cache-Control"), rec.Header().Get("Retry-After")) |
| 242 | } |
| 243 | } |
| 244 | if !f.liveSession(session) { |
| 245 | t.Fatal("a 503 ended the session") |
| 246 | } |
| 247 | rec := f.get("/signin") |
| 248 | if strings.Contains(rec.Body.String(), signinRefusal) || strings.Contains(rec.Body.String(), signinRemoved) { |
| 249 | t.Fatal("the door refused a session it could not judge") |
| 250 | } |
| 251 | if len(rec.Result().Cookies()) != 0 { |
| 252 | t.Fatalf("/signin set cookies on an undecided session: %v", rec.Result().Cookies()) |
| 253 | } |
| 254 | if !f.liveSession(session) { |
| 255 | t.Fatal("the door ended a session it could not judge") |
| 256 | } |
| 257 | if r, res := f.remembered(); res != lastsignin.Valid || r.Method != lastsignin.MethodFederated { |
| 258 | t.Fatal("the door forgot Oficina ID for a session it could not judge") |
| 259 | } |
| 260 | if f.app.admission(f.jarRequest(), true).admitted { |
| 261 | t.Fatal("the strict probe admitted a session it could not judge") |
| 262 | } |
| 263 | if !f.app.admission(f.jarRequest(), false).admitted { |
| 264 | t.Fatal("the door's probe refused a session it could not judge") |
| 265 | } |
| 266 | // And MintHomeSession's memory asks strictly: a Home session minted |
| 267 | // while storage cannot say who it is, is not remembered. |
| 268 | w := httptest.NewRecorder() |
| 269 | r := f.fresh().jarRequest() |
| 270 | if err := f.app.home.sessions.SignIn(w, r, sessions.Session{Subject: "opaque-home-alice", Method: connect.HomeMethod, AuthTime: time.Now()}); err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | f.app.rememberOficina(w, r) |
| 274 | if sets(w, f.app.auth.RememberJar().CookieName()) { |
| 275 | t.Fatal("a Home session minted on an undecided gate was remembered") |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // The probe ends nothing: asked about a removed owner's session, it |
| 280 | // reports the removal and leaves the session for the door to end. |
| 281 | func TestTheProbeEndsNothing(t *testing.T) { |
| 282 | f := newHomeRouterFixture(t) |
| 283 | f.link(t) |
| 284 | session := f.sessionCookie().Value |
| 285 | f.app.SetOwners([]string{"someone-else@example.test"}) |
| 286 | if ad := f.app.admission(f.jarRequest(), false); !ad.signedIn || ad.admitted || !ad.removed { |
| 287 | t.Fatalf("the probe found %+v, want a removed session", ad) |
| 288 | } |
| 289 | if !f.liveSession(session) { |
| 290 | t.Fatal("the probe ended the session it was asked about") |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // The door judges web sign-ins only. A session of any other method (a |
| 295 | // native app's connect session, a guest, anything Memoria does not speak |
| 296 | // for) is never refused, remembered or ended there. connect's SigninDoor |
| 297 | // takes its own native sessions as signed in (a 303 on, with Home) and |
| 298 | // anything else as signed out (the door). |
| 299 | func TestTheDoorJudgesWebSessionsOnly(t *testing.T) { |
| 300 | for _, method := range []string{connect.NativeMethod, connect.NativeWebMethod, "guest"} { |
| 301 | t.Run(method, func(t *testing.T) { |
| 302 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 303 | f.session(t, "opaque-home-alice", method, time.Now()) |
| 304 | f.remember(t, lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina}) |
| 305 | session := f.sessionCookie().Value |
| 306 | rec := f.get("/signin") |
| 307 | onward := f.app.home != nil && connect.IsHomeDerived(method) && rec.Code == http.StatusSeeOther |
| 308 | if (rec.Code != http.StatusOK && !onward) || strings.Contains(rec.Body.String(), signinRefusal) || strings.Contains(rec.Body.String(), signinRemoved) { |
| 309 | t.Fatalf("GET /signin with a %s session: %d", method, rec.Code) |
| 310 | } |
| 311 | if clears(rec, f.app.sessionCookie()) || !f.liveSession(session) { |
| 312 | t.Fatalf("the door ended a %s session", method) |
| 313 | } |
| 314 | if _, res := f.remembered(); res != lastsignin.Valid { |
| 315 | t.Fatalf("the door forgot Oficina ID for a %s session", method) |
| 316 | } |
| 317 | if ad := f.app.admission(f.jarRequest(), false); ad.signedIn { |
| 318 | t.Fatalf("the probe judged a %s session: %+v", method, ad) |
| 319 | } |
| 320 | }) |
| 321 | }) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // A removed owner's session on any of the door's states (a problem, a |
| 326 | // step-up, a failed Keymail, Sent) is ended there too, and the door is |
| 327 | // drawn, not a loop. The removal is said on each, except Sent, which |
| 328 | // rastrillo draws as Sent with no app problem (as Calendar found): the |
| 329 | // session is still ended there (Calendar's gap tests, Fable on #49). |
| 330 | func TestARemovedSessionOnTheDoorsStates(t *testing.T) { |
| 331 | for _, path := range []string{"/signin?err=expired", "/signin?reauth=1", "/signin?force=1", "/signin?sent=1"} { |
| 332 | t.Run(path, func(t *testing.T) { |
| 333 | doorModes(t, func(t *testing.T, f *homeRouterFixture) { |
| 334 | f.session(t, "alice@example.test", "magiclink", time.Now()) |
| 335 | session := f.sessionCookie().Value |
| 336 | f.app.SetOwners([]string{"someone-else@example.test"}) |
| 337 | rec := f.get(path) |
| 338 | if rec.Code != http.StatusOK { |
| 339 | t.Fatalf("GET %s for a removed session = %d %q", path, rec.Code, rec.Header().Get("Location")) |
| 340 | } |
| 341 | if !clears(rec, f.app.sessionCookie()) || f.liveSession(session) { |
| 342 | t.Fatalf("GET %s kept a removed session", path) |
| 343 | } |
| 344 | if says := strings.Contains(rec.Body.String(), signinRemoved); says != (path != "/signin?sent=1") { |
| 345 | t.Errorf("GET %s says the removal: %v", path, says) |
| 346 | } |
| 347 | if again := f.get("/signin"); again.Code != http.StatusOK || strings.Contains(again.Body.String(), signinRemoved) { |
| 348 | t.Fatalf("the next /signin: %d", again.Code) |
| 349 | } |
| 350 | }) |
| 351 | }) |
| 352 | } |
| 353 | } |
| 354 | |