fix(mcp): coordinate OAuth token refresh and authorization polling

Prevent concurrent refresh-token rotation across instances, distinguish
expired-but-refreshable tokens from active authorization, and bind OAuth
popup polling to a per-attempt ID so stale tokens cannot satisfy new consent.
This commit is contained in:
wizardchen
2026-07-23 15:33:20 +08:00
committed by lyingbug
parent c74b0caa35
commit 8ae82573ab
31 changed files with 1418 additions and 109 deletions
+48
View File
@@ -81,6 +81,36 @@ func (r *fakeOAuthRepo) DeleteTokenForPrincipal(
return nil
}
func (r *fakeOAuthRepo) TryAcquireTokenRefreshLease(
_ context.Context,
tenantID uint64,
principal types.Principal,
serviceID, leaseID string,
leaseUntil time.Time,
) (bool, error) {
row := r.tokens[fakeOAuthKey(tenantID, principal, serviceID)]
if row == nil || (row.RefreshLeaseUntil != nil && row.RefreshLeaseUntil.After(time.Now())) {
return false, nil
}
row.RefreshLeaseID = leaseID
row.RefreshLeaseUntil = &leaseUntil
return true, nil
}
func (r *fakeOAuthRepo) ReleaseTokenRefreshLease(
_ context.Context,
tenantID uint64,
principal types.Principal,
serviceID, leaseID string,
) error {
row := r.tokens[fakeOAuthKey(tenantID, principal, serviceID)]
if row != nil && row.RefreshLeaseID == leaseID {
row.RefreshLeaseID = ""
row.RefreshLeaseUntil = nil
}
return nil
}
func TestDBTokenStoreUsesPrincipal(t *testing.T) {
repo := newFakeOAuthRepo()
principal := types.Principal{Type: types.PrincipalAPIExternalUser, ID: "7:external-42"}
@@ -108,6 +138,24 @@ func TestDBTokenStoreUsesPrincipal(t *testing.T) {
require.Equal(t, expiresAt, token.ExpiresAt)
}
func TestManagedTokenStoreLeavesRefreshToOAuthRuntime(t *testing.T) {
repo := newFakeOAuthRepo()
principal := types.Principal{Type: types.PrincipalWebUser, ID: "user-1"}
store := newManagedTokenStore(repo, 7, principal, "svc-1")
require.NoError(t, store.SaveToken(context.Background(), &transport.Token{
AccessToken: "access",
RefreshToken: "refresh",
ExpiresAt: time.Now().Add(-time.Minute),
}))
token, err := store.GetToken(context.Background())
require.NoError(t, err)
require.True(t, token.ExpiresAt.IsZero(), "mcp-go must not race WeKnora's coordinated refresh")
row, err := repo.GetTokenForPrincipal(context.Background(), 7, principal, "svc-1")
require.NoError(t, err)
require.False(t, row.ExpiresAt.IsZero(), "the database must retain the real expiry for preflight checks")
}
func TestOAuthCacheKeyUsesPrincipalForOAuthServices(t *testing.T) {
service := &types.MCPService{
ID: "svc-1",