Skip to content

Fix: add properly lock when refresh access token #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/url"
"os"
"strings"
"sync"
"time"

"github.com/gin-contrib/cors"
Expand Down Expand Up @@ -429,6 +430,7 @@ type OAuthProxy struct {
provider string
encryptionKey string
resourceName string
lock sync.Mutex
}

func NewOAuthProxy() (*OAuthProxy, error) {
Expand Down Expand Up @@ -1016,6 +1018,9 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
expiresAt, ok := tokenInfo.Props["expires_at"].(float64)
if ok && expiresAt > 0 {
if time.Now().Add(5 * time.Minute).After(time.Unix(int64(expiresAt), 0)) {
// when refreshing token, we need to lock the database to avoid race conditions
// otherwise we could get save the old access token into the database when another refresh process is running
p.lock.Lock()
log.Printf("Access token is expired or will expire soon, attempting to refresh")

// Get the refresh token
Expand All @@ -1026,6 +1031,7 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
"error": "invalid_token",
"error_description": "Access token expired and no refresh token available",
})
p.lock.Unlock()
return
}

Expand All @@ -1037,6 +1043,7 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
"error": "server_error",
"error_description": "Failed to refresh token",
})
p.lock.Unlock()
return
}

Expand All @@ -1049,6 +1056,7 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
"error": "server_error",
"error_description": "OAuth credentials not configured",
})
p.lock.Unlock()
return
}

Expand All @@ -1060,6 +1068,7 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
"error": "invalid_token",
"error_description": "Failed to refresh access token",
})
p.lock.Unlock()
return
}

Expand All @@ -1070,11 +1079,13 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
"error": "server_error",
"error_description": "Failed to update grant with new token",
})
p.lock.Unlock()
return
}

// Update the token info with the new access token for the current request
tokenInfo.Props["access_token"] = newTokenInfo.AccessToken
p.lock.Unlock()

log.Printf("Successfully refreshed access token")
}
Expand Down
Loading