diff --git a/client/auth.go b/client/auth.go index 71921cf..6a9d57c 100644 --- a/client/auth.go +++ b/client/auth.go @@ -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{} diff --git a/client/auth_test.go b/client/auth_test.go index 5769575..9e935f8 100644 --- a/client/auth_test.go +++ b/client/auth_test.go @@ -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") } diff --git a/client/client.go b/client/client.go index 57c8904..7e14707 100644 --- a/client/client.go +++ b/client/client.go @@ -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 } diff --git a/cmd/login.go b/cmd/login.go index 49f3191..4f157fb 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -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") {