@@ -327,6 +327,85 @@ func (p *OAuthProxy) decryptPropsIfNeeded(props map[string]interface{}) (map[str
327327 return result , nil
328328}
329329
330+ // updateGrant updates a grant with new token information
331+ func (p * OAuthProxy ) updateGrant (grantID , userID string , oldTokenInfo * tokens.TokenInfo , newTokenInfo * providers.TokenInfo ) error {
332+ // Get the existing grant
333+ grant , err := p .db .GetGrant (grantID , userID )
334+ if err != nil {
335+ return fmt .Errorf ("failed to get grant: %w" , err )
336+ }
337+
338+ // Prepare sensitive props data
339+ sensitiveProps := map [string ]interface {}{
340+ "access_token" : newTokenInfo .AccessToken ,
341+ "refresh_token" : newTokenInfo .RefreshToken ,
342+ "expires_at" : newTokenInfo .ExpireAt ,
343+ }
344+
345+ // Add existing user info if available
346+ if grant .Props != nil {
347+ if email , ok := grant .Props ["email" ].(string ); ok {
348+ sensitiveProps ["email" ] = email
349+ }
350+ if name , ok := grant .Props ["name" ].(string ); ok {
351+ sensitiveProps ["name" ] = name
352+ }
353+ if userID , ok := grant .Props ["user_id" ].(string ); ok {
354+ sensitiveProps ["user_id" ] = userID
355+ }
356+ }
357+
358+ // use old refresh token in case new one is not provided
359+ if sensitiveProps ["refresh_token" ] == "" {
360+ sensitiveProps ["refresh_token" ] = oldTokenInfo .Props ["refresh_token" ]
361+ }
362+
363+ // Initialize props map
364+ props := make (map [string ]interface {})
365+
366+ // Check if encryption is enabled
367+ if p .encryptionKey != "" {
368+ // Decode the encryption key from base64
369+ encryptionKey , err := base64 .StdEncoding .DecodeString (p .encryptionKey )
370+ if err != nil {
371+ return fmt .Errorf ("failed to decode encryption key: %w" , err )
372+ }
373+
374+ // Validate key length (must be 32 bytes for AES-256)
375+ if len (encryptionKey ) != 32 {
376+ return fmt .Errorf ("invalid encryption key length: %d bytes (expected 32)" , len (encryptionKey ))
377+ }
378+
379+ // Encrypt the sensitive props data
380+ encryptedProps , err := encryptData (sensitiveProps , encryptionKey )
381+ if err != nil {
382+ return fmt .Errorf ("failed to encrypt props data: %w" , err )
383+ }
384+
385+ // Store encrypted data
386+ props ["encrypted_data" ] = encryptedProps .Data
387+ props ["iv" ] = encryptedProps .IV
388+ props ["algorithm" ] = encryptedProps .Algorithm
389+ props ["encrypted" ] = true
390+ } else {
391+ // Store data in plain text if no encryption key is provided
392+ for key , value := range sensitiveProps {
393+ props [key ] = value
394+ }
395+ props ["encrypted" ] = false
396+ }
397+
398+ // Update the grant with new props
399+ grant .Props = props
400+
401+ // Update the grant in the database
402+ if err := p .db .UpdateGrant (grant ); err != nil {
403+ return fmt .Errorf ("failed to update grant: %w" , err )
404+ }
405+
406+ return nil
407+ }
408+
330409// databaseAdapter adapts the database to the tokens.Database interface
331410type databaseAdapter struct {
332411 db * database.Database
@@ -984,12 +1063,19 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
9841063 return
9851064 }
9861065
987- // Update the token info with the new access token
988- tokenInfo .Props ["access_token" ] = newTokenInfo .AccessToken
989- if newTokenInfo .RefreshToken != "" {
990- tokenInfo .Props ["refresh_token" ] = newTokenInfo .RefreshToken
1066+ // Update the grant with new token information
1067+ if err := p .updateGrant (tokenInfo .GrantID , tokenInfo .UserID , tokenInfo , newTokenInfo ); err != nil {
1068+ log .Printf ("Failed to update grant: %v" , err )
1069+ c .JSON (http .StatusInternalServerError , gin.H {
1070+ "error" : "server_error" ,
1071+ "error_description" : "Failed to update grant with new token" ,
1072+ })
1073+ return
9911074 }
9921075
1076+ // Update the token info with the new access token for the current request
1077+ tokenInfo .Props ["access_token" ] = newTokenInfo .AccessToken
1078+
9931079 log .Printf ("Successfully refreshed access token" )
9941080 }
9951081 }
@@ -1042,6 +1128,7 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
10421128 },
10431129 ErrorHandler : func (w http.ResponseWriter , r * http.Request , err error ) {
10441130 log .Printf ("Proxy error: %v" , err )
1131+ c .Abort ()
10451132 },
10461133 }
10471134
0 commit comments