Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
feat: add basic auth support for tatcli (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecaruchet authored Aug 10, 2018
1 parent c363672 commit a6052d1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
15 changes: 15 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
type Client struct {
username string
password string
basicAuthUsername string
basicAuthPassword string
url string
referer string
requestTimeout time.Duration
Expand All @@ -29,6 +31,8 @@ type Client struct {
type Options struct {
Username string
Password string
BasicAuthUsername string
BasicAuthPassword string
URL string
Referer string
RequestTimeout time.Duration
Expand Down Expand Up @@ -79,6 +83,12 @@ func NewClient(opts Options) (*Client, error) {
c.maxTries = opts.MaxTries
}

// Set basic auth credentials only if the username AND the password options have been provided
if opts.BasicAuthUsername != "" && opts.BasicAuthPassword != "" {
c.basicAuthUsername = opts.BasicAuthUsername
c.basicAuthPassword = opts.BasicAuthPassword
}

return c, nil
}

Expand All @@ -87,6 +97,11 @@ func (c *Client) initHeaders(req *http.Request) error {
return ErrClientNotInitiliazed
}

// If the client is configured with basic auth credentials, add them to the request
if c.basicAuthUsername != "" && c.basicAuthPassword != "" {
req.SetBasicAuth(c.basicAuthUsername, c.basicAuthPassword)
}

req.Header.Set(TatHeaderUsername, c.username)
req.Header.Set(TatHeaderPassword, c.password)
req.Header.Set(TatHeaderXTatRefererLower, c.referer)
Expand Down
3 changes: 3 additions & 0 deletions tatcli/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (
// ShowStackTrace prints stacktrace on tatcli panic
ShowStackTrace bool

// BasicAuth enables basic auth support
BasicAuth bool

// URL of tat engine
URL string

Expand Down
31 changes: 31 additions & 0 deletions tatcli/internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/ovh/tat"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
)

var instance *tat.Client
Expand All @@ -20,10 +21,38 @@ func Client() *tat.Client {
return instance
}

// Init basic auth credentials
var basicAuthUsername, basicAuthPassword string
if viper.GetBool("basic-auth") {
// Only try to get basic auth credentials when --basic-auth flag is set

// First, try to get basic auth credentials from the configuration file
basicAuthUsername = viper.GetString("basicAuthUsername")
basicAuthPassword = viper.GetString("basicAuthPassword")

// If the username is not defined, ask the user to provide it
if basicAuthUsername == "" {
fmt.Println("No basic auth username found")
fmt.Print("Please enter your username: ")
fmt.Scan(&basicAuthUsername)
}

// If the password is not defined, ask the user to provide it
if basicAuthPassword == "" {
fmt.Printf("No basic auth password found for username %v\n", basicAuthUsername)
fmt.Printf("Please enter the password for username %v: ", basicAuthUsername)
pwd, errReadPassword := terminal.ReadPassword(0)
Check(errReadPassword)
basicAuthPassword = string(pwd)
}
}

tc, err := tat.NewClient(tat.Options{
URL: viper.GetString("url"),
Username: viper.GetString("username"),
Password: viper.GetString("password"),
BasicAuthUsername: basicAuthUsername, // Always send the basic auth credentials (if --basic-auth is not set, the value will be empty)
BasicAuthPassword: basicAuthPassword, // Always send the basic auth credentials (if --basic-auth is not set, the value will be empty)
Referer: "tatcli.v." + tat.Version,
SSLInsecureSkipVerify: viper.GetBool("sslInsecureSkipVerify"),
})
Expand All @@ -38,6 +67,8 @@ func Client() *tat.Client {
tat.IsDebug = true
}

// Set the instance for future use and return it
instance = tc
return tc
}

Expand Down
2 changes: 2 additions & 0 deletions tatcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func main() {
rootCmd.PersistentFlags().BoolVarP(&internal.Pretty, "pretty", "t", false, "Pretty Print Json Output")
rootCmd.PersistentFlags().BoolVarP(&internal.ShowStackTrace, "showStackTrace", "", false, "Show Stack Trace if tatcli panic")
rootCmd.PersistentFlags().BoolVarP(&internal.SSLInsecureSkipVerify, "sslInsecureSkipVerify", "k", false, "Skip certificate check with SSL connection")
rootCmd.PersistentFlags().BoolVarP(&internal.BasicAuth, "basic-auth", "", false, "Enable basic auth support")
rootCmd.PersistentFlags().StringVarP(&internal.URL, "url", "", "", "URL Tat Engine, facultative if you have a "+home+"/.tatcli/config.json file")
rootCmd.PersistentFlags().StringVarP(&internal.TatwebuiURL, "tatwebui-url", "", "", "URL of Tat WebUI, facultative")
rootCmd.PersistentFlags().StringVarP(&internal.Username, "username", "u", "", "username, facultative if you have a "+home+"/.tatcli/config.json file")
Expand All @@ -45,6 +46,7 @@ func main() {
viper.BindPFlag("tatwebui-url", rootCmd.PersistentFlags().Lookup("tatwebui-url"))
viper.BindPFlag("username", rootCmd.PersistentFlags().Lookup("username"))
viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password"))
viper.BindPFlag("basic-auth", rootCmd.PersistentFlags().Lookup("basic-auth"))
viper.BindPFlag("sslInsecureSkipVerify", rootCmd.PersistentFlags().Lookup("sslInsecureSkipVerify"))

if err := rootCmd.Execute(); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions tatcli/ui/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"github.com/gizak/termui"
"github.com/ovh/tat/tatcli/internal"
"github.com/spf13/cobra"
)

Expand All @@ -25,6 +26,11 @@ Example:

func runUI(args []string) {
ui := &tatui{}

// This forces a client init before starting the UI
// This way, if the client needs to ask for credentials, it will be done now
internal.Client().Version()

ui.init(args)
ui.draw(0)

Expand Down

0 comments on commit a6052d1

Please sign in to comment.