Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add the replacement Rust CLI

Manav Rathif568ae375a74
diff --git a/.github/workflows/rust-cli-test.yml b/.github/workflows/rust-cli-test.yml
new file mode 100644
index 0000000000..372a716239
--- /dev/null
+++ b/.github/workflows/rust-cli-test.yml
@@ -0,0 +1,61 @@
+name: "Test (Rust CLI)"
+
+on:
+ pull_request:
+ paths:
+ - ".github/workflows/rust-cli-test.yml"
+ - "rust/apps/cli-next/**"
+
+permissions:
+ contents: read
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.ref }}
+ cancel-in-progress: true
+
+env:
+ RUSTFLAGS: -D warnings
+
+jobs:
+ native-keyring-macos:
+ runs-on: macos-latest
+ timeout-minutes: 20
+ defaults:
+ run:
+ working-directory: rust
+
+ steps:
+ - name: Checkout
+ uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
+
+ - name: Run with disposable signing and storage
+ run: |
+ umask 077
+ keychain="$RUNNER_TEMP/cli-test.keychain-db"
+ password=$(/usr/bin/openssl rand -hex 24)
+ /usr/bin/openssl req -x509 -newkey rsa:2048 -nodes -days 1 \
+ -subj '/CN=Ente CLI CI' \
+ -addext 'basicConstraints=critical,CA:FALSE' \
+ -addext 'keyUsage=critical,digitalSignature' \
+ -addext 'extendedKeyUsage=critical,codeSigning' \
+ -keyout "$RUNNER_TEMP/signing.key" -out "$RUNNER_TEMP/signing.crt"
+ /usr/bin/openssl pkcs12 -export \
+ -inkey "$RUNNER_TEMP/signing.key" -in "$RUNNER_TEMP/signing.crt" \
+ -out "$RUNNER_TEMP/signing.p12" -passout "pass:$password"
+ security create-keychain -p "$password" "$keychain"
+ security set-keychain-settings -lut 3600 "$keychain"
+ security unlock-keychain -p "$password" "$keychain"
+ security import "$RUNNER_TEMP/signing.p12" -k "$keychain" \
+ -P "$password" -T /usr/bin/codesign
+ security set-key-partition-list -S apple-tool:,apple: -s -k "$password" "$keychain"
+ security list-keychains -d user -s "$keychain"
+ security default-keychain -d user -s "$keychain"
+ python3 apps/cli-next/scripts/macos-keyring.py test 'Ente CLI CI'
+
+ - name: Remove the disposable keychain
+ if: always()
+ run: |
+ keychain="$RUNNER_TEMP/cli-test.keychain-db"
+ if [[ -e "$keychain" ]]; then
+ security delete-keychain "$keychain"
+ fi
diff --git a/.github/workflows/rust-lint.yml b/.github/workflows/rust-lint.yml
index df812789c3..0ba8fc16c7 100644
--- a/.github/workflows/rust-lint.yml
+++ b/.github/workflows/rust-lint.yml
@@ -25,7 +25,7 @@ jobs:
- name: Install Linux dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc
+ sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc libdbus-1-dev
# Restore-only; primed on main by warm-caches.yml.
- name: Restore Rust cache
diff --git a/.github/workflows/rust-test.yml b/.github/workflows/rust-test.yml
index 0fcf78e3f2..1b0abaa77d 100644
--- a/.github/workflows/rust-test.yml
+++ b/.github/workflows/rust-test.yml
@@ -27,7 +27,7 @@ jobs:
- name: Install Linux dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc
+ sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc libdbus-1-dev dbus gnome-keyring libglib2.0-bin
- name: Setup go
uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
@@ -49,3 +49,5 @@ jobs:
# GITHUB_TOKEN raises the API rate limit for those downloads.
env:
GITHUB_TOKEN: ${{ github.token }}
+
+ - run: apps/cli-next/scripts/test-with-keyring-linux.sh --features museum
diff --git a/.github/workflows/warm-caches.yml b/.github/workflows/warm-caches.yml
index 5505e8c0b6..b6d1c09a52 100644
--- a/.github/workflows/warm-caches.yml
+++ b/.github/workflows/warm-caches.yml
@@ -2,7 +2,7 @@ name: "Warm caches"
# PR workflows cannot save caches that other PRs can restore.
# This main-only workflow primes shared caches; consumers use save-if: false.
-# Each job must mirror its consumer's commands, features and RUSTFLAGS.
+# Cache space is limited. Choose warming targets accordingly.
on:
schedule:
@@ -38,7 +38,7 @@ jobs:
- name: Install Linux dependencies
run: |
sudo apt-get update
- sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc
+ sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libayatana-appindicator3-dev librsvg2-dev libvulkan-dev glslc libdbus-1-dev
- name: Setup Rust cache
uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 791a0e84ba..b06dfdd050 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2436,6 +2436,32 @@ dependencies = [
"thiserror 2.0.18",
]
+[[package]]
+name = "ente-cli-next"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "chrono",
+ "clap",
+ "dialoguer",
+ "dirs 5.0.1",
+ "ente-accounts",
+ "ente-core",
+ "ente-photos",
+ "ente-test-support",
+ "keyring",
+ "mockito",
+ "reqwest 0.12.28",
+ "serde",
+ "serde_json",
+ "tempfile",
+ "tokio",
+ "url",
+ "uuid",
+ "windows-sys 0.61.2",
+ "zeroize",
+]
+
[[package]]
name = "ente-contacts"
version = "0.0.0"
@@ -2732,12 +2758,15 @@ name = "ente-photos"
version = "0.0.0"
dependencies = [
"ente-assets",
+ "ente-core",
"ente-exif",
"ente-location",
"quick-xml",
"rusqlite",
+ "serde",
"tempfile",
"thiserror 2.0.18",
+ "zeroize",
]
[[package]]
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index ba25a0a787..6ae03eca85 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -2,6 +2,7 @@
resolver = "2"
members = [
"apps/cli",
+ "apps/cli-next",
"apps/ensu/src-tauri",
"bindings/frb/lib",
"bindings/frb/locker",
@@ -46,6 +47,10 @@ members = [
edition = "2024"
[workspace.dependencies]
+anyhow = "1.0.103"
+chrono = { version = "0.4.44", default-features = false }
+clap = "4.6.1"
+dialoguer = { version = "0.11.0", default-features = false }
dirs = "5.0.1"
ente-accounts = { path = "crates/accounts" }
ente-assets = { path = "crates/assets" }
@@ -79,6 +84,7 @@ hkdf = "0.13.0"
hmac = "0.13.0"
image = "0.25.10"
js-sys = "0.3.102"
+keyring = { version = "3.6.3", default-features = false }
log = "0.4.29"
md-5 = "0.11.0"
mockito = "1.7.2"
@@ -107,6 +113,7 @@ urlencoding = "2.1.3"
uuid = "1.23.1"
wasm-bindgen = "0.2.125"
wasm-bindgen-futures = "0.4.75"
+windows-sys = "0.61.2"
zeroize = { version = "1.9.0", features = ["derive"] }
zip = { version = "8.6.0", default-features = false, features = ["deflate-flate2-zlib-rs"] }
diff --git a/rust/apps/cli-next/Cargo.toml b/rust/apps/cli-next/Cargo.toml
new file mode 100644
index 0000000000..16810f02d4
--- /dev/null
+++ b/rust/apps/cli-next/Cargo.toml
@@ -0,0 +1,37 @@
+[package]
+name = "ente-cli-next"
+version = "0.1.0"
+edition.workspace = true
+publish = false
+
+[features]
+museum = []
+
+[dependencies]
+anyhow.workspace = true
+chrono = { workspace = true, features = ["std"] }
+clap = { workspace = true, features = ["derive"] }
+dialoguer = { workspace = true, features = ["password"] }
+dirs.workspace = true
+ente-accounts.workspace = true
+ente-core.workspace = true
+ente-photos.workspace = true
+keyring = { workspace = true, features = ["apple-native", "windows-native", "linux-native-sync-persistent", "crypto-rust"] }
+reqwest.workspace = true
+serde.workspace = true
+serde_json.workspace = true
+tempfile.workspace = true
+tokio = { workspace = true, features = ["macros", "rt"] }
+url.workspace = true
+uuid = { workspace = true, features = ["serde", "v4"] }
+zeroize.workspace = true
+
+[target.'cfg(windows)'.dependencies]
+windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Security_Authorization"] }
+
+[dev-dependencies]
+ente-test-support.workspace = true
+mockito.workspace = true
+
+[lints]
+workspace = true
diff --git a/rust/apps/cli-next/scripts/macos-keyring.py b/rust/apps/cli-next/scripts/macos-keyring.py
new file mode 100644
index 0000000000..15fdf9dde7
--- /dev/null
+++ b/rust/apps/cli-next/scripts/macos-keyring.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+import argparse
+import json
+import subprocess
+from pathlib import Path
+
+parser = argparse.ArgumentParser(
+ description="Build or test the CLI with stable macOS Keychain access."
+)
+parser.add_argument(
+ "mode", choices=["build", "test"], help="build prints the CLI path; test runs the keyring test"
+)
+parser.add_argument("identity", help="identity from security find-identity -v -p codesigning")
+args = parser.parse_args()
+app = Path(__file__).resolve().parent.parent
+cargo = {
+ "build": ["cargo", "build", "--bin", "ente-cli-next"],
+ "test": ["cargo", "test", "--test", "cli", "--no-run"],
+}[args.mode]
+cargo += ["--locked", "--message-format=json-render-diagnostics"]
+messages = subprocess.check_output(cargo, cwd=app, text=True)
+executables = {}
+for line in messages.splitlines():
+ message = json.loads(line)
+ if message.get("executable") and message["manifest_path"] == str(app / "Cargo.toml"):
+ executables[message["target"]["kind"][0]] = message["executable"]
+
+test_name = "native_keyring_survives_separate_processes"
+if args.mode == "test":
+ listing = subprocess.check_output(
+ [executables["test"], "--ignored", "--exact", test_name, "--list"], text=True
+ )
+ if f"{test_name}: test" not in listing.splitlines():
+ raise SystemExit("Native keyring test not found; nothing was signed or run.")
+
+# The test reads keys created by the CLI; both need the same development identity.
+subprocess.run(
+ [
+ "codesign",
+ "--force",
+ "--sign",
+ args.identity,
+ "--identifier",
+ "io.ente.cli.dev",
+ *executables.values(),
+ ],
+ check=True,
+)
+subprocess.run(["codesign", "--verify", "--strict", *executables.values()], check=True)
+if args.mode == "test":
+ subprocess.run(
+ [executables["test"], "--ignored", "--exact", test_name], check=True
+ )
+else:
+ print(executables["bin"])
diff --git a/rust/apps/cli-next/scripts/test-with-keyring-linux.sh b/rust/apps/cli-next/scripts/test-with-keyring-linux.sh
new file mode 100755
index 0000000000..7e41c46bd7
--- /dev/null
+++ b/rust/apps/cli-next/scripts/test-with-keyring-linux.sh
@@ -0,0 +1,35 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+cd "$(dirname "$0")/.."
+if [[ $(uname -s) != Linux ]]; then
+ echo "The native Linux keyring test requires Linux." >&2
+ exit 1
+fi
+
+test_name=native_keyring_survives_separate_processes
+executable=$(cargo test --locked -p ente-cli-next --test cli --no-run \
+ --message-format=json-render-diagnostics "$@" | python3 -c '
+import json, sys
+artifacts = [json.loads(line) for line in sys.stdin]
+executable, = [a["executable"] for a in artifacts
+ if a.get("reason") == "compiler-artifact" and a["target"]["name"] == "cli"
+ and a.get("executable")]
+print(executable)
+')
+listing=$("$executable" --ignored --exact "$test_name" --list)
+if ! grep -Fxq "$test_name: test" <<< "$listing"; then
+ echo "Native keyring test not found; nothing was run." >&2
+ exit 1
+fi
+
+keyring_home=$(mktemp -d)
+trap 'rm -rf "$keyring_home"' EXIT
+env -i PATH="$PATH" HOME="$keyring_home" XDG_DATA_HOME="$keyring_home/data" \
+ XDG_RUNTIME_DIR="$keyring_home" dbus-run-session -- bash -euc '
+ printf %s native-test | gnome-keyring-daemon --foreground --unlock --components=secrets &
+ daemon=$!
+ trap '\''kill "$daemon" 2>/dev/null || true; wait "$daemon" 2>/dev/null || true'\'' EXIT
+ gdbus wait --session --timeout 30 org.freedesktop.secrets
+ "$@"
+ ' -- "$executable" --ignored --exact "$test_name"
diff --git a/rust/apps/cli-next/src/api.rs b/rust/apps/cli-next/src/api.rs
new file mode 100644
index 0000000000..4bf332b738
--- /dev/null
+++ b/rust/apps/cli-next/src/api.rs
@@ -0,0 +1,127 @@
+use std::{collections::BTreeMap, io::Write, path::Path, time::Duration};
+
+use anyhow::{Context, Result, ensure};
+use ente_accounts::{AccountsClient, AccountsClientConfig};
+use ente_core::{
+ Session, b64,
+ crypto::SecretVec,
+ http::{Api, ApiConfig, Auth},
+};
+use reqwest::{
+ Client, Method,
+ header::{HeaderMap, HeaderName, HeaderValue},
+ redirect::Policy,
+};
+use url::Url;
+use zeroize::Zeroizing;
+
+use crate::{
+ args::{ApiArgs, Product},
+ parse_json, read_input,
+ vault::Account,
+};
+
+pub fn http() -> Result<Client> {
+ Ok(Client::builder()
+ .user_agent(concat!("ente-cli-next/", env!("CARGO_PKG_VERSION")))
+ .connect_timeout(Duration::from_secs(15))
+ .read_timeout(Duration::from_secs(30))
+ .redirect(Policy::custom(|attempt| {
+ if attempt.previous().len() >= 10 {
+ attempt.error("too many redirects")
+ } else if attempt
+ .previous()
+ .first()
+ .is_some_and(|first| first.origin() != attempt.url().origin())
+ {
+ attempt.error("redirect leaves the selected account's origin")
+ } else {
+ attempt.follow()
+ }
+ }))
+ .build()?)
+}
+
+pub fn accounts_client(origin: &str, product: Product) -> Result<AccountsClient> {
+ Ok(AccountsClient::with_http(
+ AccountsClientConfig::new(product.client_package()).with_origin(origin),
+ http()?.into(),
+ ))
+}
+
+pub fn session(account: &Account, product: Product) -> Result<Session> {
+ let mut config = ApiConfig::new(account.origin.clone());
+ config.client_package = Some(product.client_package().into());
+ config.auth = Some(Auth::User(b64::encode_url_safe(account.token(product)?)));
+ Ok(Session {
+ api: Api::new(http()?.into(), config),
+ master_key: SecretVec::new(account.identity.master_key.clone()),
+ })
+}
+
+pub async fn raw(account: &Account, product: Product, args: ApiArgs) -> Result<()> {
+ ensure!(
+ !(args.headers.as_deref() == Some(Path::new("-"))
+ && args.body.as_deref() == Some(Path::new("-"))),
+ "headers and body cannot both read stdin"
+ );
+ let origin = Url::parse(&format!("{}/", account.origin))?;
+ let mut url = origin.join(&args.path).context("invalid API path")?;
+ ensure!(
+ url.origin() == origin.origin(),
+ "API URL must stay at the selected account's origin"
+ );
+ for query in args.query {
+ let (name, value) = query
+ .split_once('=')
+ .context("--query requires name=value")?;
+ url.query_pairs_mut().append_pair(name, value);
+ }
+ let mut headers = HeaderMap::new();
+ if let Some(path) = args.headers {
+ let values: BTreeMap<String, String> =
+ parse_json(&read_input(&path)?).context("headers must be a JSON object of strings")?;
+ for (name, value) in values {
+ let mut value = HeaderValue::from_str(&Zeroizing::new(value))?;
+ value.set_sensitive(true);
+ headers.insert(HeaderName::from_bytes(name.as_bytes())?, value);
+ }
+ }
+ if !headers.contains_key("x-auth-token") {
+ if let Some(host) = headers.get("host") {
+ ensure!(
+ host == &origin[url::Position::BeforeHost..url::Position::AfterPort],
+ "Host cannot redirect stored account credentials"
+ );
+ }
+ let token = Zeroizing::new(b64::encode_url_safe(account.token(product)?));
+ let mut value = HeaderValue::from_str(&token)?;
+ value.set_sensitive(true);
+ headers.insert("x-auth-token", value);
+ }
+ if !headers.contains_key("x-client-package") {
+ headers.insert(
+ "x-client-package",
+ HeaderValue::from_static(product.client_package()),
+ );
+ }
+ let mut request = http()?
+ .request(Method::from_bytes(args.method.as_bytes())?, url)
+ .headers(headers);
+ if let Some(path) = args.body {
+ request = request.body(read_input(&path)?.to_vec());
+ }
+ let mut response = request.send().await.map_err(reqwest::Error::without_url)?;
+ let status = response.status();
+ let mut stdout = std::io::stdout().lock();
+ while let Some(chunk) = response
+ .chunk()
+ .await
+ .map_err(reqwest::Error::without_url)?
+ {
+ stdout.write_all(&chunk)?;
+ }
+ stdout.flush()?;
+ ensure!(status.is_success(), "HTTP {status}");
+ Ok(())
+}
diff --git a/rust/apps/cli-next/src/args.rs b/rust/apps/cli-next/src/args.rs
new file mode 100644
index 0000000000..39063d2590
--- /dev/null
+++ b/rust/apps/cli-next/src/args.rs
@@ -0,0 +1,210 @@
+use std::path::PathBuf;
+
+use clap::{Args, Parser, Subcommand};
+use serde::{Deserialize, Serialize};
+
+#[derive(Parser)]
+#[command(
+ version,
+ about = "Use Ente from the command line",
+ after_help = "Start with: ente-cli-next photos login"
+)]
+pub struct Cli {
+ #[arg(long, global = true, help = "Print the result as JSON")]
+ pub json: bool,
+ #[arg(
+ long,
+ global = true,
+ value_name = "NAME",
+ help = "Use this saved account instead of the selected one"
+ )]
+ pub account: Option<String>,
+ #[command(subcommand)]
+ pub command: Command,
+}
+
+#[derive(Subcommand)]
+pub enum Command {
+ #[command(about = "Log in to Ente Photos, list albums, or call the Photos API")]
+ Photos {
+ #[command(subcommand)]
+ command: PhotosCommand,
+ },
+ #[command(about = "Log in to Ente Locker or call the Locker API")]
+ Locker {
+ #[command(subcommand)]
+ command: SessionCommand,
+ },
+ #[command(about = "Log in to Ente Auth or call the Auth API")]
+ Auth {
+ #[command(subcommand)]
+ command: SessionCommand,
+ },
+ #[command(about = "View and manage saved accounts")]
+ Account {
+ #[command(subcommand)]
+ command: AccountCommand,
+ },
+ #[command(
+ about = "Generate a vault key for unattended use",
+ after_help = "Environment:
+ ENTE_CLI_HOME Directory containing the encrypted vault.
+ ENTE_CLI_VAULT_KEY Base64 key from `vault key generate`; bypasses the OS keychain.
+
+Keep the same home and key across unattended invocations."
+ )]
+ Vault {
+ #[command(subcommand)]
+ command: VaultCommand,
+ },
+}
+
+#[derive(Subcommand)]
+pub enum PhotosCommand {
+ #[command(flatten)]
+ Session(SessionCommand),
+ #[command(about = "Work with albums")]
+ Album {
+ #[command(subcommand)]
+ command: AlbumCommand,
+ },
+}
+
+#[derive(Subcommand)]
+pub enum SessionCommand {
+ #[command(
+ about = "Log in",
+ after_help = "Non-interactive login reads JSON from --input:
+ {\"email\": \"...\", \"password\": \"...\", \"otp\": \"123456\", \"totp\": \"654321\"}
+
+otp is the email code. totp is the authenticator code. Include them only when required."
+ )]
+ Login(LoginArgs),
+ #[command(about = "Log out")]
+ Logout,
+ #[command(
+ about = "Send a raw API request",
+ after_help = "The response body is written unchanged, including when --json is set."
+ )]
+ Api(ApiArgs),
+}
+
+#[derive(Args)]
+pub struct LoginArgs {
+ #[arg(
+ long,
+ conflicts_with = "account",
+ help = "Ente server to log in to; defaults to api.ente.com"
+ )]
+ pub host: Option<String>,
+ #[arg(
+ long,
+ conflicts_with = "account",
+ help = "Name for a new saved account; defaults to the account's email"
+ )]
+ pub name: Option<String>,
+ #[arg(
+ long,
+ help = "Read login credentials as JSON from this file, or from stdin with -"
+ )]
+ pub input: Option<PathBuf>,
+}
+
+#[derive(Args)]
+pub struct ApiArgs {
+ #[arg(help = "API path or URL at the selected account's server")]
+ pub path: String,
+ #[arg(long, default_value = "GET", help = "HTTP method")]
+ pub method: String,
+ #[arg(
+ long,
+ value_name = "NAME=VALUE",
+ help = "Append a query parameter; repeat for multiple values"
+ )]
+ pub query: Vec<String>,
+ #[arg(
+ long,
+ help = "Read a JSON object of headers from this file, or from stdin with -"
+ )]
+ pub headers: Option<PathBuf>,
+ #[arg(
+ long,
+ help = "Read the request body from this file, or from stdin with -"
+ )]
+ pub body: Option<PathBuf>,
+}
+
+#[derive(Subcommand)]
+pub enum AlbumCommand {
+ #[command(about = "List your albums, including shared and hidden ones")]
+ List,
+}
+
+#[derive(Subcommand)]
+pub enum AccountCommand {
+ #[command(about = "List saved accounts")]
+ List,
+ #[command(about = "Show a saved account")]
+ View {
+ #[arg(help = "Saved account name")]
+ name: String,
+ },
+ #[command(about = "Make an account the selected one")]
+ Switch {
+ #[arg(help = "Saved account name")]
+ name: String,
+ },
+ #[command(about = "Change an account's local name")]
+ Rename {
+ #[arg(help = "Saved account name")]
+ name: String,
+ #[arg(help = "New local name")]
+ new_name: String,
+ },
+ #[command(about = "Forget an account on this machine; remote sessions stay valid")]
+ Remove {
+ #[arg(help = "Saved account name")]
+ name: String,
+ },
+}
+
+#[derive(Subcommand)]
+pub enum VaultCommand {
+ #[command(about = "Work with the vault key")]
+ Key {
+ #[command(subcommand)]
+ command: KeyCommand,
+ },
+}
+
+#[derive(Subcommand)]
+pub enum KeyCommand {
+ #[command(about = "Print a new vault key for ENTE_CLI_VAULT_KEY")]
+ Generate,
+}
+
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
+#[serde(rename_all = "lowercase")]
+pub enum Product {
+ Photos,
+ Locker,
+ Auth,
+}
+
+impl Product {
+ pub fn name(self) -> &'static str {
+ match self {
+ Self::Photos => "photos",
+ Self::Locker => "locker",
+ Self::Auth => "auth",
+ }
+ }
+
+ pub fn client_package(self) -> &'static str {
+ match self {
+ Self::Photos => "io.ente.photos",
+ Self::Locker => "io.ente.locker",
+ Self::Auth => "io.ente.auth",
+ }
+ }
+}
diff --git a/rust/apps/cli-next/src/login.rs b/rust/apps/cli-next/src/login.rs
new file mode 100644
index 0000000000..2f0971600c
--- /dev/null
+++ b/rust/apps/cli-next/src/login.rs
@@ -0,0 +1,377 @@
+use std::{
+ collections::BTreeMap,
+ io::{self, IsTerminal},
+ path::Path,
+};
+
+use anyhow::{Context, Result, ensure};
+use dialoguer::{Input, Password, Select, console::Term};
+use ente_accounts::{
+ AuthFlow, AuthFlowUi, DEFAULT_API_ORIGIN, LoginParams, OtpPurpose, SecondFactorMethod,
+ TotpPurpose,
+};
+use ente_core::http::{Api, ApiConfig};
+use serde::Deserialize;
+use url::Url;
+use uuid::Uuid;
+use zeroize::{ZeroizeOnDrop, Zeroizing};
+
+use crate::{
+ api,
+ args::{LoginArgs, Product},
+ parse_json, read_input,
+ vault::{Account, AccountKeys, State, StoredSession, Vault},
+};
+
+pub async fn login(
+ vault: Vault,
+ product: Product,
+ args: LoginArgs,
+ selected: Option<&str>,
+) -> Result<(State, usize)> {
+ let (expected, origin) = match selected {
+ Some(name) => {
+ let account = &vault.state.accounts[vault.state.named(name)?];
+ (Some(account.storage_id), account.origin.clone())
+ }
+ None => (
+ None,
+ normalize_host(args.host.as_deref().unwrap_or(DEFAULT_API_ORIGIN))?,
+ ),
+ };
+ let (snapshot, access) = vault.release();
+ Api::new(api::http()?.into(), ApiConfig::new(origin.clone()))
+ .ping()
+ .await
+ .context("cannot reach the selected Ente server")?;
+ let (params, mut ui) = LoginUi::read(args.input.as_deref())?;
+ let client = api::accounts_client(&origin, product)?;
+ let mut authenticated = AuthFlow::new(&client, &mut ui).login(params).await?;
+ client.set_auth_token(Some(ente_core::b64::encode_url_safe(
+ &authenticated.secrets.token,
+ )));
+
+ let supplied_name = args.name.is_some();
+ let requested_name = args.name;
+ let mut previous_session = None;
+ let result = async {
+ let email = client.email().await?;
+ let snapshot_existing = snapshot.accounts.iter().position(|account| {
+ account.origin == origin && account.user_id == authenticated.user_id
+ });
+ if let Some(expected) = expected {
+ ensure!(
+ snapshot_existing
+ .is_some_and(|index| snapshot.accounts[index].storage_id == expected),
+ "authenticated identity does not match --account; local state has not changed"
+ );
+ }
+ let mut new_name = requested_name.unwrap_or_else(|| email.clone());
+ if snapshot_existing.is_some() {
+ ensure!(
+ !supplied_name,
+ "account already exists; use account rename to change its local name"
+ );
+ } else {
+ while let Err(error) = snapshot.check_name(&new_name) {
+ if !ui.interactive {
+ return Err(error);
+ }
+ eprintln!("{error}");
+ new_name = Input::new()
+ .with_prompt("Local account name")
+ .interact_on(&Term::stderr())?;
+ }
+ }
+ drop(snapshot);
+
+ let mut vault = access.open()?;
+ let existing = vault
+ .state
+ .accounts
+ .iter()
+ .position(|a| a.origin == origin && a.user_id == authenticated.user_id);
+ if let Some(expected) = expected {
+ let current = vault
+ .state
+ .accounts
+ .iter()
+ .position(|account| account.storage_id == expected)
+ .context(
+ "selected account was removed during login; local state has not changed",
+ )?;
+ ensure!(
+ existing == Some(current),
+ "authenticated identity does not match --account; local state has not changed"
+ );
+ }
+ let identity = AccountKeys {
+ master_key: std::mem::take(&mut authenticated.secrets.master_key),
+ secret_key: std::mem::take(&mut authenticated.secrets.secret_key),
+ public_key: std::mem::take(&mut authenticated.secrets.public_key),
+ };
+ let session = StoredSession {
+ token: std::mem::take(&mut authenticated.secrets.token),
+ };
+ let index = if let Some(index) = existing {
+ ensure!(
+ !supplied_name,
+ "account already exists; use account rename to change its local name"
+ );
+ let account = &mut vault.state.accounts[index];
+ account.email = email;
+ account.identity = identity;
+ previous_session = account.sessions.insert(product, session);
+ index
+ } else {
+ vault.state.check_name(&new_name)?;
+ let index = vault.state.accounts.len();
+ vault.state.accounts.push(Account {
+ storage_id: Uuid::new_v4(),
+ name: new_name,
+ email,
+ origin,
+ user_id: authenticated.user_id,
+ identity,
+ sessions: BTreeMap::from([(product, session)]),
+ });
+ index
+ };
+ if expected.is_none() {
+ vault.state.selected = Some(vault.state.accounts[index].storage_id);
+ }
+ vault.save()?;
+ Ok((vault.into_state(), index))
+ }
+ .await;
+ if result.is_ok()
+ && let Some(previous) = &previous_session
+ {
+ client.set_auth_token(Some(ente_core::b64::encode_url_safe(&previous.token)));
+ }
+ if (result.is_err() || previous_session.is_some())
+ && let Err(error) = client.logout().await
+ && !matches!(&error, ente_accounts::Error::Http(error) if error.status_code() == Some(401))
+ {
+ let session = if result.is_err() {
+ "unsaved"
+ } else {
+ "previous"
+ };
+ eprintln!("Could not revoke the {session} login session: {error}");
+ }
+ result
+}
+
+fn normalize_host(host: &str) -> Result<String> {
+ let input = if host.contains("://") {
+ host.to_owned()
+ } else {
+ format!("https://{host}")
+ };
+ let mut url = Url::parse(&input).context("invalid API host")?;
+ if !host.contains("://")
+ && url.host_str().is_some_and(|h| {
+ h == "localhost"
+ || h.trim_matches(['[', ']'])
+ .parse::<std::net::IpAddr>()
+ .is_ok_and(|ip| ip.is_loopback())
+ })
+ {
+ url.set_scheme("http").expect("HTTP is a valid URL scheme");
+ }
+ ensure!(
+ matches!(url.scheme(), "http" | "https") && url.host_str().is_some(),
+ "host must be an HTTP or HTTPS origin"
+ );
+ ensure!(
+ url.username().is_empty() && url.password().is_none(),
+ "host must not contain credentials"
+ );
+ Ok(url.origin().ascii_serialization())
+}
+
+#[derive(Deserialize, ZeroizeOnDrop)]
+#[serde(deny_unknown_fields)]
+struct Credentials {
+ email: String,
+ password: String,
+ otp: Option<String>,
+ totp: Option<String>,
+}
+
+struct LoginUi {
+ interactive: bool,
+ credentials: Option<Credentials>,
+}
+
+impl LoginUi {
+ fn read(path: Option<&Path>) -> Result<(LoginParams, Self)> {
+ if let Some(path) = path {
+ let mut credentials: Credentials = parse_json(&read_input(path)?).context(
+ "login input must contain email and password, with optional otp and totp",
+ )?;
+ let params = LoginParams {
+ email: std::mem::take(&mut credentials.email),
+ password: Zeroizing::new(std::mem::take(&mut credentials.password)),
+ };
+ return Ok((
+ params,
+ Self {
+ interactive: false,
+ credentials: Some(credentials),
+ },
+ ));
+ }
+ ensure!(
+ io::stdin().is_terminal() && io::stderr().is_terminal(),
+ "noninteractive login requires --input <file> or --input -"
+ );
+ let email = Input::new()
+ .with_prompt("Email")
+ .interact_on(&Term::stderr())?;
+ let password = Password::new()
+ .with_prompt("Password")
+ .interact_on(&Term::stderr())?;
+ Ok((
+ LoginParams {
+ email,
+ password: Zeroizing::new(password),
+ },
+ Self {
+ interactive: true,
+ credentials: None,
+ },
+ ))
+ }
+
+ fn secret_prompt(&self, label: &str) -> ente_accounts::Result<String> {
+ Password::new()
+ .with_prompt(label)
+ .interact_on(&Term::stderr())
+ .map_err(|error| ente_accounts::Error::Ui(Box::new(error)))
+ }
+}
+
+impl AuthFlowUi for LoginUi {
+ fn read_email_otp(
+ &mut self,
+ _email: &str,
+ _purpose: OtpPurpose,
+ _resent: bool,
+ ) -> ente_accounts::Result<String> {
+ if self.interactive {
+ return self.secret_prompt("Email verification code");
+ }
+ self.credentials
+ .as_mut()
+ .and_then(|c| c.otp.take())
+ .ok_or_else(|| {
+ ente_accounts::Error::InvalidInput(
+ "login requires an email verification code; supply otp in --input".into(),
+ )
+ })
+ }
+
+ fn read_totp_code(&mut self, _purpose: TotpPurpose) -> ente_accounts::Result<String> {
+ if self.interactive {
+ return self.secret_prompt("Authenticator code");
+ }
+ self.credentials
+ .as_mut()
+ .and_then(|c| c.totp.take())
+ .ok_or_else(|| {
+ ente_accounts::Error::InvalidInput(
+ "login requires an authenticator code; supply totp in --input".into(),
+ )
+ })
+ }
+
+ fn report_retryable_error(&mut self, message: &str) -> ente_accounts::Result<()> {
+ if !self.interactive {
+ return Err(ente_accounts::Error::InvalidInput(message.into()));
+ }
+ eprintln!("{message}");
+ Ok(())
+ }
+
+ fn choose_second_factor(
+ &mut self,
+ methods: &[SecondFactorMethod],
+ ) -> ente_accounts::Result<SecondFactorMethod> {
+ if !self.interactive {
+ return Ok(SecondFactorMethod::Totp);
+ }
+ let labels: Vec<_> = methods
+ .iter()
+ .map(|method| match method {
+ SecondFactorMethod::Totp => "Authenticator code",
+ SecondFactorMethod::Passkey => "Passkey in browser",
+ })
+ .collect();
+ let index = Select::new()
+ .with_prompt("Verify with")
+ .items(&labels)
+ .interact_on(&Term::stderr())
+ .map_err(|error| ente_accounts::Error::Ui(Box::new(error)))?;
+ Ok(methods[index])
+ }
+
+ fn present_passkey_verification(&mut self, url: &str) -> ente_accounts::Result<()> {
+ if !self.interactive {
+ return Err(ente_accounts::Error::InvalidInput(
+ "passkey login requires an interactive terminal".into(),
+ ));
+ }
+ eprintln!("Open this URL to verify your passkey:\n{url}");
+ Ok(())
+ }
+
+ fn wait_for_passkey_verification(&mut self) -> ente_accounts::Result<()> {
+ Input::<String>::new()
+ .with_prompt("Press Enter after completing verification")
+ .allow_empty(true)
+ .interact_on(&Term::stderr())
+ .map_err(|error| ente_accounts::Error::Ui(Box::new(error)))?;
+ Ok(())
+ }
+
+ fn present_totp_secret(
+ &mut self,
+ _secret_code: &str,
+ _qr_code: &str,
+ ) -> ente_accounts::Result<()> {
+ Err(ente_accounts::Error::InvalidInput(
+ "login cannot enroll a new authenticator".into(),
+ ))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn noninteractive_codes_come_from_their_login_input_fields() {
+ let credentials: Credentials = parse_json(
+ br#"{"email":"[email protected]","password":"secret","otp":"123456","totp":"654321"}"#,
+ )
+ .unwrap();
+ let mut ui = LoginUi {
+ interactive: false,
+ credentials: Some(credentials),
+ };
+ assert_eq!(
+ ui.read_email_otp("[email protected]", OtpPurpose::Login, false)
+ .unwrap(),
+ "123456"
+ );
+ assert_eq!(ui.read_totp_code(TotpPurpose::Login).unwrap(), "654321");
+ assert!(
+ ui.read_totp_code(TotpPurpose::Login)
+ .unwrap_err()
+ .to_string()
+ .contains("supply totp in --input")
+ );
+ }
+}
diff --git a/rust/apps/cli-next/src/main.rs b/rust/apps/cli-next/src/main.rs
new file mode 100644
index 0000000000..e99ebb45cd
--- /dev/null
+++ b/rust/apps/cli-next/src/main.rs
@@ -0,0 +1,238 @@
+mod api;
+mod args;
+mod login;
+mod output;
+mod permissions;
+mod vault;
+
+use std::{
+ io::{Read, Write},
+ path::Path,
+};
+
+use anyhow::{Context, Result};
+use clap::Parser;
+use ente_core::{
+ b64,
+ crypto::{Key, PublicKey, SecretKey},
+};
+use ente_photos::collections;
+use serde::de::DeserializeOwned;
+use serde_json::json;
+use zeroize::Zeroizing;
+
+use args::{
+ AccountCommand, AlbumCommand, Cli, Command, KeyCommand, PhotosCommand, Product, SessionCommand,
+ VaultCommand,
+};
+use output::{AccountView, AlbumView};
+use vault::{State, Vault};
+
+#[tokio::main(flavor = "current_thread")]
+async fn main() {
+ if let Err(error) = run(Cli::parse()).await {
+ eprintln!("Error: {error:#}");
+ std::process::exit(1);
+ }
+}
+
+async fn run(cli: Cli) -> Result<()> {
+ let Cli {
+ json,
+ account,
+ command,
+ } = cli;
+ match command {
+ Command::Photos {
+ command: PhotosCommand::Session(command),
+ } => session(Product::Photos, command, account.as_deref(), json).await,
+ Command::Locker { command } => {
+ session(Product::Locker, command, account.as_deref(), json).await
+ }
+ Command::Auth { command } => {
+ session(Product::Auth, command, account.as_deref(), json).await
+ }
+ Command::Photos {
+ command:
+ PhotosCommand::Album {
+ command: AlbumCommand::List,
+ },
+ } => album_list(account.as_deref(), json).await,
+ Command::Account { command } => account_command(command, json),
+ Command::Vault {
+ command:
+ VaultCommand::Key {
+ command: KeyCommand::Generate,
+ },
+ } => vault_key(json),
+ }
+}
+
+fn vault_key(json_output: bool) -> Result<()> {
+ let key = Zeroizing::new(b64::encode(Key::generate().as_bytes()));
+ if json_output {
+ output::json(&json!({ "key": key.as_str() }))
+ } else {
+ writeln!(std::io::stdout(), "{}", key.as_str()).map_err(Into::into)
+ }
+}
+
+async fn album_list(selected: Option<&str>, json_output: bool) -> Result<()> {
+ let state = State::load()?;
+ let account = &state.accounts[state.resolve(selected)?];
+ let session = api::session(account, Product::Photos)?;
+ let albums = collections::list(
+ &session,
+ account.user_id,
+ &PublicKey::try_from_slice(&account.identity.public_key)?,
+ &SecretKey::try_from_slice(&account.identity.secret_key)?,
+ )
+ .await?
+ .into_iter()
+ .map(AlbumView::try_from)
+ .collect::<Result<Vec<_>>>()?;
+ if json_output {
+ output::json(&albums)
+ } else {
+ output::albums(&albums)
+ }
+}
+
+async fn session(
+ product: Product,
+ command: SessionCommand,
+ selected: Option<&str>,
+ json_output: bool,
+) -> Result<()> {
+ match command {
+ SessionCommand::Api(args) => {
+ let state = State::load()?;
+ let index = state.resolve(selected)?;
+ api::raw(&state.accounts[index], product, args).await
+ }
+ SessionCommand::Login(args) => {
+ let (state, index) = login::login(Vault::open()?, product, args, selected).await?;
+ let account = AccountView::new(&state.accounts[index], state.selected);
+ if json_output {
+ output::json(&json!({ "account": account, "product": product }))
+ } else {
+ output::accounts(&[account])
+ }
+ }
+ SessionCommand::Logout => {
+ let mut vault = Vault::open()?;
+ let index = vault.state.resolve(selected)?;
+ let account = &vault.state.accounts[index];
+ let client = api::accounts_client(&account.origin, product)?;
+ client.set_auth_token(Some(b64::encode_url_safe(account.token(product)?)));
+ if let Err(error) = client.logout().await {
+ match &error {
+ ente_accounts::Error::Http(error) if error.status_code() == Some(401) => (),
+ _ => return Err(error.into()),
+ }
+ }
+ let name = account.name.clone();
+ vault.state.accounts[index].sessions.remove(&product);
+ vault.save()?;
+ drop(vault);
+ output::action(
+ json_output,
+ &json!({ "account": name, "product": product, "loggedOut": true }),
+ &format!("Logged out of {} for {:?}.", product.name(), name),
+ )
+ }
+ }
+}
+
+fn account_command(command: AccountCommand, json_output: bool) -> Result<()> {
+ match command {
+ AccountCommand::List => {
+ let state = State::load()?;
+ let accounts: Vec<_> = state
+ .accounts
+ .iter()
+ .map(|a| AccountView::new(a, state.selected))
+ .collect();
+ if json_output {
+ output::json(&accounts)
+ } else {
+ output::accounts(&accounts)
+ }
+ }
+ AccountCommand::View { name } => {
+ let state = State::load()?;
+ let account = AccountView::new(&state.accounts[state.named(&name)?], state.selected);
+ if json_output {
+ output::json(&account)
+ } else {
+ output::accounts(&[account])
+ }
+ }
+ AccountCommand::Switch { name } => {
+ let mut vault = Vault::open()?;
+ let state = &mut vault.state;
+ let index = state.named(&name)?;
+ state.selected = Some(state.accounts[index].storage_id);
+ vault.save()?;
+ output_account(vault.into_state(), index, json_output)
+ }
+ AccountCommand::Rename { name, new_name } => {
+ let mut vault = Vault::open()?;
+ let state = &mut vault.state;
+ let index = state.named(&name)?;
+ if name != new_name {
+ state.check_name(&new_name)?;
+ state.accounts[index].name = new_name;
+ vault.save()?;
+ }
+ output_account(vault.into_state(), index, json_output)
+ }
+ AccountCommand::Remove { name } => {
+ let mut vault = Vault::open()?;
+ let state = &mut vault.state;
+ let index = state.named(&name)?;
+ let removed = state.accounts.remove(index);
+ if state.selected == Some(removed.storage_id) {
+ state.selected = None;
+ }
+ vault.save()?;
+ drop(vault);
+ output::action(
+ json_output,
+ &json!({ "removed": name }),
+ &format!("Forgot local account {name:?}; remote sessions were not revoked."),
+ )
+ }
+ }
+}
+
+fn output_account(state: State, index: usize, json_output: bool) -> Result<()> {
+ let account = AccountView::new(&state.accounts[index], state.selected);
+ if json_output {
+ output::json(&account)
+ } else {
+ output::accounts(&[account])
+ }
+}
+
+fn read_input(path: &Path) -> Result<Zeroizing<Vec<u8>>> {
+ let mut bytes = Zeroizing::new(Vec::new());
+ if path == Path::new("-") {
+ std::io::stdin().read_to_end(&mut bytes)?;
+ } else {
+ std::fs::File::open(path)
+ .with_context(|| format!("cannot open {}", path.display()))?
+ .read_to_end(&mut bytes)?;
+ }
+ Ok(bytes)
+}
+
+fn parse_json<T: DeserializeOwned>(bytes: &[u8]) -> Result<T> {
+ serde_json::from_slice(bytes).map_err(|error| {
+ anyhow::anyhow!(
+ "invalid JSON at line {}, column {}",
+ error.line(),
+ error.column()
+ )
+ })
+}
diff --git a/rust/apps/cli-next/src/output.rs b/rust/apps/cli-next/src/output.rs
new file mode 100644
index 0000000000..32de676980
--- /dev/null
+++ b/rust/apps/cli-next/src/output.rs
@@ -0,0 +1,148 @@
+use std::io::Write;
+
+use anyhow::{Context, Result};
+use chrono::{DateTime, SecondsFormat};
+use ente_photos::collections::{Collection, Visibility};
+use serde::Serialize;
+use uuid::Uuid;
+
+use crate::{args::Product, vault::Account};
+
+#[derive(Serialize)]
+pub struct AccountView<'a> {
+ id: String,
+ name: &'a str,
+ email: &'a str,
+ host: &'a str,
+ products: Vec<Product>,
+ selected: bool,
+}
+
+impl<'a> AccountView<'a> {
+ pub fn new(account: &'a Account, selected: Option<Uuid>) -> Self {
+ Self {
+ id: account.user_id.to_string(),
+ name: &account.name,
+ email: &account.email,
+ host: &account.origin,
+ products: account.sessions.keys().copied().collect(),
+ selected: selected == Some(account.storage_id),
+ }
+ }
+}
+
+#[derive(Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct AlbumView {
+ id: String,
+ name: String,
+ #[serde(rename = "type")]
+ kind: String,
+ visibility: &'static str,
+ owner_id: String,
+ updated_at: String,
+}
+
+impl TryFrom<Collection> for AlbumView {
+ type Error = anyhow::Error;
+
+ fn try_from(album: Collection) -> Result<Self> {
+ let updated_at = DateTime::from_timestamp_micros(album.updated_at_micros)
+ .context("album timestamp is outside the supported range")?
+ .to_rfc3339_opts(SecondsFormat::Micros, true);
+ Ok(Self {
+ id: album.id.to_string(),
+ name: album.name,
+ kind: album.kind,
+ visibility: match album.visibility {
+ Visibility::Visible => "visible",
+ Visibility::Archived => "archived",
+ Visibility::Hidden => "hidden",
+ },
+ owner_id: album.owner_id.to_string(),
+ updated_at,
+ })
+ }
+}
+
+pub fn json(value: &impl Serialize) -> Result<()> {
+ let mut stdout = std::io::stdout().lock();
+ serde_json::to_writer(&mut stdout, value)?;
+ writeln!(stdout)?;
+ Ok(())
+}
+
+pub fn action(as_json: bool, value: &impl Serialize, message: &str) -> Result<()> {
+ if as_json {
+ json(value)
+ } else {
+ writeln!(std::io::stdout(), "{message}").map_err(Into::into)
+ }
+}
+
+pub fn accounts(accounts: &[AccountView<'_>]) -> Result<()> {
+ let mut stdout = std::io::stdout().lock();
+ writeln!(stdout, "SELECTED\tNAME\tEMAIL\tHOST\tPRODUCTS\tID")?;
+ for account in accounts {
+ let products = account
+ .products
+ .iter()
+ .map(|p| p.name())
+ .collect::<Vec<_>>()
+ .join(",");
+ writeln!(
+ stdout,
+ "{}\t{}\t{}\t{}\t{}\t{}",
+ if account.selected { "*" } else { "" },
+ escape_controls(account.name),
+ escape_controls(account.email),
+ escape_controls(account.host),
+ products,
+ account.id
+ )?;
+ }
+ Ok(())
+}
+
+pub fn albums(albums: &[AlbumView]) -> Result<()> {
+ let mut stdout = std::io::stdout().lock();
+ writeln!(stdout, "ID\tNAME\tTYPE\tVISIBILITY\tOWNER\tUPDATED")?;
+ for album in albums {
+ writeln!(
+ stdout,
+ "{}\t{}\t{}\t{}\t{}\t{}",
+ album.id,
+ escape_controls(&album.name),
+ escape_controls(&album.kind),
+ album.visibility,
+ album.owner_id,
+ album.updated_at
+ )?;
+ }
+ Ok(())
+}
+
+fn escape_controls(value: &str) -> String {
+ let mut escaped = String::with_capacity(value.len());
+ for c in value.chars() {
+ if c.is_control() {
+ escaped.extend(c.escape_debug());
+ } else {
+ escaped.push(c);
+ }
+ }
+ escaped
+}
+
+#[cfg(test)]
+mod tests {
+ use super::escape_controls;
+
+ #[test]
+ fn human_output_preserves_printable_names() {
+ assert_eq!(
+ escape_controls("Bob's \"album\"\t\n"),
+ "Bob's \"album\"\\t\\n"
+ );
+ }
+}
diff --git a/rust/apps/cli-next/src/permissions.rs b/rust/apps/cli-next/src/permissions.rs
new file mode 100644
index 0000000000..4834b71ebd
--- /dev/null
+++ b/rust/apps/cli-next/src/permissions.rs
@@ -0,0 +1,75 @@
+use std::{fs, io, path::Path};
+
+#[cfg(unix)]
+pub fn create_home(path: &Path) -> io::Result<()> {
+ use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
+ fs::DirBuilder::new()
+ .recursive(true)
+ .mode(0o700)
+ .create(path)?;
+ fs::set_permissions(path, fs::Permissions::from_mode(0o700))
+}
+
+#[cfg(windows)]
+pub fn create_home(path: &Path) -> io::Result<()> {
+ use std::{os::windows::ffi::OsStrExt, ptr};
+ use windows_sys::Win32::{
+ Foundation::LocalFree,
+ Security::{
+ Authorization::{
+ ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1,
+ SE_FILE_OBJECT, SetNamedSecurityInfoW,
+ },
+ DACL_SECURITY_INFORMATION, GetSecurityDescriptorDacl,
+ PROTECTED_DACL_SECURITY_INFORMATION,
+ },
+ };
+
+ fs::create_dir_all(path)?;
+ let path: Vec<u16> = path.as_os_str().encode_wide().chain(Some(0)).collect();
+ // Protected DACL: only the owner, inherited by new files and directories.
+ let sddl: Vec<u16> = "D:P(A;OICI;FA;;;OW)\0".encode_utf16().collect();
+ let mut descriptor = ptr::null_mut();
+ // SAFETY: all pointers refer to live buffers. Windows allocates descriptor;
+ // the DACL borrows it until SetNamedSecurityInfoW finishes, then we free it.
+ unsafe {
+ if ConvertStringSecurityDescriptorToSecurityDescriptorW(
+ sddl.as_ptr(),
+ SDDL_REVISION_1,
+ &mut descriptor,
+ ptr::null_mut(),
+ ) == 0
+ {
+ return Err(io::Error::last_os_error());
+ }
+ let mut present = 0;
+ let mut defaulted = 0;
+ let mut dacl = ptr::null_mut();
+ let result = if GetSecurityDescriptorDacl(
+ descriptor,
+ &mut present,
+ &mut dacl,
+ &mut defaulted,
+ ) == 0
+ {
+ Err(io::Error::last_os_error())
+ } else {
+ let status = SetNamedSecurityInfoW(
+ path.as_ptr(),
+ SE_FILE_OBJECT,
+ DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
+ ptr::null_mut(),
+ ptr::null_mut(),
+ dacl,
+ ptr::null_mut(),
+ );
+ if status == 0 {
+ Ok(())
+ } else {
+ Err(io::Error::from_raw_os_error(status as i32))
+ }
+ };
+ LocalFree(descriptor);
+ result
+ }
+}
diff --git a/rust/apps/cli-next/src/vault.rs b/rust/apps/cli-next/src/vault.rs
new file mode 100644
index 0000000000..9092b0ecc0
--- /dev/null
+++ b/rust/apps/cli-next/src/vault.rs
@@ -0,0 +1,383 @@
+use std::{
+ collections::BTreeMap,
+ env,
+ fs::{self, File, OpenOptions},
+ io::ErrorKind,
+ path::{Path, PathBuf},
+};
+
+use anyhow::{Context, Result, bail, ensure};
+use ente_core::{
+ b64,
+ crypto::{Key, blob},
+};
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+use zeroize::{ZeroizeOnDrop, Zeroizing};
+
+use crate::{args::Product, parse_json, permissions};
+
+const SCHEMA_VERSION: u8 = 1;
+const VAULT_FILE: &str = "vault.json";
+
+pub struct Vault {
+ pub state: State,
+ home: PathBuf,
+ key: Key,
+ _lock: File,
+}
+
+pub(crate) struct VaultAccess {
+ home: PathBuf,
+ key: Key,
+}
+
+#[derive(Default, Serialize, Deserialize)]
+pub struct State {
+ pub accounts: Vec<Account>,
+ pub selected: Option<Uuid>,
+}
+
+#[derive(Serialize, Deserialize)]
+struct VersionedState<T> {
+ schema_version: u8,
+ state: T,
+}
+
+#[derive(Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+struct EncryptedVault {
+ data: String,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct Account {
+ pub storage_id: Uuid,
+ pub name: String,
+ pub email: String,
+ pub origin: String,
+ pub user_id: i64,
+ pub identity: AccountKeys,
+ pub sessions: BTreeMap<Product, StoredSession>,
+}
+
+#[derive(Serialize, Deserialize, ZeroizeOnDrop)]
+pub struct AccountKeys {
+ pub master_key: Vec<u8>,
+ pub secret_key: Vec<u8>,
+ pub public_key: Vec<u8>,
+}
+
+#[derive(Serialize, Deserialize, ZeroizeOnDrop)]
+pub struct StoredSession {
+ pub token: Vec<u8>,
+}
+
+impl Vault {
+ pub fn open() -> Result<Self> {
+ let override_key = environment_key()?;
+ let home = application_home()?;
+ permissions::create_home(&home).context("cannot create private CLI home")?;
+ let home = fs::canonicalize(home)?;
+ let lock = lock(&home)?;
+ let encrypted = read_vault(&home)?;
+ let key = load_key(override_key, encrypted.is_some(), || {
+ keyring::Entry::new("io.ente.cli", &home.to_string_lossy())
+ })?;
+ let state = match encrypted {
+ Some(bytes) => decrypt(&bytes, &key)?,
+ None => State::default(),
+ };
+ Ok(Self {
+ state,
+ home,
+ key,
+ _lock: lock,
+ })
+ }
+
+ pub fn into_state(self) -> State {
+ self.state
+ }
+
+ pub fn release(self) -> (State, VaultAccess) {
+ let Self {
+ state,
+ home,
+ key,
+ _lock,
+ } = self;
+ drop(_lock);
+ (state, VaultAccess { home, key })
+ }
+
+ pub fn save(&self) -> Result<()> {
+ let plaintext = Zeroizing::new(serde_json::to_vec(&VersionedState {
+ schema_version: SCHEMA_VERSION,
+ state: &self.state,
+ })?);
+ let encrypted = blob::encrypt_combined(&plaintext, &self.key)
+ .map_err(|_| anyhow::anyhow!("cannot encrypt the CLI vault"))?;
+ let mut file = tempfile::NamedTempFile::new_in(&self.home)?;
+ serde_json::to_writer(
+ &mut file,
+ &EncryptedVault {
+ data: b64::encode(&encrypted),
+ },
+ )?;
+ file.as_file().sync_all()?;
+ file.persist(self.home.join(VAULT_FILE))?;
+ #[cfg(unix)]
+ if let Err(error) = File::open(&self.home).and_then(|dir| dir.sync_all()) {
+ eprintln!("Warning: vault saved, but could not sync its directory: {error}");
+ }
+ Ok(())
+ }
+}
+
+impl VaultAccess {
+ pub fn open(self) -> Result<Vault> {
+ let Self { home, key } = self;
+ let lock = lock(&home)?;
+ let state = match read_vault(&home)? {
+ Some(bytes) => decrypt(&bytes, &key)?,
+ None => State::default(),
+ };
+ Ok(Vault {
+ state,
+ home,
+ key,
+ _lock: lock,
+ })
+ }
+}
+
+impl State {
+ pub fn load() -> Result<Self> {
+ let override_key = environment_key()?;
+ let home = application_home()?;
+ // Atomic replacement makes the file a complete snapshot without a read lock.
+ let Some(bytes) = read_vault(&home)? else {
+ return Ok(Self::default());
+ };
+ let home = fs::canonicalize(home)?;
+ let key = load_key(override_key, true, || {
+ keyring::Entry::new("io.ente.cli", &home.to_string_lossy())
+ })?;
+ decrypt(&bytes, &key)
+ }
+
+ pub fn named(&self, name: &str) -> Result<usize> {
+ self.accounts
+ .iter()
+ .position(|a| a.name == name)
+ .with_context(|| format!("no account named {name:?}; use account list"))
+ }
+
+ pub fn resolve(&self, name: Option<&str>) -> Result<usize> {
+ if let Some(name) = name {
+ return self.named(name);
+ }
+ if let Some(index) = self
+ .accounts
+ .iter()
+ .position(|a| Some(a.storage_id) == self.selected)
+ {
+ return Ok(index);
+ }
+ match self.accounts.len() {
+ 0 => bail!("no accounts; use photos login, locker login, or auth login"),
+ 1 => Ok(0),
+ _ => bail!("no account selected; use account switch <name> or --account <name>"),
+ }
+ }
+
+ pub fn check_name(&self, name: &str) -> Result<()> {
+ ensure!(!name.trim().is_empty(), "account name cannot be empty");
+ ensure!(
+ !self.accounts.iter().any(|a| a.name == name),
+ "account name {name:?} is already in use; choose another --name"
+ );
+ Ok(())
+ }
+}
+
+impl Account {
+ pub fn token(&self, product: Product) -> Result<&[u8]> {
+ self.sessions
+ .get(&product)
+ .map(|s| s.token.as_slice())
+ .with_context(|| {
+ format!(
+ "account {:?} has no {} session; use {} login --account {:?}",
+ self.name,
+ product.name(),
+ product.name(),
+ self.name
+ )
+ })
+ }
+}
+
+fn read_vault(home: &Path) -> Result<Option<Vec<u8>>> {
+ match fs::read(home.join(VAULT_FILE)) {
+ Ok(bytes) => Ok(Some(bytes)),
+ Err(error) if error.kind() == ErrorKind::NotFound => Ok(None),
+ Err(error) => Err(error).context("cannot read the CLI vault"),
+ }
+}
+
+fn lock(home: &Path) -> Result<File> {
+ let mut options = OpenOptions::new();
+ options.read(true).write(true).create(true);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::OpenOptionsExt;
+ options.mode(0o600);
+ }
+ let lock = options.open(home.join("vault.lock"))?;
+ lock.lock().context("cannot lock the CLI vault")?;
+ Ok(lock)
+}
+
+fn application_home() -> Result<PathBuf> {
+ if let Some(home) = env::var_os("ENTE_CLI_HOME") {
+ ensure!(!home.is_empty(), "ENTE_CLI_HOME cannot be empty");
+ return Ok(home.into());
+ }
+ Ok(dirs::data_local_dir()
+ .context("cannot find the application data directory; set ENTE_CLI_HOME")?
+ .join("ente-cli"))
+}
+
+fn environment_key() -> Result<Option<Key>> {
+ match env::var("ENTE_CLI_VAULT_KEY") {
+ Ok(value) => decode_key(&Zeroizing::new(value)).map(Some),
+ Err(env::VarError::NotPresent) => Ok(None),
+ Err(env::VarError::NotUnicode(_)) => {
+ bail!("ENTE_CLI_VAULT_KEY must be standard padded base64 of exactly 32 bytes")
+ }
+ }
+}
+
+fn decode_key(value: &str) -> Result<Key> {
+ let bytes = Zeroizing::new(b64::decode(value).map_err(|_| {
+ anyhow::anyhow!("ENTE_CLI_VAULT_KEY must be standard padded base64 of exactly 32 bytes")
+ })?);
+ Key::try_from_slice(&bytes).context("ENTE_CLI_VAULT_KEY must decode to exactly 32 bytes")
+}
+
+fn load_key(
+ override_key: Option<Key>,
+ exists: bool,
+ entry: impl FnOnce() -> keyring::Result<keyring::Entry>,
+) -> Result<Key> {
+ if let Some(key) = override_key {
+ return Ok(key);
+ }
+ let entry = entry().map_err(|error| keyring_error(error, exists))?;
+ match entry.get_secret() {
+ Ok(bytes) => Key::try_from_slice(&Zeroizing::new(bytes))
+ .context("system vault key is invalid; restore the original key"),
+ Err(keyring::Error::NoEntry) if !exists => {
+ let key = Key::generate();
+ entry
+ .set_secret(key.as_bytes())
+ .map_err(|error| keyring_error(error, false))?;
+ Ok(key)
+ }
+ Err(error) => Err(keyring_error(error, exists)),
+ }
+}
+
+fn keyring_error(error: keyring::Error, exists: bool) -> anyhow::Error {
+ let reason = match error {
+ keyring::Error::NoEntry => "the vault key is missing".to_owned(),
+ keyring::Error::NoStorageAccess(error) => {
+ format!("access to system secret storage was denied: {error}")
+ }
+ keyring::Error::PlatformFailure(error) => format!("system secret storage failed: {error}"),
+ keyring::Error::Ambiguous(_) => "multiple system vault keys match".to_owned(),
+ _ => "system secret storage returned an invalid vault-key entry".to_owned(),
+ };
+ if exists {
+ anyhow::anyhow!(
+ "{reason}; restore system storage access or supply the original ENTE_CLI_VAULT_KEY"
+ )
+ } else {
+ anyhow::anyhow!(
+ "{reason}; use vault key generate, retain the key securely, and supply it through ENTE_CLI_VAULT_KEY"
+ )
+ }
+}
+
+fn decrypt(bytes: &[u8], key: &Key) -> Result<State> {
+ let unlock_error = || {
+ anyhow::anyhow!(
+ "cannot unlock CLI vault: wrong key or damaged ciphertext; it has not been modified"
+ )
+ };
+ let encrypted: EncryptedVault = parse_json(bytes)
+ .context("unsupported or invalid CLI vault format; it has not been modified")?;
+ let ciphertext = b64::decode(&encrypted.data).map_err(|_| {
+ anyhow::anyhow!("unsupported or invalid CLI vault format; it has not been modified")
+ })?;
+ let plaintext =
+ Zeroizing::new(blob::decrypt_combined(&ciphertext, key).map_err(|_| unlock_error())?);
+ let version: VersionedState<serde::de::IgnoredAny> =
+ parse_json(&plaintext).context("invalid CLI vault contents; it has not been modified")?;
+ ensure!(
+ version.schema_version == SCHEMA_VERSION,
+ "unsupported CLI vault schema; it has not been modified"
+ );
+ let stored: VersionedState<State> =
+ parse_json(&plaintext).context("invalid CLI vault contents; it has not been modified")?;
+ Ok(stored.state)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn key_selection_never_replaces_an_existing_vault_key() {
+ for exists in [false, true] {
+ let key = Key::generate();
+ let expected = Key::try_from_slice(key.as_bytes()).unwrap();
+ assert_eq!(
+ load_key(Some(key), exists, || panic!(
+ "environment override touched keyring"
+ ))
+ .unwrap(),
+ expected
+ );
+
+ let entry = keyring::Entry::new_with_credential(Box::new(
+ keyring::mock::MockCredential::default(),
+ ));
+ entry.set_secret(expected.as_bytes()).unwrap();
+ assert_eq!(load_key(None, exists, || Ok(entry)).unwrap(), expected);
+ }
+ let entry = || {
+ Ok(keyring::Entry::new_with_credential(Box::new(
+ keyring::mock::MockCredential::default(),
+ )))
+ };
+ assert!(load_key(None, false, entry).is_ok());
+ let error = load_key(None, true, entry).unwrap_err().to_string();
+ assert!(error.contains("original ENTE_CLI_VAULT_KEY"));
+ assert!(!error.contains("generate"));
+
+ for exists in [false, true] {
+ let error = load_key(None, exists, || {
+ Err(keyring::Error::NoStorageAccess(Box::new(
+ std::io::Error::from(ErrorKind::PermissionDenied),
+ )))
+ })
+ .unwrap_err()
+ .to_string();
+ assert!(error.contains("denied"));
+ assert_eq!(error.contains("generate"), !exists);
+ }
+ }
+}
diff --git a/rust/apps/cli-next/tests/cli.rs b/rust/apps/cli-next/tests/cli.rs
new file mode 100644
index 0000000000..48d8e56a7e
--- /dev/null
+++ b/rust/apps/cli-next/tests/cli.rs
@@ -0,0 +1,623 @@
+use std::{
+ fs,
+ io::Write,
+ process::{Command, Output, Stdio},
+};
+
+use ente_core::{
+ b64,
+ crypto::{Header, Key, blob},
+};
+use serde_json::{Value, json};
+use tempfile::TempDir;
+
+#[test]
+fn reads_do_not_initialize_storage() {
+ for existing_home in [false, true] {
+ let home = TestHome::new();
+ let path = home.dir.path().join("unused");
+ if existing_home {
+ fs::create_dir(&path).unwrap();
+ }
+ for args in [
+ vec!["account", "list", "--json"],
+ vec!["account", "view", "missing"],
+ vec!["photos", "album", "list"],
+ vec!["photos", "api", "/users/details/v2"],
+ ] {
+ let output = home
+ .command(&args)
+ .env("ENTE_CLI_HOME", &path)
+ .output()
+ .unwrap();
+ if args[1] == "list" {
+ assert_eq!(success(output).stdout, b"[]\n");
+ } else {
+ assert!(failure(&output).contains("no account"));
+ }
+ assert_eq!(path.exists(), existing_home, "read created the CLI home");
+ if existing_home {
+ assert_eq!(fs::read_dir(&path).unwrap().count(), 0);
+ }
+ #[cfg(target_os = "linux")]
+ {
+ let output = home
+ .command(&args)
+ .env("ENTE_CLI_HOME", &path)
+ .env_remove("ENTE_CLI_VAULT_KEY")
+ .env(
+ "DBUS_SESSION_BUS_ADDRESS",
+ "unix:path=/nonexistent-ente-test-bus",
+ )
+ .output()
+ .unwrap();
+ if args[1] == "list" {
+ assert_eq!(success(output).stdout, b"[]\n");
+ } else {
+ assert!(failure(&output).contains("no account"));
+ }
+ assert_eq!(path.exists(), existing_home);
+ if existing_home {
+ assert_eq!(fs::read_dir(&path).unwrap().count(), 0);
+ }
+ }
+ }
+ }
+}
+
+#[test]
+fn deleted_collections_are_filtered_before_decryption() {
+ let mut server = mockito::Server::new();
+ let request = server
+ .mock("GET", "/collections/v2")
+ .match_query(mockito::Matcher::UrlEncoded("sinceTime".into(), "0".into()))
+ .with_header("content-type", "application/json")
+ .with_body(
+ json!({"collections": [{
+ "id": 12, "owner": {"id": 9007199254740993i64},
+ "encryptedKey": "", "name": "", "encryptedName": "",
+ "nameDecryptionNonce": "", "type": "album",
+ "updationTime": 1, "isDeleted": true
+ }]})
+ .to_string(),
+ )
+ .create();
+ let home = TestHome::new();
+ home.seed(&server.url());
+ assert_eq!(home.json(&["photos", "album", "list"]), json!([]));
+ request.assert();
+}
+
+#[cfg(unix)]
+#[test]
+fn reading_an_existing_vault_does_not_change_its_directory() {
+ use std::os::unix::fs::PermissionsExt;
+
+ let home = TestHome::new();
+ home.seed("http://localhost:8080");
+ fs::set_permissions(home.dir.path(), fs::Permissions::from_mode(0o500)).unwrap();
+ let result = home.run(&["account", "list"]);
+ let mode = fs::metadata(home.dir.path()).unwrap().permissions().mode() & 0o777;
+ fs::set_permissions(home.dir.path(), fs::Permissions::from_mode(0o700)).unwrap();
+ success(result);
+ assert_eq!(mode, 0o500, "read changed the CLI home permissions");
+ assert_eq!(fs::read_dir(home.dir.path()).unwrap().count(), 1);
+}
+
+#[test]
+fn invalid_vault_wrappers_do_not_require_decryption() {
+ let home = TestHome::new();
+ for bytes in [
+ b"different layout".as_slice(),
+ br#"{"data":"","version":2}"#,
+ br#"{"data":"not base64"}"#,
+ ] {
+ fs::write(home.dir.path().join("vault.json"), bytes).unwrap();
+ for key in [&home.key, &Key::generate()] {
+ let output = home
+ .command(&["account", "list"])
+ .env("ENTE_CLI_VAULT_KEY", b64::encode(key.as_bytes()))
+ .output()
+ .unwrap();
+ assert!(failure(&output).contains("unsupported or invalid CLI vault format"));
+ assert_eq!(fs::read(home.dir.path().join("vault.json")).unwrap(), bytes);
+ }
+ }
+}
+
+#[test]
+fn keys_and_invalid_vaults_have_no_destructive_fallback() {
+ let home = TestHome::new();
+ let generated = success(
+ home.command(&["vault", "key", "generate"])
+ .env("ENTE_CLI_VAULT_KEY", "invalid")
+ .output()
+ .unwrap(),
+ );
+ assert_eq!(
+ b64::decode(std::str::from_utf8(&generated.stdout).unwrap().trim())
+ .unwrap()
+ .len(),
+ 32
+ );
+ assert_eq!(fs::read_dir(home.dir.path()).unwrap().count(), 0);
+
+ home.seed("http://localhost:8080");
+ let original = fs::read(home.dir.path().join("vault.json")).unwrap();
+ for key in [String::new(), "invalid".into(), b64::encode(&[0u8; 31])] {
+ let output = home
+ .command(&["account", "list"])
+ .env("ENTE_CLI_VAULT_KEY", &key)
+ .output()
+ .unwrap();
+ failure(&output);
+ if !key.is_empty() {
+ assert!(!String::from_utf8_lossy(&output.stderr).contains(&key));
+ }
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ original
+ );
+ }
+ let wrong_key = failure(
+ &home
+ .command(&["account", "list"])
+ .env(
+ "ENTE_CLI_VAULT_KEY",
+ b64::encode(Key::generate().as_bytes()),
+ )
+ .output()
+ .unwrap(),
+ );
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ original
+ );
+ let encrypted = home.read_encrypted_vault();
+ for index in [0, Header::BYTES - 1, Header::BYTES, encrypted.len() - 1] {
+ let mut damaged = encrypted.clone();
+ damaged[index] ^= 1;
+ home.write_encrypted_vault(&damaged);
+ assert_eq!(failure(&home.run(&["account", "list"])), wrong_key);
+ assert_eq!(home.read_encrypted_vault(), damaged);
+ }
+ for length in [0, Header::BYTES - 1, Header::BYTES, encrypted.len() - 1] {
+ home.write_encrypted_vault(&encrypted[..length]);
+ assert_eq!(failure(&home.run(&["account", "list"])), wrong_key);
+ assert_eq!(home.read_encrypted_vault(), encrypted[..length]);
+ }
+}
+
+#[test]
+fn vault_schema_version_is_checked_after_decryption() {
+ let home = TestHome::new();
+ home.seed("http://localhost:8080");
+ home.write_vault_document(&json!({"schema_version": 2, "state": Value::Null}));
+ let original = fs::read(home.dir.path().join("vault.json")).unwrap();
+ assert!(failure(&home.run(&["account", "list"])).contains("unsupported CLI vault schema"));
+ let output = home
+ .command(&["account", "list"])
+ .env(
+ "ENTE_CLI_VAULT_KEY",
+ b64::encode(Key::generate().as_bytes()),
+ )
+ .output()
+ .unwrap();
+ assert!(failure(&output).contains("cannot unlock CLI vault"));
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ original
+ );
+}
+
+#[tokio::test]
+async fn raw_api_preserves_requests_and_responses_without_leaking_credentials() {
+ let mut origin = mockito::Server::new_async().await;
+ let mut other = mockito::Server::new_async().await;
+ let home = TestHome::new();
+ home.seed(&origin.url());
+ let secret = "--8=";
+ let escaped = other
+ .mock("GET", mockito::Matcher::Any)
+ .expect(0)
+ .create_async()
+ .await;
+ let redirect = origin
+ .mock("GET", "/redirect")
+ .with_status(302)
+ .with_header("location", &format!("{}/stolen", other.url()))
+ .create_async()
+ .await;
+ failure(&home.run(&["photos", "api", "/redirect"]));
+ failure(&home.run(&["photos", "api", &format!("{}/stolen", other.url())]));
+ redirect.assert_async().await;
+ escaped.assert_async().await;
+
+ let headers = home.dir.path().join("headers.json");
+ fs::write(&headers, br#"{"Host":"different.example.org"}"#).unwrap();
+ let mismatched_host = origin.mock("GET", "/host").expect(0).create_async().await;
+ let output = home.run(&[
+ "photos",
+ "api",
+ "/host",
+ "--headers",
+ headers.to_str().unwrap(),
+ ]);
+ assert!(failure(&output).contains("Host cannot redirect stored account credentials"));
+ assert!(output.stdout.is_empty());
+ mismatched_host.assert_async().await;
+
+ let local_redirect = origin
+ .mock("GET", "/local")
+ .with_status(307)
+ .with_header("location", "/bytes")
+ .create_async()
+ .await;
+ let body = b"raw\0bytes\n";
+ let response = origin
+ .mock("GET", "/bytes")
+ .match_header("x-auth-token", secret)
+ .match_header("x-client-package", "io.ente.photos")
+ .with_body(body)
+ .expect(2)
+ .create_async()
+ .await;
+ assert_eq!(
+ success(home.run(&["photos", "api", "/local", "--json"])).stdout,
+ body
+ );
+ assert_eq!(
+ success(home.run(&["photos", "api", &format!("{}/bytes", origin.url())])).stdout,
+ body
+ );
+ local_redirect.assert_async().await;
+ response.assert_async().await;
+
+ fs::write(&headers, br#"{"content-type":"application/octet-stream","x-client-package":"explicit.package","x-auth-token":"explicit-token"}"#).unwrap();
+ let request = origin
+ .mock("PATCH", "/request")
+ .match_query(mockito::Matcher::UrlEncoded("label".into(), "a & b".into()))
+ .match_header("x-client-package", "explicit.package")
+ .match_header("x-auth-token", "explicit-token")
+ .match_header("content-type", "application/octet-stream")
+ .match_body(body.to_vec())
+ .with_status(422)
+ .with_body(body)
+ .create_async()
+ .await;
+ let output = home.with_input(
+ &[
+ "photos",
+ "api",
+ "/request",
+ "--method",
+ "PATCH",
+ "--query",
+ "label=a & b",
+ "--headers",
+ headers.to_str().unwrap(),
+ "--body",
+ "-",
+ ],
+ body,
+ );
+ failure(&output);
+ assert_eq!(output.stdout, body);
+ request.assert_async().await;
+}
+
+#[test]
+fn private_json_errors_do_not_echo_values() {
+ let mut server = mockito::Server::new();
+ let ping = server
+ .mock("GET", "/ping")
+ .with_body(r#"{"message":"pong","id":"fixture"}"#)
+ .expect(5)
+ .create();
+ let authentication = server
+ .mock("GET", "/users/srp/attributes")
+ .match_query(mockito::Matcher::Any)
+ .expect(0)
+ .create();
+ let request = server.mock("GET", "/request").expect(0).create();
+ let home = TestHome::new();
+ home.seed(&server.url());
+ let original = fs::read(home.dir.path().join("vault.json")).unwrap();
+ let mut errors = Vec::new();
+ for input in [
+ json!({"email": "[email protected]", "password": 987654321}),
+ json!({"email": "[email protected]", "password": "secret", "otp": 987654321}),
+ json!({"email": "[email protected]", "password": "secret", "totp": 987654321}),
+ json!({"email": "[email protected]", "password": "secret", "private-987654321": "secret"}),
+ json!("private-987654321"),
+ ] {
+ let output = home.with_input(
+ &["photos", "login", "--host", &server.url(), "--input", "-"],
+ &serde_json::to_vec(&input).unwrap(),
+ );
+ assert!(output.stdout.is_empty());
+ let error = failure(&output);
+ assert!(error.contains("login input must contain"), "{error}");
+ errors.push(error);
+ }
+ for input in [
+ json!({"x-auth-token": 987654321}),
+ json!("private-987654321"),
+ ] {
+ let output = home.with_input(
+ &["photos", "api", "/request", "--headers", "-"],
+ &serde_json::to_vec(&input).unwrap(),
+ );
+ assert!(output.stdout.is_empty());
+ let error = failure(&output);
+ assert!(error.contains("headers must be a JSON object"), "{error}");
+ errors.push(error);
+ }
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ original
+ );
+ let mut state = home.read_vault();
+ state["accounts"][0]["sessions"]["photos"]["token"] = json!("private-987654321");
+ home.write_vault(&state);
+ let invalid = fs::read(home.dir.path().join("vault.json")).unwrap();
+ let output = home.run(&["account", "list"]);
+ assert!(output.stdout.is_empty());
+ errors.push(failure(&output));
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ invalid
+ );
+ ping.assert();
+ authentication.assert();
+ request.assert();
+ for error in errors {
+ assert!(
+ !error.contains("987654321"),
+ "private JSON value leaked: {error}"
+ );
+ assert!(
+ error.contains("line ") && error.contains("column "),
+ "{error}"
+ );
+ }
+}
+
+#[test]
+fn logout_reconciles_remote_and_local_session_state() {
+ let mut server = mockito::Server::new();
+ let revoked = server
+ .mock("POST", "/users/logout")
+ .match_header("x-auth-token", "--8=")
+ .match_header("x-client-package", "io.ente.photos")
+ .with_status(401)
+ .create();
+ let home = TestHome::new();
+ home.seed(&server.url());
+ success(home.run(&["photos", "logout"]));
+ assert_eq!(
+ home.json(&["account", "view", "fixture"])["products"],
+ json!([])
+ );
+ revoked.assert();
+
+ let unavailable = server
+ .mock("POST", "/users/logout")
+ .match_header("x-auth-token", "--8=")
+ .with_status(403)
+ .create();
+ let home = TestHome::new();
+ home.seed(&server.url());
+ let before = fs::read(home.dir.path().join("vault.json")).unwrap();
+ failure(&home.run(&["photos", "logout"]));
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ before
+ );
+ unavailable.assert();
+}
+
+#[test]
+fn account_updates_preserve_identity_and_selection() {
+ let home = TestHome::new();
+ home.seed("http://localhost:8080");
+ assert_eq!(
+ home.json(&["account", "list"]),
+ json!([{
+ "id": "9007199254740993", "name": "fixture", "email": "[email protected]",
+ "host": "http://localhost:8080", "products": ["photos"], "selected": true
+ }])
+ );
+ let mut state = home.read_vault();
+ let mut second = state["accounts"][0].clone();
+ second["storage_id"] = json!("f6579aae-83ef-4f28-965e-8d34883d3fe5");
+ second["name"] = json!("second");
+ second["sessions"] = json!({});
+ state["accounts"].as_array_mut().unwrap().push(second);
+ home.write_vault(&state);
+
+ success(home.run(&["account", "rename", "fixture", "renamed"]));
+ success(home.run(&["account", "rename", "second", "other"]));
+ state["accounts"][0]["name"] = json!("renamed");
+ state["accounts"][1]["name"] = json!("other");
+ assert_eq!(home.read_vault(), state);
+ failure(&home.run(&["photos", "album", "list", "--account", "other"]));
+ let original = fs::read(home.dir.path().join("vault.json")).unwrap();
+ failure(&home.run(&["account", "rename", "other", "renamed"]));
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ original
+ );
+ let before = home.read_encrypted_vault();
+ success(home.run(&["account", "switch", "other"]));
+ state["selected"] = state["accounts"][1]["storage_id"].clone();
+ assert_eq!(home.read_vault(), state);
+ let after = home.read_encrypted_vault();
+ assert_ne!(
+ before[..Header::BYTES],
+ after[..Header::BYTES],
+ "vault rewrite reused its nonce"
+ );
+}
+
+struct TestHome {
+ dir: TempDir,
+ key: Key,
+}
+
+#[test]
+#[ignore = "requires accessible native secret storage; creates and removes a disposable keyring entry"]
+fn native_keyring_survives_separate_processes() {
+ let mut home = TestHome::new();
+ let path = fs::canonicalize(home.dir.path()).unwrap();
+ let entry = keyring::Entry::new("io.ente.cli", &path.to_string_lossy()).unwrap();
+ assert!(matches!(entry.get_secret(), Err(keyring::Error::NoEntry)));
+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ success(
+ home.command(&["account", "list"])
+ .env_remove("ENTE_CLI_VAULT_KEY")
+ .output()
+ .unwrap(),
+ );
+ assert!(matches!(entry.get_secret(), Err(keyring::Error::NoEntry)));
+ assert_eq!(fs::read_dir(home.dir.path()).unwrap().count(), 0);
+ let mut server = mockito::Server::new();
+ let ping = server.mock("GET", "/ping").with_status(503).create();
+ let output = home
+ .command(&["photos", "login", "--host", &server.url()])
+ .env_remove("ENTE_CLI_VAULT_KEY")
+ .output()
+ .unwrap();
+ assert!(failure(&output).contains("cannot reach the selected Ente server"));
+ ping.assert();
+ home.key = Key::try_from_slice(&entry.get_secret().unwrap()).unwrap();
+ home.seed("http://localhost:8080");
+ success(
+ home.command(&["account", "rename", "fixture", "native"])
+ .env_remove("ENTE_CLI_VAULT_KEY")
+ .output()
+ .unwrap(),
+ );
+ let output = success(
+ home.command(&["account", "view", "native", "--json"])
+ .env_remove("ENTE_CLI_VAULT_KEY")
+ .output()
+ .unwrap(),
+ );
+ assert_eq!(
+ serde_json::from_slice::<Value>(&output.stdout).unwrap()["name"],
+ "native"
+ );
+ }));
+ let cleanup = entry.delete_credential();
+ if let Err(panic) = result {
+ std::panic::resume_unwind(panic);
+ }
+ cleanup.unwrap();
+}
+
+impl TestHome {
+ fn new() -> Self {
+ Self {
+ dir: tempfile::tempdir().unwrap(),
+ key: Key::generate(),
+ }
+ }
+
+ fn command(&self, args: &[&str]) -> Command {
+ let mut command = Command::new(env!("CARGO_BIN_EXE_ente-cli-next"));
+ command
+ .env_clear()
+ .env("ENTE_CLI_HOME", self.dir.path())
+ .env("ENTE_CLI_VAULT_KEY", b64::encode(self.key.as_bytes()))
+ .args(args)
+ .stdin(Stdio::null())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped());
+ #[cfg(target_os = "linux")]
+ if let Some(address) = std::env::var_os("DBUS_SESSION_BUS_ADDRESS") {
+ command.env("DBUS_SESSION_BUS_ADDRESS", address);
+ }
+ #[cfg(windows)]
+ if let Some(root) = std::env::var_os("SystemRoot") {
+ command.env("SystemRoot", root);
+ }
+ command
+ }
+
+ fn run(&self, args: &[&str]) -> Output {
+ self.command(args).output().unwrap()
+ }
+
+ fn with_input(&self, args: &[&str], input: &[u8]) -> Output {
+ let mut child = self.command(args).stdin(Stdio::piped()).spawn().unwrap();
+ child.stdin.take().unwrap().write_all(input).unwrap();
+ child.wait_with_output().unwrap()
+ }
+
+ fn json(&self, args: &[&str]) -> Value {
+ let output = success(self.command(args).arg("--json").output().unwrap());
+ serde_json::from_slice(&output.stdout).unwrap()
+ }
+
+ fn seed(&self, origin: &str) {
+ let id = "e6b355f0-5d4d-4fc3-b32c-a6c8f52b8210";
+ self.write_vault(&json!({
+ "selected": id,
+ "accounts": [{
+ "storage_id": id, "name": "fixture", "email": "[email protected]",
+ "origin": origin, "user_id": 9007199254740993i64,
+ "identity": {"master_key": vec![0u8; 32], "secret_key": vec![0u8; 32], "public_key": vec![0u8; 32]},
+ "sessions": {"photos": {"token": [0xfb, 0xef]}}
+ }]
+ }));
+ }
+
+ fn write_vault(&self, state: &Value) {
+ self.write_vault_document(&json!({"schema_version": 1, "state": state}));
+ }
+
+ fn write_vault_document(&self, document: &Value) {
+ let plaintext = serde_json::to_vec(document).unwrap();
+ self.write_encrypted_vault(&blob::encrypt_combined(&plaintext, &self.key).unwrap());
+ }
+
+ fn write_encrypted_vault(&self, encrypted: &[u8]) {
+ fs::write(
+ self.dir.path().join("vault.json"),
+ serde_json::to_vec(&json!({"data": b64::encode(encrypted)})).unwrap(),
+ )
+ .unwrap();
+ }
+
+ fn read_encrypted_vault(&self) -> Vec<u8> {
+ let stored: Value =
+ serde_json::from_slice(&fs::read(self.dir.path().join("vault.json")).unwrap()).unwrap();
+ b64::decode(stored["data"].as_str().unwrap()).unwrap()
+ }
+
+ fn read_vault(&self) -> Value {
+ let plaintext = blob::decrypt_combined(&self.read_encrypted_vault(), &self.key).unwrap();
+ let stored: Value = serde_json::from_slice(&plaintext).unwrap();
+ assert_eq!(stored["schema_version"], 1);
+ stored["state"].clone()
+ }
+}
+
+fn success(output: Output) -> Output {
+ assert!(
+ output.status.success(),
+ "{}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ output
+}
+
+fn failure(output: &Output) -> String {
+ assert!(!output.status.success(), "command unexpectedly succeeded");
+ String::from_utf8_lossy(&output.stderr).into_owned()
+}
+
+#[cfg(feature = "museum")]
+#[path = "support/museum.rs"]
+mod museum;
diff --git a/rust/apps/cli-next/tests/support/museum.rs b/rust/apps/cli-next/tests/support/museum.rs
new file mode 100644
index 0000000000..e920667594
--- /dev/null
+++ b/rust/apps/cli-next/tests/support/museum.rs
@@ -0,0 +1,491 @@
+use ente_accounts::{
+ AccountsClient, AccountsClientConfig, AuthFlow, AuthFlowUi, AuthenticatedAccount,
+ CreateAccountParams, OtpPurpose, SecondFactorMethod, TotpPurpose,
+};
+use ente_core::crypto::{PublicKey, blob, sealed, secretbox};
+use ente_test_support::{HARDCODED_OTT, Museum, TestResult};
+use uuid::Uuid;
+use zeroize::Zeroizing;
+
+use super::*;
+
+const PASSWORD: &str = "disposable-cli-integration-password";
+
+#[test]
+fn login_and_photos_across_processes() -> TestResult {
+ Museum::run_async(exercise)
+}
+
+#[cfg(unix)]
+#[test]
+fn login_save_failures_follow_the_vault_replacement() -> TestResult {
+ use std::os::unix::fs::PermissionsExt;
+
+ Museum::run_async(|origin| async move {
+ let email = format!("save-failure-{}@example.org", Uuid::new_v4());
+ let owner = create_account(&origin, &email).await;
+ for (relogin, before_replacement) in
+ [(false, true), (false, false), (true, true), (true, false)]
+ {
+ let home = TestHome::new();
+ if relogin {
+ login(&home, "photos", &email, &["--host", &origin]);
+ }
+ let before = session_count(&origin, &owner).await;
+ let previous_vault = fs::read(home.dir.path().join("vault.json")).ok();
+ let fifo = home.dir.path().join("login-input");
+ assert!(
+ Command::new("mkfifo")
+ .arg(&fifo)
+ .status()
+ .unwrap()
+ .success()
+ );
+ let child = home
+ .command(&[
+ "photos",
+ "login",
+ "--host",
+ &origin,
+ "--input",
+ fifo.to_str().unwrap(),
+ "--json",
+ ])
+ .spawn()
+ .unwrap();
+ // Opening the FIFO waits until login has reached credential input.
+ let mut input = fs::OpenOptions::new().write(true).open(&fifo).unwrap();
+ let lock_free = fs::File::open(home.dir.path().join("vault.lock"))
+ .unwrap()
+ .try_lock()
+ .is_ok();
+ let concurrent_name = if lock_free && relogin && !before_replacement {
+ success(home.run(&["account", "rename", &email, "renamed"]));
+ Some("renamed")
+ } else {
+ None
+ };
+ if before_replacement {
+ fs::set_permissions(home.dir.path(), fs::Permissions::from_mode(0o500)).unwrap();
+ } else {
+ // Allow replacement, but deny opening the directory for its final sync.
+ fs::set_permissions(home.dir.path(), fs::Permissions::from_mode(0o300)).unwrap();
+ }
+ input.write_all(&credentials(&email)).unwrap();
+ drop(input);
+ let output = child.wait_with_output().unwrap();
+ fs::set_permissions(home.dir.path(), fs::Permissions::from_mode(0o700)).unwrap();
+ assert!(
+ lock_free,
+ "login held the vault lock while waiting for credentials"
+ );
+ if before_replacement {
+ failure(&output);
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).ok(),
+ previous_vault
+ );
+ if relogin {
+ assert_eq!(
+ home.json(&["photos", "api", "/users/details/v2"])["email"],
+ email
+ );
+ }
+ assert_eq!(
+ session_count(&origin, &owner).await,
+ before,
+ "failed login left an unsaved server session active"
+ );
+ } else {
+ let stderr = String::from_utf8(output.stderr).unwrap();
+ assert!(
+ output.status.success(),
+ "saved vault was reported as failed: {stderr}"
+ );
+ assert!(
+ stderr.contains("vault saved, but could not sync its directory"),
+ "{stderr}"
+ );
+ let logged_in: Value = serde_json::from_slice(&output.stdout).unwrap();
+ assert_eq!(logged_in["account"]["id"], owner.user_id.to_string());
+ if let Some(name) = concurrent_name {
+ assert_eq!(logged_in["account"]["name"], name);
+ }
+ assert_eq!(
+ home.json(&["photos", "api", "/users/details/v2"])["email"],
+ email
+ );
+ assert_eq!(
+ session_count(&origin, &owner).await,
+ before + usize::from(!relogin),
+ "successful login left a replaced server session active"
+ );
+ }
+ }
+ Ok(())
+ })
+}
+
+async fn exercise(origin: String) -> TestResult {
+ let alice_email = format!("alice-{}@example.org", Uuid::new_v4());
+ let bob_email = format!("bob-{}@example.org", Uuid::new_v4());
+ let alice = create_account(&origin, &alice_email).await;
+ let alice_id = alice.user_id.to_string();
+ let bob = create_account(&origin, &bob_email).await;
+ let (album, key) = create_album(&origin, &alice, "Monsoon 🌧", false).await;
+ let (archive, archive_key) = create_album(&origin, &alice, "Archive", true).await;
+ let (hidden, hidden_key) = create_album(&origin, &alice, "Private album", false).await;
+ let (default_hidden, default_hidden_key) =
+ create_album(&origin, &alice, "Default hidden", false).await;
+ set_album_metadata(
+ &origin,
+ &alice,
+ archive,
+ &archive_key,
+ "magic-metadata",
+ json!({"visibility": 1}),
+ )
+ .await;
+ set_album_metadata(
+ &origin,
+ &alice,
+ hidden,
+ &hidden_key,
+ "magic-metadata",
+ json!({"visibility": 2}),
+ )
+ .await;
+ set_album_metadata(
+ &origin,
+ &alice,
+ default_hidden,
+ &default_hidden_key,
+ "magic-metadata",
+ json!({"subType": 1}),
+ )
+ .await;
+ let (bob_album, _) = create_album(&origin, &bob, "Bob's album", false).await;
+ let shared_key = sealed::seal(
+ key.as_bytes(),
+ &PublicKey::try_from_slice(&bob.secrets.public_key).unwrap(),
+ )
+ .unwrap();
+ reqwest::Client::new().post(format!("{origin}/collections/share"))
+ .header("x-auth-token", b64::encode_url_safe(&alice.secrets.token)).header("x-client-package", "io.ente.photos")
+ .json(&json!({"collectionID": album, "email": bob_email, "encryptedKey": b64::encode(&shared_key), "role": "VIEWER"}))
+ .send().await.unwrap().error_for_status().unwrap();
+ set_album_metadata(
+ &origin,
+ &bob,
+ album,
+ &key,
+ "sharee-magic-metadata",
+ json!({"visibility": 2}),
+ )
+ .await;
+
+ let home = TestHome::new();
+ login(&home, "photos", &alice_email, &["--host", &origin]);
+ let initial = home.read_vault();
+ let first_id = initial["accounts"][0]["storage_id"].clone();
+ assert_eq!(initial["accounts"][0]["name"], alice_email);
+ let albums = home.json(&["photos", "album", "list"]);
+ let mut listed: Vec<_> = albums
+ .as_array()
+ .unwrap()
+ .iter()
+ .map(|a| {
+ (
+ a["name"].as_str().unwrap(),
+ a["visibility"].as_str().unwrap(),
+ )
+ })
+ .collect();
+ listed.sort();
+ let expected = [
+ ("Archive", "archived"),
+ ("Default hidden", "hidden"),
+ ("Monsoon 🌧", "visible"),
+ ("Private album", "hidden"),
+ ];
+ assert_eq!(listed, expected);
+ let monsoon = albums
+ .as_array()
+ .unwrap()
+ .iter()
+ .find(|a| a["name"] == "Monsoon 🌧")
+ .unwrap();
+ assert_eq!(monsoon["id"], album.to_string());
+ assert_eq!(monsoon["ownerId"], alice_id);
+ assert_eq!(monsoon["type"], "album");
+ assert_eq!(
+ chrono::DateTime::parse_from_rfc3339(monsoon["updatedAt"].as_str().unwrap())
+ .unwrap()
+ .offset()
+ .local_minus_utc(),
+ 0
+ );
+ let human = String::from_utf8(success(home.run(&["photos", "album", "list"])).stdout).unwrap();
+ assert!(human.starts_with("ID\tNAME\tTYPE\tVISIBILITY\tOWNER\tUPDATED\n"));
+ let mut listed: Vec<_> = human
+ .lines()
+ .skip(1)
+ .map(|line| {
+ let columns: Vec<_> = line.split('\t').collect();
+ (columns[1], columns[3])
+ })
+ .collect();
+ listed.sort();
+ assert_eq!(listed, expected);
+ let raw: Value =
+ serde_json::from_slice(&success(home.run(&["photos", "api", "/collections/v2"])).stdout)
+ .unwrap();
+ assert!(raw["collections"].as_array().unwrap().iter().any(|a| a["id"] == album && a["encryptedName"].as_str().is_some_and(|s| !s.is_empty())));
+
+ // Two origins with the same server user ID are still different accounts.
+ let alias_origin = origin.replace("127.0.0.1", "localhost");
+ assert_ne!(alias_origin, origin);
+ let before = fs::read(home.dir.path().join("vault.json")).unwrap();
+ assert!(
+ failure(&home.with_input(
+ &["photos", "login", "--host", &alias_origin, "--input", "-"],
+ &credentials(&alice_email),
+ ))
+ .contains("already in use")
+ );
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ before
+ );
+ login(
+ &home,
+ "photos",
+ &alice_email,
+ &["--host", &alias_origin, "--name", "other-server"],
+ );
+ success(home.run(&["account", "rename", &alice_email, "work"]));
+ assert_eq!(home.read_vault()["accounts"][0]["storage_id"], first_id);
+
+ login(&home, "auth", &bob_email, &["--host", &origin]);
+ assert!(failure(&home.run(&["photos", "album", "list"])).contains("no photos session"));
+ login(&home, "locker", &alice_email, &["--account", "work"]);
+ let accounts = home.json(&["account", "list"]);
+ assert!(
+ accounts
+ .as_array()
+ .unwrap()
+ .iter()
+ .any(|a| a["name"] == bob_email && a["selected"] == true)
+ );
+ assert!(
+ accounts
+ .as_array()
+ .unwrap()
+ .iter()
+ .any(|a| a["name"] == "work" && a["products"] == json!(["photos", "locker"]))
+ );
+
+ login(&home, "photos", &bob_email, &["--account", &bob_email]);
+ let albums = home.json(&["photos", "album", "list"]);
+ let mut ids: Vec<_> = albums
+ .as_array()
+ .unwrap()
+ .iter()
+ .map(|a| a["id"].as_str().unwrap().to_owned())
+ .collect();
+ ids.sort();
+ let mut expected = vec![album.to_string(), bob_album.to_string()];
+ expected.sort();
+ assert_eq!(ids, expected);
+ let shared = albums
+ .as_array()
+ .unwrap()
+ .iter()
+ .find(|a| a["id"] == monsoon["id"])
+ .unwrap();
+ assert_eq!(shared["name"], "Monsoon 🌧");
+ assert_eq!(shared["ownerId"], alice_id);
+ assert_eq!(shared["visibility"], "hidden");
+
+ login(&home, "photos", &alice_email, &["--host", &origin]);
+ let state = home.read_vault();
+ assert_eq!(state["accounts"].as_array().unwrap().len(), 3);
+ assert_eq!(state["selected"], first_id);
+ assert_eq!(state["accounts"][0]["name"], "work");
+ assert!(!serde_json::to_string(&state).unwrap().contains(PASSWORD));
+ let before = fs::read(home.dir.path().join("vault.json")).unwrap();
+ assert!(
+ failure(&home.with_input(
+ &["photos", "login", "--account", "work", "--input", "-"],
+ &credentials(&bob_email),
+ ))
+ .contains("identity does not match")
+ );
+ assert_eq!(
+ fs::read(home.dir.path().join("vault.json")).unwrap(),
+ before
+ );
+
+ let token: Vec<u8> =
+ serde_json::from_value(state["accounts"][0]["sessions"]["photos"]["token"].clone())
+ .unwrap();
+ success(home.run(&["photos", "logout"]));
+ let response = reqwest::Client::new()
+ .get(format!("{origin}/collections/v2"))
+ .header("x-auth-token", b64::encode_url_safe(&token))
+ .header("x-client-package", "io.ente.photos")
+ .send()
+ .await
+ .unwrap();
+ assert_eq!(response.status(), reqwest::StatusCode::UNAUTHORIZED);
+ failure(&home.run(&["photos", "album", "list"]));
+ assert_eq!(
+ home.json(&["account", "view", "work"])["products"],
+ json!(["locker"])
+ );
+
+ for entry in fs::read_dir(home.dir.path()).unwrap() {
+ let entry = entry.unwrap();
+ let bytes = fs::read(entry.path()).unwrap();
+ assert!(
+ !bytes
+ .windows(alice_email.len())
+ .any(|b| b == alice_email.as_bytes())
+ );
+ assert!(
+ !bytes
+ .windows(PASSWORD.len())
+ .any(|b| b == PASSWORD.as_bytes())
+ );
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ assert_eq!(entry.metadata().unwrap().permissions().mode() & 0o077, 0);
+ }
+ }
+ Ok(())
+}
+
+fn login(home: &TestHome, product: &str, email: &str, options: &[&str]) {
+ let mut args = vec![product, "login", "--input", "-", "--json"];
+ args.extend_from_slice(options);
+ let output = success(home.with_input(&args, &credentials(email)));
+ let result: Value = serde_json::from_slice(&output.stdout).unwrap();
+ assert_eq!(result["product"], product);
+}
+
+fn credentials(email: &str) -> Vec<u8> {
+ serde_json::to_vec(&json!({"email": email, "password": PASSWORD, "otp": HARDCODED_OTT}))
+ .unwrap()
+}
+
+async fn create_account(origin: &str, email: &str) -> AuthenticatedAccount {
+ let client =
+ AccountsClient::new(AccountsClientConfig::new("io.ente.photos").with_origin(origin))
+ .unwrap();
+ client.send_otp(email, "signup").await.unwrap();
+ AuthFlow::new(&client, &mut NoPrompts)
+ .create_account_with_otp(
+ CreateAccountParams {
+ email: email.into(),
+ password: Zeroizing::new(PASSWORD.into()),
+ source: Some("testAccount".into()),
+ },
+ HARDCODED_OTT,
+ )
+ .await
+ .unwrap()
+}
+
+#[cfg(unix)]
+async fn session_count(origin: &str, owner: &AuthenticatedAccount) -> usize {
+ let body: Value = reqwest::Client::new()
+ .get(format!("{origin}/users/sessions"))
+ .header("x-auth-token", b64::encode_url_safe(&owner.secrets.token))
+ .header("x-client-package", "io.ente.photos")
+ .send()
+ .await
+ .unwrap()
+ .error_for_status()
+ .unwrap()
+ .json()
+ .await
+ .unwrap();
+ body["sessions"].as_array().unwrap().len()
+}
+
+async fn create_album(
+ origin: &str,
+ owner: &AuthenticatedAccount,
+ name: &str,
+ legacy: bool,
+) -> (i64, Key) {
+ let key = Key::generate();
+ let wrapped = secretbox::encrypt(
+ key.as_bytes(),
+ &Key::try_from_slice(&owner.secrets.master_key).unwrap(),
+ );
+ let encrypted_name = secretbox::encrypt(name.as_bytes(), &key);
+ let response: Value = reqwest::Client::new().post(format!("{origin}/collections"))
+ .header("x-auth-token", b64::encode_url_safe(&owner.secrets.token)).header("x-client-package", "io.ente.photos")
+ .json(&json!({
+ "encryptedKey": b64::encode(&wrapped.encrypted_data), "keyDecryptionNonce": b64::encode(wrapped.nonce.as_bytes()),
+ "name": if legacy { name } else { "" }, "encryptedName": b64::encode(&encrypted_name.encrypted_data),
+ "nameDecryptionNonce": b64::encode(encrypted_name.nonce.as_bytes()), "type": "album", "attributes": {"version": 1}
+ })).send().await.unwrap().error_for_status().unwrap().json().await.unwrap();
+ (response["collection"]["id"].as_i64().unwrap(), key)
+}
+
+async fn set_album_metadata(
+ origin: &str,
+ user: &AuthenticatedAccount,
+ album: i64,
+ key: &Key,
+ metadata_kind: &str,
+ data: Value,
+) {
+ let metadata = blob::encrypt_json(&data, key).unwrap();
+ reqwest::Client::new()
+ .put(format!("{origin}/collections/{metadata_kind}"))
+ .header("x-auth-token", b64::encode_url_safe(&user.secrets.token))
+ .header("x-client-package", "io.ente.photos")
+ .json(&json!({"id": album, "magicMetadata": {
+ "version": 1, "count": data.as_object().unwrap().len(),
+ "data": b64::encode(&metadata.encrypted_data),
+ "header": b64::encode(metadata.decryption_header.as_bytes())
+ }}))
+ .send()
+ .await
+ .unwrap()
+ .error_for_status()
+ .unwrap();
+}
+
+struct NoPrompts;
+
+impl AuthFlowUi for NoPrompts {
+ fn read_email_otp(&mut self, _: &str, _: OtpPurpose, _: bool) -> ente_accounts::Result<String> {
+ unreachable!()
+ }
+ fn read_totp_code(&mut self, _: TotpPurpose) -> ente_accounts::Result<String> {
+ unreachable!()
+ }
+ fn report_retryable_error(&mut self, _: &str) -> ente_accounts::Result<()> {
+ unreachable!()
+ }
+ fn choose_second_factor(
+ &mut self,
+ _: &[SecondFactorMethod],
+ ) -> ente_accounts::Result<SecondFactorMethod> {
+ unreachable!()
+ }
+ fn present_passkey_verification(&mut self, _: &str) -> ente_accounts::Result<()> {
+ unreachable!()
+ }
+ fn wait_for_passkey_verification(&mut self) -> ente_accounts::Result<()> {
+ unreachable!()
+ }
+ fn present_totp_secret(&mut self, _: &str, _: &str) -> ente_accounts::Result<()> {
+ unreachable!()
+ }
+}
diff --git a/rust/apps/cli/Cargo.toml b/rust/apps/cli/Cargo.toml
index 1a06708838..042dfb93b7 100644
--- a/rust/apps/cli/Cargo.toml
+++ b/rust/apps/cli/Cargo.toml
@@ -11,9 +11,9 @@ license = "AGPL-3.0-only"
museum = []
[dependencies]
-chrono = "0.4.0"
-clap = { version = "4.5.0", features = ["derive", "env"] }
-dialoguer = "0.11.0"
+chrono = { workspace = true, default-features = true }
+clap = { workspace = true, features = ["derive", "env"] }
+dialoguer = { workspace = true, default-features = true }
dirs.workspace = true
ente-accounts.workspace = true
ente-core.workspace = true
diff --git a/rust/apps/ensu/src-tauri/Cargo.toml b/rust/apps/ensu/src-tauri/Cargo.toml
index f7a2de7543..68d0da505b 100644
--- a/rust/apps/ensu/src-tauri/Cargo.toml
+++ b/rust/apps/ensu/src-tauri/Cargo.toml
@@ -11,7 +11,7 @@ ente-assets.workspace = true
ente-core.workspace = true
ente-ensu.workspace = true
ente-ensu-crypto.workspace = true
-keyring = { version = "3.0.0", default-features = false, features = ["apple-native", "windows-native", "linux-native-sync-persistent", "crypto-rust"] }
+keyring = { workspace = true, features = ["apple-native", "windows-native", "linux-native-sync-persistent", "crypto-rust"] }
log.workspace = true
notify = "6.1.1"
serde.workspace = true
@@ -29,7 +29,7 @@ libc = "0.2.0"
rustix = { version = "1.1.4", features = ["fs"] }
[target.'cfg(windows)'.dependencies]
-windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Storage_FileSystem", "Win32_System_SystemInformation"] }
+windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Storage_FileSystem", "Win32_System_SystemInformation"] }
[features]
custom-protocol = ["tauri/custom-protocol"]
diff --git a/rust/checks/dependency-boundaries/check.mjs b/rust/checks/dependency-boundaries/check.mjs
index 49f82ef582..31d3faeb93 100644
--- a/rust/checks/dependency-boundaries/check.mjs
+++ b/rust/checks/dependency-boundaries/check.mjs
@@ -27,6 +27,7 @@ const domain = metadata.packages.filter((pkg) =>
);
const httpOwners = new Set([
+ "rust/apps/cli-next/Cargo.toml",
"rust/crates/assets/Cargo.toml",
"rust/crates/core/Cargo.toml",
"rust/tools/location-dataset/Cargo.toml",
diff --git a/rust/crates/accounts/src/client.rs b/rust/crates/accounts/src/client.rs
index ff0af29dce..12d8fecc5c 100644
--- a/rust/crates/accounts/src/client.rs
+++ b/rust/crates/accounts/src/client.rs
@@ -47,8 +47,12 @@ pub struct AccountsClient {
impl AccountsClient {
pub fn new(config: AccountsClientConfig) -> Result<Self> {
+ Ok(Self::with_http(config, Http::new()?))
+ }
+
+ pub fn with_http(config: AccountsClientConfig, http: Http) -> Self {
let api = Api::new(
- Http::new()?,
+ http,
ApiConfig {
origin: config.origin,
client_package: Some(config.client_package.clone()),
@@ -57,10 +61,10 @@ impl AccountsClient {
auth: config.auth_token.map(Auth::User),
},
);
- Ok(Self {
+ Self {
api,
client_package: config.client_package,
- })
+ }
}
pub fn set_auth_token(&self, auth_token: Option<String>) {
@@ -71,6 +75,24 @@ impl AccountsClient {
&self.client_package
}
+ pub async fn email(&self) -> Result<String> {
+ #[derive(serde::Deserialize)]
+ struct Details {
+ email: String,
+ }
+
+ let details: Details = self
+ .api
+ .get("/users/details/v2")
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?;
+ Ok(details.email)
+ }
+
pub async fn get_srp_attributes(&self, email: &str) -> Result<SrpAttributes> {
let query = [("email", email.to_string())];
let response: GetSrpAttributesResponse = http::retry(|| async {
@@ -88,13 +110,12 @@ impl AccountsClient {
Ok(response.attributes)
}
- pub async fn login_with_srp(
+ pub(crate) async fn login_with_srp(
&self,
- email: &str,
password: &str,
+ srp_attrs: &SrpAttributes,
) -> Result<(AuthResponse, SecretVec)> {
- let srp_attrs = self.get_srp_attributes(email).await?;
- let creds = auth::derive_srp_credentials(password, &srp_attrs)?;
+ let creds = auth::derive_srp_credentials(password, srp_attrs)?;
let srp_salt = b64::decode(&srp_attrs.srp_salt)?;
let mut srp_session = SrpSession::new(
&srp_attrs.srp_user_id.to_string(),
diff --git a/rust/crates/accounts/src/flow.rs b/rust/crates/accounts/src/flow.rs
index ce38d02cc4..4e75fc0ad2 100644
--- a/rust/crates/accounts/src/flow.rs
+++ b/rust/crates/accounts/src/flow.rs
@@ -282,7 +282,7 @@ where
} else {
let (response, kek) = self
.client
- .login_with_srp(&params.email, &params.password)
+ .login_with_srp(&params.password, &srp_attrs)
.await?;
let response = self.resolve_second_factor(response).await?;
(response, kek)
diff --git a/rust/crates/accounts/src/lib.rs b/rust/crates/accounts/src/lib.rs
index d6c0fa9a00..1ff9a3cb30 100644
--- a/rust/crates/accounts/src/lib.rs
+++ b/rust/crates/accounts/src/lib.rs
@@ -13,4 +13,4 @@ pub use flow::{
CheckSessionValidityParams, CreateAccountParams, LoginParams, OtpPurpose, RecoveryKeyResult,
SecondFactorMethod, SessionValidity, SetupTwoFactorParams, SetupTwoFactorResult, TotpPurpose,
};
-pub use types::{AccountSecrets, AccountsClientConfig};
+pub use types::{AccountSecrets, AccountsClientConfig, DEFAULT_API_ORIGIN};
diff --git a/rust/crates/accounts/tests/accounts.rs b/rust/crates/accounts/tests/accounts.rs
index 72dd5ac4e9..8ed8099a79 100644
--- a/rust/crates/accounts/tests/accounts.rs
+++ b/rust/crates/accounts/tests/accounts.rs
@@ -108,13 +108,14 @@ async fn run(endpoint: String) -> TestResult {
};
let login = AuthFlow::new(&accounts_client(endpoint), &mut ui)
.login(LoginParams {
- email,
+ email: email.clone(),
password: Zeroizing::new(password),
})
.await
.unwrap();
assert_eq!(login.user_id, created.user_id);
assert_eq!(login.secrets.master_key, created.secrets.master_key);
+ assert_eq!(client.email().await.unwrap(), email);
assert!(client.get_two_factor_status().await.unwrap());
Ok(())
}
diff --git a/rust/crates/core/src/http.rs b/rust/crates/core/src/http.rs
index 37a6f6c81c..ba075da764 100644
--- a/rust/crates/core/src/http.rs
+++ b/rust/crates/core/src/http.rs
@@ -100,6 +100,12 @@ pub struct Http {
client: reqwest::Client,
}
+impl From<reqwest::Client> for Http {
+ fn from(client: reqwest::Client) -> Self {
+ Self { client }
+ }
+}
+
impl Http {
pub fn new() -> Result<Self, Error> {
let builder = reqwest::Client::builder();
diff --git a/rust/crates/ml/Cargo.toml b/rust/crates/ml/Cargo.toml
index f4310e2093..8eacd0cd6a 100644
--- a/rust/crates/ml/Cargo.toml
+++ b/rust/crates/ml/Cargo.toml
@@ -78,7 +78,7 @@ ort = { workspace = true, features = [
] }
[dev-dependencies]
-anyhow = "1.0.0"
+anyhow.workspace = true
flate2.workspace = true
serde.workspace = true
serde_json.workspace = true
diff --git a/rust/crates/photos/Cargo.toml b/rust/crates/photos/Cargo.toml
index d06d47fd4f..f5f658ddbb 100644
--- a/rust/crates/photos/Cargo.toml
+++ b/rust/crates/photos/Cargo.toml
@@ -4,11 +4,14 @@ edition.workspace = true
[dependencies]
ente-assets.workspace = true
+ente-core.workspace = true
ente-exif.workspace = true
ente-location.workspace = true
quick-xml.workspace = true
rusqlite = { workspace = true, default-features = true }
+serde.workspace = true
thiserror.workspace = true
+zeroize.workspace = true
[dev-dependencies]
tempfile.workspace = true
diff --git a/rust/crates/photos/src/collections.rs b/rust/crates/photos/src/collections.rs
new file mode 100644
index 0000000000..c043f2990c
--- /dev/null
+++ b/rust/crates/photos/src/collections.rs
@@ -0,0 +1,186 @@
+use ente_core::{
+ Session, b64,
+ crypto::{self, Header, Key, Nonce, PublicKey, SecretKey, blob, sealed, secretbox},
+ http,
+};
+use serde::Deserialize;
+use zeroize::Zeroizing;
+
+const DEFAULT_HIDDEN_SUBTYPE: u8 = 1;
+const ARCHIVED_VISIBILITY: u8 = 1;
+const HIDDEN_VISIBILITY: u8 = 2;
+
+#[derive(Debug)]
+pub struct Collection {
+ pub id: i64,
+ pub name: String,
+ pub kind: String,
+ pub visibility: Visibility,
+ pub owner_id: i64,
+ pub updated_at_micros: i64,
+}
+
+#[derive(Debug)]
+pub enum Visibility {
+ Visible,
+ Archived,
+ Hidden,
+}
+
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+ #[error(transparent)]
+ Http(#[from] http::Error),
+ #[error(transparent)]
+ Crypto(#[from] crypto::Error),
+ #[error(transparent)]
+ Base64(#[from] b64::DecodeError),
+ #[error("invalid collection {id}: {reason}")]
+ InvalidCollection { id: i64, reason: &'static str },
+}
+
+pub async fn list(
+ session: &Session,
+ user_id: i64,
+ public_key: &PublicKey,
+ secret_key: &SecretKey,
+) -> Result<Vec<Collection>, Error> {
+ let response: CollectionsResponse = session
+ .api
+ .get("/collections/v2")
+ .query(&[("sinceTime", 0)])
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?;
+ let master_key = Key::try_from_slice(&session.master_key)?;
+
+ response
+ .collections
+ .into_iter()
+ .filter(|c| c.is_deleted != Some(true))
+ .map(|c| {
+ let owned = c.owner.id == user_id;
+ let encrypted_key = b64::decode(&c.encrypted_key)?;
+ let key_bytes = Zeroizing::new(if owned {
+ let nonce = c.required(&c.key_decryption_nonce, "missing key nonce")?;
+ secretbox::decrypt(
+ &encrypted_key,
+ &Nonce::try_from_slice(&b64::decode(nonce)?)?,
+ &master_key,
+ )?
+ } else {
+ sealed::open(&encrypted_key, public_key, secret_key)?
+ });
+ let key = Key::try_from_slice(&key_bytes)?;
+ let name = match c.name.as_deref().filter(|name| !name.is_empty()) {
+ Some(name) => name.to_owned(),
+ None => {
+ let encrypted_name = c.required(&c.encrypted_name, "missing name")?;
+ let nonce = c.required(&c.name_decryption_nonce, "missing name nonce")?;
+ let bytes = secretbox::decrypt(
+ &b64::decode(encrypted_name)?,
+ &Nonce::try_from_slice(&b64::decode(nonce)?)?,
+ &key,
+ )?;
+ String::from_utf8(bytes).map_err(|_| Error::InvalidCollection {
+ id: c.id,
+ reason: "name is not UTF-8",
+ })?
+ }
+ };
+ let metadata = if owned {
+ c.magic_metadata.as_ref()
+ } else {
+ c.shared_magic_metadata.as_ref()
+ };
+ let visibility = match metadata {
+ Some(metadata) => {
+ let metadata = blob::decrypt_json::<Metadata>(
+ &blob::EncryptedBlob {
+ encrypted_data: b64::decode(&metadata.data)?,
+ decryption_header: Header::try_from_slice(&b64::decode(
+ &metadata.header,
+ )?)?,
+ },
+ &key,
+ )?;
+ if owned && metadata.sub_type == Some(DEFAULT_HIDDEN_SUBTYPE) {
+ HIDDEN_VISIBILITY
+ } else {
+ metadata.visibility.unwrap_or(0)
+ }
+ }
+ None => 0,
+ };
+ Ok(Collection {
+ id: c.id,
+ name,
+ kind: c.kind,
+ visibility: match visibility {
+ ARCHIVED_VISIBILITY => Visibility::Archived,
+ HIDDEN_VISIBILITY => Visibility::Hidden,
+ _ => Visibility::Visible,
+ },
+ owner_id: c.owner.id,
+ updated_at_micros: c.updation_time,
+ })
+ })
+ .collect()
+}
+
+#[derive(Deserialize)]
+struct CollectionsResponse {
+ collections: Vec<RemoteCollection>,
+}
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct RemoteCollection {
+ id: i64,
+ owner: Owner,
+ encrypted_key: String,
+ key_decryption_nonce: Option<String>,
+ encrypted_name: Option<String>,
+ name_decryption_nonce: Option<String>,
+ name: Option<String>,
+ #[serde(rename = "type")]
+ kind: String,
+ updation_time: i64,
+ is_deleted: Option<bool>,
+ magic_metadata: Option<EncryptedMetadata>,
+ shared_magic_metadata: Option<EncryptedMetadata>,
+}
+
+impl RemoteCollection {
+ fn required<'a>(
+ &self,
+ value: &'a Option<String>,
+ reason: &'static str,
+ ) -> Result<&'a str, Error> {
+ value.as_deref().ok_or(Error::InvalidCollection {
+ id: self.id,
+ reason,
+ })
+ }
+}
+
+#[derive(Deserialize)]
+struct Owner {
+ id: i64,
+}
+
+#[derive(Deserialize)]
+struct EncryptedMetadata {
+ data: String,
+ header: String,
+}
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct Metadata {
+ visibility: Option<u8>,
+ sub_type: Option<u8>,
+}
diff --git a/rust/crates/photos/src/lib.rs b/rust/crates/photos/src/lib.rs
index c06e85f79b..d093dcfe12 100644
--- a/rust/crates/photos/src/lib.rs
+++ b/rust/crates/photos/src/lib.rs
@@ -1,3 +1,4 @@
+pub mod collections;
pub mod db;
pub mod location;
pub mod metadata;