From 85ce5706ab9991e75946ad9b78f281ad264f2ef7 Mon Sep 17 00:00:00 2001 From: OpenWaygate <27287694+OpenWaygate@users.noreply.github.com> Date: Tue, 16 Jun 2026 23:26:50 +0800 Subject: [PATCH] :recycle: Replace RedirectURL global with context-based injection --- cmd/mcp.go | 3 ++- pkg/auth/verifier.go | 5 ++++- pkg/common/common.go | 36 ++++++++++++++++++++++++------------ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/cmd/mcp.go b/cmd/mcp.go index b2f7794..7b8bd07 100644 --- a/cmd/mcp.go +++ b/cmd/mcp.go @@ -65,7 +65,8 @@ func init() { Scopes: auth.Scopes, } } else { - common.RedirectURL = fmt.Sprintf("http://localhost:%d", port) + redirectURL := fmt.Sprintf("http://localhost:%d", port) + cmd.SetContext(common.CtxWithRedirectURL(cmd.Context(), redirectURL)) } return nil diff --git a/pkg/auth/verifier.go b/pkg/auth/verifier.go index fbfd82a..6b2ab9e 100644 --- a/pkg/auth/verifier.go +++ b/pkg/auth/verifier.go @@ -7,6 +7,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "strings" "time" @@ -31,7 +32,9 @@ func NewGoogleTokenVerifier(tokenInfoURL string) sdkauth.TokenVerifier { if err != nil { return nil, fmt.Errorf("%w: %w", sdkauth.ErrInvalidToken, err) } - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + _ = Body.Close() + }(resp.Body) if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf( diff --git a/pkg/common/common.go b/pkg/common/common.go index fe7a249..3b7454b 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -21,19 +21,23 @@ import ( "google.golang.org/api/youtube/v3" ) -// RedirectURL is the OAuth redirect URL used by file-based auth (CLI / stdio mode). -// Set by the command layer before EnsureService is called. -var RedirectURL = "http://localhost:8216" +type redirectURLKey struct{} + +// CtxWithRedirectURL returns a child context carrying the OAuth redirect URL. +func CtxWithRedirectURL(ctx context.Context, url string) context.Context { + return context.WithValue(ctx, redirectURLKey{}, url) +} type Fields struct { - Ctx context.Context `yaml:"-" json:"-"` - Service *youtube.Service `yaml:"-" json:"-"` - Ids []string `yaml:"ids" json:"ids,omitempty"` - MaxResults int64 `yaml:"max_results" json:"max_results,omitempty"` - Hl string `yaml:"hl" json:"hl,omitempty"` - ChannelId string `yaml:"channel_id" json:"channel_id,omitempty"` - Parts []string `yaml:"parts" json:"parts,omitempty"` - Output string `yaml:"output" json:"output,omitempty"` + Ctx context.Context `yaml:"-" json:"-"` + Service *youtube.Service `yaml:"-" json:"-"` + RedirectURL string `yaml:"-" json:"-"` + Ids []string `yaml:"ids" json:"ids,omitempty"` + MaxResults int64 `yaml:"max_results" json:"max_results,omitempty"` + Hl string `yaml:"hl" json:"hl,omitempty"` + ChannelId string `yaml:"channel_id" json:"channel_id,omitempty"` + Parts []string `yaml:"parts" json:"parts,omitempty"` + Output string `yaml:"output" json:"output,omitempty"` OnBehalfOfContentOwner string `yaml:"on_behalf_of_content_owner" json:"on_behalf_of_content_owner,omitempty"` } @@ -47,6 +51,11 @@ func (d *Fields) GetFields() *Fields { func (d *Fields) SetContext(ctx context.Context) { if d != nil { d.Ctx = ctx + if d.RedirectURL == "" { + if url, ok := ctx.Value(redirectURLKey{}).(string); ok { + d.RedirectURL = url + } + } } } @@ -72,10 +81,13 @@ func (d *Fields) EnsureService() error { } // File-based auth path (CLI / stdio mode). + if d.RedirectURL == "" { + d.RedirectURL = "http://localhost:8216" + } svc, err := auth.NewY2BService( auth.WithCredential("", pkg.Root.FS()), auth.WithCacheToken("", pkg.Root.FS()), - auth.WithRedirectURL(RedirectURL), + auth.WithRedirectURL(d.RedirectURL), ).GetService() if err != nil { return fmt.Errorf("failed to create YouTube service: %w", err)