oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package idtest
2
3import (
4 "bytes"
5 "encoding/base64"
6 "encoding/json"
7 "net/http"
8 "net/http/httptest"
9 "net/url"
10 "strings"
11 "testing"
12 "time"
13
14 "amadan.net/oficina/equipo/internal/equipo"
15 "amadan.net/rastrillo/rastrillo/assertion"
16 "amadan.net/rastrillo/rastrillo/crypto"
17)
18
19func TestIDCallbackGuardAndPendingNonce(t *testing.T) {
20 for _, kind := range []string{"missing-nonce", "wrong-origin", "duplicate-origin", "duplicate-assertion", "wrong-method", "oversize", "replay"} {
21 t.Run(kind, func(t *testing.T) {
22 f := newFixture(t, true)
23 start := f.request("GET", "/auth/home", "", nil)
24 u, _ := url.Parse(start.Header().Get("Location"))
25 nonce := u.Query().Get("nonce")
26 if kind == "missing-nonce" {
27 nonce, _ = assertion.RandomID()
28 }
29 token := f.signed(t, nonce, person, time.Now())
30 form := url.Values{"assertion": {token}}
31 origin := f.issuer
32 method := "POST"
33 switch kind {
34 case "wrong-origin":
35 origin = "https://evil.example"
36 case "duplicate-assertion":
37 form.Add("assertion", token)
38 case "wrong-method":
39 method = "GET"
40 case "oversize":
41 form.Set("assertion", strings.Repeat("x", 13*1024))
42 }
43 var outCode int
44 if kind == "duplicate-origin" {
45 req := httptest.NewRequest("POST", f.origin+"/auth/assertion", strings.NewReader(form.Encode()))
46 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
47 req.Header.Add("Origin", origin)
48 req.Header.Add("Origin", origin)
49 out := httptest.NewRecorder()
50 f.h.ServeHTTP(out, req)
51 outCode = out.Code
52 } else {
53 out := f.request(method, "/auth/assertion", origin, form)
54 if kind == "replay" {
55 if out.Code != 303 {
56 t.Fatal(out.Code)
57 }
58 out = f.request(method, "/auth/assertion", origin, form)
59 }
60 outCode = out.Code
61 }
62 if outCode < 400 {
63 t.Fatal("bad callback accepted", kind, outCode)
64 }
65 var n int
66 if e := f.db.Writer().QueryRow("SELECT count(*) FROM oficina_handoff_inbox").Scan(&n); e != nil {
67 t.Fatal(e)
68 }
69 want := 0
70 if kind == "replay" {
71 want = 1
72 }
73 if n != want {
74 t.Fatal("invalid callback stored receipt", n)
75 }
76 })
77 }
78}
79func TestIDIdenticalAdmissionAndNoAccountSwitch(t *testing.T) {
80 for _, kind := range []string{"removed", "epoch", "lookup-error", "other-account"} {
81 t.Run(kind, func(t *testing.T) {
82 f := newFixture(t, true)
83 local := f.link(t)
84 done, _ := f.exchange(t, nil, nil, person)
85 home := cookie(t, done, "__Host-rastrillo_session")
86 switch kind {
87 case "removed":
88 f.db.Writer().Exec("UPDATE members SET active=0")
89 case "epoch":
90 f.db.Writer().Exec("UPDATE members SET session_epoch=?", time.Now().Unix()+1)
91 case "lookup-error":
92 f.db.Writer().Exec("ALTER TABLE members RENAME TO unavailable_members")
93 case "other-account":
94 f.db.Writer().Exec("INSERT INTO members(address,role,active,session_epoch) VALUES('other@example.invalid','member',1,0)")
95 other := f.local(t, "other@example.invalid")
96 out, _ := f.exchange(t, nil, other, person)
97 if out.Code < 400 {
98 t.Fatal("Home replaced another account")
99 }
100 return
101 }
102 for _, session := range []*http.Cookie{local, home} {
103 if out := f.request("GET", "/work", "", nil, session); out.Code == 200 {
104 t.Fatal("inadmissible session accepted", kind)
105 }
106 }
107 })
108 }
109}
110func TestIDConfigurationPins(t *testing.T) {
111 k, e := crypto.Generate()
112 if e != nil {
113 t.Fatal(e)
114 }
115 manifest, _ := json.Marshal(map[string]string{"old": base64.StdEncoding.EncodeToString(k.SignPub())})
116 env := map[string]string{"EQUIPO_ORIGIN": appOrigin, "EQUIPO_INSTANCE_KEY": "config-key", "CARLOS_SMTP_HOST": "smtp.example", "CARLOS_SMTP_FROM": "sender@example.invalid"}
117 get := func(k string) string { return env[k] }
118 cfg, e := equipo.LoadConfig(get)
119 if e != nil || cfg.Home != nil {
120 t.Fatal("default on", e)
121 }
122 env["EQUIPO_HOME_ISSUER"] = issuer
123 if _, e = equipo.LoadConfig(get); e == nil {
124 t.Fatal("partial config accepted")
125 }
126 env["EQUIPO_HOME_CONSUMER_ID"] = "equipo"
127 env["EQUIPO_HOME_KEYS_BASE64"] = base64.StdEncoding.EncodeToString(manifest)
128 env["EQUIPO_HOME_DEK_BASE64"] = base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{7}, 32))
129 cfg, e = equipo.LoadConfig(get)
130 if e != nil || cfg.Home == nil {
131 t.Fatal("valid pins", e)
132 }
133 newer, e := crypto.Generate()
134 if e != nil {
135 t.Fatal(e)
136 }
137 keys := map[string][]byte{"old": k.SignPub(), "new": newer.SignPub()}
138 now := time.Now()
139 id, _ := assertion.RandomID()
140 nonce, _ := assertion.RandomID()
141 claims := assertion.Claims{Version: 1, KeyID: "new", Issuer: issuer, Audience: appOrigin, Subject: person, ID: id, Nonce: nonce, IssuedAt: now.Unix(), ExpiresAt: now.Unix() + 120, AuthTime: now.Unix()}
142 token, e := assertion.Sign(newer, claims)
143 if e != nil {
144 t.Fatal(e)
145 }
146 old, e := assertion.NewVerifier(issuer, appOrigin, cfg.Home.Keys)
147 if e != nil {
148 t.Fatal(e)
149 }
150 if _, e = old.Verify(token, now); e == nil {
151 t.Fatal("unknown pin accepted")
152 }
153 both, e := assertion.NewVerifier(issuer, appOrigin, keys)
154 if e != nil {
155 t.Fatal(e)
156 }
157 if _, e = both.Verify(token, now); e != nil {
158 t.Fatal("rotation pins", e)
159 }
160 manifest = []byte(`{"old":"bad","old":"bad"}`)
161 env["EQUIPO_HOME_KEYS_BASE64"] = base64.StdEncoding.EncodeToString(manifest)
162 if _, e = equipo.LoadConfig(get); e == nil {
163 t.Fatal("duplicate pin accepted")
164 }
165}
166