mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 10:58:21 +00:00
Merge pull request #12 from Tchoupinax/main
feat: allow password to be taken from pipe
This commit is contained in:
commit
e5c267da42
3 changed files with 36 additions and 13 deletions
|
@ -2,13 +2,11 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/passbolt/go-passbolt-cli/util"
|
"github.com/passbolt/go-passbolt-cli/util"
|
||||||
"github.com/passbolt/go-passbolt/api"
|
"github.com/passbolt/go-passbolt/api"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"golang.org/x/term"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// verifyCMD represents the verify command
|
// verifyCMD represents the verify command
|
||||||
|
@ -35,12 +33,12 @@ var verifyCMD = &cobra.Command{
|
||||||
userPassword := viper.GetString("userPassword")
|
userPassword := viper.GetString("userPassword")
|
||||||
if userPassword == "" {
|
if userPassword == "" {
|
||||||
fmt.Print("Enter Password:")
|
fmt.Print("Enter Password:")
|
||||||
bytepw, err := term.ReadPassword(int(syscall.Stdin))
|
pw, err := util.ReadPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
return fmt.Errorf("Reading Password: %w", err)
|
return fmt.Errorf("Reading Password: %w", err)
|
||||||
}
|
}
|
||||||
userPassword = string(bytepw)
|
userPassword = pw
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/passbolt/go-passbolt-cli/util"
|
"github.com/passbolt/go-passbolt-cli/util"
|
||||||
"github.com/passbolt/go-passbolt/api"
|
"github.com/passbolt/go-passbolt/api"
|
||||||
|
@ -13,7 +12,6 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/tobischo/gokeepasslib/v3"
|
"github.com/tobischo/gokeepasslib/v3"
|
||||||
w "github.com/tobischo/gokeepasslib/v3/wrappers"
|
w "github.com/tobischo/gokeepasslib/v3/wrappers"
|
||||||
"golang.org/x/term"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// KeepassExportCmd Exports a Passbolt Keepass
|
// KeepassExportCmd Exports a Passbolt Keepass
|
||||||
|
@ -56,12 +54,12 @@ func KeepassExport(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
if keepassPassword == "" {
|
if keepassPassword == "" {
|
||||||
fmt.Print("Enter Keepass Password:")
|
fmt.Print("Enter Keepass Password:")
|
||||||
bytepw, err := term.ReadPassword(int(syscall.Stdin))
|
pw, err := util.ReadPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
return fmt.Errorf("Reading Keepass Password: %w", err)
|
return fmt.Errorf("Reading Keepass Password: %w", err)
|
||||||
}
|
}
|
||||||
keepassPassword = string(bytepw)
|
keepassPassword = pw
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
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"
|
||||||
|
@ -14,6 +17,31 @@ import (
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ReadPassword reads a Password interactively or via Pipe
|
||||||
|
func ReadPassword() (string, error) {
|
||||||
|
var fd int
|
||||||
|
var pass []byte
|
||||||
|
if term.IsTerminal(syscall.Stdin) {
|
||||||
|
fmt.Print("Enter Password:")
|
||||||
|
|
||||||
|
fd = syscall.Stdin
|
||||||
|
inputPass, err := term.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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,12 +97,11 @@ func GetClient(ctx context.Context) (*api.Client, error) {
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
var code string
|
var code string
|
||||||
fmt.Print("Enter TOTP:")
|
fmt.Print("Enter TOTP:")
|
||||||
bytepw, err := term.ReadPassword(int(syscall.Stdin))
|
code, err := ReadPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
return http.Cookie{}, fmt.Errorf("Reading TOTP: %w", err)
|
return http.Cookie{}, fmt.Errorf("Reading TOTP: %w", err)
|
||||||
}
|
}
|
||||||
code = string(bytepw)
|
|
||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
req := api.MFAChallangeResponse{
|
req := api.MFAChallangeResponse{
|
||||||
TOTP: code,
|
TOTP: code,
|
||||||
|
|
Loading…
Add table
Reference in a new issue