Skip to content
Merged
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions internal/api/handlers/management/auth_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2445,9 +2445,12 @@ func (h *Handler) RequestGitHubToken(c *gin.Context) {
FileName: fileName,
Storage: tokenStorage,
Metadata: map[string]any{
"email": userInfo.Email,
"username": username,
"name": userInfo.Name,
"email": userInfo.Email,
"username": username,
"name": userInfo.Name,
"access_token": tokenData.AccessToken,
"token_type": tokenData.TokenType,
"scope": tokenData.Scope,
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this correctly adds the necessary fields to the metadata, it duplicates data already present in the tokenStorage object. This could become a maintenance issue if the CopilotTokenStorage struct is updated in the future, as those changes would need to be manually mirrored here.

To make this more robust and avoid data duplication, you could generate the metadata map directly from the tokenStorage object. This would ensure the metadata is always a complete and accurate reflection of the storage object.

For example, you could use JSON marshaling and unmarshaling to achieve this:

// A helper to convert storage to metadata
func tokenStorageToMetadata(storage any) (map[string]any, error) {
    data, err := json.Marshal(storage)
    if err != nil {
        return nil, err
    }
    var metadata map[string]any
    if err := json.Unmarshal(data, &metadata); err != nil {
        return nil, err
    }
    return metadata, nil
}

// In RequestGitHubToken:
tokenStorage := copilotAuth.CreateTokenStorage(bundle)
metadata, err := tokenStorageToMetadata(tokenStorage)
if err != nil {
    c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to create token metadata: %v", err)})
    return
}

record := &cliproxyauth.Auth{
    Provider: "github-copilot",
    FileName: fileName,
    Storage:  tokenStorage,
    Metadata: metadata,
}

This approach centralizes the definition of the persisted data in the CopilotTokenStorage struct, improving maintainability.

}

Expand Down
Loading