π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[cli] Add support for exporting codes
Neeraj Gupta687258708347
diff --git a/cli/internal/api/authenticator.go b/cli/internal/api/authenticator.goindex cc1060031c..6322e0e756 100644--- a/cli/internal/api/authenticator.go+++ b/cli/internal/api/authenticator.go@@ -21,7 +21,7 @@ func (c *Client) GetAuthKey(ctx context.Context) (*models.AuthKey, error) {return &res, err}-func (c *Client) GetDiff(ctx context.Context, sinceTime int64, limit int64) ([]models.AuthEntity, error) {+func (c *Client) GetAuthDiff(ctx context.Context, sinceTime int64, limit int64) ([]models.AuthEntity, error) {var res struct {Diff []models.AuthEntity `json:"diff"`}diff --git a/cli/internal/api/models/authenticator.go b/cli/internal/api/models/authenticator.goindex d2aee3cef0..2b9c9c23ac 100644--- a/cli/internal/api/models/authenticator.go+++ b/cli/internal/api/models/authenticator.go@@ -7,10 +7,10 @@ type AuthKey struct {}type AuthEntity struct {- ID string `json:"id" binding:"required"`- EncryptedKey *string `json:"encryptedKey"`- Header *string `json:"header"`- IsDeleted bool `json:"isDeleted" binding:"required"`- CreatedAt int64 `json:"createdAt" binding:"required"`- UpdatedAt int64 `json:"updatedAt" binding:"required"`+ ID string `json:"id" binding:"required"`+ EncryptedData *string `json:"encryptedData"`+ Header *string `json:"header"`+ IsDeleted bool `json:"isDeleted" binding:"required"`+ CreatedAt int64 `json:"createdAt" binding:"required"`+ UpdatedAt int64 `json:"updatedAt" binding:"required"`}diff --git a/cli/pkg/mapper/authenticator.go b/cli/pkg/mapper/authenticator.gonew file mode 100644index 0000000000..e4c997dd54--- /dev/null+++ b/cli/pkg/mapper/authenticator.go@@ -0,0 +1,24 @@+package mapper++import (+ "context"+ "encoding/json"+ "fmt"+ "github.com/ente-io/cli/internal/api/models"+ eCrypto "github.com/ente-io/cli/internal/crypto"+)++func MapRemoteAuthEntityToString(ctx context.Context, authEntity models.AuthEntity, authKey []byte) (*string, error) {+ _, decrypted, err := eCrypto.DecryptChaChaBase64(*authEntity.EncryptedData, authKey, *authEntity.Header)+ if err != nil {+ return nil, fmt.Errorf("failed to decrypt auth enityt %s: %v", authEntity.ID, err)+ }+ decryptedStr := string(decrypted)+ // json decode the string+ var jsonDecodedStr string+ err = json.Unmarshal([]byte(decryptedStr), &jsonDecodedStr)+ if err != nil {+ return nil, fmt.Errorf("failed to decode json %s: %v", decryptedStr, err)+ }+ return &jsonDecodedStr, nil+}diff --git a/cli/pkg/model/constants.go b/cli/pkg/model/constants.goindex 6ffef41dde..7dbf5bca9a 100644--- a/cli/pkg/model/constants.go+++ b/cli/pkg/model/constants.go@@ -12,6 +12,7 @@ const (const (CollectionsSyncKey = "lastCollectionSync"CollectionsFileSyncKeyFmt = "collectionFilesSync-%d"+ AuthenticatorSyncKey = "lastAuthenticatorSync")type ContextKey stringdiff --git a/cli/pkg/secrets/key_holder.go b/cli/pkg/secrets/key_holder.goindex f399b41367..b7b8981a8c 100644--- a/cli/pkg/secrets/key_holder.go+++ b/cli/pkg/secrets/key_holder.go@@ -4,6 +4,7 @@ import ("context""fmt""github.com/ente-io/cli/internal/api"+ "github.com/ente-io/cli/internal/api/models"eCrypto "github.com/ente-io/cli/internal/crypto""github.com/ente-io/cli/pkg/model""github.com/ente-io/cli/utils/encoding"@@ -76,3 +77,19 @@ func (k *KeyHolder) GetCollectionKey(ctx context.Context, collection api.Collectreturn collKey, nil}}++func (k *KeyHolder) GetAuthenticatorKey(ctx context.Context, authKey models.AuthKey) ([]byte, error) {+ accSecretInfo := k.GetAccountSecretInfo(ctx)+ userID := ctx.Value("user_id").(int64)+ if authKey.UserID == userID {+ key, keyErr := eCrypto.SecretBoxOpen(+ encoding.DecodeBase64(authKey.EncryptedKey),+ encoding.DecodeBase64(authKey.Header),+ accSecretInfo.MasterKey)+ if keyErr != nil {+ return nil, fmt.Errorf("auth key %d drive failed %s", authKey.UserID, keyErr)+ }+ return key, nil+ }+ return nil, fmt.Errorf("accountSecInfo not found for user %d", authKey.UserID)+}diff --git a/cli/pkg/sync.go b/cli/pkg/sync.goindex af8cc9f649..48f5e436be 100644--- a/cli/pkg/sync.go+++ b/cli/pkg/sync.go@@ -38,7 +38,10 @@ func (c *ClICtrl) Export(filter model.Filter) error {continue}if account.App == api.AppAuth {- log.Printf("Skip account %s: auth export is not supported", account.Email)+ err := c.SyncAuthAccount(account, filter)+ if err != nil {+ return err+ }continue}log.Println("start sync")diff --git a/cli/pkg/sync_authenticator.go b/cli/pkg/sync_authenticator.gonew file mode 100644index 0000000000..ccf65d6b22--- /dev/null+++ b/cli/pkg/sync_authenticator.go@@ -0,0 +1,109 @@+package pkg++import (+ "context"+ "encoding/base64"+ "fmt"+ "github.com/ente-io/cli/internal/api/models"+ "github.com/ente-io/cli/pkg/mapper"+ "github.com/ente-io/cli/pkg/model"+ "log"+ "os"+ "time"+)++func (c *ClICtrl) SyncAuthAccount(account model.Account, filters model.Filter) error {+ secretInfo, err := c.KeyHolder.LoadSecrets(account)+ if err != nil {+ return err+ }+ ctx := c.buildRequestContext(context.Background(), account, filters)+ err = createDataBuckets(c.DB, account)+ if err != nil {+ return err+ }+ c.Client.AddToken(account.AccountKey(), base64.URLEncoding.EncodeToString(secretInfo.Token))+ data, err := c.fetchRemoteAuthenticatorData(ctx)+ if err != nil {+ log.Printf("Error fetching entities: %s", err)+ return err+ }+ return c.writeAuthExport(ctx, account, data)+}++func (c *ClICtrl) writeAuthExport(ctx context.Context, account model.Account, data []string) error {+ exportDir := account.ExportDir+ if exportDir == "" {+ return fmt.Errorf("export directory not set")+ }+ outputFile := fmt.Sprintf("%s/ente_auth.txt", exportDir)+ // if outout file exists, create a backup with current datetime+ if _, err := os.Stat(outputFile); err == nil {+ backupFile := fmt.Sprintf("%s/ente_auth_%s.txt", exportDir, time.Now().Format("2006-01-02_15-04-05"))+ if err := os.Rename(outputFile, backupFile); err != nil {+ return fmt.Errorf("error creating backup file: %v", err)+ }+ }+ // write the data to the output file, one line per entity+ file, err := os.Create(outputFile)+ if err != nil {+ return fmt.Errorf("error creating output file: %v", err)+ }+ defer file.Close()+ for _, line := range data {+ if _, err := file.WriteString(line + "\n"); err != nil {+ return fmt.Errorf("error writing to output file: %v", err)+ }+ }+ return nil+}++func (c *ClICtrl) fetchRemoteAuthenticatorData(ctx context.Context) ([]string, error) {+ var entities []models.AuthEntity+ hasMore := true+ sinceTime := int64(0)++ for hasMore {+ fetched, err := c.Client.GetAuthDiff(ctx, sinceTime, 500)+ if err != nil {+ return nil, fmt.Errorf("failed to get entities: %s", err)+ }+ entities = append(entities, fetched...)+ if len(fetched) < 500 {+ hasMore = false+ } else {+ for _, entity := range fetched {+ if entity.UpdatedAt > sinceTime {+ sinceTime = entity.UpdatedAt+ }+ }+ }+ }+ if len(entities) == 0 {+ log.Println("No data to export")+ return nil, nil+ }+ var key []byte+ var err error+ authKey, authKeyErr := c.Client.GetAuthKey(ctx)+ if authKeyErr != nil {+ return nil, fmt.Errorf("failed to get auth key: %s", authKeyErr)+ } else {+ key, err = c.KeyHolder.GetAuthenticatorKey(ctx, *authKey)+ if err != nil {+ return nil, fmt.Errorf("failed to decrypt auth key: %s", err)+ }+ }+ var codes []string+ for _, authEntity := range entities {+ if authEntity.IsDeleted {+ continue+ }+ code, mapErr := mapper.MapRemoteAuthEntityToString(ctx, authEntity, key)+ if mapErr != nil {+ return nil, mapErr+ }+ codes = append(codes, *code)+ }+ return codes, nil+}