Skip to content

Commit 97ef59f

Browse files
committed
add the Verify command
1 parent 07c02c6 commit 97ef59f

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

cmd/verify.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"syscall"
6+
7+
"github.com/speatzle/go-passbolt-cli/util"
8+
"github.com/speatzle/go-passbolt/api"
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
"golang.org/x/term"
12+
)
13+
14+
// verifyCMD represents the verify command
15+
var verifyCMD = &cobra.Command{
16+
Use: "verify",
17+
Short: "Verify Setup the Server Verification",
18+
Long: `Verify Setup the Server Verification. You need to run this once after that the Server will always be verified if the same config is used`,
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
ctx := util.GetContext()
21+
22+
viper.Set("serverVerifyToken", "")
23+
viper.Set("serverVerifyEncToken", "")
24+
25+
serverAddress := viper.GetString("serverAddress")
26+
if serverAddress == "" {
27+
return fmt.Errorf("serverAddress is not defined")
28+
}
29+
30+
userPrivateKey := viper.GetString("userPrivateKey")
31+
if userPrivateKey == "" {
32+
return fmt.Errorf("userPrivateKey is not defined")
33+
}
34+
35+
userPassword := viper.GetString("userPassword")
36+
if userPassword == "" {
37+
fmt.Print("Enter Password:")
38+
bytepw, err := term.ReadPassword(int(syscall.Stdin))
39+
if err != nil {
40+
fmt.Println()
41+
return fmt.Errorf("Reading Password: %w", err)
42+
}
43+
userPassword = string(bytepw)
44+
fmt.Println()
45+
}
46+
47+
client, err := api.NewClient(nil, "", serverAddress, userPrivateKey, userPassword)
48+
if err != nil {
49+
return fmt.Errorf("Creating Client: %w", err)
50+
}
51+
52+
client.Debug = viper.GetBool("debug")
53+
54+
token, enctoken, err := client.SetupServerVerification(ctx)
55+
if err != nil {
56+
return fmt.Errorf("Setup Verification: %w", err)
57+
}
58+
viper.Set("serverVerifyToken", token)
59+
viper.Set("serverVerifyEncToken", enctoken)
60+
61+
if viper.ConfigFileUsed() == "" {
62+
err := viper.SafeWriteConfig()
63+
if err != nil {
64+
return fmt.Errorf("Writing Config: %w", err)
65+
}
66+
} else {
67+
err := viper.WriteConfig()
68+
if err != nil {
69+
return fmt.Errorf("Writing Config: %w", err)
70+
}
71+
}
72+
fmt.Println("Verification Enabled")
73+
return nil
74+
},
75+
}
76+
77+
func init() {
78+
rootCmd.AddCommand(verifyCMD)
79+
}

util/client.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func GetClient(ctx context.Context) (*api.Client, error) {
3131
fmt.Print("Enter Password:")
3232
bytepw, err := term.ReadPassword(int(syscall.Stdin))
3333
if err != nil {
34-
fmt.Println("\n")
34+
fmt.Println()
3535
return nil, fmt.Errorf("Reading Password: %w", err)
3636
}
3737
userPassword = string(bytepw)
38-
fmt.Println("\n")
38+
fmt.Println()
3939
}
4040

4141
client, err := api.NewClient(nil, "", serverAddress, userPrivateKey, userPassword)
@@ -45,6 +45,16 @@ func GetClient(ctx context.Context) (*api.Client, error) {
4545

4646
client.Debug = viper.GetBool("debug")
4747

48+
token := viper.GetString("serverVerifyToken")
49+
encToken := viper.GetString("serverVerifyEncToken")
50+
51+
if token != "" {
52+
err = client.VerifyServer(ctx, token, encToken)
53+
if err != nil {
54+
return nil, fmt.Errorf("Verifing Server: %w", err)
55+
}
56+
}
57+
4858
switch viper.GetString("mfaMode") {
4959
case "interactive-totp":
5060
client.MFACallback = func(ctx context.Context, c *api.Client, res *api.APIResponse) (http.Cookie, error) {

0 commit comments

Comments
 (0)