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",
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()

View File

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