diff --git a/azure/login/login.go b/azure/login/login.go index ded59719c..54a4e5aa6 100644 --- a/azure/login/login.go +++ b/azure/login/login.go @@ -20,9 +20,12 @@ import ( "context" "encoding/json" "fmt" + "log" "net/http" "net/url" + "os/exec" "path/filepath" + "runtime" "strconv" "time" @@ -134,9 +137,7 @@ func (login AzureLoginService) Login(ctx context.Context, requestedTenantID stri if redirectURL == "" { return errors.Wrap(errdefs.ErrLoginFailed, "empty redirect URL") } - if err = login.apiHelper.openAzureLoginPage(redirectURL); err != nil { - return err - } + login.apiHelper.openAzureLoginPage(redirectURL) select { case <-ctx.Done(): @@ -301,3 +302,21 @@ 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 f7cebc703..d84b20430 100644 --- a/azure/login/loginHelper.go +++ b/azure/login/loginHelper.go @@ -25,22 +25,21 @@ import ( "net/url" "strings" - "github.com/pkg/browser" "github.com/pkg/errors" ) type apiHelper interface { queryToken(data url.Values, tenantID string) (azureToken, error) - openAzureLoginPage(redirectURL string) error + openAzureLoginPage(redirectURL string) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) } type azureAPIHelper struct{} -func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) error { +func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) { state := randomString("", 10) authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes) - return browser.OpenURL(authURL) + openbrowser(authURL) } func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) { diff --git a/azure/login/login_test.go b/azure/login/login_test.go index 5b1b84681..012fa1590 100644 --- a/azure/login/login_test.go +++ b/azure/login/login_test.go @@ -370,7 +370,6 @@ func (s *MockAzureHelper) queryAuthorizationAPI(authorizationURL string, authori return args.Get(0).([]byte), args.Int(1), args.Error(2) } -func (s *MockAzureHelper) openAzureLoginPage(redirectURL string) error { +func (s *MockAzureHelper) openAzureLoginPage(redirectURL string) { s.Called(redirectURL) - return nil } diff --git a/go.mod b/go.mod index a941d09cd..8f3f711ea 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,6 @@ require ( github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/runc v0.1.1 // indirect - github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 github.com/pkg/errors v0.9.1 github.com/robpike/filter v0.0.0-20150108201509-2984852a2183 github.com/sirupsen/logrus v1.6.0 diff --git a/go.sum b/go.sum index 3af941bd1..46749f9d8 100644 --- a/go.sum +++ b/go.sum @@ -249,8 +249,6 @@ github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59P github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700 h1:eNUVfm/RFLIi1G7flU5/ZRTHvd4kcVuzfRnL6OFlzCI= github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98= -github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=