Prototype working with the register endpoint.

This commit is contained in:
Mariano Uvalle 2023-11-06 07:33:52 +00:00
parent c3eb1d72b4
commit a8b497d426
8 changed files with 389 additions and 1 deletions

39
cmd/cli/main.go Normal file
View file

@ -0,0 +1,39 @@
package main
import (
"encoding/base64"
)
func b64(i []byte) string {
return base64.StdEncoding.EncodeToString(i)
}
var (
priv1 = "~/dev/ccclip/keys1/private.key"
pub1 = "~/dev/ccclip/keys1/public.key"
priv2 = "~/dev/ccclip/keys2/private.key"
pub2 = "~/dev/ccclip/keys2/public.key"
)
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))
}

52
cmd/cli/register.go Normal file
View file

@ -0,0 +1,52 @@
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"github.com/AYM1607/ccclip/internal/server"
"github.com/spf13/cobra"
)
var email string
var password string
func init() {
rootCmd.AddCommand(registerCmd)
registerCmd.Flags().StringVarP(&email, "email", "e", "", "email will be your login identifier")
registerCmd.Flags().StringVarP(&password, "password", "p", "", "password will secure your account")
registerCmd.MarkFlagRequired("email")
registerCmd.MarkFlagRequired("password")
}
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)
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
}
log.Println(string(resBody))
return nil
},
}

34
cmd/cli/root.go Normal file
View file

@ -0,0 +1,34 @@
package main
import (
"log"
"github.com/spf13/cobra"
)
var keyset int
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ccclip",
Short: "copy strings to and from your end to end encrypted cloud clipboard",
Long: `copy strings to and from your end to end encrypted cloud clipboard`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
func init() {
rootCmd.PersistentFlags().IntVarP(&keyset, "keyset", "k", 0, "which key set to use, can be 1 or 2")
rootCmd.MarkPersistentFlagRequired("keyset")
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
log.Fatalln(err.Error())
}
}