register, register device and get devices working.
This commit is contained in:
parent
a8b497d426
commit
320dc46010
14 changed files with 405 additions and 65 deletions
39
cmd/cli/getDevices.go
Normal file
39
cmd/cli/getDevices.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/configfile"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(getDevicesCmd)
|
||||
}
|
||||
|
||||
var getDevicesCmd = &cobra.Command{
|
||||
Use: "get-devices",
|
||||
Short: "Register a user with a given email and password",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cc, err := configfile.EnsureAndGet()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cc.DeviceId == "" {
|
||||
return errors.New("your device is not registered")
|
||||
}
|
||||
pvk, err := configfile.LoadPrivateKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
devices, err := apiclient.GetDevices(cc.DeviceId, pvk)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.NewEncoder(os.Stdout).Encode(devices)
|
||||
},
|
||||
}
|
||||
|
|
@ -2,38 +2,20 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/server/client"
|
||||
)
|
||||
|
||||
func b64(i []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(i)
|
||||
}
|
||||
|
||||
var (
|
||||
priv1 = "~/dev/ccclip/keys1/private.key"
|
||||
pub1 = "~/dev/ccclip/keys1/public.key"
|
||||
var apiclient *client.Client
|
||||
|
||||
priv2 = "~/dev/ccclip/keys2/private.key"
|
||||
pub2 = "~/dev/ccclip/keys2/public.key"
|
||||
)
|
||||
func init() {
|
||||
apiclient = client.New("http://localhost:8080")
|
||||
}
|
||||
|
||||
func main() {
|
||||
rootCmd.Execute()
|
||||
// key1 := crypto.LoadPrivateKey("../keys1/private.key")
|
||||
// key2 := crypto.LoadPrivateKey("../keys2/private.key")
|
||||
|
||||
// secretMsg := "new-some-secret-messageeee"
|
||||
|
||||
// encrypted := crypto.Encrypt(
|
||||
// crypto.NewSharedKey(key1, key2.PublicKey(), crypto.SendDirection),
|
||||
// []byte(secretMsg),
|
||||
// )
|
||||
|
||||
// fmt.Printf("Message %q was encrypted to %q\n", secretMsg, b64(encrypted))
|
||||
|
||||
// decrypted := crypto.Decrypt(
|
||||
// crypto.NewSharedKey(key2, key1.PublicKey(), crypto.ReceiveDirection),
|
||||
// encrypted,
|
||||
// )
|
||||
|
||||
// fmt.Printf("Message was decrypted as %q\n", string(decrypted))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"fmt"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/server"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/configfile"
|
||||
)
|
||||
|
||||
var email string
|
||||
|
|
@ -28,25 +25,16 @@ var registerCmd = &cobra.Command{
|
|||
Use: "register",
|
||||
Short: "Register a user with a given email and password",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
req := server.RegisterRequest{
|
||||
Email: email,
|
||||
Password: password,
|
||||
}
|
||||
reqJson, err := json.Marshal(req)
|
||||
err := apiclient.Register(email, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := http.Post("http://localhost:8080/register", "application/json", bytes.NewReader(reqJson))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resBody, err := io.ReadAll(res.Body)
|
||||
defer res.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not register user: %w", err)
|
||||
}
|
||||
|
||||
log.Println(string(resBody))
|
||||
return nil
|
||||
cc, err := configfile.EnsureAndGet()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cc.Email = email
|
||||
return configfile.Write(cc)
|
||||
},
|
||||
}
|
||||
|
|
|
|||
58
cmd/cli/registerDevice.go
Normal file
58
cmd/cli/registerDevice.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/configfile"
|
||||
"github.com/AYM1607/ccclip/pkg/crypto"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(registerDeviceCommand)
|
||||
|
||||
registerDeviceCommand.Flags().StringVarP(&password, "password", "p", "", "password for your account")
|
||||
|
||||
registerDeviceCommand.MarkFlagRequired("password")
|
||||
}
|
||||
|
||||
var registerDeviceCommand = &cobra.Command{
|
||||
Use: "register-device",
|
||||
Short: "Register a device for the given user",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
cc, err := configfile.EnsureAndGet()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cc.Email == "" {
|
||||
return errors.New("you don't have an account configured for thist device")
|
||||
}
|
||||
|
||||
if cc.DeviceId != "" {
|
||||
return errors.New("this device is already registered")
|
||||
}
|
||||
|
||||
pvk := crypto.NewPrivateKey()
|
||||
pbk := pvk.PublicKey()
|
||||
|
||||
res, err := apiclient.RegisterDevice(cc.Email, password, pbk.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write the key files first, if those fail to write then we should not
|
||||
// save the device Id.
|
||||
cc.DeviceId = res.DeviceID
|
||||
err = configfile.SavePrivateKey(pvk)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = configfile.SavePublicKey(pbk)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return configfile.Write(cc)
|
||||
},
|
||||
}
|
||||
|
|
@ -3,11 +3,10 @@ package main
|
|||
import (
|
||||
"log"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/configfile"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var keyset int
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "ccclip",
|
||||
|
|
@ -19,9 +18,9 @@ var rootCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().IntVarP(&keyset, "keyset", "k", 0, "which key set to use, can be 1 or 2")
|
||||
rootCmd.PersistentFlags().StringVarP(&configfile.Path, "config-path", "c", "", "directory where to store the config file")
|
||||
|
||||
rootCmd.MarkPersistentFlagRequired("keyset")
|
||||
rootCmd.MarkPersistentFlagRequired("config-path")
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
|
|
|
|||
1
cmd/cli/util.go
Normal file
1
cmd/cli/util.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package main
|
||||
Loading…
Add table
Add a link
Reference in a new issue