| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func (f *bridgeFixture) personID(t *testing.T, address string) int64 { |
| 13 | t.Helper() |
| 14 | var id int64 |
| 15 | if err := f.db.QueryRow("SELECT id FROM people WHERE email=?", address).Scan(&id); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | return id |
| 19 | } |
| 20 | |
| 21 | // routes wires NewRoutes over the fixture's bridge, as an app does, so the |
| 22 | // navigation marker and the bridge share their hooks. |
| 23 | func (f *bridgeFixture) routes(t *testing.T) *Routes { |
| 24 | t.Helper() |
| 25 | h, err := NewRoutes(f.b, RoutesConfig{Namespace: "oficina/docs", Origin: bridgeOrigin, Issuer: bridgeIssuer, SessionCookie: f.sessions.cookie, DEK: bytes.Repeat([]byte{41}, 32), |
| 26 | ReadSession: func(r *http.Request) (*Session, error) { |
| 27 | s, ok := f.sessions.From(r) |
| 28 | if !ok { |
| 29 | return nil, nil |
| 30 | } |
| 31 | return &s, nil |
| 32 | }, |
| 33 | SignOut: func(http.ResponseWriter, *http.Request) {}, |
| 34 | LocalSignin: func(http.ResponseWriter, *http.Request) {}, |
| 35 | Protect: func(next http.Handler) http.Handler { return next }, |
| 36 | Render: f.b.render}) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | return h |
| 41 | } |
| 42 | |
| 43 | // TestLinkedIsTheStoredLinkNotTheSessionMethod is the menu bug: someone |
| 44 | // linked who signs in with the app's own email link is still linked. |
| 45 | func TestLinkedIsTheStoredLinkNotTheSessionMethod(t *testing.T) { |
| 46 | f := newBridgeFixture(t) |
| 47 | local := f.local(t, "one@example.com", "magiclink", time.Time{}) |
| 48 | f.local(t, "two@example.com", "magiclink", time.Time{}) |
| 49 | one, two := f.personID(t, "one@example.com"), f.personID(t, "two@example.com") |
| 50 | ctx := t.Context() |
| 51 | if linked, err := f.b.Linked(ctx, one); err != nil || linked { |
| 52 | t.Fatal("unlinked person reported linked", linked, err) |
| 53 | } |
| 54 | f.link(t, local, "opaque-one") |
| 55 | if linked, err := f.b.Linked(ctx, one); err != nil || !linked { |
| 56 | t.Fatal("linked person reported unlinked", linked, err) |
| 57 | } |
| 58 | if linked, err := f.b.Linked(ctx, two); err != nil || linked { |
| 59 | t.Fatal("another person's link leaked", linked, err) |
| 60 | } |
| 61 | if linked, err := f.b.Linked(ctx, 0); err != nil || linked { |
| 62 | t.Fatal("no person reported linked", linked, err) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // TestConfirmEndsOnAccountsConnected: after "Connect accounts" the person |
| 67 | // sees that it worked, on a GET, and only once the link is stored. |
| 68 | func TestConfirmEndsOnAccountsConnected(t *testing.T) { |
| 69 | f := newBridgeFixture(t) |
| 70 | local := f.local(t, "one@example.com", "magiclink", time.Time{}) |
| 71 | proof, cookies := f.linkProof(t, local, "opaque-one") |
| 72 | w := httptest.NewRecorder() |
| 73 | f.b.ConfirmPost(w, bridgeRequest(http.MethodPost, "/account/home/confirm", append(cookies, proof))) |
| 74 | if w.Code != 303 || w.Header().Get("Location") != connectedURL("/") { |
| 75 | t.Fatalf("confirm=%d %q", w.Code, w.Header().Get("Location")) |
| 76 | } |
| 77 | var links int |
| 78 | 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 { |
| 79 | t.Fatal("confirm did not store the link", links, err) |
| 80 | } |
| 81 | done := httptest.NewRecorder() |
| 82 | f.b.ConfirmPage(done, bridgeRequest(http.MethodGet, connectedURL("/"), []*http.Cookie{local})) |
| 83 | if done.Code != 200 || done.Body.String() != "success" { |
| 84 | t.Fatalf("connected page=%d %q", done.Code, done.Body.String()) |
| 85 | } |
| 86 | other := f.local(t, "two@example.com", "magiclink", time.Time{}) |
| 87 | crafted := httptest.NewRecorder() |
| 88 | f.b.ConfirmPage(crafted, bridgeRequest(http.MethodGet, connectedURL("/"), []*http.Cookie{other})) |
| 89 | if crafted.Code != 403 || crafted.Body.String() == "success" { |
| 90 | t.Fatalf("unlinked person shown success: %d", crafted.Code) |
| 91 | } |
| 92 | anonymous := httptest.NewRecorder() |
| 93 | f.b.ConfirmPage(anonymous, bridgeRequest(http.MethodGet, connectedURL("/"), nil)) |
| 94 | if anonymous.Code != 403 { |
| 95 | t.Fatalf("signed-out success page=%d", anonymous.Code) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // unknownArrival runs an ordinary exchange for a subject with no link and |
| 100 | // returns the navigation marker it left. |
| 101 | func (f *bridgeFixture) unknownArrival(t *testing.T, subject string) *http.Cookie { |
| 102 | t.Helper() |
| 103 | pending, nonce := f.start(t, nil, "/d/existing") |
| 104 | inbox := f.callback(t, subject, nonce, time.Now().Add(-time.Hour)) |
| 105 | w := httptest.NewRecorder() |
| 106 | f.b.Finish(w, bridgeRequest(http.MethodGet, "/auth/finish", []*http.Cookie{pending, inbox})) |
| 107 | if w.Code != 403 || w.Body.String() != "unknown" { |
| 108 | t.Fatalf("unknown arrival=%d %q", w.Code, w.Body.String()) |
| 109 | } |
| 110 | bridgeNoSession(t, f.b, w) |
| 111 | return bridgeCookie(t, w, localLinkNavigationCookie) |
| 112 | } |
| 113 | |
| 114 | // TestUnknownAssertionResumesLinkAfterLocalSignIn: an Oficina assertion |
| 115 | // with no link, then a local sign-in, lands on the confirm step for a |
| 116 | // fresh Oficina assertion, with no menu to hunt for. Every existing rule |
| 117 | // still holds: fresh local auth, a fresh Home assertion bound to this |
| 118 | // session, and an explicit confirm before anything is stored. |
| 119 | func TestUnknownAssertionResumesLinkAfterLocalSignIn(t *testing.T) { |
| 120 | f := newBridgeFixture(t) |
| 121 | h := f.routes(t) |
| 122 | marker := f.unknownArrival(t, "opaque-one") |
| 123 | local := f.local(t, "one@example.com", "magiclink", time.Time{}) |
| 124 | w := httptest.NewRecorder() |
| 125 | h.LocalComplete(w, bridgeRequest(http.MethodGet, LocalCompletePath, []*http.Cookie{marker, local})) |
| 126 | if w.Code != 303 || w.Header().Get("Location") != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" { |
| 127 | t.Fatalf("local sign-in did not resume the link: %d %q", w.Code, w.Header().Get("Location")) |
| 128 | } |
| 129 | init := bridgeCookie(t, w, homeInitCookie) |
| 130 | cookies := []*http.Cookie{local, init} |
| 131 | start := httptest.NewRecorder() |
| 132 | f.b.Start(start, bridgeRequest(http.MethodGet, w.Header().Get("Location"), cookies)) |
| 133 | target, _ := url.Parse(start.Header().Get("Location")) |
| 134 | if start.Code != 303 || target.Query().Get("max_age") != "300" { |
| 135 | t.Fatal("resumed link did not ask Oficina for fresh authentication", start.Code) |
| 136 | } |
| 137 | pending := bridgeCookie(t, start, "__Host-oficina-assertion-pending") |
| 138 | inbox := f.callback(t, "opaque-one", target.Query().Get("nonce"), time.Now().Add(-30*time.Second)) |
| 139 | finish := httptest.NewRecorder() |
| 140 | f.b.Finish(finish, bridgeRequest(http.MethodGet, "/auth/finish", append(cookies, pending, inbox))) |
| 141 | if finish.Code != 303 || finish.Header().Get("Location") != "/account/home/confirm" { |
| 142 | t.Fatalf("finish=%d %q", finish.Code, finish.Header().Get("Location")) |
| 143 | } |
| 144 | proof := bridgeCookie(t, finish, homeProofCookie) |
| 145 | var links int |
| 146 | if err := f.db.QueryRow("SELECT COUNT(*) FROM home_identity_links").Scan(&links); err != nil || links != 0 { |
| 147 | t.Fatal("linked before the explicit confirm", links, err) |
| 148 | } |
| 149 | page := httptest.NewRecorder() |
| 150 | f.b.ConfirmPage(page, bridgeRequest(http.MethodGet, "/account/home/confirm", []*http.Cookie{local, proof})) |
| 151 | if page.Code != 200 || page.Body.String() != "confirm" { |
| 152 | t.Fatalf("confirm page=%d %q", page.Code, page.Body.String()) |
| 153 | } |
| 154 | post := httptest.NewRecorder() |
| 155 | f.b.ConfirmPost(post, bridgeRequest(http.MethodPost, "/account/home/confirm", []*http.Cookie{local, proof})) |
| 156 | if post.Code != 303 { |
| 157 | t.Fatalf("confirm=%d", post.Code) |
| 158 | } |
| 159 | if id, err := f.b.Resolve(t.Context(), "opaque-one"); err != nil || id.ID != f.personID(t, "one@example.com") { |
| 160 | t.Fatal("resumed link not stored", id, err) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // TestUnknownMarkerExpiresToTheMenuPath: a remembered arrival older than |
| 165 | // the marker's lifetime is ignored, and the person lands as from any sign |
| 166 | // in, with the menu item still on offer. A stale local sign-in never |
| 167 | // resumes, and the menu's own marker still goes to the Connect page. |
| 168 | func TestUnknownMarkerExpiresToTheMenuPath(t *testing.T) { |
| 169 | f := newBridgeFixture(t) |
| 170 | h := f.routes(t) |
| 171 | marker := f.unknownArrival(t, "opaque-one") |
| 172 | later := time.Now().Add(time.Duration(localLinkNavigationLifetime+1) * time.Second) |
| 173 | // The local sign-in is fresh at the later time too, so only the |
| 174 | // marker's own expiry can decide. |
| 175 | local := f.local(t, "one@example.com", "magiclink", later.Add(-time.Minute)) |
| 176 | h.navigation.now = func() time.Time { return later } |
| 177 | w := httptest.NewRecorder() |
| 178 | h.LocalComplete(w, bridgeRequest(http.MethodGet, LocalCompletePath, []*http.Cookie{marker, local})) |
| 179 | if w.Code != 303 || w.Header().Get("Location") != "/" { |
| 180 | t.Fatalf("expired marker still resumed: %d %q", w.Code, w.Header().Get("Location")) |
| 181 | } |
| 182 | for _, c := range w.Result().Cookies() { |
| 183 | if c.Name == homeInitCookie && c.MaxAge > 0 { |
| 184 | t.Fatal("expired marker began a link") |
| 185 | } |
| 186 | } |
| 187 | h.navigation.now = time.Now |
| 188 | local = f.local(t, "one@example.com", "magiclink", time.Time{}) |
| 189 | stale := f.local(t, "one@example.com", "keymail", time.Now().Add(-10*time.Minute)) |
| 190 | w = httptest.NewRecorder() |
| 191 | h.LocalComplete(w, bridgeRequest(http.MethodGet, LocalCompletePath, []*http.Cookie{f.unknownArrival(t, "opaque-one"), stale})) |
| 192 | if w.Header().Get("Location") != "/" { |
| 193 | t.Fatalf("stale local sign-in resumed: %q", w.Header().Get("Location")) |
| 194 | } |
| 195 | begin := httptest.NewRecorder() |
| 196 | h.navigation.Begin(begin, bridgeRequest(http.MethodGet, "/account/home", nil)) |
| 197 | w = httptest.NewRecorder() |
| 198 | h.LocalComplete(w, bridgeRequest(http.MethodGet, LocalCompletePath, []*http.Cookie{bridgeCookie(t, begin, localLinkNavigationCookie), local})) |
| 199 | if w.Header().Get("Location") != "/account/home" { |
| 200 | t.Fatalf("menu marker changed: %q", w.Header().Get("Location")) |
| 201 | } |
| 202 | } |
| 203 | |