Alexander Belanger 5 лет назад
Родитель
Сommit
978e602598
7 измененных файлов с 65 добавлено и 11 удалено
  1. 16 1
      cli/cmd/api/api.go
  2. 25 4
      cli/cmd/auth.go
  3. 10 0
      cli/cmd/config.go
  4. 1 1
      cli/cmd/errors.go
  5. 1 2
      cli/cmd/open.go
  6. 9 0
      cli/cmd/root.go
  7. 3 3
      cmd/docker-credential-porter/helper/helper.go

+ 16 - 1
cli/cmd/api/api.go

@@ -17,6 +17,7 @@ type Client struct {
 	HTTPClient     *http.Client
 	Cookie         *http.Cookie
 	CookieFilePath string
+	Token          string
 }
 
 // HTTPError is the Porter error response returned if a request fails
@@ -47,11 +48,25 @@ func NewClient(baseURL string, cookieFileName string) *Client {
 	return client
 }
 
+func NewClientWithToken(baseURL, token string) *Client {
+	client := &Client{
+		BaseURL: baseURL,
+		Token:   token,
+		HTTPClient: &http.Client{
+			Timeout: time.Minute,
+		},
+	}
+
+	return client
+}
+
 func (c *Client) sendRequest(req *http.Request, v interface{}, useCookie bool) (*HTTPError, error) {
 	req.Header.Set("Content-Type", "application/json; charset=utf-8")
 	req.Header.Set("Accept", "application/json; charset=utf-8")
 
-	if cookie, _ := c.getCookie(); useCookie && cookie != nil {
+	if c.Token != "" {
+		req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
+	} else if cookie, _ := c.getCookie(); useCookie && cookie != nil {
 		c.Cookie = cookie
 		req.AddCookie(c.Cookie)
 	}

+ 25 - 4
cli/cmd/auth.go

@@ -55,6 +55,8 @@ var logoutCmd = &cobra.Command{
 	},
 }
 
+var token string = ""
+
 func init() {
 	rootCmd.AddCommand(authCmd)
 
@@ -68,10 +70,31 @@ func init() {
 		getHost(),
 		"host url of Porter instance",
 	)
+
+	loginCmd.PersistentFlags().StringVar(
+		&token,
+		"token",
+		"",
+		"bearer token for authentication",
+	)
 }
 
 func login() error {
-	client := api.NewClient(getHost()+"/api", "cookie.json")
+	var client *api.Client
+
+	if token != "" {
+		// set the token in config
+		err := setToken(token)
+
+		if err != nil {
+			return err
+		}
+
+		client = api.NewClientWithToken(getHost()+"/api", token)
+	} else {
+		client = api.NewClient(getHost()+"/api", "cookie.json")
+	}
+
 	user, _ := client.AuthCheck(context.Background())
 
 	if user != nil {
@@ -117,8 +140,6 @@ func login() error {
 }
 
 func register() error {
-	host := getHost()
-
 	fmt.Println("Please register your admin account with an email and password:")
 
 	username, err := utils.PromptPlaintext("Email: ")
@@ -133,7 +154,7 @@ func register() error {
 		return err
 	}
 
-	client := api.NewClient(host+"/api", "cookie.json")
+	client := GetAPIClient()
 
 	resp, err := client.CreateUser(context.Background(), &api.CreateUserRequest{
 		Email:    username,

+ 10 - 0
cli/cmd/config.go

@@ -203,6 +203,12 @@ func setHost(host string) error {
 	return err
 }
 
+func setToken(token string) error {
+	viper.Set("token", token)
+	err := viper.WriteConfig()
+	return err
+}
+
 func getHost() string {
 	if host != "" {
 		return host
@@ -211,6 +217,10 @@ func getHost() string {
 	return viper.GetString("host")
 }
 
+func getToken() string {
+	return viper.GetString("token")
+}
+
 func getClusterID() uint {
 	if clusterID != 0 {
 		return clusterID

+ 1 - 1
cli/cmd/errors.go

@@ -9,7 +9,7 @@ import (
 )
 
 func checkLoginAndRun(args []string, runner func(user *api.AuthCheckResponse, client *api.Client, args []string) error) error {
-	client := api.NewClient(getHost()+"/api", "cookie.json")
+	client := GetAPIClient()
 
 	user, err := client.AuthCheck(context.Background())
 

+ 1 - 2
cli/cmd/open.go

@@ -4,7 +4,6 @@ import (
 	"context"
 	"fmt"
 
-	"github.com/porter-dev/porter/cli/cmd/api"
 	"github.com/porter-dev/porter/cli/cmd/utils"
 
 	"github.com/spf13/cobra"
@@ -14,7 +13,7 @@ var openCmd = &cobra.Command{
 	Use:   "open",
 	Short: "Opens the browser at the currently set Porter instance",
 	Run: func(cmd *cobra.Command, args []string) {
-		client := api.NewClient(getHost()+"/api", "cookie.json")
+		client := GetAPIClient()
 
 		user, err := client.AuthCheck(context.Background())
 

+ 9 - 0
cli/cmd/root.go

@@ -6,6 +6,7 @@ import (
 	"path/filepath"
 
 	"github.com/fatih/color"
+	"github.com/porter-dev/porter/cli/cmd/api"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
 	"k8s.io/client-go/util/homedir"
@@ -70,3 +71,11 @@ func Setup() {
 		viper.WriteConfig()
 	}
 }
+
+func GetAPIClient() *api.Client {
+	if token := viper.GetString("token"); token != "" {
+		return api.NewClientWithToken(getHost()+"/api", token)
+	}
+
+	return api.NewClient(getHost()+"/api", "cookie.json")
+}

+ 3 - 3
cmd/docker-credential-porter/helper/helper.go

@@ -73,7 +73,7 @@ func (p *PorterHelper) getGCR(serverURL string) (user string, secret string, err
 		host := viper.GetString("host")
 		projID := viper.GetUint("project")
 
-		client := api.NewClient(host+"/api", "cookie.json")
+		client := cmd.GetAPIClient()
 
 		// get a token from the server
 		tokenResp, err := client.GetGCRAuthorizationToken(context.Background(), projID, &api.GetGCRTokenRequest{
@@ -128,7 +128,7 @@ func (p *PorterHelper) getDOCR(serverURL string) (user string, secret string, er
 		host := viper.GetString("host")
 		projID := viper.GetUint("project")
 
-		client := api.NewClient(host+"/api", "cookie.json")
+		client := cmd.GetAPIClient()
 
 		if p.Debug {
 			log.Printf("MAKING REQUEST", host, projID)
@@ -199,7 +199,7 @@ func (p *PorterHelper) getECR(serverURL string) (user string, secret string, err
 		host := viper.GetString("host")
 		projID := viper.GetUint("project")
 
-		client := api.NewClient(host+"/api", "cookie.json")
+		client := cmd.GetAPIClient()
 
 		// get a token from the server
 		tokenResp, err := client.GetECRAuthorizationToken(context.Background(), projID, matches[3])