refactor(auth login): use PlainHttpClient for OAuth flow

Signed-off-by: Babak K. Shandiz <babakks@github.com>
This commit is contained in:
Babak K. Shandiz 2025-10-13 16:46:45 +01:00
parent ae9a7ed542
commit 365ca1a901
No known key found for this signature in database
GPG key ID: 9472CAEFF56C742E
3 changed files with 38 additions and 23 deletions

View file

@ -20,12 +20,13 @@ import (
)
type LoginOptions struct {
IO *iostreams.IOStreams
Config func() (gh.Config, error)
HttpClient func() (*http.Client, error)
GitClient *git.Client
Prompter shared.Prompt
Browser browser.Browser
IO *iostreams.IOStreams
Config func() (gh.Config, error)
HttpClient func() (*http.Client, error)
PlainHttpClient func() (*http.Client, error)
GitClient *git.Client
Prompter shared.Prompt
Browser browser.Browser
MainExecutable string
@ -43,12 +44,13 @@ type LoginOptions struct {
func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Command {
opts := &LoginOptions{
IO: f.IOStreams,
Config: f.Config,
HttpClient: f.HttpClient,
GitClient: f.GitClient,
Prompter: f.Prompter,
Browser: f.Browser,
IO: f.IOStreams,
Config: f.Config,
HttpClient: f.HttpClient,
PlainHttpClient: f.PlainHttpClient,
GitClient: f.GitClient,
Prompter: f.Prompter,
Browser: f.Browser,
}
var tokenStdin bool
@ -190,6 +192,11 @@ func loginRun(opts *LoginOptions) error {
return cmdutil.SilentError
}
plainHTTPClient, err := opts.PlainHttpClient()
if err != nil {
return err
}
httpClient, err := opts.HttpClient()
if err != nil {
return err
@ -210,16 +217,17 @@ func loginRun(opts *LoginOptions) error {
}
return shared.Login(&shared.LoginOptions{
IO: opts.IO,
Config: authCfg,
HTTPClient: httpClient,
Hostname: hostname,
Interactive: opts.Interactive,
Web: opts.Web,
Scopes: opts.Scopes,
GitProtocol: opts.GitProtocol,
Prompter: opts.Prompter,
Browser: opts.Browser,
IO: opts.IO,
Config: authCfg,
HTTPClient: httpClient,
PlainHTTPClient: plainHTTPClient,
Hostname: hostname,
Interactive: opts.Interactive,
Web: opts.Web,
Scopes: opts.Scopes,
GitProtocol: opts.GitProtocol,
Prompter: opts.Prompter,
Browser: opts.Browser,
CredentialFlow: &shared.GitCredentialFlow{
Prompter: opts.Prompter,
HelperConfig: &gitcredentials.HelperConfig{

View file

@ -483,6 +483,9 @@ func Test_loginRun_nontty(t *testing.T) {
tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
tt.opts.PlainHttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
if tt.httpStubs != nil {
tt.httpStubs(reg)
}
@ -775,6 +778,9 @@ func Test_loginRun_Survey(t *testing.T) {
tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
tt.opts.PlainHttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
if tt.httpStubs != nil {
tt.httpStubs(reg)
} else {

View file

@ -30,6 +30,7 @@ type LoginOptions struct {
IO *iostreams.IOStreams
Config iconfig
HTTPClient *http.Client
PlainHTTPClient *http.Client
Hostname string
Interactive bool
Web bool
@ -149,7 +150,7 @@ func Login(opts *LoginOptions) error {
if authMode == 0 {
var err error
authToken, username, err = authflow.AuthFlow(hostname, opts.IO, "", append(opts.Scopes, additionalScopes...), opts.Interactive, opts.Browser, opts.CopyToClipboard)
authToken, username, err = authflow.AuthFlow(opts.PlainHTTPClient, hostname, opts.IO, "", append(opts.Scopes, additionalScopes...), opts.Interactive, opts.Browser, opts.CopyToClipboard)
if err != nil {
return fmt.Errorf("failed to authenticate via web browser: %w", err)
}