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:
@@ -2,6 +2,7 @@ package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
@@ -11,9 +12,9 @@ import (
|
||||
|
||||
// dbTokenStore is a transport.TokenStore backed by the MCPOAuthRepository,
|
||||
// scoped to a single (tenant, principal, service) tuple. The mcp-go OAuth handler
|
||||
// calls GetToken before each request (refreshing via refresh_token when
|
||||
// expired) and SaveToken after a successful authorization or refresh, so this
|
||||
// store transparently persists refreshed tokens back to the database.
|
||||
// calls SaveToken after a successful authorization or refresh. Runtime MCP
|
||||
// transports receive the managedTokenStore wrapper below so refresh decisions
|
||||
// stay in WeKnora's coordinated lifecycle instead of the dependency.
|
||||
type dbTokenStore struct {
|
||||
repo interfaces.MCPOAuthRepository
|
||||
tenantID uint64
|
||||
@@ -21,6 +22,30 @@ type dbTokenStore struct {
|
||||
serviceID string
|
||||
}
|
||||
|
||||
// managedTokenStore hides local expiry from mcp-go transports. WeKnora checks
|
||||
// the persisted ExpiresAt before every operation and performs the coordinated
|
||||
// refresh itself; allowing the dependency to also auto-refresh would bypass
|
||||
// the cross-instance lease and collapse refresh failures into a generic
|
||||
// authorization-required error.
|
||||
type managedTokenStore struct {
|
||||
*dbTokenStore
|
||||
}
|
||||
|
||||
func newManagedTokenStore(
|
||||
repo interfaces.MCPOAuthRepository, tenantID uint64, principal types.Principal, serviceID string,
|
||||
) *managedTokenStore {
|
||||
return &managedTokenStore{dbTokenStore: newDBTokenStore(repo, tenantID, principal, serviceID)}
|
||||
}
|
||||
|
||||
func (s *managedTokenStore) GetToken(ctx context.Context) (*transport.Token, error) {
|
||||
token, err := s.dbTokenStore.GetToken(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
token.ExpiresAt = time.Time{}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// newDBTokenStore creates a per-principal, per-service token store.
|
||||
func newDBTokenStore(
|
||||
repo interfaces.MCPOAuthRepository, tenantID uint64, principal types.Principal, serviceID string,
|
||||
@@ -59,6 +84,12 @@ func (s *dbTokenStore) SaveToken(ctx context.Context, token *transport.Token) er
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if token == nil || token.AccessToken == "" {
|
||||
return fmt.Errorf("OAuth token response did not contain an access_token")
|
||||
}
|
||||
if token.TokenType == "" {
|
||||
token.TokenType = "Bearer"
|
||||
}
|
||||
expiresAt := token.ExpiresAt
|
||||
if expiresAt.IsZero() && token.ExpiresIn > 0 {
|
||||
expiresAt = time.Now().Add(time.Duration(token.ExpiresIn) * time.Second)
|
||||
|
||||
Reference in New Issue
Block a user