package handoff import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/assertion" ) const testFreshStart = "/auth/fresh/start" func pathConsumer(t *testing.T, f fixture, start string, freshAuth bool, done func(http.ResponseWriter, *http.Request, assertion.Claims) error) *Consumer { t.Helper() c, e := NewConsumer(ConsumerConfig{Store: f.s, ConsumerID: "directory", StartPath: start, RequireFreshAuth: freshAuth, Now: func() time.Time { return f.now }, Complete: done}) if e != nil { t.Fatal(e) } return c } func TestStartPathDefaultsToPackageConstant(t *testing.T) { f := fresh(t) done := func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil } for _, configured := range []string{"", StartPath} { if c := pathConsumer(t, f, configured, false, done); c.StartPath() != StartPath || c.cfg.StartPath != StartPath { t.Fatalf("%q: start path %q", configured, c.StartPath()) } } if c := pathConsumer(t, f, testFreshStart, true, done); c.StartPath() != testFreshStart { t.Fatal("custom start path lost") } } func TestStartPathRefusedAtConstruction(t *testing.T) { f := fresh(t) for _, bad := range []string{ CallbackPath, FinishPath, "/auth", "/auth/", "/", "/team/fresh/start", "/signin/fresh", "/authorize/fresh", "auth/fresh/start", "/auth/fresh/start/", "/auth//fresh/start", "/auth/./start", "/auth/fresh/../start", "/auth/fresh/start?return_to=%2F", "/auth/fresh/start?", "/auth/fresh/start#x", "/auth/Fresh/start", "/auth/fresh%2Fstart", "/auth/fresh start", "/auth/fresh\x00start", "/auth/fresh\\start", "/auth/fresh.start", "https://app.example.com/auth/fresh/start", "//app.example.com/auth/fresh/start", "/auth/" + strings.Repeat("a", 123), } { for _, freshAuth := range []bool{false, true} { if c, e := NewConsumer(ConsumerConfig{Store: f.s, ConsumerID: "directory", StartPath: bad, RequireFreshAuth: freshAuth, Complete: func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil }}); c != nil || e != ErrConfig { t.Fatalf("start path %q accepted", bad) } } } if _, e := NewConsumer(ConsumerConfig{Store: f.s, ConsumerID: "directory", StartPath: "/auth/" + strings.Repeat("a", 122), Complete: func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil }}); e != nil { t.Fatal("128-byte start path refused") } } func TestCustomStartPathIsTheOnlyStart(t *testing.T) { f := fresh(t) done := func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil } ordinary := pathConsumer(t, f, "", false, done) freshC := pathConsumer(t, f, testFreshStart, true, done) for _, tc := range []struct { c *Consumer path string }{{freshC, StartPath}, {ordinary, testFreshStart}, {freshC, testFreshStart + "/"}, {freshC, "/auth/fresh"}} { rec := httptest.NewRecorder() tc.c.Start(rec, request("GET", tc.path+"?return_to=%2Fteam", "")) if rec.Code != 404 || len(rec.Result().Cookies()) != 0 || rec.Header().Get("Location") != "" || rec.Header().Get("Cache-Control") != "no-store" || count(t, f.db, "oficina_handoff_pending") != 0 { t.Fatalf("%s served on the wrong consumer: %d", tc.path, rec.Code) } } rec := httptest.NewRecorder() freshC.Start(rec, request("POST", testFreshStart, "")) if rec.Code != 405 || rec.Header().Get("Allow") != "GET" { t.Fatal("custom start accepted POST") } // Neither start path is a return target, so a finish never re-enters a start. for _, c := range []*Consumer{ordinary, freshC} { for _, back := range []string{StartPath, testFreshStart} { rec := httptest.NewRecorder() c.Start(rec, request("GET", c.StartPath()+"?"+url.Values{"return_to": {back}}.Encode(), "")) if rec.Code != 400 || len(rec.Result().Cookies()) != 0 { t.Fatalf("return_to %s accepted at %s", back, c.StartPath()) } } } rec = httptest.NewRecorder() freshC.Start(rec, request("GET", testFreshStart+"?return_to=%2Fteam%3Fteam%3Dabc", "")) target, e := url.Parse(rec.Header().Get("Location")) if rec.Code != 303 || e != nil || target.Host != "id.example.com" || target.Path != "/authorize" || target.Query().Get("consumer") != "directory" || target.Query().Get("max_age") != "300" { t.Fatal("custom start not served") } } // Two consumers share one Store and ConsumerID as the directory will mount // them: ordinary at StartPath, fresh at its own path, one Callback, and either // consumer's Finish. The pending row alone decides the freshness class. func TestTwoStartPathsOneStoreKeepTheirFreshness(t *testing.T) { setup := func(t *testing.T) (fixture, *Consumer, *Consumer, *int) { f := fresh(t) completed := new(int) done := func(w http.ResponseWriter, r *http.Request, a assertion.Claims) error { *completed++; return nil } return f, pathConsumer(t, f, "", false, done), pathConsumer(t, f, testFreshStart, true, done), completed } begin := func(t *testing.T, f fixture, c *Consumer, back string) (*httptest.ResponseRecorder, string) { t.Helper() rec := httptest.NewRecorder() c.Start(rec, request("GET", c.StartPath()+"?"+url.Values{"return_to": {back}}.Encode(), "")) target, e := url.Parse(rec.Header().Get("Location")) if rec.Code != 303 || e != nil || target.Query().Get("consumer") != "directory" { t.Fatal("start failed", rec.Code) } if got, want := target.Query().Get("max_age"), map[bool]string{true: "300"}[c.cfg.RequireFreshAuth]; got != want || len(target.Query()) != map[bool]int{false: 2, true: 3}[c.cfg.RequireFreshAuth] { t.Fatalf("max_age %q forwarded by start at %s", got, c.StartPath()) } return rec, target.Query().Get("nonce") } receive := func(t *testing.T, f fixture, c *Consumer, token string) *httptest.ResponseRecorder { t.Helper() rec := httptest.NewRecorder() c.Callback(rec, request("POST", CallbackPath, url.Values{"assertion": {token}}.Encode())) if rec.Code != 303 { t.Fatal("callback refused", rec.Code) } return rec } finish := func(c *Consumer, secret, handle *http.Cookie) *httptest.ResponseRecorder { r := request("GET", FinishPath, "") r.AddCookie(secret) r.AddCookie(handle) rec := httptest.NewRecorder() c.Finish(rec, r) return rec } for _, tc := range []struct { name string freshStart bool age int64 finishOnFresh bool accept bool wantLocation string }{ {"fresh recent via ordinary finish", true, 10, false, true, "/team?team=abc"}, {"fresh recent via fresh finish", true, 10, true, true, "/team?team=abc"}, {"fresh boundary", true, 300, false, true, "/team?team=abc"}, {"fresh stale via ordinary finish", true, 301, false, false, ""}, {"fresh stale via fresh finish", true, 301, true, false, ""}, {"fresh future", true, -1, false, false, ""}, {"ordinary stale via ordinary finish", false, 900, false, true, "/team?team=abc"}, {"ordinary stale via fresh finish", false, 900, true, true, "/team?team=abc"}, } { t.Run(tc.name, func(t *testing.T) { f, ordinary, freshC, completed := setup(t) starter := map[bool]*Consumer{false: ordinary, true: freshC}[tc.freshStart] start, nonce := begin(t, f, starter, "/team?team=abc") callback := receive(t, f, ordinary, f.timedToken(t, nonce, f.now.Unix()-tc.age)) rec := finish(map[bool]*Consumer{false: ordinary, true: freshC}[tc.finishOnFresh], matchingCookie(t, start, pendingCookie), matchingCookie(t, callback, inboxCookie)) if tc.accept { if rec.Code != 303 || rec.Header().Get("Location") != tc.wantLocation || *completed != 1 { t.Fatalf("refused: %d", rec.Code) } } else if rec.Code != 400 || *completed != 0 || rec.Header().Get("Location") != "" { t.Fatalf("stale proof finished a fresh row: %d", rec.Code) } if count(t, f.db, "oficina_handoff_pending") != 0 || count(t, f.db, "oficina_handoff_inbox") != 0 || count(t, f.db, "oficina_handoff_replay") != 1 { t.Fatal("proof not consumed exactly once") } }) } // No cross-talk: a proof minted for one start path's nonce cannot be // paired with the other path's pending row, in either direction, and the // refusal leaves both rows and the proof unconsumed. t.Run("cross pairing", func(t *testing.T) { f, ordinary, freshC, completed := setup(t) freshStart, freshNonce := begin(t, f, freshC, "/team") plainStart, plainNonce := begin(t, f, ordinary, "/documents") for _, pair := range []struct { secret *httptest.ResponseRecorder nonce string }{{freshStart, plainNonce}, {plainStart, freshNonce}} { callback := receive(t, f, ordinary, f.timedToken(t, pair.nonce, f.now.Unix()-900)) for _, c := range []*Consumer{ordinary, freshC} { if rec := finish(c, matchingCookie(t, pair.secret, pendingCookie), matchingCookie(t, callback, inboxCookie)); rec.Code != 400 || *completed != 0 { t.Fatal("proof finished another start path's row") } } } if count(t, f.db, "oficina_handoff_pending") != 2 || count(t, f.db, "oficina_handoff_replay") != 0 { t.Fatal("cross pairing consumed state") } }) }