Return an error if unable to open the browser

Also use github.com/pkg/browser instead of our own code
This commit is contained in:
Djordje Lukic 2020-07-01 16:47:49 +02:00
parent c9449dbf83
commit e22b0ba676
5 changed files with 12 additions and 26 deletions

View File

@ -20,12 +20,9 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"net/url" "net/url"
"os/exec"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"time" "time"
@ -137,7 +134,9 @@ func (login AzureLoginService) Login(ctx context.Context, requestedTenantID stri
if redirectURL == "" { if redirectURL == "" {
return errors.Wrap(errdefs.ErrLoginFailed, "empty redirect URL") return errors.Wrap(errdefs.ErrLoginFailed, "empty redirect URL")
} }
login.apiHelper.openAzureLoginPage(redirectURL) if err = login.apiHelper.openAzureLoginPage(redirectURL); err != nil {
return err
}
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -302,21 +301,3 @@ func (login AzureLoginService) refreshToken(currentRefreshToken string, tenantID
return toOAuthToken(token), nil 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,21 +25,22 @@ import (
"net/url" "net/url"
"strings" "strings"
"github.com/pkg/browser"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
type apiHelper interface { type apiHelper interface {
queryToken(data url.Values, tenantID string) (azureToken, error) queryToken(data url.Values, tenantID string) (azureToken, error)
openAzureLoginPage(redirectURL string) openAzureLoginPage(redirectURL string) error
queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error)
} }
type azureAPIHelper struct{} type azureAPIHelper struct{}
func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) { func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) error {
state := randomString("", 10) state := randomString("", 10)
authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes) authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes)
openbrowser(authURL) return browser.OpenURL(authURL)
} }
func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) { func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) {

View File

@ -370,6 +370,7 @@ func (s *MockAzureHelper) queryAuthorizationAPI(authorizationURL string, authori
return args.Get(0).([]byte), args.Int(1), args.Error(2) 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) s.Called(redirectURL)
return nil
} }

1
go.mod
View File

@ -39,6 +39,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v0.1.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/pkg/errors v0.9.1
github.com/robpike/filter v0.0.0-20150108201509-2984852a2183 github.com/robpike/filter v0.0.0-20150108201509-2984852a2183
github.com/sirupsen/logrus v1.6.0 github.com/sirupsen/logrus v1.6.0

2
go.sum
View File

@ -245,6 +245,8 @@ 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 h1:eNUVfm/RFLIi1G7flU5/ZRTHvd4kcVuzfRnL6OFlzCI=
github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= 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/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.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-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=