Add register and verify commands

This commit is contained in:
Finn 2020-12-11 03:39:52 -08:00
parent 10ff70408d
commit 0f7100527b
3 changed files with 125 additions and 1 deletions

View file

@ -1,4 +1,4 @@
// Copyright © 2018 Finn Herzfeld <finn@janky.solutions>
// Copyright © 2020 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

View file

@ -0,0 +1,58 @@
// Copyright © 2020 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 cmd
import (
"fmt"
"log"
"math/rand"
"github.com/spf13/cobra"
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
)
// registerCmd represents the register command
var registerCmd = &cobra.Command{
Use: "register",
Short: "Register a new account.",
Long: `Register a new signal account, generating new keys if the number was previously registered.`,
Run: func(cmd *cobra.Command, args []string) {
requestID := fmt.Sprint("signald-cli-", rand.Intn(1000))
err := s.RawRequest(v0.LegacyRequest{
Type: "register",
Username: username,
})
if err != nil {
log.Fatal("error sending request: ", err)
}
go s.Listen(nil)
r := <-s.GetResponseListener(requestID)
account, err := r.Data.MarshalJSON()
if err != nil {
log.Fatal("error rendering response:", err)
}
fmt.Println("validation code requested. submit the code using signald-cli verify", string(account))
},
}
func init() {
RootCmd.AddCommand(registerCmd)
registerCmd.Flags().StringVarP(&username, "username", "u", "", "The phone number of the new account to register)")
must(registerCmd.MarkFlagRequired("username"))
}

View file

@ -0,0 +1,66 @@
// Copyright © 2020 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 cmd
import (
"fmt"
"log"
"math/rand"
"github.com/spf13/cobra"
"gitlab.com/signald/signald-go/signald/client-protocol/v0"
)
var (
code string
)
// verify represents the verify command
var verify = &cobra.Command{
Use: "verify",
Short: "Verify an account confirmation code and complete account setup",
Long: `Verify the phone number on a new account by submitting a verification code, completing the account registration process.`,
Run: func(cmd *cobra.Command, args []string) {
requestID := fmt.Sprint("signald-cli-", rand.Intn(1000))
err := s.RawRequest(v0.LegacyRequest{
Type: "verify",
Username: username,
Code: code,
})
if err != nil {
log.Fatal("error sending request: ", err)
}
go s.Listen(nil)
r := <-s.GetResponseListener(requestID)
account, err := r.Data.MarshalJSON()
if err != nil {
log.Fatal("error rendering response:", err)
}
fmt.Println("account created:", string(account))
},
}
func init() {
RootCmd.AddCommand(verify)
verify.Flags().StringVarP(&username, "username", "u", "", "The phone number the verification code was sent to")
must(verify.MarkFlagRequired("username"))
verify.Flags().StringVarP(&code, "code", "c", "", "The verification code")
must(verify.MarkFlagRequired("code"))
}