Move deactivation of token usage for authentication into Authenticate()

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-02 19:30:49 -04:00
parent cdc54111a1
commit 4f3892368e
2 changed files with 7 additions and 21 deletions

View File

@ -14,10 +14,7 @@ var loginCmd = &cobra.Command{
Short: "Log in to a Portainer instance", Short: "Log in to a Portainer instance",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Get auth token // Get auth token
config := common.GetDefaultClientConfig() client, err := common.GetDefaultClient()
config.DoNotUseToken = true
client, err := common.NewClient(common.GetDefaultHttpClient(), config)
common.CheckError(err) common.CheckError(err)
authToken, err := client.Authenticate() authToken, err := client.Authenticate()

View File

@ -77,9 +77,7 @@ func (n *PortainerClient) do(uri, method string, request io.Reader, requestType
if !n.doNotUseToken { if !n.doNotUseToken {
if n.token == "" { if n.token == "" {
clientClone := n.Clone() n.token, err = n.Authenticate()
clientClone.doNotUseToken = true
n.token, err = clientClone.Authenticate()
if err != nil { if err != nil {
return return
} }
@ -144,11 +142,16 @@ func (n *PortainerClient) Authenticate() (token string, err error) {
respBody := AuthenticateUserResponse{} respBody := AuthenticateUserResponse{}
previousDoNotUseTokenValue := n.doNotUseToken
n.doNotUseToken = true
err = n.doJSON("auth", http.MethodPost, &reqBody, &respBody) err = n.doJSON("auth", http.MethodPost, &reqBody, &respBody)
if err != nil { if err != nil {
return return
} }
n.doNotUseToken = previousDoNotUseTokenValue
token = respBody.Jwt token = respBody.Jwt
return return
@ -258,20 +261,6 @@ func (n *PortainerClient) GetStatus() (status Status, err error) {
return 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 // Create a new client
func NewClient(httpClient *http.Client, config ClientConfig) (c *PortainerClient, err error) { func NewClient(httpClient *http.Client, config ClientConfig) (c *PortainerClient, err error) {
apiUrl, err := url.Parse(strings.TrimRight(config.Url, "/") + "/api/") apiUrl, err := url.Parse(strings.TrimRight(config.Url, "/") + "/api/")