feat: allow password to be taken from pipe

This commit is contained in:
Tchoupinax 2022-05-25 19:00:09 +02:00
parent 4768cd1333
commit df187e8a5f

View file

@ -1,19 +1,47 @@
package util package util
import ( import (
"bufio"
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"os"
"strings"
"syscall" "syscall"
"github.com/passbolt/go-passbolt/api" "github.com/passbolt/go-passbolt/api"
"github.com/passbolt/go-passbolt/helper" "github.com/passbolt/go-passbolt/helper"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term" "golang.org/x/term"
) )
func readPassword() (string, error) {
var fd int
var pass []byte
if terminal.IsTerminal(syscall.Stdin) {
fmt.Print("Enter Password:")
fd = syscall.Stdin
inputPass, err := terminal.ReadPassword(fd)
if err != nil {
return "", err
}
pass = inputPass
} else {
reader := bufio.NewReader(os.Stdin)
s, err := reader.ReadString('\n')
if err != nil {
return "", err
}
pass = []byte(s)
}
return strings.Replace(string(pass), "\n", "", 1), nil
}
// GetClient gets a Logged in Passbolt Client // GetClient gets a Logged in Passbolt Client
func GetClient(ctx context.Context) (*api.Client, error) { func GetClient(ctx context.Context) (*api.Client, error) {
serverAddress := viper.GetString("serverAddress") serverAddress := viper.GetString("serverAddress")
@ -28,13 +56,13 @@ func GetClient(ctx context.Context) (*api.Client, error) {
userPassword := viper.GetString("userPassword") userPassword := viper.GetString("userPassword")
if userPassword == "" { if userPassword == "" {
fmt.Print("Enter Password:") cliPassword, err := readPassword()
bytepw, err := term.ReadPassword(int(syscall.Stdin))
if err != nil { if err != nil {
fmt.Println() fmt.Println()
return nil, fmt.Errorf("Reading Password: %w", err) return nil, fmt.Errorf("Reading Password: %w", err)
} }
userPassword = string(bytepw)
userPassword = cliPassword
fmt.Println() fmt.Println()
} }