oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "crypto/sha256"
5 "encoding/base64"
6 "encoding/json"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/home/connect"
14 "amadan.net/rastrillo/rastrillo/sessions"
15)
16
17// An Oficina ID native session ends seven days after it was minted
18// (connect.NativeLifetime, Home spec 2026-11-14-oficina-id-native.md §7),
19// so a revoked device grant or "Sign out all devices" holds within a week.
20// rastrillo/native's browser approval (/native/request, /native/authorize,
21// /native/exchange) mints a copy of the approving session with the same
22// method and a fresh created_at. If a native session could approve, it
23// could renew itself every six days with no Home grant behind it, forever.
24// So a native or native-web session never approves another sign-in; a
25// browser session still does.
26
27// approvalRequest is one of the native approval flow's requests, sent
28// through the router with the given session cookie (if any).
29func approvalRequest(f *routerHomeFixture, cookie *http.Cookie, method, path, body string) *httptest.ResponseRecorder {
30 b := f.browser()
31 if cookie != nil {
32 b.cookies[cookie.Name] = cookie
33 }
34 r := b.request(method, path, body)
35 if strings.HasPrefix(body, "{") {
36 r.Header.Set("Content-Type", "application/json")
37 r.Header.Set("Accept", "application/json")
38 }
39 return b.serve(r)
40}
41
42// approvalBegin is POST /native/request; it returns the request's id and
43// the verifier only the app holds.
44func approvalBegin(t *testing.T, f *routerHomeFixture) (id, verifier string) {
45 t.Helper()
46 verifier = exchangeProof(t)
47 sum := sha256.Sum256([]byte(verifier))
48 rec := approvalRequest(f, nil, "POST", "/native/request", `{"challenge":"`+base64.RawURLEncoding.EncodeToString(sum[:])+`"}`)
49 var out struct{ ID string }
50 if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &out) != nil || out.ID == "" {
51 t.Fatalf("POST /native/request = %d %q", rec.Code, rec.Body.String())
52 }
53 return out.ID, verifier
54}
55
56// approvalExchange is POST /native/exchange; minted is the new session's
57// cookie value, "" when nothing was minted.
58func approvalExchange(t *testing.T, f *routerHomeFixture, id, verifier string) (code int, minted string) {
59 t.Helper()
60 rec := approvalRequest(f, nil, "POST", "/native/exchange", `{"id":"`+id+`","verifier":"`+verifier+`"}`)
61 var out struct{ Value string }
62 _ = json.Unmarshal(rec.Body.Bytes(), &out)
63 return rec.Code, out.Value
64}
65
66func ageSession(t *testing.T, f *routerHomeFixture, token string, age time.Duration) {
67 t.Helper()
68 at := time.Now().Add(-age).UTC().Format(time.RFC3339)
69 res, err := f.app.db.Writer().Exec("UPDATE sessions SET created_at=? WHERE token_hash=?", at, sessions.HashToken(token))
70 if err != nil {
71 t.Fatal(err)
72 }
73 if n, _ := res.RowsAffected(); n != 1 {
74 t.Fatalf("age session: %d rows", n)
75 }
76}
77
78// probeNativeApproval has a six-day-old session (still inside its seven
79// days) try to approve a native sign-in, and fails with what that bought
80// if it could.
81func probeNativeApproval(t *testing.T, f *routerHomeFixture, session nativeAnswer) {
82 t.Helper()
83 ageSession(t, f, session.Value, 6*24*time.Hour)
84 if r := f.nativeList(t, session); r.Code != 200 {
85 t.Fatalf("a six-day-old native session was refused before the probe: %d", r.Code)
86 }
87 cookie := &http.Cookie{Name: session.Name, Value: session.Value}
88 id, verifier := approvalBegin(t, f)
89 page := approvalRequest(f, cookie, "GET", "/native/authorize?id="+id, "")
90 approve := approvalRequest(f, cookie, "POST", "/native/authorize?id="+id, "")
91 code, minted := approvalExchange(t, f, id, verifier)
92 if minted != "" {
93 s, _ := f.sessions.From(sessionRequest(nativeAnswer{Name: session.Name, Value: minted}))
94 ageSession(t, f, session.Value, connect.NativeLifetime+time.Hour)
95 original := f.nativeList(t, session).Code
96 renewed := f.nativeList(t, nativeAnswer{Name: session.Name, Value: minted}).Code
97 t.Fatalf("a %s session approved a native sign-in: authorize GET=%d POST=%d, exchange=%d minted method=%q created %s ago; with the original past seven days its list=%d, the minted one's=%d",
98 session.methodOf(t, f), page.Code, approve.Code, code, s.Method, time.Since(s.At).Round(time.Second), original, renewed)
99 }
100 if page.Code == 200 || approve.Code == 200 {
101 t.Fatalf("approval page served to a native session: GET=%d POST=%d", page.Code, approve.Code)
102 }
103 if page.Code != http.StatusUnauthorized || approve.Code != http.StatusUnauthorized {
104 t.Fatalf("refusal = GET %d POST %d, want the signed-out 401", page.Code, approve.Code)
105 }
106 if code != http.StatusAccepted {
107 t.Fatalf("exchange after a refused approval = %d, want 202 (still pending)", code)
108 }
109}
110
111func (a nativeAnswer) methodOf(t *testing.T, f *routerHomeFixture) string {
112 t.Helper()
113 s, ok := f.sessions.From(sessionRequest(a))
114 if !ok {
115 return "(no session)"
116 }
117 return s.Method
118}
119
120// homeNativeFixture is a Home-mode Docs with alice linked to pr_alice, and
121// the browser she linked from (which holds a browser session).
122func homeNativeFixture(t *testing.T) (*routerHomeFixture, *routerBrowser) {
123 t.Helper()
124 f := newRouterHomeFixture(t)
125 b := f.browser()
126 b.localSignin(t, "alice@example.com")
127 b.linkProof(t, "pr_alice", "alice@example.com")
128 routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/"))
129 return f, b
130}
131
132func TestNativeSessionCannotApproveANativeSignIn(t *testing.T) {
133 f, _ := homeNativeFixture(t)
134 rec, answer := f.nativeExchange(t, "pr_alice", nil)
135 if rec.Code != 200 || answer.Value == "" {
136 t.Fatalf("native exchange=%d %s", rec.Code, rec.Body.String())
137 }
138 if m := answer.methodOf(t, f); m != connect.NativeMethod {
139 t.Fatalf("native exchange minted %q", m)
140 }
141 probeNativeApproval(t, f, answer)
142}
143
144// "Open on the web" sessions age like native ones, so they cannot approve
145// either. connect does not mint them yet; the row is what it will mint.
146func TestNativeWebSessionCannotApproveANativeSignIn(t *testing.T) {
147 f, _ := homeNativeFixture(t)
148 value, err := f.sessions.Mint(sessions.Session{Subject: "pr_alice", Method: connect.NativeWebMethod, AuthTime: time.Now().Add(-time.Minute)})
149 if err != nil {
150 t.Fatal(err)
151 }
152 answer := nativeAnswer{Name: f.sessions.CookieName(), Value: value}
153 probeNativeApproval(t, f, answer)
154}
155
156func TestTeamNativeSessionCannotApproveANativeSignIn(t *testing.T) {
157 f := newTeamRouterFixture(t)
158 rec, answer := f.nativeExchange(t, teamSubject('b'), map[string]string{"choice": "new"})
159 if rec.Code != 200 || answer.Value == "" {
160 t.Fatalf("native exchange=%d %s", rec.Code, rec.Body.String())
161 }
162 probeNativeApproval(t, f.routerHomeFixture, answer)
163}
164
165// The control: the browser session alice signed in with still approves,
166// through the same requests, and the exchange mints her app a session.
167func TestBrowserSessionStillApprovesANativeSignIn(t *testing.T) {
168 f, b := homeNativeFixture(t)
169 cookie := b.cookies[f.sessions.CookieName()]
170 if cookie == nil {
171 t.Fatal("no browser session")
172 }
173 id, verifier := approvalBegin(t, f)
174 if r := approvalRequest(f, cookie, "GET", "/native/authorize?id="+id, ""); r.Code != 200 {
175 t.Fatalf("approval page = %d", r.Code)
176 }
177 if r := approvalRequest(f, cookie, "POST", "/native/authorize?id="+id, ""); r.Code != 200 {
178 t.Fatalf("approval = %d %q", r.Code, r.Body.String())
179 }
180 code, minted := approvalExchange(t, f, id, verifier)
181 if code != 200 || minted == "" {
182 t.Fatalf("exchange = %d", code)
183 }
184 if r := f.nativeList(t, nativeAnswer{Name: f.sessions.CookieName(), Value: minted}); r.Code != 200 {
185 t.Fatalf("minted session list = %d", r.Code)
186 }
187}
188