| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/rand" |
| 6 | "encoding/base64" |
| 7 | "encoding/json" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "net/url" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/home/connect" |
| 16 | "amadan.net/oficina/home/handoff" |
| 17 | "amadan.net/rastrillo/rastrillo/sessions" |
| 18 | ) |
| 19 | |
| 20 | // The native app's Oficina ID sign-in (Home spec |
| 21 | // 2026-11-14-oficina-id-native.md §7): POST /native/oficina-id trades an |
| 22 | // assertion Home made for the app's verifier for a Docs session, with no |
| 23 | // browser and no cookie. |
| 24 | |
| 25 | type nativeAnswer struct { |
| 26 | State string `json:"state"` |
| 27 | Name string `json:"name"` |
| 28 | Value string `json:"value"` |
| 29 | ExpiresAt int64 `json:"expires_at"` |
| 30 | } |
| 31 | |
| 32 | // nativeExchange posts what a native app posts: JSON, no Origin, no |
| 33 | // Sec-Fetch-Site, no cookies. |
| 34 | func (f *routerHomeFixture) nativeExchange(t *testing.T, subject string, extra map[string]string) (*httptest.ResponseRecorder, nativeAnswer) { |
| 35 | t.Helper() |
| 36 | raw := make([]byte, 32) |
| 37 | if _, err := rand.Read(raw); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | verifier := base64.RawURLEncoding.EncodeToString(raw) |
| 41 | challenge, _ := handoff.NativeChallenge(verifier) |
| 42 | nonce, _ := handoff.NativeNonce(challenge) |
| 43 | body := map[string]string{"assertion": f.token(t, nonce, subject, time.Now().Add(-time.Minute), nil), "verifier": verifier} |
| 44 | for k, v := range extra { |
| 45 | body[k] = v |
| 46 | } |
| 47 | encoded, err := json.Marshal(body) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | r := httptest.NewRequest("POST", bridgeOrigin+connect.NativeExchangePath, bytes.NewReader(encoded)) |
| 52 | r.Header.Set("Content-Type", "application/json") |
| 53 | rec := f.browser().serve(r) |
| 54 | if len(rec.Result().Cookies()) != 0 { |
| 55 | t.Fatal("the native exchange set a cookie") |
| 56 | } |
| 57 | var answer nativeAnswer |
| 58 | if err := json.Unmarshal(rec.Body.Bytes(), &answer); err != nil { |
| 59 | t.Fatalf("answer %d %q: %v", rec.Code, rec.Body.String(), err) |
| 60 | } |
| 61 | return rec, answer |
| 62 | } |
| 63 | |
| 64 | // nativeList is the native app's document list, with the minted session. |
| 65 | func (f *routerHomeFixture) nativeList(t *testing.T, a nativeAnswer) *httptest.ResponseRecorder { |
| 66 | t.Helper() |
| 67 | r := httptest.NewRequest("GET", bridgeOrigin+"/", nil) |
| 68 | r.Header.Set("Accept", "application/json") |
| 69 | r.AddCookie(&http.Cookie{Name: a.Name, Value: a.Value}) |
| 70 | return f.browser().serve(r) |
| 71 | } |
| 72 | |
| 73 | func TestNativeOficinaIDOpensALinkedAccount(t *testing.T) { |
| 74 | f := newRouterHomeFixture(t) |
| 75 | alice, err := f.app.store.EnsurePerson(t.Context(), "alice@example.com") |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if _, err = f.app.store.createDocument(t.Context(), alice, "Alice original", "", VisibilityPrivate); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | b := f.browser() |
| 83 | b.localSignin(t, "alice@example.com") |
| 84 | b.linkProof(t, "pr_alice", "alice@example.com") |
| 85 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 86 | |
| 87 | rec, answer := f.nativeExchange(t, "pr_alice", nil) |
| 88 | if rec.Code != 200 || answer.State != "ok" || answer.Name != f.sessions.CookieName() || answer.Value == "" { |
| 89 | t.Fatalf("exchange=%d %s", rec.Code, rec.Body.String()) |
| 90 | } |
| 91 | s, ok := f.sessions.From(sessionRequest(answer)) |
| 92 | if !ok || s.Method != connect.NativeMethod || s.Subject != "pr_alice" { |
| 93 | t.Fatalf("minted %+v", s) |
| 94 | } |
| 95 | list := f.nativeList(t, answer) |
| 96 | if list.Code != 200 || !strings.Contains(list.Body.String(), "Alice original") { |
| 97 | t.Fatalf("native list=%d %s", list.Code, list.Body.String()) |
| 98 | } |
| 99 | |
| 100 | // A subject with no account here: unknown, and the same for everyone. |
| 101 | stranger, _ := f.nativeExchange(t, "pr_stranger", nil) |
| 102 | chose, _ := f.nativeExchange(t, "pr_other", map[string]string{"choice": "new"}) |
| 103 | for _, r := range []*httptest.ResponseRecorder{stranger, chose} { |
| 104 | if r.Code != 403 || r.Body.String() != `{"state":"unknown"}` { |
| 105 | t.Fatalf("unknown=%d %s", r.Code, r.Body.String()) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Seven days after it was minted, however recently used, the native |
| 111 | // session opens nothing: the app re-mints long before. |
| 112 | func TestNativeSessionEndsAfterSevenDays(t *testing.T) { |
| 113 | f := newRouterHomeFixture(t) |
| 114 | b := f.browser() |
| 115 | b.localSignin(t, "alice@example.com") |
| 116 | b.linkProof(t, "pr_alice", "alice@example.com") |
| 117 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 118 | _, answer := f.nativeExchange(t, "pr_alice", nil) |
| 119 | if f.nativeList(t, answer).Code != 200 { |
| 120 | t.Fatal("a fresh native session was refused") |
| 121 | } |
| 122 | old := time.Now().Add(-connect.NativeLifetime - time.Hour).UTC().Format(time.RFC3339) |
| 123 | if _, err := f.app.db.Writer().Exec("UPDATE sessions SET created_at=? WHERE token_hash=?", old, sessions.HashToken(answer.Value)); err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | if r := f.nativeList(t, answer); r.Code == 200 { |
| 127 | t.Fatal("a native session older than seven days opened the list") |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // The exchange is not a browser route: a request carrying a browser's |
| 132 | // Origin is refused by the exchange itself, not waved through. |
| 133 | func TestNativeExchangeRefusesABrowser(t *testing.T) { |
| 134 | f := newRouterHomeFixture(t) |
| 135 | r := httptest.NewRequest("POST", bridgeOrigin+connect.NativeExchangePath, strings.NewReader(`{"assertion":"x","verifier":"y"}`)) |
| 136 | r.Header.Set("Content-Type", "application/json") |
| 137 | r.Header.Set("Origin", bridgeOrigin) |
| 138 | rec := f.browser().serve(r) |
| 139 | if rec.Code != 400 || rec.Body.String() != `{"state":"invalid"}` { |
| 140 | t.Fatalf("browser POST=%d %s", rec.Code, rec.Body.String()) |
| 141 | } |
| 142 | form := url.Values{"assertion": {"x"}}.Encode() |
| 143 | r = httptest.NewRequest("POST", bridgeOrigin+connect.NativeExchangePath, strings.NewReader(form)) |
| 144 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 145 | if rec = f.browser().serve(r); rec.Code != 400 { |
| 146 | t.Fatalf("form POST=%d", rec.Code) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // In team mode a roster member with no account here chooses: 409, nothing |
| 151 | // made, then choice "new" makes the Home-only principal and signs in. |
| 152 | func TestTeamNativeMemberChooses(t *testing.T) { |
| 153 | f := newTeamRouterFixture(t) |
| 154 | rec, answer := f.nativeExchange(t, teamSubject('b'), nil) |
| 155 | if rec.Code != 409 || answer.State != "choose" { |
| 156 | t.Fatalf("member without a person=%d %s", rec.Code, rec.Body.String()) |
| 157 | } |
| 158 | if f.app.store.team.principals == nil { |
| 159 | t.Fatal("no principal store") |
| 160 | } |
| 161 | if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('b')); e == nil { |
| 162 | t.Fatal("409 allocated a principal") |
| 163 | } |
| 164 | rec, answer = f.nativeExchange(t, teamSubject('b'), map[string]string{"choice": "new"}) |
| 165 | if rec.Code != 200 || answer.State != "ok" { |
| 166 | t.Fatalf("choice new=%d %s", rec.Code, rec.Body.String()) |
| 167 | } |
| 168 | if list := f.nativeList(t, answer); list.Code != 200 { |
| 169 | t.Fatalf("native list=%d", list.Code) |
| 170 | } |
| 171 | // Non-members get the identical unknown, whatever they send. |
| 172 | for _, extra := range []map[string]string{nil, {"choice": "new"}} { |
| 173 | if rec, _ := f.nativeExchange(t, teamSubject('z'), extra); rec.Code != 403 || rec.Body.String() != `{"state":"unknown"}` { |
| 174 | t.Fatalf("non-member=%d %s", rec.Code, rec.Body.String()) |
| 175 | } |
| 176 | } |
| 177 | // The roster unavailable: 503, nothing made. |
| 178 | f.directory.change(nil, true) |
| 179 | f.clock.step(61 * time.Second) |
| 180 | if rec, _ := f.nativeExchange(t, teamSubject('c'), map[string]string{"choice": "new"}); rec.Code != 503 { |
| 181 | t.Fatalf("undecided=%d %s", rec.Code, rec.Body.String()) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func sessionRequest(a nativeAnswer) *http.Request { |
| 186 | r := httptest.NewRequest("GET", bridgeOrigin+"/", nil) |
| 187 | r.AddCookie(&http.Cookie{Name: a.Name, Value: a.Value}) |
| 188 | return r |
| 189 | } |
| 190 | |