| 1 | package grant |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "path/filepath" |
| 8 | "sync" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "golang.org/x/oauth2" |
| 13 | ) |
| 14 | |
| 15 | func tokenFixture(t *testing.T, store *Store) TokenResponse { |
| 16 | t.Helper() |
| 17 | v := oauth2.GenerateVerifier() |
| 18 | code, err := store.CreateCode(t.Context(), Grant{Subject: "person", ClientID: "client", Teams: []string{"acme"}, Scopes: []string{"notes:write", "offline_access"}}, Code{Redirect: "https://client.example/callback", Challenge: oauth2.S256ChallengeFromVerifier(v), Resource: "https://connect.example/mcp"}) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | token, err := store.Exchange(t.Context(), code, "client", "https://client.example/callback", "https://connect.example/mcp", v) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | return token |
| 27 | } |
| 28 | |
| 29 | func TestConcurrentRefreshRevokesFamily(t *testing.T) { |
| 30 | db, err := sql.Open("sqlite", filepath.Join(t.TempDir(), "grants.db")+"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(1000)") |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | defer db.Close() |
| 35 | db.SetMaxOpenConns(8) |
| 36 | store, err := NewStore(t.Context(), db) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | for range 12 { |
| 41 | original := tokenFixture(t, store) |
| 42 | start := make(chan struct{}) |
| 43 | var wg sync.WaitGroup |
| 44 | results := make(chan TokenResponse, 2) |
| 45 | failures := make(chan error, 2) |
| 46 | for range 2 { |
| 47 | wg.Go(func() { |
| 48 | <-start |
| 49 | token, e := store.Refresh(t.Context(), original.RefreshToken, "client", "") |
| 50 | if e != nil { |
| 51 | failures <- e |
| 52 | } else { |
| 53 | results <- token |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | close(start) |
| 58 | wg.Wait() |
| 59 | close(results) |
| 60 | close(failures) |
| 61 | if len(results) != 1 || len(failures) != 1 { |
| 62 | t.Fatalf("successes=%d failures=%d", len(results), len(failures)) |
| 63 | } |
| 64 | for e := range failures { |
| 65 | if !errors.Is(e, ErrCredential) { |
| 66 | t.Fatalf("replay failed without revoking: %v", e) |
| 67 | } |
| 68 | } |
| 69 | for token := range results { |
| 70 | if _, e := store.Verify(t.Context(), token.AccessToken, "https://connect.example/mcp"); e == nil { |
| 71 | t.Fatal("concurrent replay left family authorized") |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestSweepRetainsReplayEvidenceUntilGrantExpires(t *testing.T) { |
| 78 | s, _, _ := setup(t) |
| 79 | store := s.cfg.Store |
| 80 | now := time.Now() |
| 81 | store.Now = func() time.Time { return now } |
| 82 | first := tokenFixture(t, store) |
| 83 | second, err := store.Refresh(t.Context(), first.RefreshToken, "client", "") |
| 84 | if err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | if _, err = store.DB.Exec("INSERT INTO agent_pending(hash,expires) VALUES('abandoned',?)", now.Add(-time.Minute).Unix()); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | now = now.Add(31 * 24 * time.Hour) |
| 91 | if err = store.Sweep(context.Background()); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | var access, pending, refresh int |
| 95 | store.DB.QueryRow("SELECT COUNT(*) FROM agent_tokens WHERE kind='access'").Scan(&access) |
| 96 | store.DB.QueryRow("SELECT COUNT(*) FROM agent_pending").Scan(&pending) |
| 97 | store.DB.QueryRow("SELECT COUNT(*) FROM agent_tokens WHERE kind='refresh'").Scan(&refresh) |
| 98 | if access != 0 || pending != 0 || refresh != 2 { |
| 99 | t.Fatal("incorrect retention", access, pending, refresh) |
| 100 | } |
| 101 | if _, err = store.Refresh(t.Context(), first.RefreshToken, "client", ""); !errors.Is(err, ErrCredential) { |
| 102 | t.Fatal("replay not refused", err) |
| 103 | } |
| 104 | if _, err = store.Refresh(t.Context(), second.RefreshToken, "client", ""); !errors.Is(err, ErrCredential) { |
| 105 | t.Fatal("family not revoked", err) |
| 106 | } |
| 107 | if err = store.Sweep(t.Context()); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | var tokens, grants int |
| 111 | store.DB.QueryRow("SELECT COUNT(*) FROM agent_tokens").Scan(&tokens) |
| 112 | store.DB.QueryRow("SELECT COUNT(*) FROM agent_grants").Scan(&grants) |
| 113 | if tokens != 0 || grants != 0 { |
| 114 | t.Fatal("revoked family retained", tokens, grants) |
| 115 | } |
| 116 | } |
| 117 | |