refactor: derive copilot metadata from storage

This commit is contained in:
GrothKeiran
2026-03-13 13:10:01 +00:00
parent 339a81b650
commit 3960c93d51

View File

@@ -2438,20 +2438,20 @@ func (h *Handler) RequestGitHubToken(c *gin.Context) {
if label == "" {
label = username
}
metadata, errMeta := copilotTokenMetadata(tokenStorage)
if errMeta != nil {
log.Errorf("Failed to build token metadata: %v", errMeta)
SetOAuthSessionError(state, "Failed to build token metadata")
return
}
record := &coreauth.Auth{
ID: fileName,
Provider: "github-copilot",
Label: label,
FileName: fileName,
Storage: tokenStorage,
Metadata: map[string]any{
"email": userInfo.Email,
"username": username,
"name": userInfo.Name,
"access_token": tokenData.AccessToken,
"token_type": tokenData.TokenType,
"scope": tokenData.Scope,
},
Metadata: metadata,
}
savedPath, errSave := h.saveTokenRecord(ctx, record)
@@ -2476,6 +2476,21 @@ func (h *Handler) RequestGitHubToken(c *gin.Context) {
})
}
func copilotTokenMetadata(storage *copilot.CopilotTokenStorage) (map[string]any, error) {
if storage == nil {
return nil, fmt.Errorf("token storage is nil")
}
payload, errMarshal := json.Marshal(storage)
if errMarshal != nil {
return nil, fmt.Errorf("marshal token storage: %w", errMarshal)
}
metadata := make(map[string]any)
if errUnmarshal := json.Unmarshal(payload, &metadata); errUnmarshal != nil {
return nil, fmt.Errorf("unmarshal token storage: %w", errUnmarshal)
}
return metadata, nil
}
func (h *Handler) RequestIFlowCookieToken(c *gin.Context) {
ctx := context.Background()