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" "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,10 @@ 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 +302,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

@ -23,6 +23,8 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"net/url" "net/url"
"os/exec"
"runtime"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -30,16 +32,16 @@ import (
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 openbrowser(authURL)
} }
func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) { 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 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 ( var (
letterRunes = []rune("abcdefghijklmnopqrstuvwxyz123456789") 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) 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
} }