π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add support for storing accounts
Neeraj Gupta1b45bc0e0c70
diff --git a/cmd/account.go b/cmd/account.goindex 0ea4141794..0a1e30f877 100644--- a/cmd/account.go+++ b/cmd/account.go@@ -33,7 +33,6 @@ var addAccCmd = &cobra.Command{}func init() {-// Add 'config' subcommands to the root commandrootCmd.AddCommand(accountCmd)// Add 'config' subcommands to the 'config' commanddiff --git a/internal/api/client.go b/internal/api/client.goindex ed82b1c061..fde874bd46 100644--- a/internal/api/client.go+++ b/internal/api/client.go@@ -31,6 +31,7 @@ func readValueFromContext(ctx context.Context, key string) interface{} {value := ctx.Value(key)return value}+func NewClient(p Params) *Client {enteAPI := resty.New()if p.Trace {diff --git a/pkg/account.go b/pkg/account.goindex 2809edf5d7..324aff557f 100644--- a/pkg/account.go+++ b/pkg/account.go@@ -7,7 +7,6 @@ import ("encoding/json""fmt""log"- "runtime"bolt "go.etcd.io/bbolt")@@ -15,15 +14,9 @@ import (const AccBucket = "accounts"func (c *ClICtrl) AddAccount(cxt context.Context) {+var flowErr errordefer func() {- if r := recover(); r != nil {- fmt.Println("Panic occurred:", r)- // Print the stack trace- stackTrace := make([]byte, 1024*8)- stackTrace = stackTrace[:runtime.Stack(stackTrace, false)]- fmt.Printf("Stack Trace:\n%s", stackTrace)- }if flowErr != nil {log.Fatal(flowErr)}@@ -60,42 +53,71 @@ func (c *ClICtrl) AddAccount(cxt context.Context) {authResponse, flowErr = c.validateTOTP(cxt, authResponse)}if authResponse.EncryptedToken == "" || authResponse.KeyAttributes == nil {- log.Fatal("no encrypted token or keyAttributes")- return+ panic("no encrypted token or keyAttributes")}masterKey, token, decErr := c.decryptMasterKeyAndToken(cxt, authResponse, keyEncKey)if decErr != nil {flowErr = decErrreturn}- // print length- fmt.Printf("master key length: %d\n", len(masterKey))- fmt.Printf("token length: %d\n", len(token))+ err := c.storeAccount(cxt, email, authResponse.ID, app, masterKey, token)+ if err != nil {+ flowErr = err+ return+ } else {+ fmt.Println("Account added successfully")+ }+}++func (c *ClICtrl) storeAccount(ctx context.Context, email string, userID int64, app api.App, masterKey, token []byte) error {+ // get password+ secret := GetOrCreateClISecret()+ err := c.DB.Update(func(tx *bolt.Tx) error {+ b, err := tx.CreateBucketIfNotExists([]byte(AccBucket))+ if err != nil {+ return err+ }+ accInfo := model.AccountInfo{+ Email: email,+ UserID: userID,+ MasterKey: *model.MakeEncString(string(masterKey), secret),+ Token: *model.MakeEncString(string(token), secret),+ App: app,+ }+ accInfoBytes, err := json.Marshal(accInfo)+ if err != nil {+ return err+ }+ accountKey := fmt.Sprintf("%s-%d", app, userID)+ return b.Put([]byte(accountKey), accInfoBytes)+ })+ return err}func (c *ClICtrl) ListAccounts(cxt context.Context) error {err := c.DB.View(func(tx *bolt.Tx) error {b := tx.Bucket([]byte(AccBucket))- log.Printf("total accounts: %d", b.Stats().KeyN)+ fmt.Printf("Configured accounts: %d\n", b.Stats().KeyN)err := b.ForEach(func(k, v []byte) error {- var info AccountInfo+ var info model.AccountInfoerr := json.Unmarshal(v, &info)if err != nil {return err}- log.Println(info)+ fmt.Println("====================================")+ fmt.Println("Email: ", info.Email)+ fmt.Println("UserID: ", info.UserID)+ fmt.Println("App: ", info.App)+ fmt.Println("====================================")return nil})if err != nil {- log.Fatal("error listing accounts", err)return err}return nil})if err != nil {- log.Fatal("error listing accounts", err)return err}return err-}diff --git a/pkg/model/account.go b/pkg/model/account.gonew file mode 100644index 0000000000..ab45adde1c--- /dev/null+++ b/pkg/model/account.go@@ -0,0 +1,11 @@+package model++import "cli-go/internal/api"++type AccountInfo struct {+ Email string `json:"email" binding:"required"`+ UserID int64 `json:"userID" binding:"required"`+ App api.App `json:"app" binding:"required"`+ MasterKey EncString `json:"masterKey" binding:"required"`+ Token EncString `json:"token" binding:"required"`+}diff --git a/pkg/model/enc_string.go b/pkg/model/enc_string.gonew file mode 100644index 0000000000..d6ec410748--- /dev/null+++ b/pkg/model/enc_string.go@@ -0,0 +1,30 @@+package model++import (+ "cli-go/internal/crypto"+ "log"+)++type EncString struct {+ CipherText string `json:"cipherText"`+ Nonce string `json:"nonce"`+}++func MakeEncString(plainText string, key []byte) *EncString {+ cipher, nonce, err := crypto.EncryptChaCha20poly1305([]byte(plainText), key)+ if err != nil {+ log.Fatalf("failed to encrypt %s", err)+ }+ return &EncString{+ CipherText: string(cipher),+ Nonce: string(nonce),+ }+}++func (e *EncString) MustDecrypt(key []byte) string {+ plainBytes, err := crypto.DecryptChaCha20poly1305([]byte(e.CipherText), key, []byte(e.Nonce))+ if err != nil {+ panic(err)+ }+ return string(plainBytes)+}diff --git a/pkg/secret.go b/pkg/secret.gonew file mode 100644index 0000000000..476f0fe679--- /dev/null+++ b/pkg/secret.go@@ -0,0 +1,30 @@+package pkg++import (+ "crypto/rand"+ "errors"+ "fmt"+ "github.com/zalando/go-keyring"+ "log"+)++func GetOrCreateClISecret() []byte {+ // get password+ secret, err := keyring.Get("ente-cli-cli", "ghost")+ if err != nil {+ if !errors.Is(err, keyring.ErrNotFound) {+ log.Fatal(fmt.Errorf("error getting password from keyring: %w", err))+ }+ key := make([]byte, 32)+ _, err = rand.Read(key)+ if err != nil {+ // handle error here+ }+ keySetErr := keyring.Set("ente-cli-cli", "ghost", string(key))+ if keySetErr != nil {+ log.Fatal(fmt.Errorf("error setting password in keyring: %w", keySetErr))+ }+ secret = string(key)+ }+ return []byte(secret)+}