Revert "Return an error if unable to open the browser"

This reverts commit e22b0ba676.
This commit is contained in:
guillaume.tardif 2020-07-07 09:19:06 +02:00
parent 43d8f2fa18
commit 1d8e0ef9e6
5 changed files with 26 additions and 12 deletions

View File

@ -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)
}
}

View File

@ -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) {

View File

@ -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
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=