diff --git a/azure/login/login.go b/azure/login/login.go index 54a4e5aa6..712691854 100644 --- a/azure/login/login.go +++ b/azure/login/login.go @@ -20,12 +20,9 @@ import ( "context" "encoding/json" "fmt" - "log" "net/http" "net/url" - "os/exec" "path/filepath" - "runtime" "strconv" "time" @@ -137,7 +134,10 @@ func (login AzureLoginService) Login(ctx context.Context, requestedTenantID stri if redirectURL == "" { return errors.Wrap(errdefs.ErrLoginFailed, "empty redirect URL") } - login.apiHelper.openAzureLoginPage(redirectURL) + + if err = login.apiHelper.openAzureLoginPage(redirectURL); err != nil { + return err + } select { case <-ctx.Done(): @@ -302,21 +302,3 @@ func (login AzureLoginService) refreshToken(currentRefreshToken string, tenantID return toOAuthToken(token), nil } - -func openbrowser(url string) { - var err error - - switch runtime.GOOS { - case "linux": - err = exec.Command("xdg-open", url).Start() - case "windows": - err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() - case "darwin": - err = exec.Command("open", url).Start() - default: - err = fmt.Errorf("unsupported platform") - } - if err != nil { - log.Fatal(err) - } -} diff --git a/azure/login/loginHelper.go b/azure/login/loginHelper.go index d84b20430..818c5c14f 100644 --- a/azure/login/loginHelper.go +++ b/azure/login/loginHelper.go @@ -23,6 +23,8 @@ import ( "math/rand" "net/http" "net/url" + "os/exec" + "runtime" "strings" "github.com/pkg/errors" @@ -30,16 +32,16 @@ import ( type apiHelper interface { queryToken(data url.Values, tenantID string) (azureToken, error) - openAzureLoginPage(redirectURL string) + openAzureLoginPage(redirectURL string) error queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) } type azureAPIHelper struct{} -func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) { +func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) error { state := randomString("", 10) authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes) - openbrowser(authURL) + return openbrowser(authURL) } func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) { @@ -78,6 +80,19 @@ func (helper azureAPIHelper) queryToken(data url.Values, tenantID string) (azure return token, nil } +func openbrowser(url string) error { + switch runtime.GOOS { + case "linux": + return exec.Command("xdg-open", url).Start() + case "windows": + return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + return exec.Command("open", url).Start() + default: + return fmt.Errorf("unsupported platform") + } +} + var ( letterRunes = []rune("abcdefghijklmnopqrstuvwxyz123456789") ) diff --git a/azure/login/login_test.go b/azure/login/login_test.go index 012fa1590..5b1b84681 100644 --- a/azure/login/login_test.go +++ b/azure/login/login_test.go @@ -370,6 +370,7 @@ func (s *MockAzureHelper) queryAuthorizationAPI(authorizationURL string, authori return args.Get(0).([]byte), args.Int(1), args.Error(2) } -func (s *MockAzureHelper) openAzureLoginPage(redirectURL string) { +func (s *MockAzureHelper) openAzureLoginPage(redirectURL string) error { s.Called(redirectURL) + return nil }