package connect import ( "bytes" "net/http" "net/http/httptest" "net/url" "testing" "time" ) func (f *bridgeFixture) personID(t *testing.T, address string) int64 { t.Helper() var id int64 if err := f.db.QueryRow("SELECT id FROM people WHERE email=?", address).Scan(&id); err != nil { t.Fatal(err) } return id } // routes wires NewRoutes over the fixture's bridge, as an app does, so the // navigation marker and the bridge share their hooks. func (f *bridgeFixture) routes(t *testing.T) *Routes { t.Helper() h, err := NewRoutes(f.b, RoutesConfig{Namespace: "oficina/docs", Origin: bridgeOrigin, Issuer: bridgeIssuer, SessionCookie: f.sessions.cookie, DEK: bytes.Repeat([]byte{41}, 32), ReadSession: func(r *http.Request) (*Session, error) { s, ok := f.sessions.From(r) if !ok { return nil, nil } return &s, nil }, SignOut: func(http.ResponseWriter, *http.Request) {}, LocalSignin: func(http.ResponseWriter, *http.Request) {}, Protect: func(next http.Handler) http.Handler { return next }, Render: f.b.render}) if err != nil { t.Fatal(err) } return h } // TestLinkedIsTheStoredLinkNotTheSessionMethod is the menu bug: someone // linked who signs in with the app's own email link is still linked. func TestLinkedIsTheStoredLinkNotTheSessionMethod(t *testing.T) { f := newBridgeFixture(t) local := f.local(t, "one@example.com", "magiclink", time.Time{}) f.local(t, "two@example.com", "magiclink", time.Time{}) one, two := f.personID(t, "one@example.com"), f.personID(t, "two@example.com") ctx := t.Context() if linked, err := f.b.Linked(ctx, one); err != nil || linked { t.Fatal("unlinked person reported linked", linked, err) } f.link(t, local, "opaque-one") if linked, err := f.b.Linked(ctx, one); err != nil || !linked { t.Fatal("linked person reported unlinked", linked, err) } if linked, err := f.b.Linked(ctx, two); err != nil || linked { t.Fatal("another person's link leaked", linked, err) } if linked, err := f.b.Linked(ctx, 0); err != nil || linked { t.Fatal("no person reported linked", linked, err) } } // TestConfirmEndsOnAccountsConnected: after "Connect accounts" the person // sees that it worked, on a GET, and only once the link is stored. func TestConfirmEndsOnAccountsConnected(t *testing.T) { f := newBridgeFixture(t) local := f.local(t, "one@example.com", "magiclink", time.Time{}) proof, cookies := f.linkProof(t, local, "opaque-one") w := httptest.NewRecorder() f.b.ConfirmPost(w, bridgeRequest(http.MethodPost, "/account/home/confirm", append(cookies, proof))) if w.Code != 303 || w.Header().Get("Location") != connectedURL("/") { t.Fatalf("confirm=%d %q", w.Code, w.Header().Get("Location")) } var links int if err := f.db.QueryRow("SELECT COUNT(*) FROM home_identity_links WHERE subject='opaque-one' AND person_id=?", f.personID(t, "one@example.com")).Scan(&links); err != nil || links != 1 { t.Fatal("confirm did not store the link", links, err) } done := httptest.NewRecorder() f.b.ConfirmPage(done, bridgeRequest(http.MethodGet, connectedURL("/"), []*http.Cookie{local})) if done.Code != 200 || done.Body.String() != "success" { t.Fatalf("connected page=%d %q", done.Code, done.Body.String()) } other := f.local(t, "two@example.com", "magiclink", time.Time{}) crafted := httptest.NewRecorder() f.b.ConfirmPage(crafted, bridgeRequest(http.MethodGet, connectedURL("/"), []*http.Cookie{other})) if crafted.Code != 403 || crafted.Body.String() == "success" { t.Fatalf("unlinked person shown success: %d", crafted.Code) } anonymous := httptest.NewRecorder() f.b.ConfirmPage(anonymous, bridgeRequest(http.MethodGet, connectedURL("/"), nil)) if anonymous.Code != 403 { t.Fatalf("signed-out success page=%d", anonymous.Code) } } // unknownArrival runs an ordinary exchange for a subject with no link and // returns the navigation marker it left. func (f *bridgeFixture) unknownArrival(t *testing.T, subject string) *http.Cookie { t.Helper() pending, nonce := f.start(t, nil, "/d/existing") inbox := f.callback(t, subject, nonce, time.Now().Add(-time.Hour)) w := httptest.NewRecorder() f.b.Finish(w, bridgeRequest(http.MethodGet, "/auth/finish", []*http.Cookie{pending, inbox})) if w.Code != 403 || w.Body.String() != "unknown" { t.Fatalf("unknown arrival=%d %q", w.Code, w.Body.String()) } bridgeNoSession(t, f.b, w) return bridgeCookie(t, w, localLinkNavigationCookie) } // TestUnknownAssertionResumesLinkAfterLocalSignIn: an Oficina assertion // with no link, then a local sign-in, lands on the confirm step for a // fresh Oficina assertion, with no menu to hunt for. Every existing rule // still holds: fresh local auth, a fresh Home assertion bound to this // session, and an explicit confirm before anything is stored. func TestUnknownAssertionResumesLinkAfterLocalSignIn(t *testing.T) { f := newBridgeFixture(t) h := f.routes(t) marker := f.unknownArrival(t, "opaque-one") local := f.local(t, "one@example.com", "magiclink", time.Time{}) w := httptest.NewRecorder() h.LocalComplete(w, bridgeRequest(http.MethodGet, LocalCompletePath, []*http.Cookie{marker, local})) if w.Code != 303 || w.Header().Get("Location") != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { t.Fatalf("local sign-in did not resume the link: %d %q", w.Code, w.Header().Get("Location")) } init := bridgeCookie(t, w, homeInitCookie) cookies := []*http.Cookie{local, init} start := httptest.NewRecorder() f.b.Start(start, bridgeRequest(http.MethodGet, w.Header().Get("Location"), cookies)) target, _ := url.Parse(start.Header().Get("Location")) if start.Code != 303 || target.Query().Get("max_age") != "300" { t.Fatal("resumed link did not ask Oficina for fresh authentication", start.Code) } pending := bridgeCookie(t, start, "__Host-oficina-assertion-pending") inbox := f.callback(t, "opaque-one", target.Query().Get("nonce"), time.Now().Add(-30*time.Second)) finish := httptest.NewRecorder() f.b.Finish(finish, bridgeRequest(http.MethodGet, "/auth/finish", append(cookies, pending, inbox))) if finish.Code != 303 || finish.Header().Get("Location") != "/account/home/confirm" { t.Fatalf("finish=%d %q", finish.Code, finish.Header().Get("Location")) } proof := bridgeCookie(t, finish, homeProofCookie) var links int if err := f.db.QueryRow("SELECT COUNT(*) FROM home_identity_links").Scan(&links); err != nil || links != 0 { t.Fatal("linked before the explicit confirm", links, err) } page := httptest.NewRecorder() f.b.ConfirmPage(page, bridgeRequest(http.MethodGet, "/account/home/confirm", []*http.Cookie{local, proof})) if page.Code != 200 || page.Body.String() != "confirm" { t.Fatalf("confirm page=%d %q", page.Code, page.Body.String()) } post := httptest.NewRecorder() f.b.ConfirmPost(post, bridgeRequest(http.MethodPost, "/account/home/confirm", []*http.Cookie{local, proof})) if post.Code != 303 { t.Fatalf("confirm=%d", post.Code) } if id, err := f.b.Resolve(t.Context(), "opaque-one"); err != nil || id.ID != f.personID(t, "one@example.com") { t.Fatal("resumed link not stored", id, err) } } // TestUnknownMarkerExpiresToTheMenuPath: a remembered arrival older than // the marker's lifetime is ignored, and the person lands as from any sign // in, with the menu item still on offer. A stale local sign-in never // resumes, and the menu's own marker still goes to the Connect page. func TestUnknownMarkerExpiresToTheMenuPath(t *testing.T) { f := newBridgeFixture(t) h := f.routes(t) marker := f.unknownArrival(t, "opaque-one") later := time.Now().Add(time.Duration(localLinkNavigationLifetime+1) * time.Second) // The local sign-in is fresh at the later time too, so only the // marker's own expiry can decide. local := f.local(t, "one@example.com", "magiclink", later.Add(-time.Minute)) h.navigation.now = func() time.Time { return later } w := httptest.NewRecorder() h.LocalComplete(w, bridgeRequest(http.MethodGet, LocalCompletePath, []*http.Cookie{marker, local})) if w.Code != 303 || w.Header().Get("Location") != "/" { t.Fatalf("expired marker still resumed: %d %q", w.Code, w.Header().Get("Location")) } for _, c := range w.Result().Cookies() { if c.Name == homeInitCookie && c.MaxAge > 0 { t.Fatal("expired marker began a link") } } h.navigation.now = time.Now local = f.local(t, "one@example.com", "magiclink", time.Time{}) stale := f.local(t, "one@example.com", "keymail", time.Now().Add(-10*time.Minute)) w = httptest.NewRecorder() h.LocalComplete(w, bridgeRequest(http.MethodGet, LocalCompletePath, []*http.Cookie{f.unknownArrival(t, "opaque-one"), stale})) if w.Header().Get("Location") != "/" { t.Fatalf("stale local sign-in resumed: %q", w.Header().Get("Location")) } begin := httptest.NewRecorder() h.navigation.Begin(begin, bridgeRequest(http.MethodGet, "/account/home", nil)) w = httptest.NewRecorder() h.LocalComplete(w, bridgeRequest(http.MethodGet, LocalCompletePath, []*http.Cookie{bridgeCookie(t, begin, localLinkNavigationCookie), local})) if w.Header().Get("Location") != "/account/home" { t.Fatalf("menu marker changed: %q", w.Header().Get("Location")) } }