Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

31.8 KB
Raw Blame
//! Saved passwords in the person's Bitwarden vault (`sandbox::bitwarden`).
//!
//! The vault routes are the person's only: no token 401, a bot's turn token 403. The rest runs
//! against a real Bitwarden-compatible server when `KWNT_TEST_VAULTWARDEN` names one, e.g. a
//! throwaway Vaultwarden with signups open:
//!
//! ```sh
//! docker run -d --rm --name kwnt-bwtest -p 127.0.0.1:18990:80 -e SIGNUPS_ALLOWED=true \
//!   -e I_REALLY_WANT_VOLATILE_STORAGE=true vaultwarden/server
//! KWNT_TEST_VAULTWARDEN=http://127.0.0.1:18990 cargo test --test bitwarden_test
//! ```
//!
//! Without it those tests pass without doing anything (and say so).

#[path = "support/skip.rs"]
mod skip;

use axum::{
    body::{to_bytes, Body},
    http::{Request, StatusCode},
    Router,
};
use kwnt::{
    broker::PermissionBroker,
    config::Config,
    db::{models::NewTeam, spaces::SpaceId, Database},
    router::LayaRouter,
    sandbox::{bitwarden, browser::BrowserController, credentials::CredentialStore, PendingCapture},
    server::{continuity::SelfScope, create_router, mcp_runtime, AppState},
    tools::ToolRunner,
};
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use tempfile::{tempdir, TempDir};
use tower::ServiceExt;

struct Env {
    app: Router,
    state: AppState,
    data: PathBuf,
    _tmp: TempDir,
}

fn config_for(data: &Path) -> Config {
    Config { data_dir: data.to_path_buf(), browser_cdp_url: Some("http://127.0.0.1:9".into()), ..Config::default() }
}

async fn setup_at(tmp: TempDir) -> Env {
    let data = tmp.path().join("data");
    let db = Database::init(&data).await.unwrap();
    let config = config_for(&data);
    let state = AppState::new(
        config.clone(),
        db,
        ToolRunner::new(data.join("workspace")),
        PermissionBroker::new(),
        LayaRouter::new(config.laya_url.clone()),
    );
    Env { app: create_router(state.clone()), state, data, _tmp: tmp }
}

async fn setup() -> Env {
    setup_at(tempdir().unwrap()).await
}

async fn call(app: &Router, method: &str, uri: &str, bearer: Option<&str>, body: Option<Value>) -> (StatusCode, Value) {
    let mut req = Request::builder().method(method).uri(uri).header("content-type", "application/json");
    if let Some(t) = bearer {
        req = req.header("authorization", format!("Bearer {t}"));
    }
    let body = body.map(|b| Body::from(b.to_string())).unwrap_or_else(Body::empty);
    let resp = app.clone().oneshot(req.body(body).unwrap()).await.unwrap();
    let status = resp.status();
    let bytes = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
    (status, serde_json::from_slice(&bytes).unwrap_or(Value::Null))
}

/// `call` from another machine (a non-loopback Host), without a token.
async fn call_remote(app: &Router, method: &str, uri: &str, body: Option<Value>) -> (StatusCode, Value) {
    let req = Request::builder().method(method).uri(uri).header("content-type", "application/json").header("host", "kwnt.example.com");
    let body = body.map(|b| Body::from(b.to_string())).unwrap_or_else(Body::empty);
    let resp = app.clone().oneshot(req.body(body).unwrap()).await.unwrap();
    let status = resp.status();
    let bytes = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
    (status, serde_json::from_slice(&bytes).unwrap_or(Value::Null))
}

impl Env {
    async fn person(&self, method: &str, uri: &str, body: Option<Value>) -> (StatusCode, Value) {
        call(&self.app, method, uri, Some(self.state.api_token.reveal()), body).await
    }
    async fn team(&self, name: &str) -> String {
        let bot = self.state.db.list_kwants(&SpaceId::default_space()).await.unwrap().remove(0);
        let team = NewTeam {
            name: name.into(),
            description: None,
            icon: None,
            lead_kwant_id: bot.id.clone(),
            member_kwants: vec![bot.id],
            workspace_path: None,
            repo_url: None,
        };
        self.state.db.create_team(&SpaceId::default_space(), &team).await.unwrap().id
    }
    async fn turn_token(&self, folder: &Path) -> String {
        let bot = self.state.db.list_kwants(&SpaceId::default_space()).await.unwrap().remove(0);
        let scope = SelfScope { kwant_id: bot.id.clone(), kwant_name: bot.name.clone(), ..Default::default() };
        mcp_runtime::prepare_turn_with_self(&self.state, &bot, None, Some(folder.to_path_buf()), Some(scope))
            .await
            .unwrap()
            .1
    }
}

fn files_under(dir: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    for e in std::fs::read_dir(dir).into_iter().flatten().flatten() {
        let p = e.path();
        if p.is_dir() {
            out.extend(files_under(&p));
        } else {
            out.push(p);
        }
    }
    out
}

fn no_file_contains(dir: &Path, needle: &str) {
    for f in files_under(dir) {
        let bytes = std::fs::read(&f).unwrap_or_default();
        assert!(!String::from_utf8_lossy(&bytes).contains(needle), "{needle} in clear in {}", f.display());
    }
}

#[tokio::test]
async fn vault_routes_are_the_persons_only() {
    let e = setup().await;
    let folder = tempdir().unwrap();
    let turn = e.turn_token(folder.path()).await;
    let routes: Vec<(&str, &str, Option<Value>)> = vec![
        ("GET", "/api/passwords/store", None),
        ("POST", "/api/passwords/bitwarden/unlock", Some(json!({"server_url": "http://127.0.0.1:9", "email": "[email protected]", "master_password": "DUMMY"}))),
        ("POST", "/api/passwords/bitwarden/lock", None),
        ("DELETE", "/api/passwords/bitwarden", Some(json!({"master_password": "DUMMY"}))),
        ("POST", "/api/passwords/migrate", None),
    ];
    for (method, uri, body) in &routes {
        let (s, v) = call_remote(&e.app, method, uri, body.clone()).await;
        assert_eq!(s, StatusCode::UNAUTHORIZED, "{method} {uri} from another machine without a token: {v}");
        let (s, v) = call(&e.app, method, uri, Some(&turn), body.clone()).await;
        assert_eq!(s, StatusCode::FORBIDDEN, "{method} {uri} with a bot's turn token: {v}");
    }
    let (s, v) = e.person("GET", "/api/passwords/store", None).await;
    assert_eq!(s, StatusCode::OK);
    assert_eq!(v["store"], "local");
    assert_eq!(v["locked"], false);
    assert_eq!(v["bitwarden"], Value::Null);
    // Not set up: nothing to lock, forget or move into
    let (s, _) = e.person("POST", "/api/passwords/migrate", None).await;
    assert_eq!(s, StatusCode::LOCKED);
    // A plain-http server elsewhere is refused before anything is sent
    let (s, v) = e
        .person("POST", "/api/passwords/bitwarden/unlock", Some(json!({"server_url": "http://pw.example.org", "email": "[email protected]", "master_password": "DUMMY"})))
        .await;
    assert_eq!(s, StatusCode::BAD_REQUEST, "{v}");
    assert!(!e.data.join("kwnt-vault").join(bitwarden::CONFIG_FILE).exists());
}

fn server() -> Option<String> {
    skip::opt_in("KWNT_TEST_VAULTWARDEN", "the live Bitwarden test")
}

const MASTER: &str = "DUMMY-master-pw-kwnt-test";

async fn new_account(server: &str) -> String {
    let email = format!("kwnt-{}@kwnt.test", uuid::Uuid::new_v4().simple());
    // Vaultwarden's minimum; real accounts use 600000
    bitwarden::register_test_account(server, &email, MASTER, 100_000).await.expect("register a throwaway account");
    email
}

/// The person unlocks, passwords are saved, filled on the exact origin only, survive in the
/// vault, and after a restart nothing is filled until the person unlocks again. Nothing that
/// unlocks the vault, and no saved password, is ever written under kwnt's data folder.
#[tokio::test]
async fn bitwarden_end_to_end() {
    let Some(server) = server() else { return };
    let email = new_account(&server).await;
    let tmp = tempdir().unwrap();
    let tmp_path = tmp.path().to_path_buf();
    let e = setup_at(tmp).await;
    let team = e.team("bwteam").await;

    // A local password from before Bitwarden
    let (s, _) = e
        .person("POST", &format!("/api/sandbox/{team}/passwords"), Some(json!({"origin": "https://old.example.com", "username": "olduser", "password": "DUMMY-LOCAL-PW"})))
        .await;
    assert_eq!(s, StatusCode::OK);
    // and one a deleted bot left behind (audit F-11): counted, and moved too
    e.state.credentials.local().save("0b5e1e7e-dead-4bad-8bad-000000000011", "https://orphan.example.com", "ou", "DUMMY-ORPHAN-PW").await.unwrap();

    // Wrong master password
    let unlock = |pw: &str| json!({"server_url": server, "email": email, "master_password": pw});
    let (s, v) = e.person("POST", "/api/passwords/bitwarden/unlock", Some(unlock("DUMMY-wrong"))).await;
    assert_eq!(s, StatusCode::UNAUTHORIZED, "{v}");
    assert_eq!(v["wrong_password"], true);
    assert!(!e.data.join("kwnt-vault").join(bitwarden::CONFIG_FILE).exists(), "nothing set up on a failed unlock");

    let (s, v) = e.person("POST", "/api/passwords/bitwarden/unlock", Some(unlock(MASTER))).await;
    assert_eq!(s, StatusCode::OK, "{v}");
    assert_eq!(v["store"], "bitwarden");
    assert_eq!(v["locked"], false);
    assert_eq!(v["bitwarden"]["email"], email);
    assert_eq!(v["bitwarden"]["unlocked"], true);
    assert_eq!(v["bitwarden"]["login"], "password");
    assert_eq!(v["local_count"], 2);

    // Another account can't replace it without forgetting this one
    let (s, _) = e
        .person("POST", "/api/passwords/bitwarden/unlock", Some(json!({"server_url": server, "email": "[email protected]", "master_password": MASTER})))
        .await;
    assert_eq!(s, StatusCode::CONFLICT);

    // Move the local one over
    let (s, v) = e.person("POST", "/api/passwords/migrate", None).await;
    assert_eq!(s, StatusCode::OK, "{v}");
    assert_eq!(v, json!({"moved": 2, "failed": 0}));
    assert_eq!(e.state.credentials.local_count().await, 0);
    let left: Vec<_> = std::fs::read_dir(e.data.join("kwnt-vault"))
        .unwrap()
        .flatten()
        .filter(|f| f.file_name().to_string_lossy().ends_with(".bin"))
        .collect();
    assert!(left.is_empty(), "Move to Bitwarden leaves no built-in file: {left:?}");

    // Save one through the API and one the browser captured
    let uri = format!("/api/sandbox/{team}/passwords");
    let (s, v) = e.person("POST", &uri, Some(json!({"origin": "https://login.example.com/x", "username": "alice", "password": "DUMMY-BW-PW-1"}))).await;
    assert_eq!(s, StatusCode::OK, "{v}");
    let cid = v["credential"]["id"].as_str().unwrap().to_string();
    let ctrl = BrowserController::new(9);
    e.state.browser_controllers.lock().await.insert(team.clone(), ctrl.clone());
    ctrl.hold_for_confirmation(PendingCapture {
        origin: "https://shop.example.com".into(),
        username: "bob".into(),
        password: "DUMMY-BW-PW-2".into(),
        captured_at: "2026-10-20T00:00:00Z".into(),
    })
    .await;
    let (s, v) = e.person("POST", &format!("{uri}/pending"), None).await;
    assert_eq!(s, StatusCode::OK, "{v}");

    let (_, v) = e.person("GET", &uri, None).await;
    let origins: Vec<&str> = v["passwords"].as_array().unwrap().iter().map(|p| p["origin"].as_str().unwrap()).collect();
    assert_eq!(origins.len(), 3, "{v}");
    for o in ["https://old.example.com", "https://login.example.com", "https://shop.example.com"] {
        assert!(origins.contains(&o), "{o} in {v}");
    }
    assert!(!v.to_string().contains("DUMMY"), "metadata only: {v}");

    // Exact origin only
    let vault = &e.state.credentials;
    assert_eq!(vault.for_origin(&team, "https://login.example.com/other").await.unwrap().unwrap().password, "DUMMY-BW-PW-1");
    assert!(vault.for_origin(&team, "https://evil.login.example.com").await.unwrap().is_none());
    assert!(vault.for_origin(&team, "http://login.example.com").await.unwrap().is_none());
    assert!(vault.for_origin("another-owner", "https://login.example.com").await.unwrap().is_none());

    // Reveal needs the master password, not just the API token
    let reveal = format!("{uri}/{cid}/reveal");
    let (s, v) = e.person("POST", &reveal, None).await;
    assert_eq!(s, StatusCode::UNAUTHORIZED, "{v}");
    assert!(!v.to_string().contains("DUMMY-BW"));
    let (s, v) = e.person("POST", &reveal, Some(json!({"master_password": MASTER}))).await;
    assert_eq!(s, StatusCode::OK, "{v}");
    assert_eq!(v["password"], "DUMMY-BW-PW-1");

    // An update keeps one item
    let (s, _) = e.person("POST", &uri, Some(json!({"origin": "https://login.example.com", "username": "alice", "password": "DUMMY-BW-PW-1b"}))).await;
    assert_eq!(s, StatusCode::OK);
    let (_, v) = e.person("GET", &uri, None).await;
    assert_eq!(v["passwords"].as_array().unwrap().len(), 3);

    // Nothing secret on disk: no password, master password, or token
    no_file_contains(&tmp_path, "DUMMY-BW-PW");
    no_file_contains(&tmp_path, "DUMMY-LOCAL-PW");
    no_file_contains(&tmp_path, MASTER);
    let cfg = std::fs::read_to_string(e.data.join("kwnt-vault").join(bitwarden::CONFIG_FILE)).unwrap();
    let cfg: Value = serde_json::from_str(&cfg).unwrap();
    let mut keys: Vec<&str> = cfg.as_object().unwrap().keys().map(String::as_str).collect();
    keys.sort();
    assert_eq!(keys, vec!["client_id", "device_id", "email", "server_url"], "{cfg}");

    // A restart: the vault is locked, nothing is filled or listed
    let restarted = kwnt::sandbox::vault::PasswordVault::open(&e.data);
    assert!(restarted.is_locked().await);
    assert!(restarted.for_origin(&team, "https://login.example.com").await.unwrap().is_none());
    assert!(restarted.list(&team).await.is_err());

    // Lock through the API: same
    let (s, v) = e.person("POST", "/api/passwords/bitwarden/lock", None).await;
    assert_eq!(s, StatusCode::OK);
    assert_eq!(v["locked"], true);
    assert_eq!(v["bitwarden"]["unlocked"], false);
    assert!(vault.for_origin(&team, "https://login.example.com").await.unwrap().is_none());
    let (s, v) = e.person("GET", &uri, None).await;
    assert_eq!(s, StatusCode::LOCKED, "{v}");
    assert_eq!(v["locked"], true);
    // A capture made while locked waits instead of being lost
    ctrl.hold_for_confirmation(PendingCapture {
        origin: "https://later.example.com".into(),
        username: "carol".into(),
        password: "DUMMY-BW-PW-3".into(),
        captured_at: "2026-10-20T00:00:00Z".into(),
    })
    .await;
    let (s, _) = e.person("POST", &format!("{uri}/pending"), None).await;
    assert_eq!(s, StatusCode::LOCKED);
    assert!(ctrl.pending().await.is_some(), "the capture still waits");

    // Unlock again (the saved server and email are reused) and the items are back
    let (s, v) = e.person("POST", "/api/passwords/bitwarden/unlock", Some(json!({"master_password": MASTER}))).await;
    assert_eq!(s, StatusCode::OK, "{v}");
    assert_eq!(v["bitwarden"]["item_count"], 4, "the 3 saved here and the moved leftover");
    assert_eq!(vault.for_origin(&team, "https://login.example.com").await.unwrap().unwrap().password, "DUMMY-BW-PW-1b");

    // Delete moves it to Bitwarden's trash and kwnt stops using it
    let (s, v) = e.person("DELETE", &format!("{uri}/{cid}"), None).await;
    assert_eq!(s, StatusCode::OK);
    assert_eq!(v["status"], "deleted");
    assert!(vault.for_origin(&team, "https://login.example.com").await.unwrap().is_none());

    // Forget needs the master password
    let (s, _) = e.person("DELETE", "/api/passwords/bitwarden", Some(json!({"master_password": "DUMMY-wrong"}))).await;
    assert_eq!(s, StatusCode::UNAUTHORIZED);
    let (s, v) = e.person("DELETE", "/api/passwords/bitwarden", Some(json!({"master_password": MASTER}))).await;
    assert_eq!(s, StatusCode::OK, "{v}");
    assert_eq!(v["store"], "local");
    assert_eq!(v["bitwarden"], Value::Null);
    assert!(!e.data.join("kwnt-vault").join(bitwarden::CONFIG_FILE).exists());
}

/// Five wrong master passwords in a row on reveal lock the vault.
#[tokio::test]
async fn wrong_master_passwords_lock_the_vault() {
    let Some(server) = server() else { return };
    let email = new_account(&server).await;
    let e = setup().await;
    let team = e.team("bwlock").await;
    let (s, v) = e
        .person("POST", "/api/passwords/bitwarden/unlock", Some(json!({"server_url": server, "email": email, "master_password": MASTER})))
        .await;
    assert_eq!(s, StatusCode::OK, "{v}");
    let uri = format!("/api/sandbox/{team}/passwords");
    let (_, v) = e.person("POST", &uri, Some(json!({"origin": "https://a.example.com", "username": "u", "password": "DUMMY-LOCKOUT"}))).await;
    let reveal = format!("{uri}/{}/reveal", v["credential"]["id"].as_str().unwrap());
    for _ in 0..bitwarden::MAX_WRONG_PASSWORDS - 1 {
        let (s, _) = e.person("POST", &reveal, Some(json!({"master_password": "DUMMY-guess"}))).await;
        assert_eq!(s, StatusCode::UNAUTHORIZED);
    }
    // A right one resets the count
    let (s, _) = e.person("POST", &reveal, Some(json!({"master_password": MASTER}))).await;
    assert_eq!(s, StatusCode::OK);
    for _ in 0..bitwarden::MAX_WRONG_PASSWORDS - 1 {
        let (s, _) = e.person("POST", &reveal, Some(json!({"master_password": "DUMMY-guess"}))).await;
        assert_eq!(s, StatusCode::UNAUTHORIZED);
    }
    let (s, v) = e.person("POST", &reveal, Some(json!({"master_password": "DUMMY-guess"}))).await;
    assert_eq!(s, StatusCode::LOCKED, "the fifth wrong one locks: {v}");
    assert!(e.state.credentials.is_locked().await);
    let (s, _) = e.person("POST", &reveal, Some(json!({"master_password": MASTER}))).await;
    assert_eq!(s, StatusCode::LOCKED, "locked until the person unlocks again");
}

/// kwnt holds the unlocked vault key in memory, so a same-user bot must not read its memory or
/// environment: `PR_SET_DUMPABLE` 0 makes `/proc/<kwnt>/{environ,mem,…}` root's.
#[cfg(target_os = "linux")]
#[test]
fn kwnt_is_not_dumpable() {
    use std::io::BufRead;
    use std::os::unix::fs::MetadataExt;
    let mut child = std::process::Command::new(env!("CARGO_BIN_EXE_kwnt"))
        .args(["mcp-oauth-mock-server", "--port", "0"])
        .stdout(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    // Running, past main's start: it prints its address once listening
    let mut line = String::new();
    std::io::BufReader::new(child.stdout.take().unwrap()).read_line(&mut line).unwrap();
    assert!(line.contains("mock OAuth MCP server"), "{line}");
    let proc_dir = PathBuf::from(format!("/proc/{}", child.id()));
    let environ = std::fs::metadata(proc_dir.join("environ")).unwrap();
    let readable = std::fs::read(proc_dir.join("environ")).is_ok();
    child.kill().unwrap();
    child.wait().unwrap();
    assert_eq!(environ.uid(), 0, "/proc/<kwnt>/environ belongs to root");
    assert!(!readable, "kwnt's environment is unreadable to its own user");
}

// ---- 0.7.3 C1, C2, C4 (github-connect-073/spec.md): a mock Bitwarden on this machine ----

mod mock {
    use axum::{extract::State, http::StatusCode, routing::{get, post}, Form, Json, Router};
    use kwnt::sandbox::bitwarden::{self, Kdf, SymKey};
    use ring::rand::SystemRandom;
    use serde_json::{json, Value};
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};

    pub const PASSWORD: &str = "DUMMY-master-pw-073";
    pub const EMAIL: &str = "[email protected]";
    const ITERATIONS: u32 = 5000;

    #[derive(Clone)]
    struct Mock {
        prelogin: Value,
        hash: String,
        token: Value,
    }

    /// How the account's key comes back from `/connect/token`.
    pub enum KeyIn {
        TopLevel,
        /// Only `UserDecryptionOptions.MasterPasswordUnlock`; `camel` names its members as newer
        /// servers do.
        MasterPasswordUnlock { camel: bool },
    }

    /// Serves prelogin, token and sync for one account whose master-key salt is `salt`.
    /// `prelogin_salt`: what prelogin says about it (None: nothing, as older servers).
    pub async fn serve(salt: &str, prelogin_salt: Option<&str>, key_in: KeyIn) -> String {
        let state = Arc::new(account(salt, prelogin_salt, key_in));
        let app = Router::new()
            .route("/identity/accounts/prelogin", post(|State(m): State<Arc<Mock>>| async move { Json(m.prelogin.clone()) }))
            .route(
                "/identity/connect/token",
                post(|State(m): State<Arc<Mock>>, Form(f): Form<HashMap<String, String>>| async move {
                    if f.get("password") == Some(&m.hash) {
                        (StatusCode::OK, Json(m.token.clone()))
                    } else {
                        (StatusCode::BAD_REQUEST, Json(wrong_password()))
                    }
                }),
            )
            .with_state(state);
        listen(app).await
    }

    fn account(salt: &str, prelogin_salt: Option<&str>, key_in: KeyIn) -> Mock {
        let rng = SystemRandom::new();
        let kdf = Kdf::Pbkdf2 { iterations: ITERATIONS };
        let mk = bitwarden::master_key_salted(PASSWORD, salt, kdf).unwrap();
        let user_key = SymKey::stretch(&mk).encrypt(&[7u8; 64], &rng).unwrap();
        let mut prelogin = json!({"kdf": 0, "kdfIterations": ITERATIONS, "kdfMemory": null, "kdfParallelism": null});
        if let Some(s) = prelogin_salt {
            prelogin["salt"] = json!(s);
            prelogin["kdfSettings"] = json!({"kdfType": 0, "iterations": ITERATIONS});
        }
        let mut token = json!({"access_token": "DUMMY-ACCESS", "expires_in": 3600, "refresh_token": "DUMMY-REFRESH", "token_type": "Bearer"});
        match key_in {
            KeyIn::TopLevel => token["Key"] = json!(user_key),
            KeyIn::MasterPasswordUnlock { camel: false } => {
                token["UserDecryptionOptions"] = json!({"HasMasterPassword": true, "MasterPasswordUnlock": {
                    "Kdf": {"KdfType": 0, "Iterations": ITERATIONS, "Memory": null, "Parallelism": null},
                    "MasterKeyEncryptedUserKey": user_key, "Salt": salt}});
            }
            KeyIn::MasterPasswordUnlock { camel: true } => {
                token["userDecryptionOptions"] = json!({"hasMasterPassword": true, "masterPasswordUnlock": {
                    "kdf": {"kdfType": 0, "iterations": ITERATIONS},
                    "masterKeyEncryptedUserKey": user_key, "salt": salt}});
            }
        }
        Mock { prelogin, hash: bitwarden::auth_hash(&mk, PASSWORD).to_string(), token }
    }

    fn wrong_password() -> Value {
        json!({"error": "invalid_grant", "error_description": "invalid_username_or_password",
            "ErrorModel": {"Message": "Username or password is incorrect. Try again."}})
    }

    /// Adds sync and serves `app` on a free local port.
    async fn listen(app: Router) -> String {
        let app = app.route("/api/sync", get(|| async { Json(json!({"profile": {"id": "u-073"}, "folders": [], "ciphers": []})) }));
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        format!("http://{addr}")
    }

    /// The authenticator code the two-step account takes.
    pub const CODE: &str = "123456";
    /// A code the two-step mock answers with 429, as a server that is rate limiting.
    pub const CODE_429: &str = "429429";

    #[derive(Clone)]
    struct TwoStep {
        mock: Arc<Mock>,
        devices: Arc<Mutex<Vec<String>>>,
    }

    /// One account (email salt, key at the top level) with authenticator two-step login whose code
    /// is `CODE`. Each `deviceIdentifier` the token endpoint sees is pushed onto `devices`.
    pub async fn serve_two_step(devices: Arc<Mutex<Vec<String>>>) -> String {
        let salt = bitwarden::email_salt(EMAIL);
        let state = TwoStep { mock: Arc::new(account(&salt, Some(&salt), KeyIn::TopLevel)), devices };
        let app = Router::new()
            .route("/identity/accounts/prelogin", post(|State(t): State<TwoStep>| async move { Json(t.mock.prelogin.clone()) }))
            .route(
                "/identity/connect/token",
                post(|State(t): State<TwoStep>, Form(f): Form<HashMap<String, String>>| async move {
                    t.devices.lock().unwrap().push(f.get("deviceIdentifier").cloned().unwrap_or_default());
                    if f.get("password") != Some(&t.mock.hash) {
                        return (StatusCode::BAD_REQUEST, Json(wrong_password()));
                    }
                    // A refused code and a first try look the same, as Bitwarden's do: the same
                    // 400, `invalid_grant` and list of methods
                    let challenge = json!({"error": "invalid_grant", "error_description": "invalid_username_or_password",
                        "TwoFactorProviders": ["0"], "TwoFactorProviders2": {"0": null},
                        "ErrorModel": {"Message": "Two-step token is invalid. Try again."}});
                    match (f.get("twoFactorProvider").map(String::as_str), f.get("twoFactorToken").map(String::as_str)) {
                        (Some("0"), Some(CODE)) => (StatusCode::OK, Json(t.mock.token.clone())),
                        (_, Some(CODE_429)) => (StatusCode::TOO_MANY_REQUESTS, Json(json!({"message": "Slow down."}))),
                        _ => (StatusCode::BAD_REQUEST, Json(challenge)),
                    }
                }),
            )
            .with_state(state);
        listen(app).await
    }
}

fn unlock_req(server: &str, password: &str) -> bitwarden::UnlockRequest {
    // Not `..Default::default()`: the request wipes itself on drop
    let mut req = bitwarden::UnlockRequest::default();
    req.server_url = Some(server.into());
    req.email = Some(mock::EMAIL.into());
    req.master_password = password.into();
    req
}

#[tokio::test]
async fn the_servers_salt_is_used_when_it_differs_from_the_email() {
    let salt = "Salt-Not-The-Email-7f3a";
    let server = mock::serve(salt, Some(salt), mock::KeyIn::TopLevel).await;
    let dir = tempdir().unwrap();
    let store = bitwarden::BitwardenStore::open(dir.path());
    let status = store.unlock(unlock_req(&server, mock::PASSWORD)).await.expect("unlocks with prelogin's salt, as given");
    assert_eq!(status.email, mock::EMAIL);
    assert!(store.is_unlocked().await);
    // Checking the master password later (reveal, forget) uses the same salt
    assert!(store.verify_master_password(mock::PASSWORD).await.unwrap());
    assert!(!store.verify_master_password("DUMMY-not-it").await.unwrap());

    // The same account behind a server that names no salt: the email salt can't open it
    let server = mock::serve(salt, None, mock::KeyIn::TopLevel).await;
    let dir = tempdir().unwrap();
    let store = bitwarden::BitwardenStore::open(dir.path());
    let err = store.unlock(unlock_req(&server, mock::PASSWORD)).await.expect_err("refused");
    assert_eq!(err.kind(), "wrong");
    // An empty salt falls back to the email
    let server = mock::serve(&bitwarden::email_salt(mock::EMAIL), Some(""), mock::KeyIn::TopLevel).await;
    let store = bitwarden::BitwardenStore::open(dir.path());
    store.unlock(unlock_req(&server, mock::PASSWORD)).await.expect("email salt");
}

#[tokio::test]
async fn the_account_key_can_come_only_from_master_password_unlock() {
    for camel in [false, true] {
        let salt = "Another-Salt-c2";
        let server = mock::serve(salt, Some(salt), mock::KeyIn::MasterPasswordUnlock { camel }).await;
        let dir = tempdir().unwrap();
        let store = bitwarden::BitwardenStore::open(dir.path());
        store.unlock(unlock_req(&server, mock::PASSWORD)).await.unwrap_or_else(|e| panic!("camel {camel}: {e}"));
        assert!(store.is_unlocked().await);
        assert!(dir.path().join(bitwarden::CONFIG_FILE).exists(), "set up");
        assert!(store.verify_master_password(mock::PASSWORD).await.unwrap());
    }
}

#[tokio::test]
async fn a_failed_connect_sets_nothing_up() {
    let salt = bitwarden::email_salt(mock::EMAIL);
    let server = mock::serve(&salt, Some(&salt), mock::KeyIn::TopLevel).await;
    let e = setup().await;
    let config = e.data.join(kwnt::sandbox::credentials::VAULT_DIR).join(bitwarden::CONFIG_FILE);
    for (server, password, status) in [
        (server.as_str(), "DUMMY-wrong-pw", StatusCode::UNAUTHORIZED),
        ("http://127.0.0.1:9", mock::PASSWORD, StatusCode::BAD_GATEWAY),
    ] {
        let body = json!({"server_url": server, "email": mock::EMAIL, "master_password": password});
        let (s, v) = e.person("POST", "/api/passwords/bitwarden/unlock", Some(body)).await;
        assert_eq!(s, status, "{server}: {v}");
        assert!(!v.to_string().contains("DUMMY"), "{v}");
        assert!(!config.exists(), "{server}: no bitwarden.json");
        assert_eq!(e.state.credentials.store_name(), "local", "{server}");
        assert!(!e.state.credentials.bitwarden().is_configured());
    }
    no_file_contains(&e.data, "DUMMY-wrong-pw");
    // The right password then connects
    let body = json!({"server_url": server, "email": mock::EMAIL, "master_password": mock::PASSWORD});
    let (s, v) = e.person("POST", "/api/passwords/bitwarden/unlock", Some(body)).await;
    assert_eq!(s, StatusCode::OK, "{v}");
    assert_eq!(v["store"], "bitwarden", "{v}");
    assert!(config.exists());
}

// ---- PR #527: a refused two-step code is not a wrong master password, and every try before the
// account is set up comes from one device ----

fn code_req(server: &str, provider: u32, code: &str) -> bitwarden::UnlockRequest {
    let mut req = unlock_req(server, mock::PASSWORD);
    req.two_factor_provider = Some(provider);
    req.two_factor_token = Some(code.into());
    req
}

#[tokio::test]
async fn a_refused_two_step_code_is_not_a_wrong_password() {
    let devices = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let server = mock::serve_two_step(devices.clone()).await;
    let dir = tempdir().unwrap();
    let store = bitwarden::BitwardenStore::open(dir.path());
    let err = store.unlock(unlock_req(&server, mock::PASSWORD)).await.expect_err("asks for a code");
    assert!(matches!(err, bitwarden::UnlockError::TwoFactor(ref p) if *p == ["0"]), "{err:?}");
    let err = store.unlock(code_req(&server, 0, "000000")).await.expect_err("refused");
    assert!(matches!(err, bitwarden::UnlockError::TwoFactorRejected(ref p) if *p == ["0"]), "{err:?}");
    // A 429 says nothing about the code
    let err = store.unlock(code_req(&server, 0, mock::CODE_429)).await.expect_err("rate limited");
    assert_eq!(err.kind(), "server", "{err:?}");
    // A wrong password without a code is still a wrong password
    let err = store.unlock(unlock_req(&server, "DUMMY-wrong-pw")).await.expect_err("wrong");
    assert_eq!(err.kind(), "wrong");
    assert!(!dir.path().join(bitwarden::CONFIG_FILE).exists(), "nothing set up yet");
    // The code as the app shows it, with its space
    store.unlock(code_req(&server, 0, "123 456")).await.expect("the right code connects");
    assert!(store.is_unlocked().await);
    let id = store.config().expect("set up").device_id;
    assert!(uuid::Uuid::parse_str(&id).is_ok(), "{id}");
    let seen = devices.lock().unwrap().clone();
    assert_eq!(seen.len(), 5, "{seen:?}");
    assert!(seen.iter().all(|d| *d == id), "one device for every try: {seen:?}");

    // Another kwnt with the same server and account is another device: the id is not worked out
    // from them, or anyone could pass for a device the account already trusts
    let dir2 = tempdir().unwrap();
    let other = bitwarden::BitwardenStore::open(dir2.path());
    other.unlock(unlock_req(&server, mock::PASSWORD)).await.expect_err("asks for a code");
    let other_id = devices.lock().unwrap().last().cloned().unwrap();
    assert!(uuid::Uuid::parse_str(&other_id).is_ok(), "{other_id}");
    assert_ne!(other_id, id);
}

#[tokio::test]
async fn a_refused_code_is_answered_as_one() {
    let server = mock::serve_two_step(Default::default()).await;
    let e = setup().await;
    let body = json!({"server_url": server, "email": mock::EMAIL, "master_password": mock::PASSWORD,
        "two_factor_provider": 0, "two_factor_token": "000000"});
    let (s, v) = e.person("POST", "/api/passwords/bitwarden/unlock", Some(body)).await;
    assert_eq!(s, StatusCode::UNAUTHORIZED, "{v}");
    assert_eq!(v["needs"], "two_factor", "{v}");
    assert_eq!(v["code_rejected"], true, "{v}");
    assert_eq!(v["providers"], json!(["0"]), "{v}");
    assert_ne!(v["wrong_password"], true, "{v}");
    assert!(!v.to_string().contains("DUMMY"), "{v}");
}