Add username and password to PortainerClient.AuthenticateUser()

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-26 02:53:42 -04:00
parent 854db31543
commit 1efa02abf3
4 changed files with 25 additions and 8 deletions

View File

@ -2,6 +2,12 @@ package client
import "net/http"
// AuthenticateUserOptions represents options passed to PortainerClient.AuthenticateUser()
type AuthenticateUserOptions struct {
Username string
Password string
}
// AuthenticateUserRequest represents the body of a request to POST /auth
type AuthenticateUserRequest struct {
Username string
@ -13,10 +19,10 @@ type AuthenticateUserResponse struct {
Jwt string
}
func (n *portainerClientImp) AuthenticateUser() (token string, err error) {
func (n *portainerClientImp) AuthenticateUser(options AuthenticateUserOptions) (token string, err error) {
reqBody := AuthenticateUserRequest{
Username: n.user,
Password: n.password,
Username: options.Username,
Password: options.Password,
}
respBody := AuthenticateUserResponse{}

View File

@ -42,7 +42,10 @@ func TestClientAuthenticates(t *testing.T) {
Password: "a",
UserAgent: "GE007",
})
token, err := customClient.AuthenticateUser()
token, err := customClient.AuthenticateUser(AuthenticateUserOptions{
Username: "admin",
Password: "a",
})
assert.Nil(t, err)
assert.Equal(t, token, "somerandomtoken")
}

View File

@ -23,7 +23,7 @@ type Config struct {
// PortainerClient represents a Portainer API client
type PortainerClient interface {
// AuthenticateUser a user to get an auth token
AuthenticateUser() (token string, err error)
AuthenticateUser(options AuthenticateUserOptions) (token string, err error)
// Get endpoints
EndpointList() ([]portainer.Endpoint, error)
@ -155,7 +155,10 @@ func (n *portainerClientImp) doJSON(uri, method string, headers http.Header, req
func (n *portainerClientImp) doJSONWithToken(uri, method string, headers http.Header, request interface{}, response interface{}) (err error) {
// Ensure there is an auth token
if n.token == "" {
n.token, err = n.AuthenticateUser()
n.token, err = n.AuthenticateUser(AuthenticateUserOptions{
Username: n.user,
Password: n.password,
})
if err != nil {
return
}

View File

@ -3,6 +3,8 @@ package cmd
import (
"fmt"
"github.com/greenled/portainer-stack-utils/client"
"github.com/greenled/portainer-stack-utils/common"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -15,14 +17,17 @@ var loginCmd = &cobra.Command{
Short: "Log in to a Portainer instance",
Run: func(cmd *cobra.Command, args []string) {
// Get auth token
client, err := common.GetDefaultClient()
portainerClient, err := common.GetDefaultClient()
common.CheckError(err)
user := viper.GetString("user")
logrus.WithFields(logrus.Fields{
"user": user,
}).Debug("Getting auth token")
authToken, err := client.AuthenticateUser()
authToken, err := portainerClient.AuthenticateUser(client.AuthenticateUserOptions{
Username: viper.GetString("user"),
Password: viper.GetString("password"),
})
common.CheckError(err)
if viper.GetBool("login.print") {