| new file mode 100644 |
| index 0000000..5e46429 |
| --- /dev/null |
| +++ b/internal/docs/native_approval_test.go |
| @@ -0,0 +1,187 @@ |
| +package docs |
| + |
| +import ( |
| + "crypto/sha256" |
| + "encoding/base64" |
| + "encoding/json" |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect" |
| + "amadan.net/rastrillo/rastrillo/sessions" |
| +) |
| + |
| +// An Oficina ID native session ends seven days after it was minted |
| +// (connect.NativeLifetime, Home spec 2026-11-14-oficina-id-native.md §7), |
| +// so a revoked device grant or "Sign out all devices" holds within a week. |
| +// rastrillo/native's browser approval (/native/request, /native/authorize, |
| +// /native/exchange) mints a copy of the approving session with the same |
| +// method and a fresh created_at. If a native session could approve, it |
| +// could renew itself every six days with no Home grant behind it, forever. |
| +// So a native or native-web session never approves another sign-in; a |
| +// browser session still does. |
| + |
| +// approvalRequest is one of the native approval flow's requests, sent |
| +// through the router with the given session cookie (if any). |
| +func approvalRequest(f *routerHomeFixture, cookie *http.Cookie, method, path, body string) *httptest.ResponseRecorder { |
| + b := f.browser() |
| + if cookie != nil { |
| + b.cookies[cookie.Name] = cookie |
| + } |
| + r := b.request(method, path, body) |
| + if strings.HasPrefix(body, "{") { |
| + r.Header.Set("Content-Type", "application/json") |
| + r.Header.Set("Accept", "application/json") |
| + } |
| + return b.serve(r) |
| +} |
| + |
| +// approvalBegin is POST /native/request; it returns the request's id and |
| +// the verifier only the app holds. |
| +func approvalBegin(t *testing.T, f *routerHomeFixture) (id, verifier string) { |
| + t.Helper() |
| + verifier = exchangeProof(t) |
| + sum := sha256.Sum256([]byte(verifier)) |
| + rec := approvalRequest(f, nil, "POST", "/native/request", `{"challenge":"`+base64.RawURLEncoding.EncodeToString(sum[:])+`"}`) |
| + var out struct{ ID string } |
| + if rec.Code != 200 || 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 |
| +} |
| + |
| +// approvalExchange is POST /native/exchange; minted is the new session's |
| +// cookie value, "" when nothing was minted. |
| +func approvalExchange(t *testing.T, f *routerHomeFixture, id, verifier string) (code int, minted string) { |
| + t.Helper() |
| + rec := approvalRequest(f, nil, "POST", "/native/exchange", `{"id":"`+id+`","verifier":"`+verifier+`"}`) |
| + var out struct{ Value string } |
| + _ = json.Unmarshal(rec.Body.Bytes(), &out) |
| + return rec.Code, out.Value |
| +} |
| + |
| +func ageSession(t *testing.T, f *routerHomeFixture, token string, age time.Duration) { |
| + t.Helper() |
| + at := time.Now().Add(-age).UTC().Format(time.RFC3339) |
| + res, err := f.app.db.Writer().Exec("UPDATE sessions SET created_at=? WHERE token_hash=?", at, sessions.HashToken(token)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if n, _ := res.RowsAffected(); n != 1 { |
| + t.Fatalf("age session: %d rows", n) |
| + } |
| +} |
| + |
| +// probeNativeApproval has a six-day-old session (still inside its seven |
| +// days) try to approve a native sign-in, and fails with what that bought |
| +// if it could. |
| +func probeNativeApproval(t *testing.T, f *routerHomeFixture, session nativeAnswer) { |
| + t.Helper() |
| + ageSession(t, f, session.Value, 6*24*time.Hour) |
| + if r := f.nativeList(t, session); r.Code != 200 { |
| + t.Fatalf("a six-day-old native session was refused before the probe: %d", r.Code) |
| + } |
| + cookie := &http.Cookie{Name: session.Name, Value: session.Value} |
| + id, verifier := approvalBegin(t, f) |
| + page := approvalRequest(f, cookie, "GET", "/native/authorize?id="+id, "") |
| + approve := approvalRequest(f, cookie, "POST", "/native/authorize?id="+id, "") |
| + code, minted := approvalExchange(t, f, id, verifier) |
| + if minted != "" { |
| + s, _ := f.sessions.From(sessionRequest(nativeAnswer{Name: session.Name, Value: minted})) |
| + ageSession(t, f, session.Value, connect.NativeLifetime+time.Hour) |
| + original := f.nativeList(t, session).Code |
| + renewed := f.nativeList(t, nativeAnswer{Name: session.Name, Value: minted}).Code |
| + 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", |
| + session.methodOf(t, f), page.Code, approve.Code, code, s.Method, time.Since(s.At).Round(time.Second), original, renewed) |
| + } |
| + if page.Code == 200 || approve.Code == 200 { |
| + t.Fatalf("approval page served to a native session: GET=%d POST=%d", page.Code, approve.Code) |
| + } |
| + if page.Code != http.StatusUnauthorized || approve.Code != http.StatusUnauthorized { |
| + t.Fatalf("refusal = GET %d POST %d, want the signed-out 401", page.Code, approve.Code) |
| + } |
| + if code != http.StatusAccepted { |
| + t.Fatalf("exchange after a refused approval = %d, want 202 (still pending)", code) |
| + } |
| +} |
| + |
| +func (a nativeAnswer) methodOf(t *testing.T, f *routerHomeFixture) string { |
| + t.Helper() |
| + s, ok := f.sessions.From(sessionRequest(a)) |
| + if !ok { |
| + return "(no session)" |
| + } |
| + return s.Method |
| +} |
| + |
| +// homeNativeFixture is a Home-mode Docs with alice linked to pr_alice, and |
| +// the browser she linked from (which holds a browser session). |
| +func homeNativeFixture(t *testing.T) (*routerHomeFixture, *routerBrowser) { |
| + t.Helper() |
| + f := newRouterHomeFixture(t) |
| + b := f.browser() |
| + b.localSignin(t, "alice@example.com") |
| + b.linkProof(t, "pr_alice", "alice@example.com") |
| + routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| + return f, b |
| +} |
| + |
| +func TestNativeSessionCannotApproveANativeSignIn(t *testing.T) { |
| + f, _ := homeNativeFixture(t) |
| + rec, answer := f.nativeExchange(t, "pr_alice", nil) |
| + if rec.Code != 200 || answer.Value == "" { |
| + t.Fatalf("native exchange=%d %s", rec.Code, rec.Body.String()) |
| + } |
| + if m := answer.methodOf(t, f); m != connect.NativeMethod { |
| + t.Fatalf("native exchange minted %q", m) |
| + } |
| + probeNativeApproval(t, f, answer) |
| +} |
| + |
| +// "Open on the web" sessions age like native ones, so they cannot approve |
| +// either. connect does not mint them yet; the row is what it will mint. |
| +func TestNativeWebSessionCannotApproveANativeSignIn(t *testing.T) { |
| + f, _ := homeNativeFixture(t) |
| + value, err := f.sessions.Mint(sessions.Session{Subject: "pr_alice", Method: connect.NativeWebMethod, AuthTime: time.Now().Add(-time.Minute)}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + answer := nativeAnswer{Name: f.sessions.CookieName(), Value: value} |
| + probeNativeApproval(t, f, answer) |
| +} |
| + |
| +func TestTeamNativeSessionCannotApproveANativeSignIn(t *testing.T) { |
| + f := newTeamRouterFixture(t) |
| + rec, answer := f.nativeExchange(t, teamSubject('b'), map[string]string{"choice": "new"}) |
| + if rec.Code != 200 || answer.Value == "" { |
| + t.Fatalf("native exchange=%d %s", rec.Code, rec.Body.String()) |
| + } |
| + probeNativeApproval(t, f.routerHomeFixture, answer) |
| +} |
| + |
| +// The control: the browser session alice signed in with still approves, |
| +// through the same requests, and the exchange mints her app a session. |
| +func TestBrowserSessionStillApprovesANativeSignIn(t *testing.T) { |
| + f, b := homeNativeFixture(t) |
| + cookie := b.cookies[f.sessions.CookieName()] |
| + if cookie == nil { |
| + t.Fatal("no browser session") |
| + } |
| + id, verifier := approvalBegin(t, f) |
| + if r := approvalRequest(f, cookie, "GET", "/native/authorize?id="+id, ""); r.Code != 200 { |
| + t.Fatalf("approval page = %d", r.Code) |
| + } |
| + if r := approvalRequest(f, cookie, "POST", "/native/authorize?id="+id, ""); r.Code != 200 { |
| + t.Fatalf("approval = %d %q", r.Code, r.Body.String()) |
| + } |
| + code, minted := approvalExchange(t, f, id, verifier) |
| + if code != 200 || minted == "" { |
| + t.Fatalf("exchange = %d", code) |
| + } |
| + if r := f.nativeList(t, nativeAnswer{Name: f.sessions.CookieName(), Value: minted}); r.Code != 200 { |
| + t.Fatalf("minted session list = %d", r.Code) |
| + } |
| +} |