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:
+111
-5
@@ -41,8 +41,9 @@ type OAuthState struct {
|
||||
type oauthStateStore struct {
|
||||
rdb *redis.Client
|
||||
|
||||
mu sync.Mutex
|
||||
mem map[string]memStateEntry
|
||||
mu sync.Mutex
|
||||
mem map[string]memStateEntry
|
||||
attempts map[string]memAttemptEntry
|
||||
}
|
||||
|
||||
type memStateEntry struct {
|
||||
@@ -50,8 +51,28 @@ type memStateEntry struct {
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
// OAuthAttempt is the non-secret, authenticated status of one authorization
|
||||
// flow. It is kept separately from OAuthState because Take consumes the PKCE
|
||||
// state before the token exchange finishes, while the opener still needs to
|
||||
// distinguish this callback from an older token stored for the same service.
|
||||
type OAuthAttempt struct {
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
Principal types.Principal `json:"principal"`
|
||||
ServiceID string `json:"service_id"`
|
||||
Completed bool `json:"completed"`
|
||||
}
|
||||
|
||||
type memAttemptEntry struct {
|
||||
value OAuthAttempt
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
func newOAuthStateStore(rdb *redis.Client) *oauthStateStore {
|
||||
s := &oauthStateStore{rdb: rdb, mem: make(map[string]memStateEntry)}
|
||||
s := &oauthStateStore{
|
||||
rdb: rdb,
|
||||
mem: make(map[string]memStateEntry),
|
||||
attempts: make(map[string]memAttemptEntry),
|
||||
}
|
||||
if rdb == nil {
|
||||
go s.gcLoop()
|
||||
}
|
||||
@@ -66,21 +87,101 @@ func (s *oauthStateStore) key(state string) string {
|
||||
return "weknora:mcp_oauth_state:" + state
|
||||
}
|
||||
|
||||
func (s *oauthStateStore) attemptKey(state string) string {
|
||||
return s.key(state) + ":attempt"
|
||||
}
|
||||
|
||||
// Put stores a state with a fixed TTL.
|
||||
func (s *oauthStateStore) Put(ctx context.Context, state string, value OAuthState) error {
|
||||
attempt := OAuthAttempt{
|
||||
TenantID: value.TenantID,
|
||||
Principal: value.Principal.Normalize(),
|
||||
ServiceID: value.ServiceID,
|
||||
}
|
||||
if s.rdb != nil {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.rdb.Set(ctx, s.key(state), data, oauthStateTTL).Err()
|
||||
attemptData, err := json.Marshal(attempt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pipe := s.rdb.TxPipeline()
|
||||
pipe.Set(ctx, s.key(state), data, oauthStateTTL)
|
||||
pipe.Set(ctx, s.attemptKey(state), attemptData, oauthStateTTL)
|
||||
_, err = pipe.Exec(ctx)
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.mem[state] = memStateEntry{value: value, expiresAt: time.Now().Add(oauthStateTTL)}
|
||||
expiresAt := time.Now().Add(oauthStateTTL)
|
||||
s.mem[state] = memStateEntry{value: value, expiresAt: expiresAt}
|
||||
s.attempts[state] = memAttemptEntry{value: attempt, expiresAt: expiresAt}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CompleteAttempt marks an authorization attempt complete only after the code
|
||||
// exchange has successfully persisted a token.
|
||||
func (s *oauthStateStore) CompleteAttempt(ctx context.Context, state string) error {
|
||||
if s.rdb != nil {
|
||||
data, err := s.rdb.Get(ctx, s.attemptKey(state)).Bytes()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
return fmt.Errorf("oauth attempt not found or expired")
|
||||
}
|
||||
return err
|
||||
}
|
||||
var attempt OAuthAttempt
|
||||
if err := json.Unmarshal(data, &attempt); err != nil {
|
||||
return err
|
||||
}
|
||||
attempt.Completed = true
|
||||
data, err = json.Marshal(attempt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.rdb.Set(ctx, s.attemptKey(state), data, oauthStateTTL).Err()
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
entry, ok := s.attempts[state]
|
||||
if !ok || time.Now().After(entry.expiresAt) {
|
||||
delete(s.attempts, state)
|
||||
return fmt.Errorf("oauth attempt not found or expired")
|
||||
}
|
||||
entry.value.Completed = true
|
||||
entry.expiresAt = time.Now().Add(oauthStateTTL)
|
||||
s.attempts[state] = entry
|
||||
return nil
|
||||
}
|
||||
|
||||
// Attempt returns the status record for one authorization flow.
|
||||
func (s *oauthStateStore) Attempt(ctx context.Context, state string) (OAuthAttempt, error) {
|
||||
if s.rdb != nil {
|
||||
data, err := s.rdb.Get(ctx, s.attemptKey(state)).Bytes()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
return OAuthAttempt{}, fmt.Errorf("oauth attempt not found or expired")
|
||||
}
|
||||
return OAuthAttempt{}, err
|
||||
}
|
||||
var attempt OAuthAttempt
|
||||
if err := json.Unmarshal(data, &attempt); err != nil {
|
||||
return OAuthAttempt{}, err
|
||||
}
|
||||
return attempt, nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
entry, ok := s.attempts[state]
|
||||
if !ok || time.Now().After(entry.expiresAt) {
|
||||
delete(s.attempts, state)
|
||||
return OAuthAttempt{}, fmt.Errorf("oauth attempt not found or expired")
|
||||
}
|
||||
return entry.value, nil
|
||||
}
|
||||
|
||||
// Take retrieves and deletes a state (single-use). Returns an error if the
|
||||
// state is unknown or expired.
|
||||
func (s *oauthStateStore) Take(ctx context.Context, state string) (OAuthState, error) {
|
||||
@@ -122,6 +223,11 @@ func (s *oauthStateStore) gcLoop() {
|
||||
delete(s.mem, k)
|
||||
}
|
||||
}
|
||||
for k, v := range s.attempts {
|
||||
if now.After(v.expiresAt) {
|
||||
delete(s.attempts, k)
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user