|
| 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 | +} |
0 commit comments