From 15bfb5447b38da8bcd27914a48ab2ab3e34eb4b1 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 9 Sep 2020 14:02:23 -0500 Subject: [PATCH] split auth flow code into new internal package --- Makefile | 4 ++-- .../{config/config_setup.go => authflow/flow.go} | 14 +++++--------- .../config_success.go => authflow/success.go} | 2 +- pkg/cmd/auth/login/login.go | 3 ++- pkg/cmd/auth/refresh/refresh.go | 3 ++- 5 files changed, 12 insertions(+), 14 deletions(-) rename internal/{config/config_setup.go => authflow/flow.go} (85%) rename internal/{config/config_success.go => authflow/success.go} (98%) diff --git a/Makefile b/Makefile index decf5125c..51af20952 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,8 @@ endif GO_LDFLAGS := -X github.com/cli/cli/command.Version=$(GH_VERSION) $(GO_LDFLAGS) GO_LDFLAGS := -X github.com/cli/cli/command.BuildDate=$(BUILD_DATE) $(GO_LDFLAGS) ifdef GH_OAUTH_CLIENT_SECRET - GO_LDFLAGS := -X github.com/cli/cli/internal/config.oauthClientID=$(GH_OAUTH_CLIENT_ID) $(GO_LDFLAGS) - GO_LDFLAGS := -X github.com/cli/cli/internal/config.oauthClientSecret=$(GH_OAUTH_CLIENT_SECRET) $(GO_LDFLAGS) + GO_LDFLAGS := -X github.com/cli/cli/internal/authflow.oauthClientID=$(GH_OAUTH_CLIENT_ID) $(GO_LDFLAGS) + GO_LDFLAGS := -X github.com/cli/cli/internal/authflow.oauthClientSecret=$(GH_OAUTH_CLIENT_SECRET) $(GO_LDFLAGS) endif bin/gh: $(BUILD_FILES) diff --git a/internal/config/config_setup.go b/internal/authflow/flow.go similarity index 85% rename from internal/config/config_setup.go rename to internal/authflow/flow.go index 649e105b7..1c804931f 100644 --- a/internal/config/config_setup.go +++ b/internal/authflow/flow.go @@ -1,4 +1,4 @@ -package config +package authflow import ( "bufio" @@ -10,6 +10,7 @@ import ( "github.com/cli/cli/api" "github.com/cli/cli/auth" + "github.com/cli/cli/internal/config" "github.com/cli/cli/pkg/browser" "github.com/cli/cli/utils" "github.com/mattn/go-colorable" @@ -22,14 +23,9 @@ var ( oauthClientSecret = "34ddeff2b558a23d38fba8a6de74f086ede1cc0b" ) -// IsGitHubApp reports whether an OAuth app is "GitHub CLI" or "GitHub CLI (dev)" -func IsGitHubApp(id string) bool { - // this intentionally doesn't use `oauthClientID` because that is a variable - // that can potentially be changed at build time via GH_OAUTH_CLIENT_ID - return id == "178c6fc778ccc68e1d6a" || id == "4d747ba5675d5d66553f" -} - -func AuthFlowWithConfig(cfg Config, hostname, notice string, additionalScopes []string) (string, error) { +func AuthFlowWithConfig(cfg config.Config, hostname, notice string, additionalScopes []string) (string, error) { + // TODO this probably shouldn't live in this package. It should probably be in a new package that + // depends on both iostreams and config. stderr := colorable.NewColorableStderr() token, userLogin, err := authFlow(hostname, stderr, notice, additionalScopes) diff --git a/internal/config/config_success.go b/internal/authflow/success.go similarity index 98% rename from internal/config/config_success.go rename to internal/authflow/success.go index c7a681bac..a546e58d7 100644 --- a/internal/config/config_success.go +++ b/internal/authflow/success.go @@ -1,4 +1,4 @@ -package config +package authflow const oauthSuccessPage = ` diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index eb58484ec..c0a24c5c5 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -9,6 +9,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/api" + "github.com/cli/cli/internal/authflow" "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghinstance" "github.com/cli/cli/pkg/cmd/auth/client" @@ -202,7 +203,7 @@ func loginRun(opts *LoginOptions) error { } if authMode == 0 { - _, err := config.AuthFlowWithConfig(cfg, hostname, "", []string{}) + _, err := authflow.AuthFlowWithConfig(cfg, hostname, "", []string{}) if err != nil { return fmt.Errorf("failed to authenticate via web browser: %w", err) } diff --git a/pkg/cmd/auth/refresh/refresh.go b/pkg/cmd/auth/refresh/refresh.go index 757903bdb..7fe5a8c5f 100644 --- a/pkg/cmd/auth/refresh/refresh.go +++ b/pkg/cmd/auth/refresh/refresh.go @@ -5,6 +5,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/internal/authflow" "github.com/cli/cli/internal/config" "github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/iostreams" @@ -26,7 +27,7 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra. IO: f.IOStreams, Config: f.Config, AuthFlow: func(cfg config.Config, hostname string, scopes []string) error { - _, err := config.AuthFlowWithConfig(cfg, hostname, "", scopes) + _, err := authflow.AuthFlowWithConfig(cfg, hostname, "", scopes) return err }, }