From c824084ea9030a745ae1a4968db94aa04ad09be1 Mon Sep 17 00:00:00 2001 From: Finn Date: Sun, 31 Jan 2021 04:59:36 -0800 Subject: [PATCH] Add signaldctl create account --- cmd/signaldctl/cmd/create/account/account.go | 130 ++++++++++++++++++ cmd/signaldctl/cmd/create/account/link.go | 86 ++++++++++++ .../cmd/create/account/request-code.go | 45 ++++++ cmd/signaldctl/cmd/create/account/verify.go | 45 ++++++ cmd/signaldctl/cmd/create/root.go | 28 ++++ cmd/signaldctl/cmd/root.go | 4 +- cmd/signaldctl/common/output-formats.go | 11 +- signald/utils.go | 2 +- 8 files changed, 344 insertions(+), 7 deletions(-) create mode 100644 cmd/signaldctl/cmd/create/account/account.go create mode 100644 cmd/signaldctl/cmd/create/account/link.go create mode 100644 cmd/signaldctl/cmd/create/account/request-code.go create mode 100644 cmd/signaldctl/cmd/create/account/verify.go create mode 100644 cmd/signaldctl/cmd/create/root.go diff --git a/cmd/signaldctl/cmd/create/account/account.go b/cmd/signaldctl/cmd/create/account/account.go new file mode 100644 index 0000000..90dd1d5 --- /dev/null +++ b/cmd/signaldctl/cmd/create/account/account.go @@ -0,0 +1,130 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package account + +import ( + "log" + + "github.com/spf13/cobra" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/signald/client-protocol/v0" +) + +var ( + actionLink bool + actionRequestCode bool + actionVerify bool + + phoneNumber string + code string + voice bool + noWait bool + + CreateAccountCmd = &cobra.Command{ + Use: "account", + Aliases: []string{"accounts"}, + Short: "create an account", + PreRun: func(_ *cobra.Command, _ []string) { + + actionCount := 0 + if actionLink { + actionCount++ + } + if actionRequestCode { + actionCount++ + } + if actionVerify { + actionCount++ + } + if actionCount > 1 || (actionCount == 0 && common.OutputFormat != common.OutputFormatInteractive) { + log.Fatal("invalid arguments: must select exactly one of --request-code, --verify or --link") + } + + if actionLink { + if phoneNumber != "" { + log.Fatal("cannot use --phone-number with --link") + } + if code != "" { + log.Fatal("cannot use --code with --link") + } + if voice { + log.Fatal("cannot use --voice with --link") + } + } + + if actionRequestCode { + if phoneNumber == "" { + log.Fatal("--phone-number required") + } + if code != "" { + log.Fatal("cannot use --code with --request-code") + } + if noWait { + log.Fatal("cannot use --no-wait with --request-code") + } + } + + if actionVerify { + if phoneNumber == "" { + log.Fatal("--phone-number required") + } + if code == "" { + log.Fatal("--code required") + } + if voice { + log.Fatal("cannot use --voice with --verify") + } + if noWait { + log.Fatal("cannot use --no-wait with --verify") + } + } + }, + Run: func(_ *cobra.Command, _ []string) { + if actionLink { + link() + } + + if actionRequestCode { + requestCode() + } + + if actionVerify { + verify() + } + }, + } +) + +func init() { + CreateAccountCmd.Flags().BoolVarP(&actionLink, "link", "l", false, "link an existing account") + CreateAccountCmd.Flags().BoolVarP(&actionRequestCode, "request-code", "r", false, "request a verification code") + CreateAccountCmd.Flags().BoolVarP(&actionVerify, "verify", "v", false, "submit a verification code and complete a new account registration") + + CreateAccountCmd.Flags().StringVarP(&phoneNumber, "number", "n", "", "only with --request-code or --verify. phone number being registered") + CreateAccountCmd.Flags().StringVarP(&code, "code", "c", "", "only with --verify. verification code to submit") + CreateAccountCmd.Flags().BoolVarP(&voice, "voice", "V", false, "only with --request-code. request verification code be sent via an automated voice call (code is sent via SMS by default)") + CreateAccountCmd.Flags().BoolVar(&noWait, "no-wait", false, "only with --link. return after the linking URI is printed to stdout. By default, --link will wait until the server acknowledges that the linking has completed") +} + +func getResponse(c chan v0.LegacyResponse, id string) v0.LegacyResponse { + for { + message := <-c + if message.ID == id { + return message + } + } +} diff --git a/cmd/signaldctl/cmd/create/account/link.go b/cmd/signaldctl/cmd/create/account/link.go new file mode 100644 index 0000000..b490e4f --- /dev/null +++ b/cmd/signaldctl/cmd/create/account/link.go @@ -0,0 +1,86 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package account + +import ( + "encoding/json" + "log" + "os" + + "github.com/jedib0t/go-pretty/v6/table" + "github.com/mdp/qrterminal" + "gopkg.in/yaml.v2" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/signald" + "gitlab.com/signald/signald-go/signald/client-protocol/v0" +) + +func link() { + requestID := signald.GenerateID() + err := common.Signald.RawRequest(v0.LegacyRequest{Type: "link", ID: requestID}) + if err != nil { + log.Fatal("error sending request: ", err) + } + c := make(chan v0.LegacyResponse) + go common.Signald.Listen(c) + uri := getResponse(c, requestID) + if uri.Type != "linking_uri" { + log.Fatalf("unexpected response from signald when requesting link: %+v", uri) + } + + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(uri.Data.URI) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(uri.Data.URI) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatCSV, common.OutputFormatTable: + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"URI"}) + t.AppendRow(table.Row{uri.Data.URI}) + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + t.Render() + } + case common.OutputFormatInteractive, common.OutputFormatDefault: + qrterminal.Generate(uri.Data.URI, qrterminal.M, os.Stdout) + default: + log.Fatal("unsupported output format") + } + + if noWait { + return + } + + finish := getResponse(c, requestID) + if finish.Type == "linking_successful" { + log.Println("linking successful") + return + } + if finish.Type == "linking_error" { + log.Fatal("error from signald:", finish.Data.Message) + } else { + log.Fatal("unexpected message from signald:", finish.Data.Message) + } +} diff --git a/cmd/signaldctl/cmd/create/account/request-code.go b/cmd/signaldctl/cmd/create/account/request-code.go new file mode 100644 index 0000000..5d069b0 --- /dev/null +++ b/cmd/signaldctl/cmd/create/account/request-code.go @@ -0,0 +1,45 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package account + +import ( + "log" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/signald" + "gitlab.com/signald/signald-go/signald/client-protocol/v0" +) + +func requestCode() { + requestID := signald.GenerateID() + err := common.Signald.RawRequest(v0.LegacyRequest{ + Type: "register", + ID: requestID, + Username: phoneNumber, + Voice: voice, + }) + if err != nil { + log.Fatal("error sending request to signald: ", err) + } + c := make(chan v0.LegacyResponse) + go common.Signald.Listen(c) + response := getResponse(c, requestID) + if response.Type == "verification_required" { + log.Fatal("verification code requested. re-run with --verify") + } else { + log.Fatalf("unexpected response from signald when requesting verification code: %+v", response) + } +} diff --git a/cmd/signaldctl/cmd/create/account/verify.go b/cmd/signaldctl/cmd/create/account/verify.go new file mode 100644 index 0000000..97f1e64 --- /dev/null +++ b/cmd/signaldctl/cmd/create/account/verify.go @@ -0,0 +1,45 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package account + +import ( + "log" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/signald" + "gitlab.com/signald/signald-go/signald/client-protocol/v0" +) + +func verify() { + requestID := signald.GenerateID() + err := common.Signald.RawRequest(v0.LegacyRequest{ + Type: "verify", + ID: requestID, + Username: phoneNumber, + Code: code, + }) + if err != nil { + log.Fatal("error sending request to signald: ", err) + } + c := make(chan v0.LegacyResponse) + go common.Signald.Listen(c) + response := getResponse(c, requestID) + if response.Type == "verification_succeeded" { + log.Fatal("verification code requested. re-run with --verify") + } else { + log.Fatalf("unexpected response from signald when requesting verification code: %+v", response) + } +} diff --git a/cmd/signaldctl/cmd/create/root.go b/cmd/signaldctl/cmd/create/root.go new file mode 100644 index 0000000..e623c70 --- /dev/null +++ b/cmd/signaldctl/cmd/create/root.go @@ -0,0 +1,28 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package create + +import ( + "github.com/spf13/cobra" + + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/create/account" +) + +var CreateCmd = &cobra.Command{Use: "create"} + +func init() { + CreateCmd.AddCommand(account.CreateAccountCmd) +} diff --git a/cmd/signaldctl/cmd/root.go b/cmd/signaldctl/cmd/root.go index 1519586..74fa33d 100644 --- a/cmd/signaldctl/cmd/root.go +++ b/cmd/signaldctl/cmd/root.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/create" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/get" "gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/signald" @@ -58,8 +59,9 @@ func init() { cobra.OnInitialize(initConfig) RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.signaldctl.yaml)") RootCmd.PersistentFlags().StringVarP(&socketPath, "socket", "s", "/var/run/signald/signald.sock", "the path to the signald socket file") - RootCmd.PersistentFlags().StringVarP(&common.OutputFormat, "output-format", "o", "table", "the output format. Options are table (default), json or yaml") + RootCmd.PersistentFlags().StringVarP(&common.OutputFormat, "output-format", "o", "default", "the output format. Options are table (default), json or yaml") RootCmd.AddCommand(get.GetCmd) + RootCmd.AddCommand(create.CreateCmd) } // initConfig reads in config file and ENV variables if set. diff --git a/cmd/signaldctl/common/output-formats.go b/cmd/signaldctl/common/output-formats.go index c73af1b..3e5aa62 100644 --- a/cmd/signaldctl/common/output-formats.go +++ b/cmd/signaldctl/common/output-formats.go @@ -1,9 +1,10 @@ package common const ( - OutputFormatDefault = "default" - OutputFormatCSV = "csv" - OutputFormatTable = "table" - OutputFormatJSON = "json" - OutputFormatYAML = "yaml" + OutputFormatDefault = "default" + OutputFormatCSV = "csv" + OutputFormatTable = "table" + OutputFormatJSON = "json" + OutputFormatYAML = "yaml" + OutputFormatInteractive = "interactive" ) diff --git a/signald/utils.go b/signald/utils.go index 4e0fa3d..58cea90 100644 --- a/signald/utils.go +++ b/signald/utils.go @@ -8,7 +8,7 @@ const idsize = 10 var charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789") -// GenerateID is a helper function to generate random request IDs. +// GenerateID is a helper function to generate random request IDs. func GenerateID() string { id := make([]rune, idsize) for i := range id {