@@ -327,6 +327,80 @@ func (p *OAuthProxy) decryptPropsIfNeeded(props map[string]interface{}) (map[str
327
327
return result , nil
328
328
}
329
329
330
+ // updateGrant updates a grant with new token information
331
+ func (p * OAuthProxy ) updateGrant (grantID , userID string , 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
+ // Initialize props map
359
+ props := make (map [string ]interface {})
360
+
361
+ // Check if encryption is enabled
362
+ if p .encryptionKey != "" {
363
+ // Decode the encryption key from base64
364
+ encryptionKey , err := base64 .StdEncoding .DecodeString (p .encryptionKey )
365
+ if err != nil {
366
+ return fmt .Errorf ("failed to decode encryption key: %w" , err )
367
+ }
368
+
369
+ // Validate key length (must be 32 bytes for AES-256)
370
+ if len (encryptionKey ) != 32 {
371
+ return fmt .Errorf ("invalid encryption key length: %d bytes (expected 32)" , len (encryptionKey ))
372
+ }
373
+
374
+ // Encrypt the sensitive props data
375
+ encryptedProps , err := encryptData (sensitiveProps , encryptionKey )
376
+ if err != nil {
377
+ return fmt .Errorf ("failed to encrypt props data: %w" , err )
378
+ }
379
+
380
+ // Store encrypted data
381
+ props ["encrypted_data" ] = encryptedProps .Data
382
+ props ["iv" ] = encryptedProps .IV
383
+ props ["algorithm" ] = encryptedProps .Algorithm
384
+ props ["encrypted" ] = true
385
+ } else {
386
+ // Store data in plain text if no encryption key is provided
387
+ for key , value := range sensitiveProps {
388
+ props [key ] = value
389
+ }
390
+ props ["encrypted" ] = false
391
+ }
392
+
393
+ // Update the grant with new props
394
+ grant .Props = props
395
+
396
+ // Update the grant in the database
397
+ if err := p .db .UpdateGrant (grant ); err != nil {
398
+ return fmt .Errorf ("failed to update grant: %w" , err )
399
+ }
400
+
401
+ return nil
402
+ }
403
+
330
404
// databaseAdapter adapts the database to the tokens.Database interface
331
405
type databaseAdapter struct {
332
406
db * database.Database
@@ -984,7 +1058,17 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
984
1058
return
985
1059
}
986
1060
987
- // Update the token info with the new access token
1061
+ // Update the grant with new token information
1062
+ if err := p .updateGrant (tokenInfo .GrantID , tokenInfo .UserID , newTokenInfo ); err != nil {
1063
+ log .Printf ("Failed to update grant: %v" , err )
1064
+ c .JSON (http .StatusInternalServerError , gin.H {
1065
+ "error" : "server_error" ,
1066
+ "error_description" : "Failed to update grant with new token" ,
1067
+ })
1068
+ return
1069
+ }
1070
+
1071
+ // Update the token info with the new access token for the current request
988
1072
tokenInfo .Props ["access_token" ] = newTokenInfo .AccessToken
989
1073
if newTokenInfo .RefreshToken != "" {
990
1074
tokenInfo .Props ["refresh_token" ] = newTokenInfo .RefreshToken
@@ -1042,6 +1126,7 @@ func (p *OAuthProxy) mcpProxyHandler(c *gin.Context) {
1042
1126
},
1043
1127
ErrorHandler : func (w http.ResponseWriter , r * http.Request , err error ) {
1044
1128
log .Printf ("Proxy error: %v" , err )
1129
+ c .Abort ()
1045
1130
},
1046
1131
}
1047
1132
0 commit comments