Test native sign-in across the two sessions packages
native.go's bridge hands rastrillo/native's server a pre-move *sessions.Sessions while the app reads sessions through amadan.net/rastrillo's package. The existing native test covers one happy path on http. These three drive the exchange through the router on an https origin (the __Host- cookie production uses): - exchange: 202 before approval, 404 for a wrong verifier, a new token under the cookie name the app reads, the same person (their document is in the minted session's list), and no second exchange; - expiry: an expired browser session cannot approve, an approval that expires before the exchange gets 401, and a minted session expires; - revocation: signing the browser out between approval and exchange gets 401, and /signout ends a minted session and leaves the browser's. Shown red by mutating native.go four ways (an http origin, sessions minted already expired, a minted row that resolves to someone else, deletes ignored); the output is in polish-audit/r3/scripts/ migrate-docs-native-red.txt.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+246
−0
internal/docs/native_exchange_test.go+246 −0
CI passed — run details
diff --git a/internal/docs/native_exchange_test.go b/internal/docs/native_exchange_test.go| new file mode 100644 |
| index 0000000..fa2bb49 |
| --- /dev/null |
| +++ b/internal/docs/native_exchange_test.go |
| @@ -0,0 +1,246 @@ |
| +package docs |
| + |
| +import ( |
| + "crypto/rand" |
| + "crypto/sha256" |
| + "encoding/base64" |
| + "encoding/json" |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/rastrillo/rastrillo/sessions" |
| +) |
| + |
| +// The native sign-in crosses two rastrillo module paths. The app's |
| +// sessions are amadan.net/rastrillo/rastrillo's; rastrillo/native's |
| +// server only takes github.com/carlosframework/rastrillo's, which |
| +// native.go builds over the same database (the one import |
| +// TestNoPreMoveRastrillo allows). These tests drive the whole exchange |
| +// through the router, on an https origin as production is (so the cookie |
| +// is the __Host- one), and a change to either package's sessions table, |
| +// cookie name, token hash or time layout fails here rather than in the |
| +// Mac app: approval of a browser session, the exchange minting a new |
| +// one, that one being an ordinary session to the app, and expiry and |
| +// revocation holding on both sides. |
| + |
| +const exchangeOrigin = "https://docs.example" |
| + |
| +func exchangeApp(t *testing.T) (*App, *http.Cookie) { |
| + t.Helper() |
| + app, err := NewForTestWith(t, Config{Origin: exchangeOrigin}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return app, exchangeSessionCookie(t, signedInForTest(t, app, "alice@example.com")) |
| +} |
| + |
| +func exchangeSessionCookie(t *testing.T, cookies []*http.Cookie) *http.Cookie { |
| + t.Helper() |
| + for _, c := range cookies { |
| + if strings.Contains(c.Name, "session") { |
| + return c |
| + } |
| + } |
| + t.Fatalf("no session cookie in %v", cookies) |
| + return nil |
| +} |
| + |
| +// exchangeDo is a native client's request: JSON, same-origin. |
| +func exchangeDo(t *testing.T, app *App, c *http.Cookie, method, path, body string) *httptest.ResponseRecorder { |
| + t.Helper() |
| + r := httptest.NewRequest(method, path, strings.NewReader(body)) |
| + r.Header.Set("Accept", "application/json") |
| + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + if strings.HasPrefix(body, "{") { |
| + r.Header.Set("Content-Type", "application/json") |
| + } |
| + r.Header.Set("Sec-Fetch-Site", "same-origin") |
| + if c != nil { |
| + r.AddCookie(c) |
| + } |
| + w := httptest.NewRecorder() |
| + app.Handler().ServeHTTP(w, r) |
| + return w |
| +} |
| + |
| +func exchangeProof(t *testing.T) string { |
| + t.Helper() |
| + b := make([]byte, 32) |
| + if _, err := rand.Read(b); err != nil { |
| + t.Fatal(err) |
| + } |
| + return base64.RawURLEncoding.EncodeToString(b) |
| +} |
| + |
| +// exchangeRequest starts a native sign-in and returns its id and verifier. |
| +func exchangeRequest(t *testing.T, app *App) (id, verifier string) { |
| + t.Helper() |
| + verifier = exchangeProof(t) |
| + sum := sha256.Sum256([]byte(verifier)) |
| + rec := exchangeDo(t, app, nil, http.MethodPost, "/native/request", `{"challenge":"`+base64.RawURLEncoding.EncodeToString(sum[:])+`"}`) |
| + var out struct{ ID string } |
| + if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &out) != nil || out.ID == "" { |
| + t.Fatalf("POST /native/request = %d %q", rec.Code, rec.Body.String()) |
| + } |
| + return out.ID, verifier |
| +} |
| + |
| +func exchangeApprove(t *testing.T, app *App, browser *http.Cookie, id string) int { |
| + t.Helper() |
| + return exchangeDo(t, app, browser, http.MethodPost, "/native/authorize?id="+id, "").Code |
| +} |
| + |
| +func exchangeFinish(t *testing.T, app *App, id, verifier string) *httptest.ResponseRecorder { |
| + t.Helper() |
| + return exchangeDo(t, app, nil, http.MethodPost, "/native/exchange", `{"id":"`+id+`","verifier":"`+verifier+`"}`) |
| +} |
| + |
| +// exchangeMint runs a whole approved exchange and returns the minted cookie. |
| +func exchangeMint(t *testing.T, app *App, browser *http.Cookie) *http.Cookie { |
| + t.Helper() |
| + id, verifier := exchangeRequest(t, app) |
| + if code := exchangeApprove(t, app, browser, id); code != http.StatusOK { |
| + t.Fatalf("approval = %d", code) |
| + } |
| + rec := exchangeFinish(t, app, id, verifier) |
| + var minted struct{ Name, Value string } |
| + if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &minted) != nil || minted.Value == "" { |
| + t.Fatalf("exchange = %d %q", rec.Code, rec.Body.String()) |
| + } |
| + return &http.Cookie{Name: minted.Name, Value: minted.Value} |
| +} |
| + |
| +// exchangeList is GET / as a native client: 200 signed in, 303 to the |
| +// door signed out. |
| +func exchangeList(t *testing.T, app *App, c *http.Cookie) *httptest.ResponseRecorder { |
| + t.Helper() |
| + return exchangeDo(t, app, c, http.MethodGet, "/", "") |
| +} |
| + |
| +func exchangeSetExpiry(t *testing.T, app *App, token string, at time.Time) { |
| + t.Helper() |
| + res, err := app.db.Writer().Exec(`UPDATE sessions SET expires_at = ? WHERE token_hash = ?`, |
| + at.UTC().Format(time.RFC3339), sessions.HashToken(token)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if n, _ := res.RowsAffected(); n != 1 { |
| + t.Fatalf("expire session: %d rows", n) |
| + } |
| +} |
| + |
| +func TestNativeExchangeMintsASessionTheAppAccepts(t *testing.T) { |
| + app, browser := exchangeApp(t) |
| + if browser.Name != "__Host-rastrillo_session" { |
| + t.Fatalf("browser session cookie is %q; this test means to cover the https name", browser.Name) |
| + } |
| + created := exchangeDo(t, app, browser, http.MethodPost, "/documents", "title=Native+probe") |
| + var doc struct{ Sqid string } |
| + if created.Code != http.StatusCreated || json.Unmarshal(created.Body.Bytes(), &doc) != nil || doc.Sqid == "" { |
| + t.Fatalf("creating a document = %d %q", created.Code, created.Body.String()) |
| + } |
| + id, verifier := exchangeRequest(t, app) |
| + |
| + if rec := exchangeFinish(t, app, id, verifier); rec.Code != http.StatusAccepted { |
| + t.Fatalf("exchange before approval = %d, want 202", rec.Code) |
| + } |
| + if code := exchangeApprove(t, app, browser, id); code != http.StatusOK { |
| + t.Fatalf("approval = %d", code) |
| + } |
| + if rec := exchangeFinish(t, app, id, exchangeProof(t)); rec.Code != http.StatusNotFound { |
| + t.Fatalf("exchange with the wrong verifier = %d, want 404", rec.Code) |
| + } |
| + rec := exchangeFinish(t, app, id, verifier) |
| + var minted struct{ Name, Value string } |
| + if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &minted) != nil { |
| + t.Fatalf("exchange = %d %q", rec.Code, rec.Body.String()) |
| + } |
| + // The pre-move package names the cookie the app reads, and mints a |
| + // new token rather than handing back the browser's. |
| + if minted.Name != browser.Name { |
| + t.Fatalf("exchange minted cookie %q; the app reads %q", minted.Name, browser.Name) |
| + } |
| + if minted.Value == "" || minted.Value == browser.Value { |
| + t.Fatal("exchange did not mint a session of its own") |
| + } |
| + native := &http.Cookie{Name: minted.Name, Value: minted.Value} |
| + list := exchangeList(t, app, native) |
| + if list.Code != http.StatusOK { |
| + t.Fatalf("the app refuses the session native minted: GET / = %d", list.Code) |
| + } |
| + // And it is the same person: their document is in the list. |
| + if !strings.Contains(list.Body.String(), doc.Sqid) { |
| + t.Fatal("the minted session is somebody else: the browser's document is not in its list") |
| + } |
| + // Spent: the same id never exchanges twice. |
| + if rec := exchangeFinish(t, app, id, verifier); rec.Code != http.StatusNotFound { |
| + t.Fatalf("second exchange = %d, want 404", rec.Code) |
| + } |
| +} |
| + |
| +func TestNativeSessionExpiryHolds(t *testing.T) { |
| + app, browser := exchangeApp(t) |
| + |
| + // A browser session that has expired cannot approve. |
| + id, _ := exchangeRequest(t, app) |
| + exchangeSetExpiry(t, app, browser.Value, time.Now().Add(-time.Minute)) |
| + if code := exchangeApprove(t, app, browser, id); code == http.StatusOK { |
| + t.Fatal("an expired browser session approved a native sign-in") |
| + } |
| + |
| + // Approved while live, expired before the exchange: refused. |
| + exchangeSetExpiry(t, app, browser.Value, time.Now().Add(time.Hour)) |
| + id, verifier := exchangeRequest(t, app) |
| + if code := exchangeApprove(t, app, browser, id); code != http.StatusOK { |
| + t.Fatalf("approval = %d", code) |
| + } |
| + exchangeSetExpiry(t, app, browser.Value, time.Now().Add(-time.Minute)) |
| + if rec := exchangeFinish(t, app, id, verifier); rec.Code != http.StatusUnauthorized { |
| + t.Fatalf("exchange of an expired approval = %d, want 401", rec.Code) |
| + } |
| + |
| + // A minted session expires like any other. |
| + exchangeSetExpiry(t, app, browser.Value, time.Now().Add(time.Hour)) |
| + native := exchangeMint(t, app, browser) |
| + if code := exchangeList(t, app, native).Code; code != http.StatusOK { |
| + t.Fatalf("live minted session: GET / = %d", code) |
| + } |
| + exchangeSetExpiry(t, app, native.Value, time.Now().Add(-time.Minute)) |
| + if code := exchangeList(t, app, native).Code; code == http.StatusOK { |
| + t.Fatal("the app still accepts a minted session past its expiry") |
| + } |
| +} |
| + |
| +func TestNativeSessionRevocationHolds(t *testing.T) { |
| + app, browser := exchangeApp(t) |
| + |
| + // Signing the browser out between approval and exchange refuses it. |
| + id, verifier := exchangeRequest(t, app) |
| + if code := exchangeApprove(t, app, browser, id); code != http.StatusOK { |
| + t.Fatalf("approval = %d", code) |
| + } |
| + if code := exchangeDo(t, app, browser, http.MethodPost, "/signout", "").Code; code >= 400 { |
| + t.Fatalf("POST /signout = %d", code) |
| + } |
| + if rec := exchangeFinish(t, app, id, verifier); rec.Code != http.StatusUnauthorized { |
| + t.Fatalf("exchange after the browser signed out = %d, want 401", rec.Code) |
| + } |
| + |
| + // The app signing out a minted session (the app's own sessions |
| + // package, deleting a row the pre-move package wrote) ends it, and |
| + // leaves the browser's alone. |
| + app, browser = exchangeApp(t) |
| + native := exchangeMint(t, app, browser) |
| + if code := exchangeDo(t, app, native, http.MethodPost, "/signout", "").Code; code >= 400 { |
| + t.Fatalf("POST /signout with the minted session = %d", code) |
| + } |
| + if code := exchangeList(t, app, native).Code; code == http.StatusOK { |
| + t.Fatal("a signed-out minted session still reaches /") |
| + } |
| + if code := exchangeList(t, app, browser).Code; code != http.StatusOK { |
| + t.Fatalf("signing native out ended the browser's session too: GET / = %d", code) |
| + } |
| +} |