oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "encoding/base64"
5 "encoding/json"
6 "net/http/httptest"
7 "slices"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/home/internal/home/sealtest"
12 "amadan.net/rastrillo/rastrillo/webauthn/authtest"
13)
14
15// finishBody is the web finish fields plus what the app adds.
16func finishBody(d *device, body map[string]string) map[string]string {
17 body["device_key"] = d.public()
18 body["platform"] = d.platform
19 body["userHandle"] = "ignored-handle"
20 return body
21}
22
23func nativeChallenge(t *testing.T, w *httptest.ResponseRecorder) ([]byte, []string) {
24 t.Helper()
25 if w.Code != 200 {
26 t.Fatalf("begin: %d %s", w.Code, w.Body.String())
27 }
28 var out struct {
29 Challenge string `json:"challenge"`
30 RPID string `json:"rp_id"`
31 Credentials []string `json:"credentials"`
32 }
33 if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil || out.RPID != credentialHost {
34 t.Fatal("begin reply", w.Body.String())
35 }
36 raw, err := base64.RawURLEncoding.DecodeString(out.Challenge)
37 if err != nil {
38 t.Fatal(err)
39 }
40 return raw, out.Credentials
41}
42
43// §5: "Sign in with a passkey" needs no email; Home verifies the platform
44// ceremony exactly as the web does and mints a passkey grant.
45func TestNativePasskeySignIn(t *testing.T) {
46 t.Parallel()
47 app := newNativeApp(t)
48 session := signInFully(t, app, sealtest.CanaryAddress)
49 key := enrol(t, app, session)
50 d := newDevice(t)
51 challenge, creds := nativeChallenge(t, d.call(t, app, nativePrefix+"/passkey/begin", map[string]string{"device_key": d.public(), "platform": "ios"}, ""))
52 if len(creds) != 0 {
53 t.Fatal("a discoverable begin named credentials")
54 }
55 zero := uint32(0)
56 body := finishBody(d, assertionBody(key, challenge, authtest.Options{RPID: credentialHost, Origin: Origin, Count: &zero}))
57 // Bound to the device key: another key cannot finish it.
58 if w := newDevice(t).call(t, app, nativePrefix+"/passkey/finish", body, ""); w.Code != 401 {
59 t.Fatal("another device finished the ceremony", w.Code)
60 }
61 // The body's device_key must be the bound key.
62 swapped := map[string]string{}
63 for k, v := range body {
64 swapped[k] = v
65 }
66 swapped["device_key"] = newDevice(t).public()
67 if w := d.call(t, app, nativePrefix+"/passkey/finish", swapped, ""); w.Code != 401 {
68 t.Fatal("a body naming another key finished", w.Code)
69 }
70 w := d.call(t, app, nativePrefix+"/passkey/finish", body, "")
71 out := decodeReply(t, w)
72 if w.Code != 200 || out["grant"] == nil || out["identity_id"] != app.identityID(subjectOf(t, app, session)) {
73 t.Fatal("finish", w.Code, w.Body.String())
74 }
75 var method, platform string
76 app.db.Writer().QueryRow("SELECT method,platform FROM home_id_grants").Scan(&method, &platform)
77 if method != "passkey" || platform != "ios" {
78 t.Fatal("grant", method, platform)
79 }
80 // Single use.
81 if w := d.call(t, app, nativePrefix+"/passkey/finish", body, ""); w.Code == 200 {
82 t.Fatal("a challenge was used twice")
83 }
84 sealtest.AssertNoPlaintext(t, app.dbPath)
85}
86
87// §5: the exact RP, origin and user-verification checks, and a failed
88// ceremony still spends its challenge.
89func TestNativePasskeyRejectsBadCeremonies(t *testing.T) {
90 t.Parallel()
91 for _, kind := range []string{"origin", "rp", "uv", "signature", "unknown"} {
92 t.Run(kind, func(t *testing.T) {
93 app := newNativeApp(t)
94 session := signInFully(t, app, sealtest.CanaryAddress)
95 key := enrol(t, app, session)
96 d := newDevice(t)
97 challenge, _ := nativeChallenge(t, d.call(t, app, nativePrefix+"/passkey/begin", map[string]string{"device_key": d.public(), "platform": "macos"}, ""))
98 zero := uint32(0)
99 opts := authtest.Options{RPID: credentialHost, Origin: Origin, Count: &zero}
100 switch kind {
101 case "origin":
102 opts.Origin = "https://oficina.works"
103 case "rp":
104 opts.RPID = "oficina.works"
105 case "uv":
106 opts.Flags = authtest.FlagUserPresent
107 case "signature":
108 opts.CorruptSig = true
109 case "unknown":
110 key, _ = authtest.New()
111 }
112 w := d.call(t, app, nativePrefix+"/passkey/finish", finishBody(d, assertionBody(key, challenge, opts)), "")
113 if w.Code == 200 {
114 t.Fatal("bad ceremony minted a grant")
115 }
116 good := finishBody(d, assertionBody(key, challenge, authtest.Options{RPID: credentialHost, Origin: Origin, Count: &zero}))
117 if w := d.call(t, app, nativePrefix+"/passkey/finish", good, ""); w.Code == 200 {
118 t.Fatal("a refused challenge was reusable")
119 }
120 if n := count(t, app, "SELECT count(*) FROM home_id_grants"); n != 0 {
121 t.Fatal("grant row after a refusal")
122 }
123 })
124 }
125}
126
127// §4 + §5: a code on an enrolled account, finished with that person's
128// passkey, mints an email+passkey grant; another person's passkey cannot.
129func TestNativePendingFinishedByPasskey(t *testing.T) {
130 t.Parallel()
131 app := newNativeApp(t)
132 session := signInFully(t, app, sealtest.CanaryAddress)
133 key := enrol(t, app, session)
134 otherSession := signInFully(t, app, "other@example.invalid")
135 otherKey := enrol(t, app, otherSession)
136 d := newDevice(t)
137 start := d.start(t, app, sealtest.CanaryAddress)
138 pending := decodeReply(t, d.verify(t, app, start, lastCode(t, app)))["pending"].(string)
139 e := newDevice(t)
140 if w := e.call(t, app, nativePrefix+"/passkey/begin", map[string]string{"pending": pending, "device_key": e.public(), "platform": "macos"}, ""); w.Code != 401 {
141 t.Fatal("another key began a pending", w.Code)
142 }
143 challenge, creds := nativeChallenge(t, d.call(t, app, nativePrefix+"/passkey/begin", map[string]string{"pending": pending, "device_key": d.public(), "platform": "macos"}, ""))
144 if len(creds) != 1 || creds[0] != base64.RawURLEncoding.EncodeToString(key.CredID) {
145 t.Fatal("pending begin is not restricted to the person", creds)
146 }
147 zero := uint32(0)
148 if w := d.call(t, app, nativePrefix+"/passkey/finish", finishBody(d, assertionBody(otherKey, challenge, authtest.Options{RPID: credentialHost, Origin: Origin, Count: &zero})), ""); w.Code == 200 {
149 t.Fatal("another person's passkey finished the pending")
150 }
151 challenge, _ = nativeChallenge(t, d.call(t, app, nativePrefix+"/passkey/begin", map[string]string{"pending": pending, "device_key": d.public(), "platform": "macos"}, ""))
152 w := d.call(t, app, nativePrefix+"/passkey/finish", finishBody(d, assertionBody(key, challenge, authtest.Options{RPID: credentialHost, Origin: Origin, Count: &zero})), "")
153 if w.Code != 200 || decodeReply(t, w)["grant"] == nil {
154 t.Fatal("pending finish", w.Code, w.Body.String())
155 }
156 var method string
157 app.db.Writer().QueryRow("SELECT method FROM home_id_grants").Scan(&method)
158 if method != "email+passkey" {
159 t.Fatal("method", method)
160 }
161 if n := count(t, app, "SELECT count(*) FROM home_id_native_pending"); n != 0 {
162 t.Fatal("pending survived")
163 }
164}
165
166// §13 Domains: the association file's content, and nothing else in it.
167func TestNativeAssociationFile(t *testing.T) {
168 t.Parallel()
169 app := newNativeApp(t)
170 r := httptest.NewRequest("GET", Origin+aasaPath, nil)
171 w := httptest.NewRecorder()
172 app.Router().ServeHTTP(w, r)
173 if w.Code != 200 || w.Header().Get("Content-Type") != "application/json" || w.Header().Get("Location") != "" {
174 t.Fatal("aasa", w.Code, w.Header())
175 }
176 var got map[string]map[string][]string
177 if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
178 t.Fatal(err)
179 }
180 want := []string{"9U4A84C4WK.works.oficina.calls", "9U4A84C4WK.works.oficina.docs", "9U4A84C4WK.works.oficina.sheets", "9U4A84C4WK.works.oficina.calendar", "9U4A84C4WK.works.oficina.memoria", "9U4A84C4WK.works.oficina.equipo", "9U4A84C4WK.works.oficina.base"}
181 if len(got) != 1 || len(got["webcredentials"]) != 1 || strings.Join(got["webcredentials"]["apps"], ",") != strings.Join(want, ",") {
182 t.Fatal("aasa content", w.Body.String())
183 }
184 // Base's native app signs in with Oficina ID too (Base native slice 1).
185 if !slices.Contains(got["webcredentials"]["apps"], "9U4A84C4WK.works.oficina.base") {
186 t.Fatal("Base's App ID missing", w.Body.String())
187 }
188 if strings.Contains(w.Body.String(), "applinks") {
189 t.Fatal("applinks in slice A")
190 }
191 // Only the credential origin serves it.
192 r = httptest.NewRequest("GET", directoryOrigin+aasaPath, nil)
193 w = httptest.NewRecorder()
194 app.Router().ServeHTTP(w, r)
195 if w.Code == 200 {
196 t.Fatal("served on the directory host")
197 }
198}
199
200// §4: every native endpoint refuses browser evidence and non-JSON, and
201// renders nothing.
202func TestNativeEndpointsRefuseBrowsers(t *testing.T) {
203 t.Parallel()
204 app := newNativeApp(t)
205 for _, path := range []string{"/code/start", "/code/resend", "/code/verify", "/passkey/begin", "/passkey/finish", "/assert", "/signout"} {
206 for _, h := range []map[string]string{{"Origin": Origin}, {"Sec-Fetch-Site": "same-origin"}, {"Content-Type": "application/x-www-form-urlencoded"}} {
207 r := httptest.NewRequest("POST", Origin+nativePrefix+path, strings.NewReader("{}"))
208 r.Header.Set("Content-Type", "application/json")
209 for k, v := range h {
210 r.Header.Set(k, v)
211 }
212 w := httptest.NewRecorder()
213 app.Router().ServeHTTP(w, r)
214 if w.Code < 400 || w.Header().Get("Content-Type") != "application/json" || w.Header().Get("Cache-Control") != "no-store" || strings.Contains(w.Body.String(), "<") {
215 t.Fatal(path, h, w.Code, w.Header())
216 }
217 }
218 }
219 // The legacy role mounts none of it.
220 legacy := newTestApp(t)
221 r := httptest.NewRequest("POST", Origin+nativePrefix+"/code/start", strings.NewReader("{}"))
222 r.Header.Set("Content-Type", "application/json")
223 w := httptest.NewRecorder()
224 legacy.Router().ServeHTTP(w, r)
225 if w.Code != 404 && w.Code != 405 {
226 t.Fatal("legacy role served native", w.Code)
227 }
228}
229