return error if pb opening browser for login

This commit is contained in:
guillaume.tardif 2020-07-07 09:29:45 +02:00 committed by Guillaume Tardif
parent 1d8e0ef9e6
commit 2d73f180ed
3 changed files with 24 additions and 26 deletions

View File

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

View File

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

View File

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