| 1 | package oficina |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "os" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/oficina/equipo/internal/equipo/oficina/oficinatest" |
| 17 | "amadan.net/rastrillo/rastrillo/budget" |
| 18 | ) |
| 19 | |
| 20 | func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } |
| 21 | |
| 22 | // memStore is a Store in memory that records its audit. |
| 23 | type memStore struct { |
| 24 | mu sync.Mutex |
| 25 | sealed map[string]string |
| 26 | audit []string |
| 27 | saves int |
| 28 | fail bool |
| 29 | } |
| 30 | |
| 31 | func (m *memStore) Load(_ context.Context, person string) (string, error) { |
| 32 | m.mu.Lock() |
| 33 | defer m.mu.Unlock() |
| 34 | return m.sealed[person], nil |
| 35 | } |
| 36 | |
| 37 | func (m *memStore) Save(_ context.Context, person, sealed, audit string) error { |
| 38 | m.mu.Lock() |
| 39 | defer m.mu.Unlock() |
| 40 | if m.fail { |
| 41 | return errors.New("disk full") |
| 42 | } |
| 43 | m.sealed[person] = sealed |
| 44 | m.saves++ |
| 45 | if audit != "" { |
| 46 | m.audit = append(m.audit, person+" "+audit) |
| 47 | } |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | func (m *memStore) Delete(_ context.Context, person, audit, detail string) error { |
| 52 | m.mu.Lock() |
| 53 | defer m.mu.Unlock() |
| 54 | delete(m.sealed, person) |
| 55 | m.audit = append(m.audit, strings.TrimSpace(person+" "+audit+" "+detail)) |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | func (m *memStore) Audit(_ context.Context, person, kind, detail string) error { |
| 60 | m.mu.Lock() |
| 61 | defer m.mu.Unlock() |
| 62 | m.audit = append(m.audit, strings.TrimSpace(person+" "+kind+" "+detail)) |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | func (m *memStore) audits() string { |
| 67 | m.mu.Lock() |
| 68 | defer m.mu.Unlock() |
| 69 | return strings.Join(m.audit, "\n") |
| 70 | } |
| 71 | |
| 72 | // sealer seals by encoding with the domain, so a test can open what was |
| 73 | // stored and see it is not the token as it is. |
| 74 | type sealer struct{} |
| 75 | |
| 76 | func (sealer) Seal(domain string, v any) (string, error) { |
| 77 | b, err := json.Marshal(v) |
| 78 | return "sealed:" + base64.RawURLEncoding.EncodeToString(append([]byte(domain+"|"), b...)), err |
| 79 | } |
| 80 | |
| 81 | func (sealer) Open(domain, raw string, v any) error { |
| 82 | b, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(raw, "sealed:")) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | rest, ok := strings.CutPrefix(string(b), domain+"|") |
| 87 | if !ok { |
| 88 | return errors.New("wrong domain") |
| 89 | } |
| 90 | return json.Unmarshal([]byte(rest), v) |
| 91 | } |
| 92 | |
| 93 | type clock struct { |
| 94 | mu sync.Mutex |
| 95 | t time.Time |
| 96 | } |
| 97 | |
| 98 | func (c *clock) now() time.Time { |
| 99 | c.mu.Lock() |
| 100 | defer c.mu.Unlock() |
| 101 | return c.t |
| 102 | } |
| 103 | |
| 104 | func (c *clock) add(d time.Duration) { |
| 105 | c.mu.Lock() |
| 106 | c.t = c.t.Add(d) |
| 107 | c.mu.Unlock() |
| 108 | } |
| 109 | |
| 110 | type rig struct { |
| 111 | fake *oficinatest.Connect |
| 112 | store *memStore |
| 113 | clock *clock |
| 114 | c *Connector |
| 115 | } |
| 116 | |
| 117 | const redirect = "https://equipo.example.com/oficina/connect/callback" |
| 118 | |
| 119 | func newRig(t *testing.T) *rig { |
| 120 | t.Helper() |
| 121 | fake := oficinatest.Start(t) |
| 122 | fake.Redirect = redirect |
| 123 | r := &rig{fake: fake, store: &memStore{sealed: map[string]string{}}, clock: &clock{t: time.Now()}} |
| 124 | c, err := New(Config{Origin: fake.URL, ClientID: fake.ClientID, Redirect: redirect, Now: r.clock.now}, r.store, sealer{}) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | r.c = c |
| 129 | return r |
| 130 | } |
| 131 | |
| 132 | // consent starts the flow for person and follows connect's consent, |
| 133 | // which approves itself, to the callback's query. |
| 134 | func (r *rig) consent(t *testing.T, person string) url.Values { |
| 135 | t.Helper() |
| 136 | start, err := r.c.Start(context.Background(), person, "https://base.example.com/ask") |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} |
| 141 | res, err := client.Get(start) |
| 142 | if err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | res.Body.Close() |
| 146 | loc, err := url.Parse(res.Header.Get("Location")) |
| 147 | if res.StatusCode != http.StatusSeeOther || err != nil || !strings.HasPrefix(loc.String(), redirect+"?") { |
| 148 | t.Fatalf("consent: %d %q", res.StatusCode, res.Header.Get("Location")) |
| 149 | } |
| 150 | return loc.Query() |
| 151 | } |
| 152 | |
| 153 | func (r *rig) connect(t *testing.T, person string) { |
| 154 | t.Helper() |
| 155 | q := r.consent(t, person) |
| 156 | res, err := r.c.Peek(context.Background(), q) |
| 157 | if err != nil || res.Person != person || res.ReturnTo != "https://base.example.com/ask" { |
| 158 | t.Fatalf("peek: %+v %v", res, err) |
| 159 | } |
| 160 | if err := r.c.Finish(context.Background(), person, q.Get("state"), q.Get("code")); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // refreshToken is the person's refresh token as stored, opened. |
| 166 | func (r *rig) refreshToken(t *testing.T, person string) string { |
| 167 | t.Helper() |
| 168 | sealed := r.store.sealed[person] |
| 169 | if sealed == "" { |
| 170 | return "" |
| 171 | } |
| 172 | var tok string |
| 173 | if err := (sealer{}).Open(Domain, sealed, &tok); err != nil { |
| 174 | t.Fatal(err) |
| 175 | } |
| 176 | return tok |
| 177 | } |
| 178 | |
| 179 | // TestConnectPKCE: the authorization request is a public client's with |
| 180 | // S256 PKCE for connect's resource, asking for every read and write |
| 181 | // scope connect offers and offline_access, never a share scope. The code |
| 182 | // is exchanged with the verifier (the fake checks it), the refresh token |
| 183 | // stored sealed and the access token used. |
| 184 | func TestConnectPKCE(t *testing.T) { |
| 185 | r := newRig(t) |
| 186 | start, err := r.c.Start(context.Background(), "ana@example.com", "") |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | u, _ := url.Parse(start) |
| 191 | q := u.Query() |
| 192 | if u.Scheme+"://"+u.Host != r.fake.URL || u.Path != "/oauth/authorize" || q.Get("client_id") != r.fake.ClientID || q.Get("redirect_uri") != redirect || |
| 193 | q.Get("response_type") != "code" || q.Get("code_challenge_method") != "S256" || q.Get("resource") != r.fake.URL+"/mcp" || len(q.Get("state")) < 32 || len(q.Get("code_challenge")) != 43 { |
| 194 | t.Fatalf("authorize: %s", start) |
| 195 | } |
| 196 | scopes := " " + q.Get("scope") + " " |
| 197 | for _, want := range []string{" offline_access ", " calendar:read ", " calendar:write ", " docs:write "} { |
| 198 | if !strings.Contains(scopes, want) { |
| 199 | t.Errorf("scope %q missing: %s", want, scopes) |
| 200 | } |
| 201 | } |
| 202 | if strings.Contains(scopes, "share") { |
| 203 | t.Errorf("a share scope asked for: %s", scopes) |
| 204 | } |
| 205 | |
| 206 | r.connect(t, "ana@example.com") |
| 207 | if ex, _ := r.fake.Counts(); ex != 1 { |
| 208 | t.Errorf("exchanges %d", ex) |
| 209 | } |
| 210 | refresh := r.refreshToken(t, "ana@example.com") |
| 211 | if refresh == "" || strings.Contains(r.store.sealed["ana@example.com"], refresh) { |
| 212 | t.Fatalf("refresh token not stored sealed: %q", r.store.sealed["ana@example.com"]) |
| 213 | } |
| 214 | origin, tok, ok, err := r.c.Host(context.Background(), "ana@example.com") |
| 215 | if err != nil || !ok || origin != r.fake.URL || !r.fake.Live(tok) { |
| 216 | t.Fatalf("host: %q %v %v", origin, ok, err) |
| 217 | } |
| 218 | if _, refreshes := r.fake.Counts(); refreshes != 0 { |
| 219 | t.Errorf("the first access token was not cached: %d refreshes", refreshes) |
| 220 | } |
| 221 | if ok, _ := r.c.Connected(context.Background(), "ana@example.com"); !ok { |
| 222 | t.Error("not connected") |
| 223 | } |
| 224 | if !strings.Contains(r.store.audits(), "ana@example.com oficina.connect") { |
| 225 | t.Errorf("audit: %s", r.store.audits()) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // TestConnectState: the callback's check (Peek) needs a state Start |
| 230 | // issued, current, from connect's issuer, and exchanges nothing; only |
| 231 | // the person who started the consent can finish it, once. |
| 232 | func TestConnectState(t *testing.T) { |
| 233 | r := newRig(t) |
| 234 | ctx := context.Background() |
| 235 | q := r.consent(t, "ana@example.com") |
| 236 | for range 2 { |
| 237 | if res, err := r.c.Peek(ctx, q); err != nil || res.Person != "ana@example.com" { |
| 238 | t.Fatalf("peek: %+v %v", res, err) |
| 239 | } |
| 240 | } |
| 241 | if ex, _ := r.fake.Counts(); ex != 0 || len(r.store.sealed) != 0 { |
| 242 | t.Fatalf("the callback's check exchanged %d, stored %v", ex, r.store.sealed) |
| 243 | } |
| 244 | // Another person's bearer: refused, and the state is used up. |
| 245 | if err := r.c.Finish(ctx, "bo@example.com", q.Get("state"), q.Get("code")); !errors.Is(err, ErrNotYours) { |
| 246 | t.Errorf("another person: %v", err) |
| 247 | } |
| 248 | if err := r.c.Finish(ctx, "ana@example.com", q.Get("state"), q.Get("code")); !errors.Is(err, ErrExpired) { |
| 249 | t.Errorf("after another person tried: %v", err) |
| 250 | } |
| 251 | if ex, _ := r.fake.Counts(); ex != 0 || len(r.store.sealed) != 0 { |
| 252 | t.Errorf("exchanged %d, stored %v", ex, r.store.sealed) |
| 253 | } |
| 254 | if a := r.store.audits(); !strings.Contains(a, "bo@example.com oficina.connect_failed not_theirs") { |
| 255 | t.Errorf("audit: %s", a) |
| 256 | } |
| 257 | |
| 258 | q = r.consent(t, "ana@example.com") |
| 259 | if err := r.c.Finish(ctx, "ana@example.com", q.Get("state"), q.Get("code")); err != nil { |
| 260 | t.Fatal(err) |
| 261 | } |
| 262 | if err := r.c.Finish(ctx, "ana@example.com", q.Get("state"), q.Get("code")); !errors.Is(err, ErrExpired) { |
| 263 | t.Errorf("a replayed finish: %v", err) |
| 264 | } |
| 265 | if res, err := r.c.Peek(ctx, q); !errors.Is(err, ErrExpired) || res.Person != "" { |
| 266 | t.Errorf("a used state: %+v %v", res, err) |
| 267 | } |
| 268 | forged := url.Values{"state": {random()}, "code": {"x"}, "iss": {r.fake.URL}} |
| 269 | if res, err := r.c.Peek(ctx, forged); !errors.Is(err, ErrExpired) || res.Person != "" { |
| 270 | t.Errorf("a state never issued: %+v %v", res, err) |
| 271 | } |
| 272 | |
| 273 | q = r.consent(t, "bo@example.com") |
| 274 | q.Set("iss", "https://elsewhere.example.com") |
| 275 | if res, err := r.c.Peek(ctx, q); !errors.Is(err, ErrExpired) || res.Person != "bo@example.com" || res.ReturnTo == "" { |
| 276 | t.Errorf("another issuer: %+v %v", res, err) |
| 277 | } |
| 278 | if err := r.c.Finish(ctx, "bo@example.com", q.Get("state"), q.Get("code")); !errors.Is(err, ErrExpired) { |
| 279 | t.Errorf("a state that failed the check: %v", err) |
| 280 | } |
| 281 | |
| 282 | q = r.consent(t, "bo@example.com") |
| 283 | r.clock.add(11 * time.Minute) |
| 284 | if _, err := r.c.Peek(ctx, q); !errors.Is(err, ErrExpired) { |
| 285 | t.Errorf("an old state: %v", err) |
| 286 | } |
| 287 | |
| 288 | r.fake.Deny = true |
| 289 | q = r.consent(t, "bo@example.com") |
| 290 | if _, err := r.c.Peek(ctx, q); !errors.Is(err, ErrDenied) { |
| 291 | t.Errorf("a refusal: %v", err) |
| 292 | } |
| 293 | if r.store.sealed["bo@example.com"] != "" { |
| 294 | t.Errorf("stored %v", r.store.sealed) |
| 295 | } |
| 296 | if a := r.store.audits(); !strings.Contains(a, "bo@example.com oficina.connect_failed access_denied") || !strings.Contains(a, "bo@example.com oficina.connect_failed state") { |
| 297 | t.Errorf("audit: %s", a) |
| 298 | } |
| 299 | |
| 300 | // A code the fake refuses stores nothing. |
| 301 | q = r.consent(t, "bo@example.com") |
| 302 | if err := r.c.Finish(ctx, "bo@example.com", q.Get("state"), "not-the-code"); !errors.Is(err, ErrExpired) || r.store.sealed["bo@example.com"] != "" { |
| 303 | t.Errorf("a bad code: %v %v", err, r.store.sealed) |
| 304 | } |
| 305 | if a := r.store.audits(); !strings.Contains(a, "oficina.connect_failed invalid_grant") { |
| 306 | t.Errorf("audit: %s", a) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // TestConnectHostRejectedRace: Rejected while Host runs never makes Host |
| 311 | // answer ok without a token (run with -race). |
| 312 | func TestConnectHostRejectedRace(t *testing.T) { |
| 313 | r := newRig(t) |
| 314 | r.connect(t, "ana@example.com") |
| 315 | done := make(chan struct{}) |
| 316 | var rejecting sync.WaitGroup |
| 317 | for range 4 { |
| 318 | rejecting.Add(1) |
| 319 | go func() { |
| 320 | defer rejecting.Done() |
| 321 | for { |
| 322 | select { |
| 323 | case <-done: |
| 324 | return |
| 325 | default: |
| 326 | r.c.Rejected("ana@example.com") |
| 327 | } |
| 328 | } |
| 329 | }() |
| 330 | } |
| 331 | var hosts sync.WaitGroup |
| 332 | for range 4 { |
| 333 | hosts.Add(1) |
| 334 | go func() { |
| 335 | defer hosts.Done() |
| 336 | for range 200 { |
| 337 | if _, tok, ok, err := r.c.Host(context.Background(), "ana@example.com"); err != nil || !ok || tok == "" { |
| 338 | t.Errorf("host: %q %v %v", tok, ok, err) |
| 339 | return |
| 340 | } |
| 341 | } |
| 342 | }() |
| 343 | } |
| 344 | hosts.Wait() |
| 345 | close(done) |
| 346 | rejecting.Wait() |
| 347 | } |
| 348 | |
| 349 | // TestConnectRefreshRotates: an expired access token is refreshed, the |
| 350 | // rotated refresh token replaces the stored one, and the consumed one is |
| 351 | // never sent again (connect would revoke the connection). |
| 352 | func TestConnectRefreshRotates(t *testing.T) { |
| 353 | r := newRig(t) |
| 354 | ctx := context.Background() |
| 355 | r.connect(t, "ana@example.com") |
| 356 | first := r.refreshToken(t, "ana@example.com") |
| 357 | _, before, _, _ := r.c.Host(ctx, "ana@example.com") |
| 358 | for i := 1; i <= 3; i++ { |
| 359 | r.clock.add(5 * time.Minute) |
| 360 | _, tok, ok, err := r.c.Host(ctx, "ana@example.com") |
| 361 | if err != nil || !ok || tok == before || !r.fake.Live(tok) { |
| 362 | t.Fatalf("refresh %d: %v %v", i, ok, err) |
| 363 | } |
| 364 | before = tok |
| 365 | if _, n := r.fake.Counts(); n != i { |
| 366 | t.Errorf("refreshes %d, want %d", n, i) |
| 367 | } |
| 368 | } |
| 369 | now := r.refreshToken(t, "ana@example.com") |
| 370 | if now == first || r.fake.Live(first) || !r.fake.Live(now) { |
| 371 | t.Errorf("not rotated: first live %v, now live %v", r.fake.Live(first), r.fake.Live(now)) |
| 372 | } |
| 373 | // Rejected drops the cached access token: the next Host refreshes. |
| 374 | r.c.Rejected("ana@example.com") |
| 375 | if _, tok, ok, _ := r.c.Host(ctx, "ana@example.com"); !ok || tok == before { |
| 376 | t.Error("a rejected token was kept") |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // TestConnectRefreshOnce: callers at once share one refresh. |
| 381 | func TestConnectRefreshOnce(t *testing.T) { |
| 382 | r := newRig(t) |
| 383 | r.connect(t, "ana@example.com") |
| 384 | r.clock.add(5 * time.Minute) |
| 385 | var wg sync.WaitGroup |
| 386 | for range 10 { |
| 387 | wg.Add(1) |
| 388 | go func() { |
| 389 | defer wg.Done() |
| 390 | if _, _, ok, err := r.c.Host(context.Background(), "ana@example.com"); !ok || err != nil { |
| 391 | t.Errorf("host: %v %v", ok, err) |
| 392 | } |
| 393 | }() |
| 394 | } |
| 395 | wg.Wait() |
| 396 | if _, n := r.fake.Counts(); n != 1 { |
| 397 | t.Errorf("refreshes %d", n) |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // TestConnectRefreshRevoked: a connection revoked at connect is |
| 402 | // forgotten on the next refresh, audited without a token. |
| 403 | func TestConnectRefreshRevoked(t *testing.T) { |
| 404 | r := newRig(t) |
| 405 | ctx := context.Background() |
| 406 | r.connect(t, "ana@example.com") |
| 407 | r.fake.RevokeAll() |
| 408 | r.clock.add(5 * time.Minute) |
| 409 | if _, _, ok, err := r.c.Host(ctx, "ana@example.com"); ok || err != nil { |
| 410 | t.Fatalf("revoked: %v %v", ok, err) |
| 411 | } |
| 412 | if ok, _ := r.c.Connected(ctx, "ana@example.com"); ok || len(r.store.sealed) != 0 { |
| 413 | t.Error("a revoked connection is kept") |
| 414 | } |
| 415 | if a := r.store.audits(); !strings.Contains(a, "ana@example.com oficina.refresh_failed invalid_grant") { |
| 416 | t.Errorf("audit: %s", a) |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // TestConnectRefreshUnavailable: connect down is an error and keeps the |
| 421 | // connection; a rotated token that could not be stored is still used. |
| 422 | func TestConnectRefreshUnavailable(t *testing.T) { |
| 423 | r := newRig(t) |
| 424 | ctx := context.Background() |
| 425 | r.connect(t, "ana@example.com") |
| 426 | r.fake.Down = true |
| 427 | r.clock.add(5 * time.Minute) |
| 428 | if _, _, _, err := r.c.Host(ctx, "ana@example.com"); err == nil { |
| 429 | t.Fatal("no error with connect down") |
| 430 | } |
| 431 | if ok, _ := r.c.Connected(ctx, "ana@example.com"); !ok { |
| 432 | t.Error("forgotten while connect was down") |
| 433 | } |
| 434 | if a := r.store.audits(); !strings.Contains(a, "oficina.refresh_failed unavailable") { |
| 435 | t.Errorf("audit: %s", a) |
| 436 | } |
| 437 | r.fake.Down = false |
| 438 | r.store.fail = true |
| 439 | if _, _, ok, err := r.c.Host(ctx, "ana@example.com"); ok || err == nil { |
| 440 | t.Errorf("a rotation that could not be stored: %v %v", ok, err) |
| 441 | } |
| 442 | r.store.fail = false |
| 443 | r.c.Rejected("ana@example.com") |
| 444 | if _, tok, ok, err := r.c.Host(ctx, "ana@example.com"); !ok || err != nil || !r.fake.Live(tok) { |
| 445 | t.Fatalf("after the store recovered: %v %v", ok, err) |
| 446 | } |
| 447 | if !r.fake.Live(r.refreshToken(t, "ana@example.com")) { |
| 448 | t.Error("the stored refresh token is not the current one") |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // TestConnectDisconnect revokes at connect and forgets the connection. |
| 453 | func TestConnectDisconnect(t *testing.T) { |
| 454 | r := newRig(t) |
| 455 | ctx := context.Background() |
| 456 | r.connect(t, "ana@example.com") |
| 457 | refresh := r.refreshToken(t, "ana@example.com") |
| 458 | if err := r.c.Disconnect(ctx, "ana@example.com"); err != nil { |
| 459 | t.Fatal(err) |
| 460 | } |
| 461 | if got := r.fake.Revoked(); len(got) != 1 || got[0] != refresh || r.fake.Live(refresh) { |
| 462 | t.Errorf("revoked %v", got) |
| 463 | } |
| 464 | if _, _, ok, _ := r.c.Host(ctx, "ana@example.com"); ok || len(r.store.sealed) != 0 { |
| 465 | t.Error("still connected") |
| 466 | } |
| 467 | if a := r.store.audits(); !strings.Contains(a, "ana@example.com oficina.disconnect") { |
| 468 | t.Errorf("audit: %s", a) |
| 469 | } |
| 470 | // Disconnecting with nothing connected changes nothing. |
| 471 | if err := r.c.Disconnect(ctx, "ana@example.com"); err != nil || len(r.fake.Revoked()) != 1 { |
| 472 | t.Errorf("second disconnect: %v %v", err, r.fake.Revoked()) |
| 473 | } |
| 474 | if a := r.store.audits(); strings.Contains(a, refresh) { |
| 475 | t.Errorf("a token in the audit: %s", a) |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // TestConnectConfig: an origin must be https (http only on loopback), |
| 480 | // with a client id and an https redirect. |
| 481 | func TestConnectConfig(t *testing.T) { |
| 482 | for _, cfg := range []Config{ |
| 483 | {Origin: "http://connect.example.com", ClientID: "c", Redirect: redirect}, |
| 484 | {Origin: "https://connect.example.com/path", ClientID: "c", Redirect: redirect}, |
| 485 | {Origin: "https://connect.example.com", Redirect: redirect}, |
| 486 | {Origin: "https://connect.example.com", ClientID: "c"}, |
| 487 | } { |
| 488 | if _, err := New(cfg, &memStore{}, sealer{}); err == nil { |
| 489 | t.Errorf("accepted %+v", cfg) |
| 490 | } |
| 491 | } |
| 492 | if _, err := New(Config{Origin: "https://connect.example.com", ClientID: "c", Redirect: redirect}, &memStore{}, sealer{}); err != nil { |
| 493 | t.Error(err) |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // With reading off for the person (checked after their lock is held, |
| 498 | // right before the request), no token refresh is sent (abilities §6.1). |
| 499 | func TestConnectRefreshNeedsAllowed(t *testing.T) { |
| 500 | r := newRig(t) |
| 501 | ctx := context.Background() |
| 502 | r.connect(t, "ana@example.com") |
| 503 | off := errors.New("reading off") |
| 504 | r.c.SetAllowed(func(context.Context, string) error { return off }) |
| 505 | r.clock.add(5 * time.Minute) |
| 506 | if _, _, ok, err := r.c.Host(ctx, "ana@example.com"); !errors.Is(err, off) || ok { |
| 507 | t.Fatalf("refresh with reading off: %v %v", ok, err) |
| 508 | } |
| 509 | if _, n := r.fake.Counts(); n != 0 { |
| 510 | t.Errorf("%d refreshes sent", n) |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // A consent's Finish that waits on the person's lock (a refresh in |
| 515 | // flight) checks again once it holds it: reading switched off meanwhile |
| 516 | // means no code is exchanged and nothing is stored (Astra phase 2 round |
| 517 | // 4, P1). |
| 518 | func TestConnectFinishChecksAfterLock(t *testing.T) { |
| 519 | r := newRig(t) |
| 520 | ctx := context.Background() |
| 521 | q := r.consent(t, "ana@example.com") |
| 522 | var mu sync.Mutex |
| 523 | var off error |
| 524 | r.c.SetAllowed(func(context.Context, string) error { |
| 525 | mu.Lock() |
| 526 | defer mu.Unlock() |
| 527 | return off |
| 528 | }) |
| 529 | lock := r.c.lock("ana@example.com") |
| 530 | lock.Lock() |
| 531 | done := make(chan error, 1) |
| 532 | go func() { done <- r.c.Finish(ctx, "ana@example.com", q.Get("state"), q.Get("code")) }() |
| 533 | // Finish is past its own checks and waiting on the lock. |
| 534 | time.Sleep(50 * time.Millisecond) |
| 535 | mu.Lock() |
| 536 | off = errors.New("reading off") |
| 537 | mu.Unlock() |
| 538 | lock.Unlock() |
| 539 | if err := <-done; err == nil { |
| 540 | t.Fatal("finished with reading off") |
| 541 | } |
| 542 | if n, _ := r.fake.Counts(); n != 0 { |
| 543 | t.Errorf("%d code exchanges sent", n) |
| 544 | } |
| 545 | if r.store.sealed["ana@example.com"] != "" { |
| 546 | t.Error("a connection was stored") |
| 547 | } |
| 548 | } |
| 549 | |