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" 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 // AuthenticateUserRequest represents the body of a request to POST /auth
type AuthenticateUserRequest struct { type AuthenticateUserRequest struct {
Username string Username string
@ -13,10 +19,10 @@ type AuthenticateUserResponse struct {
Jwt string Jwt string
} }
func (n *portainerClientImp) AuthenticateUser() (token string, err error) { func (n *portainerClientImp) AuthenticateUser(options AuthenticateUserOptions) (token string, err error) {
reqBody := AuthenticateUserRequest{ reqBody := AuthenticateUserRequest{
Username: n.user, Username: options.Username,
Password: n.password, Password: options.Password,
} }
respBody := AuthenticateUserResponse{} respBody := AuthenticateUserResponse{}

View File

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

View File

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

View File

@ -3,6 +3,8 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/greenled/portainer-stack-utils/client"
"github.com/greenled/portainer-stack-utils/common" "github.com/greenled/portainer-stack-utils/common"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -15,14 +17,17 @@ 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
client, err := common.GetDefaultClient() portainerClient, err := common.GetDefaultClient()
common.CheckError(err) common.CheckError(err)
user := viper.GetString("user") user := viper.GetString("user")
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"user": user, "user": user,
}).Debug("Getting auth token") }).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) common.CheckError(err)
if viper.GetBool("login.print") { if viper.GetBool("login.print") {