Add signaldctl create account
This commit is contained in:
parent
a822a2785c
commit
c824084ea9
8 changed files with 344 additions and 7 deletions
130
cmd/signaldctl/cmd/create/account/account.go
Normal file
130
cmd/signaldctl/cmd/create/account/account.go
Normal file
|
@ -0,0 +1,130 @@
|
|||
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
|
||||
//
|
||||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
86
cmd/signaldctl/cmd/create/account/link.go
Normal file
86
cmd/signaldctl/cmd/create/account/link.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
|
||||
//
|
||||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
45
cmd/signaldctl/cmd/create/account/request-code.go
Normal file
45
cmd/signaldctl/cmd/create/account/request-code.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
|
||||
//
|
||||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
45
cmd/signaldctl/cmd/create/account/verify.go
Normal file
45
cmd/signaldctl/cmd/create/account/verify.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
|
||||
//
|
||||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
28
cmd/signaldctl/cmd/create/root.go
Normal file
28
cmd/signaldctl/cmd/create/root.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
|
||||
//
|
||||
// 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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)
|
||||
}
|
|
@ -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.
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue