| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // directoryCall is a native app's request to the directory. |
| 13 | func directoryCall(t *testing.T, f chooserFixture, method, path, body string, cookie *http.Cookie, headers map[string]string) *httptest.ResponseRecorder { |
| 14 | t.Helper() |
| 15 | r := httptest.NewRequest(method, directoryOrigin+path, strings.NewReader(body)) |
| 16 | if body != "" { |
| 17 | r.Header.Set("Content-Type", "application/json") |
| 18 | } |
| 19 | if cookie != nil { |
| 20 | r.AddCookie(cookie) |
| 21 | } |
| 22 | for k, v := range headers { |
| 23 | r.Header.Set(k, v) |
| 24 | } |
| 25 | w := httptest.NewRecorder() |
| 26 | f.p.directory.Router().ServeHTTP(w, r) |
| 27 | return w |
| 28 | } |
| 29 | |
| 30 | func exchangeBody(assertion, verifier string, extra ...string) string { |
| 31 | m := map[string]string{"assertion": assertion, "verifier": verifier} |
| 32 | for i := 0; i+1 < len(extra); i += 2 { |
| 33 | m[extra[i]] = extra[i+1] |
| 34 | } |
| 35 | b, _ := json.Marshal(m) |
| 36 | return string(b) |
| 37 | } |
| 38 | |
| 39 | // §9: assert for the directory, exchange it with the verifier, read the |
| 40 | // teams. Zero teams is an empty list. |
| 41 | func TestNativeDirectoryTeams(t *testing.T) { |
| 42 | t.Parallel() |
| 43 | f := newMountedChooser(t) |
| 44 | cred := f.p.credential |
| 45 | d := newDevice(t) |
| 46 | grant, _ := d.signInByCode(t, cred, "teams@example.invalid") |
| 47 | var person string |
| 48 | cred.db.Writer().QueryRow("SELECT person_ref FROM home_id_grants").Scan(&person) |
| 49 | |
| 50 | mint := func(seed byte) (string, string) { |
| 51 | verifier, challenge := verifierFor(seed) |
| 52 | w := d.assert(t, cred, grant, directoryOrigin, challenge) |
| 53 | if w.Code != 200 { |
| 54 | t.Fatal("assert", w.Code, w.Body.String()) |
| 55 | } |
| 56 | return decodeReply(t, w)["assertion"].(string), verifier |
| 57 | } |
| 58 | token, verifier := mint(10) |
| 59 | // A wrong verifier, a browser and a non-JSON body are refused. |
| 60 | wrongVerifier, _ := verifierFor(11) |
| 61 | if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, wrongVerifier), nil, nil); w.Code != 401 || decodeReply(t, w)["state"] != "failed" { |
| 62 | t.Fatal("wrong verifier", w.Code) |
| 63 | } |
| 64 | if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, map[string]string{"Origin": directoryOrigin}); w.Code != 400 || decodeReply(t, w)["state"] != "invalid" { |
| 65 | t.Fatal("a browser exchanged", w.Code) |
| 66 | } |
| 67 | w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, nil) |
| 68 | if w.Code != 200 || len(w.Result().Cookies()) != 0 { |
| 69 | t.Fatal("exchange", w.Code, w.Body.String(), w.Header()) |
| 70 | } |
| 71 | out := decodeReply(t, w) |
| 72 | if out["state"] != "ok" { |
| 73 | t.Fatal("state", out) |
| 74 | } |
| 75 | cookie := &http.Cookie{Name: out["name"].(string), Value: out["value"].(string)} |
| 76 | if exp := int64(out["expires_at"].(float64)); exp > time.Now().Unix()+7*day || exp < time.Now().Unix()+7*day-60 { |
| 77 | t.Fatal("native directory session is not seven days", exp) |
| 78 | } |
| 79 | // target is echoed, choice ignored, as connect's exchange does. |
| 80 | t2, v2 := mint(12) |
| 81 | w2 := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(t2, v2, "target", "/teams", "choice", "new"), nil, nil) |
| 82 | if w2.Code != 200 || decodeReply(t, w2)["target"] != "/teams" { |
| 83 | t.Fatal("target", w2.Code, w2.Body.String()) |
| 84 | } |
| 85 | if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(t2, v2, "choice", "old"), nil, nil); w.Code != 400 { |
| 86 | t.Fatal("unknown choice", w.Code) |
| 87 | } |
| 88 | // The same assertion twice is a replay. |
| 89 | if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, nil); w.Code != 401 { |
| 90 | t.Fatal("replayed exchange", w.Code) |
| 91 | } |
| 92 | |
| 93 | w = directoryCall(t, f, "GET", nativeTeamsPath, "", cookie, nil) |
| 94 | if w.Code != 200 || strings.TrimSpace(w.Body.String()) != "[]" || w.Header().Get("Cache-Control") != "no-store" { |
| 95 | t.Fatal("zero teams", w.Code, w.Body.String()) |
| 96 | } |
| 97 | f.team(t, "AAA111", "blue", "Blue team", person) |
| 98 | f.team(t, "BBB222", "green", "Not yours", chooserOther) |
| 99 | w = directoryCall(t, f, "GET", nativeTeamsPath, "", cookie, nil) |
| 100 | var teams []nativeTeam |
| 101 | if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &teams) != nil || len(teams) != 1 { |
| 102 | t.Fatal("teams", w.Code, w.Body.String()) |
| 103 | } |
| 104 | tm := teams[0] |
| 105 | if tm.SQID != "AAA111" || tm.Label != "blue" || tm.Name != "Blue team" || tm.Role != "member" || len(tm.Apps) != 1 || tm.Apps[0] != (nativeTeamApp{"docs", "https://blue.docs.oficina.works", "blue-docs"}) { |
| 106 | t.Fatalf("team %+v", tm) |
| 107 | } |
| 108 | // A browser cannot read it, nor a browser session. |
| 109 | if w := directoryCall(t, f, "GET", nativeTeamsPath, "", cookie, map[string]string{"Sec-Fetch-Site": "same-origin"}); w.Code != 403 { |
| 110 | t.Fatal("a browser read teams", w.Code) |
| 111 | } |
| 112 | if w := directoryCall(t, f, "GET", nativeTeamsPath, "", f.session(t, person), nil); w.Code != 401 { |
| 113 | t.Fatal("a browser session read the native list", w.Code) |
| 114 | } |
| 115 | if w := directoryCall(t, f, "GET", nativeTeamsPath, "", nil, nil); w.Code != 401 { |
| 116 | t.Fatal("anonymous teams", w.Code) |
| 117 | } |
| 118 | // Sign-out ends the native directory session, and only that. |
| 119 | if w := directoryCall(t, f, "POST", nativeDirectorySignout, "{}", cookie, nil); w.Code != 200 { |
| 120 | t.Fatal("directory signout", w.Code) |
| 121 | } |
| 122 | if w := directoryCall(t, f, "GET", nativeTeamsPath, "", cookie, nil); w.Code != 401 { |
| 123 | t.Fatal("a signed-out session read teams", w.Code) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // §12: the exchange accepts only native nonces. An assertion minted through |
| 128 | // /authorize, with a browser's nonce, does not redeem here. |
| 129 | func TestNativeDirectoryRefusesBrowserNonce(t *testing.T) { |
| 130 | t.Parallel() |
| 131 | f := newMountedChooser(t) |
| 132 | session := signInFully(t, f.p.credential, "web@example.invalid") |
| 133 | _, intent := f.p.start(t) |
| 134 | token, _ := f.p.mint(t, intent, session) |
| 135 | for seed := byte(0); seed < 4; seed++ { |
| 136 | verifier, _ := verifierFor(seed) |
| 137 | if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, nil); w.Code == 200 { |
| 138 | t.Fatal("a browser assertion redeemed natively") |
| 139 | } |
| 140 | } |
| 141 | // An assertion for another audience fails too. |
| 142 | d := newDevice(t) |
| 143 | grant, _ := d.signInByCode(t, f.p.credential, "aud@example.invalid") |
| 144 | verifier, challenge := verifierFor(20) |
| 145 | w := d.assert(t, f.p.credential, grant, "https://blue.docs.oficina.works", challenge) |
| 146 | if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(decodeReply(t, w)["assertion"].(string), verifier), nil, nil); w.Code != 401 { |
| 147 | t.Fatal("swapped audience redeemed", w.Code) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // The zero-teams help (§9, Paul's ruling H): help text only while Alta is |
| 152 | // blocked. |
| 153 | func TestNativeDirectoryNoTeamsHelp(t *testing.T) { |
| 154 | t.Parallel() |
| 155 | f := newMountedChooser(t) |
| 156 | w := directoryCall(t, f, "GET", noTeamsHelpPath, "", nil, nil) |
| 157 | if w.Code != 200 || !strings.Contains(w.Body.String(), "not on a team yet") || strings.Contains(w.Body.String(), "Create a team") { |
| 158 | t.Fatal("help", w.Code) |
| 159 | } |
| 160 | } |
| 161 | |