From 4f3892368ead1228bf7b47fb4a1289b1adced6d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Fri, 2 Aug 2019 19:30:49 -0400 Subject: [PATCH] Move deactivation of token usage for authentication into Authenticate() --- cmd/login.go | 5 +---- common/client.go | 23 ++++++----------------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/cmd/login.go b/cmd/login.go index 8283509..206135d 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -14,10 +14,7 @@ var loginCmd = &cobra.Command{ Short: "Log in to a Portainer instance", Run: func(cmd *cobra.Command, args []string) { // Get auth token - config := common.GetDefaultClientConfig() - config.DoNotUseToken = true - - client, err := common.NewClient(common.GetDefaultHttpClient(), config) + client, err := common.GetDefaultClient() common.CheckError(err) authToken, err := client.Authenticate() diff --git a/common/client.go b/common/client.go index 164d9e9..2ff3494 100644 --- a/common/client.go +++ b/common/client.go @@ -77,9 +77,7 @@ func (n *PortainerClient) do(uri, method string, request io.Reader, requestType if !n.doNotUseToken { if n.token == "" { - clientClone := n.Clone() - clientClone.doNotUseToken = true - n.token, err = clientClone.Authenticate() + n.token, err = n.Authenticate() if err != nil { return } @@ -144,11 +142,16 @@ func (n *PortainerClient) Authenticate() (token string, err error) { respBody := AuthenticateUserResponse{} + previousDoNotUseTokenValue := n.doNotUseToken + n.doNotUseToken = true + err = n.doJSON("auth", http.MethodPost, &reqBody, &respBody) if err != nil { return } + n.doNotUseToken = previousDoNotUseTokenValue + token = respBody.Jwt return @@ -258,20 +261,6 @@ func (n *PortainerClient) GetStatus() (status Status, err error) { return } -// Get a clone of the client -func (n *PortainerClient) Clone() (c *PortainerClient) { - c = &PortainerClient{ - httpClient: n.httpClient, - url: n.url, - user: n.user, - password: n.password, - token: n.token, - doNotUseToken: n.doNotUseToken, - } - - return -} - // Create a new client func NewClient(httpClient *http.Client, config ClientConfig) (c *PortainerClient, err error) { apiUrl, err := url.Parse(strings.TrimRight(config.Url, "/") + "/api/")