oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package handoff
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/rastrillo/rastrillo/assertion"
12)
13
14const testFreshStart = "/auth/fresh/start"
15
16func pathConsumer(t *testing.T, f fixture, start string, freshAuth bool, done func(http.ResponseWriter, *http.Request, assertion.Claims) error) *Consumer {
17 t.Helper()
18 c, e := NewConsumer(ConsumerConfig{Store: f.s, ConsumerID: "directory", StartPath: start, RequireFreshAuth: freshAuth, Now: func() time.Time { return f.now }, Complete: done})
19 if e != nil {
20 t.Fatal(e)
21 }
22 return c
23}
24
25func TestStartPathDefaultsToPackageConstant(t *testing.T) {
26 f := fresh(t)
27 done := func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil }
28 for _, configured := range []string{"", StartPath} {
29 if c := pathConsumer(t, f, configured, false, done); c.StartPath() != StartPath || c.cfg.StartPath != StartPath {
30 t.Fatalf("%q: start path %q", configured, c.StartPath())
31 }
32 }
33 if c := pathConsumer(t, f, testFreshStart, true, done); c.StartPath() != testFreshStart {
34 t.Fatal("custom start path lost")
35 }
36}
37
38func TestStartPathRefusedAtConstruction(t *testing.T) {
39 f := fresh(t)
40 for _, bad := range []string{
41 CallbackPath, FinishPath, "/auth", "/auth/", "/", "/team/fresh/start", "/signin/fresh", "/authorize/fresh",
42 "auth/fresh/start", "/auth/fresh/start/", "/auth//fresh/start", "/auth/./start", "/auth/fresh/../start",
43 "/auth/fresh/start?return_to=%2F", "/auth/fresh/start?", "/auth/fresh/start#x", "/auth/Fresh/start",
44 "/auth/fresh%2Fstart", "/auth/fresh start", "/auth/fresh\x00start", "/auth/fresh\\start", "/auth/fresh.start",
45 "https://app.example.com/auth/fresh/start", "//app.example.com/auth/fresh/start", "/auth/" + strings.Repeat("a", 123),
46 } {
47 for _, freshAuth := range []bool{false, true} {
48 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 {
49 t.Fatalf("start path %q accepted", bad)
50 }
51 }
52 }
53 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 {
54 t.Fatal("128-byte start path refused")
55 }
56}
57
58func TestCustomStartPathIsTheOnlyStart(t *testing.T) {
59 f := fresh(t)
60 done := func(http.ResponseWriter, *http.Request, assertion.Claims) error { return nil }
61 ordinary := pathConsumer(t, f, "", false, done)
62 freshC := pathConsumer(t, f, testFreshStart, true, done)
63 for _, tc := range []struct {
64 c *Consumer
65 path string
66 }{{freshC, StartPath}, {ordinary, testFreshStart}, {freshC, testFreshStart + "/"}, {freshC, "/auth/fresh"}} {
67 rec := httptest.NewRecorder()
68 tc.c.Start(rec, request("GET", tc.path+"?return_to=%2Fteam", ""))
69 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 {
70 t.Fatalf("%s served on the wrong consumer: %d", tc.path, rec.Code)
71 }
72 }
73 rec := httptest.NewRecorder()
74 freshC.Start(rec, request("POST", testFreshStart, ""))
75 if rec.Code != 405 || rec.Header().Get("Allow") != "GET" {
76 t.Fatal("custom start accepted POST")
77 }
78 // Neither start path is a return target, so a finish never re-enters a start.
79 for _, c := range []*Consumer{ordinary, freshC} {
80 for _, back := range []string{StartPath, testFreshStart} {
81 rec := httptest.NewRecorder()
82 c.Start(rec, request("GET", c.StartPath()+"?"+url.Values{"return_to": {back}}.Encode(), ""))
83 if rec.Code != 400 || len(rec.Result().Cookies()) != 0 {
84 t.Fatalf("return_to %s accepted at %s", back, c.StartPath())
85 }
86 }
87 }
88 rec = httptest.NewRecorder()
89 freshC.Start(rec, request("GET", testFreshStart+"?return_to=%2Fteam%3Fteam%3Dabc", ""))
90 target, e := url.Parse(rec.Header().Get("Location"))
91 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" {
92 t.Fatal("custom start not served")
93 }
94}
95
96// Two consumers share one Store and ConsumerID as the directory will mount
97// them: ordinary at StartPath, fresh at its own path, one Callback, and either
98// consumer's Finish. The pending row alone decides the freshness class.
99func TestTwoStartPathsOneStoreKeepTheirFreshness(t *testing.T) {
100 setup := func(t *testing.T) (fixture, *Consumer, *Consumer, *int) {
101 f := fresh(t)
102 completed := new(int)
103 done := func(w http.ResponseWriter, r *http.Request, a assertion.Claims) error { *completed++; return nil }
104 return f, pathConsumer(t, f, "", false, done), pathConsumer(t, f, testFreshStart, true, done), completed
105 }
106 begin := func(t *testing.T, f fixture, c *Consumer, back string) (*httptest.ResponseRecorder, string) {
107 t.Helper()
108 rec := httptest.NewRecorder()
109 c.Start(rec, request("GET", c.StartPath()+"?"+url.Values{"return_to": {back}}.Encode(), ""))
110 target, e := url.Parse(rec.Header().Get("Location"))
111 if rec.Code != 303 || e != nil || target.Query().Get("consumer") != "directory" {
112 t.Fatal("start failed", rec.Code)
113 }
114 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] {
115 t.Fatalf("max_age %q forwarded by start at %s", got, c.StartPath())
116 }
117 return rec, target.Query().Get("nonce")
118 }
119 receive := func(t *testing.T, f fixture, c *Consumer, token string) *httptest.ResponseRecorder {
120 t.Helper()
121 rec := httptest.NewRecorder()
122 c.Callback(rec, request("POST", CallbackPath, url.Values{"assertion": {token}}.Encode()))
123 if rec.Code != 303 {
124 t.Fatal("callback refused", rec.Code)
125 }
126 return rec
127 }
128 finish := func(c *Consumer, secret, handle *http.Cookie) *httptest.ResponseRecorder {
129 r := request("GET", FinishPath, "")
130 r.AddCookie(secret)
131 r.AddCookie(handle)
132 rec := httptest.NewRecorder()
133 c.Finish(rec, r)
134 return rec
135 }
136 for _, tc := range []struct {
137 name string
138 freshStart bool
139 age int64
140 finishOnFresh bool
141 accept bool
142 wantLocation string
143 }{
144 {"fresh recent via ordinary finish", true, 10, false, true, "/team?team=abc"},
145 {"fresh recent via fresh finish", true, 10, true, true, "/team?team=abc"},
146 {"fresh boundary", true, 300, false, true, "/team?team=abc"},
147 {"fresh stale via ordinary finish", true, 301, false, false, ""},
148 {"fresh stale via fresh finish", true, 301, true, false, ""},
149 {"fresh future", true, -1, false, false, ""},
150 {"ordinary stale via ordinary finish", false, 900, false, true, "/team?team=abc"},
151 {"ordinary stale via fresh finish", false, 900, true, true, "/team?team=abc"},
152 } {
153 t.Run(tc.name, func(t *testing.T) {
154 f, ordinary, freshC, completed := setup(t)
155 starter := map[bool]*Consumer{false: ordinary, true: freshC}[tc.freshStart]
156 start, nonce := begin(t, f, starter, "/team?team=abc")
157 callback := receive(t, f, ordinary, f.timedToken(t, nonce, f.now.Unix()-tc.age))
158 rec := finish(map[bool]*Consumer{false: ordinary, true: freshC}[tc.finishOnFresh], matchingCookie(t, start, pendingCookie), matchingCookie(t, callback, inboxCookie))
159 if tc.accept {
160 if rec.Code != 303 || rec.Header().Get("Location") != tc.wantLocation || *completed != 1 {
161 t.Fatalf("refused: %d", rec.Code)
162 }
163 } else if rec.Code != 400 || *completed != 0 || rec.Header().Get("Location") != "" {
164 t.Fatalf("stale proof finished a fresh row: %d", rec.Code)
165 }
166 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 {
167 t.Fatal("proof not consumed exactly once")
168 }
169 })
170 }
171 // No cross-talk: a proof minted for one start path's nonce cannot be
172 // paired with the other path's pending row, in either direction, and the
173 // refusal leaves both rows and the proof unconsumed.
174 t.Run("cross pairing", func(t *testing.T) {
175 f, ordinary, freshC, completed := setup(t)
176 freshStart, freshNonce := begin(t, f, freshC, "/team")
177 plainStart, plainNonce := begin(t, f, ordinary, "/documents")
178 for _, pair := range []struct {
179 secret *httptest.ResponseRecorder
180 nonce string
181 }{{freshStart, plainNonce}, {plainStart, freshNonce}} {
182 callback := receive(t, f, ordinary, f.timedToken(t, pair.nonce, f.now.Unix()-900))
183 for _, c := range []*Consumer{ordinary, freshC} {
184 if rec := finish(c, matchingCookie(t, pair.secret, pendingCookie), matchingCookie(t, callback, inboxCookie)); rec.Code != 400 || *completed != 0 {
185 t.Fatal("proof finished another start path's row")
186 }
187 }
188 }
189 if count(t, f.db, "oficina_handoff_pending") != 2 || count(t, f.db, "oficina_handoff_replay") != 0 {
190 t.Fatal("cross pairing consumed state")
191 }
192 })
193}
194