Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

39.6 KB
Raw Blame
//! The Codync cloud (Cloudflare relay + accounts, spec §4.2, §4.3, §8.4, §9.6): its
//! `/v1/host/*` API (signed with `Codync-Sig`), the state pull that may only renew or remove
//! account devices, never add one, and account access requests with the commit-then-reveal
//! SAS. Which cloud this host uses lives in `cloud_url`, the relay socket in `relay`.

use crate::LockExt;
use crate::hub::Hub;
use crate::remote::crypto::{self, b64};
use crate::store::{Device, DeviceAccount, DeviceSource, Scope, Store, now_ms};
use anyhow::{Context, Result, anyhow, bail};
use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use tokio::sync::watch;

pub use super::cloud_url::{Address, RelayKind, TokenChange, current, set_cloud, set_cloud_url, show, url, when_on};

/// How long one successful state pull keeps an account device allowed.
pub const LEASE_MS: i64 = 15 * 60 * 1000;
const TIMEOUT: Duration = Duration::from_secs(15);
/// Grant revocations the cloud hasn't confirmed yet (kv, JSON array of grant ids).
const PENDING_REVOKES: &str = "grant_revokes";
/// kv: the saved `Approval`.
const APPROVAL: &str = "account_approval";

pub fn approval(store: &Store) -> Approval {
    if store.kv_get(APPROVAL).as_deref() == Some("auto") { Approval::Auto } else { Approval::Code }
}

/// When this host sent its recent SAS nonces (kv, JSON array of ms).
const SAS_NONCES: &str = "sas_nonces";
/// Every host nonce is one guess at the phone's code for whoever controls the cloud, revealed
/// or not: at most this many per hour keeps a 6-digit SAS out of reach of a search.
const SAS_NONCES_PER_HOUR: usize = 5;
const HOUR_MS: i64 = 60 * 60 * 1000;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Owner {
    pub user_id: String,
    #[serde(default)]
    pub email: Option<String>,
}

/// `cloudStatus` and the `cloud` event.
#[derive(Serialize, Clone, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[allow(clippy::struct_excessive_bools, reason = "independent facts the apps show, sent as JSON")]
pub struct CloudStatus {
    pub enabled: bool,
    pub url: Option<String>,
    /// The relay in use has a registration token. The token itself is never sent.
    pub token_set: bool,
    pub registered: bool,
    pub connected: bool,
    pub owner: Option<Owner>,
    pub last_error: Option<String>,
    pub approval: Approval,
    /// The user's cloud assistant may message this computer's bots (`setAssistants`, local only).
    pub assistants: bool,
    /// Whose relay: on a private one there is no account, access request or relayed screen.
    pub relay: RelayKind,
    /// In no account while it could join one (the Codync cloud on): a signed-in phone that pairs is asked about it,
    /// and phones reach it only over Wi-Fi or Tailscale until it joins. Kept by [`update_status`], so every app words
    /// it the same.
    pub can_join_account: bool,
    /// Turned on, the cloud would leave this computer in no account on a Codync cloud, so the desktop app asks to add
    /// it first. Known with the cloud off too, unlike the fields above; kept by [`update_status`].
    pub joins_account_on_enable: bool,
}

/// How account devices get in (`setApproval`, local only).
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum Approval {
    /// The user compares the 6-digit code and approves each device here.
    #[default]
    Code,
    /// Approved as soon as the SAS exchange completes, with no one comparing the code.
    Auto,
}

/// A pending account access request, with this host's own nonce (memory only).
#[derive(Clone)]
struct Request {
    id: String,
    device_key: [u8; 32],
    device_name: String,
    platform: String,
    email: Option<String>,
    commit: [u8; 32],
    host_nonce: [u8; 32],
    /// The SAS, once the device revealed a nonce that matches its commit.
    code: Option<String>,
    created_at: i64,
    expires_at: i64,
}

impl Request {
    fn json(&self) -> Value {
        json!({
            "requestId": self.id,
            "deviceKey": b64(&self.device_key),
            "deviceName": self.device_name,
            "platform": self.platform,
            "email": self.email,
            "code": self.code,
            "createdAt": self.created_at,
            "expiresAt": self.expires_at,
        })
    }
}

pub struct Cloud {
    status: Mutex<CloudStatus>,
    requests: Mutex<Vec<Request>>,
    /// One state pull at a time.
    pulling: tokio::sync::Mutex<()>,
    /// Held while the account changes and while pairing saves a phone against it (`owner::while_unchanged`).
    pub(super) account: Mutex<()>,
    /// Bumped when the cloud is turned on or off or pointed elsewhere: the relay restarts.
    pub config: watch::Sender<u64>,
    /// Bumped when this computer's account changes: phones read the relay address again.
    pub account_changed: watch::Sender<u64>,
    /// Wakes a computer without its relay socket: a pairing code or a claim needs it now.
    pub relay_wake: tokio::sync::Notify,
    /// When the relay no longer stays up for a claim just signed (`owner::wants_relay`).
    pub(super) join_pending_until: Mutex<i64>,
}

impl Default for Cloud {
    fn default() -> Self {
        Self {
            status: Mutex::default(),
            requests: Mutex::default(),
            pulling: tokio::sync::Mutex::new(()),
            account: Mutex::default(),
            config: watch::Sender::new(0),
            account_changed: watch::Sender::new(0),
            relay_wake: tokio::sync::Notify::new(),
            join_pending_until: Mutex::default(),
        }
    }
}

impl Cloud {
    pub fn status(&self) -> CloudStatus {
        self.status.locked().clone()
    }

    pub fn requests_json(&self) -> Vec<Value> {
        self.requests.locked().iter().map(Request::json).collect()
    }
}

/// Changes the status and tells this computer's apps when it actually changed.
pub fn update_status(hub: &Hub, f: impl FnOnce(&mut CloudStatus)) {
    let joins_account_on_enable = super::owner::joins_account_on_enable(&hub.store);
    let changed = {
        let mut s = hub.cloud.status.locked();
        let before = s.clone();
        f(&mut s);
        s.can_join_account = s.enabled && s.relay == RelayKind::Codync && s.owner.is_none();
        s.joins_account_on_enable = joins_account_on_enable;
        (*s != before).then(|| s.clone())
    };
    if let Some(s) = changed {
        let _ = hub.events.send(json!({"type": "cloud", "cloud": s}));
    }
}

fn emit_requests(hub: &Hub) {
    let _ = hub.events.send(json!({"type": "accessRequests", "requests": hub.cloud.requests_json()}));
}

/// An error answer from the cloud (`{"error":{"code","message"}}`).
#[derive(Debug)]
pub struct CloudError {
    pub status: u16,
    pub code: String,
    pub message: String,
}

impl std::fmt::Display for CloudError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "the Codync cloud refused ({} {}): {}", self.status, self.code, self.message)
    }
}

impl std::error::Error for CloudError {}

fn cloud_status_of(e: &anyhow::Error) -> Option<u16> {
    e.downcast_ref::<CloudError>().map(|c| c.status)
}

/// Something that has to wait for the other side first (409).
#[derive(Debug)]
pub struct Conflict(pub &'static str);

impl std::fmt::Display for Conflict {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.0)
    }
}

impl std::error::Error for Conflict {}

/// The lowercase `Host` header value for `base` (with a non-default port), as `Codync-Sig` signs it.
pub fn authority(base: &str) -> Result<String> {
    let u = reqwest::Url::parse(base).context("invalid cloud URL")?;
    let host = u.host_str().ok_or_else(|| anyhow!("cloud URL has no host"))?;
    Ok(match u.port() {
        Some(p) => format!("{host}:{p}"),
        None => host.to_owned(),
    }
    .to_ascii_lowercase())
}

const NO_ACCOUNTS: &str = "This computer uses a private relay, which has no Codync accounts or remote screen.";

/// The Codync cloud's base URL. Accounts, access requests, grants, public webhooks and the
/// screen's TURN live only there: refused while the cloud is off or a private relay is in use.
pub fn codync_url(hub: &Hub) -> Result<String> {
    match current(&hub.store) {
        None => bail!("The Codync cloud is off on this computer."),
        Some(address) if address.kind == RelayKind::Private => bail!(NO_ACCOUNTS),
        Some(address) => Ok(address.url),
    }
}

/// Refuses an account change (claim, approval) while this computer uses a private relay.
pub fn require_accounts(store: &Store) -> Result<()> {
    if current(store).is_some_and(|address| address.kind == RelayKind::Private) {
        bail!(NO_ACCOUNTS);
    }
    Ok(())
}

/// A `/v1/host/*` request signed with `Codync-Sig`.
fn signed(hub: &Hub, base: &str, method: Method, path: &str, body: Option<&Value>) -> Result<reqwest::RequestBuilder> {
    let bytes = body.map(serde_json::to_vec).transpose()?.unwrap_or_default();
    let sig = hub.identity.sign_request(method.as_str(), &authority(base)?, path, &bytes);
    let req = crate::http().request(method, format!("{base}{path}")).header("Codync-Sig", sig).timeout(TIMEOUT);
    Ok(if body.is_some() { req.header("content-type", "application/json").body(bytes) } else { req })
}

/// One signed `/v1/host/*` call.
pub(super) async fn call(hub: &Hub, base: &str, method: Method, path: &str, body: Option<&Value>) -> Result<Value> {
    answer(signed(hub, base, method, path, body)?).await
}

/// Sends `req`; a refusal becomes a `CloudError`.
async fn answer(req: reqwest::RequestBuilder) -> Result<Value> {
    let res = req.send().await.context("the Codync cloud is unreachable")?;
    let status = res.status();
    let v: Value = res.json().await.unwrap_or(Value::Null);
    if !status.is_success() {
        let e = &v["error"];
        return Err(CloudError {
            status: status.as_u16(),
            code: e["code"].as_str().unwrap_or("internal").to_owned(),
            message: e["message"].as_str().unwrap_or_default().to_owned(),
        }
        .into());
    }
    Ok(v)
}

/// `POST /v1/host/register`: makes this computer known to the cloud (no account needed).
pub async fn register(hub: &Hub, cloud: &Address) -> Result<()> {
    let body = json!({
        "boxKey": hub.identity.box_pub_b64(),
        "name": crate::service::host_name(),
        "platform": std::env::consts::OS,
        "device": tokio::task::spawn_blocking(crate::service::device).await?,
        "version": env!("CARGO_PKG_VERSION"),
    });
    let mut req = signed(hub, &cloud.url, Method::POST, "/v1/host/register", Some(&body))?;
    // A private relay requires it to enroll a new computer. It stays out of the URL `hello`
    // publishes, so phones never see it, and no other request carries it.
    if let Some(token) = &cloud.token {
        req = req.header("Codync-Relay-Token", token);
    }
    let v = answer(req).await?;
    if v["computerId"].as_str() != Some(hub.identity.computer_id().as_str()) {
        bail!("the cloud registered a different computer id");
    }
    tracing::info!(computer_id = hub.identity.computer_id(), "registered with the Codync cloud");
    update_status(hub, |s| s.registered = true);
    Ok(())
}

/// Short-lived media credentials, issued only after the host checks screen authorization.
pub async fn screen_ice(hub: &Hub, device_key: &str) -> Result<crate::screen::IceConfig> {
    let value =
        call(hub, &codync_url(hub)?, Method::POST, "/v1/host/screen-ice", Some(&json!({"deviceKey": device_key})))
            .await?;
    let config: crate::screen::IceConfig =
        serde_json::from_value(value).context("invalid screen relay configuration")?;
    if config.ice_servers.is_empty() || config.expires_at <= now_ms() + 60_000 {
        bail!("the screen relay returned expired or empty credentials");
    }
    Ok(config)
}

// MARK: state (§4.3)

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct State {
    owner: Option<Owner>,
    #[serde(default)]
    grants: Vec<Grant>,
    #[serde(default)]
    requests: Vec<StateRequest>,
    /// The account's phones that signed out: they keep their access, but get no alerts until they sign back in.
    #[serde(default)]
    signed_out_devices: Vec<SignedOut>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SignedOut {
    device_key: String,
    /// How long before the cloud answered (ms): relative, so its clock needn't match this computer's.
    signed_out_ago: i64,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Grant {
    pub grant_id: String,
    pub device_key: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct StateRequest {
    request_id: String,
    device_key: String,
    #[serde(default)]
    device_name: String,
    #[serde(default)]
    platform: String,
    #[serde(default)]
    email: Option<String>,
    commit: String,
    #[serde(default)]
    host_nonce: Option<String>,
    #[serde(default)]
    device_nonce: Option<String>,
    #[serde(default)]
    created_at: i64,
    #[serde(default)]
    expires_at: i64,
}

/// `GET /v1/host/state` and apply it: renew or remove account devices, answer access requests.
pub async fn pull(hub: &Hub, base: &str) -> Result<()> {
    let _one = hub.cloud.pulling.lock().await;
    let state: State = serde_json::from_value(call(hub, base, Method::GET, "/v1/host/state", None).await?)
        .context("reading the cloud state")?;
    apply_grants(hub, &state.grants)?;
    // Only tickets from before signing out: the phone sends a new one on signing back in, which can arrive before
    // the cloud hears of it. Counted back from after the answer, a slow one drops a ticket a moment too new (sent
    // again on the phone's next connection) rather than keep one too old.
    let now = now_ms();
    for device in &state.signed_out_devices {
        hub.store.remove_push_tickets_until(&device.device_key, now - device.signed_out_ago)?;
    }
    super::owner::remember(hub, state.owner.as_ref())?;
    let in_account = state.owner.is_some();
    // A pull that works also settles an earlier failed one.
    update_status(hub, |s| {
        s.owner = state.owner;
        s.last_error = None;
    });
    if !in_account {
        super::owner::out_of_account(hub)?;
    }
    sync_requests(hub, base, state.requests).await;
    if approval(&hub.store) == Approval::Auto {
        // The SAS exchange still runs (the device's commit must match); only the user's comparison is skipped.
        let ready: Vec<String> =
            hub.cloud.requests.locked().iter().filter(|r| r.code.is_some()).map(|r| r.id.clone()).collect();
        for id in ready {
            if let Err(e) = decide(hub, &id, true, Approval::Auto).await {
                tracing::warn!(error = format!("{e:#}"), request_id = id, "couldn't auto-approve an access request");
            }
        }
    }
    retry_revokes(hub, base).await;
    Ok(())
}

/// The cloud can only shorten access: a local account device listed with the same grant and
/// key is renewed, one missing is removed; grants this host never approved are ignored.
pub fn apply_grants(hub: &Hub, grants: &[Grant]) -> Result<()> {
    let until = now_ms() + LEASE_MS;
    let mut known = vec![false; grants.len()];
    for d in hub.store.devices()? {
        if d.source != DeviceSource::Account {
            continue;
        }
        let listed = grants.iter().position(|g| Some(&g.grant_id) == d.grant_id.as_ref() && g.device_key == d.key);
        if let Some(i) = listed {
            known[i] = true;
            hub.store.set_lease(&d.key, until)?;
        } else {
            tracing::info!(device = d.key, "account device's grant is gone from the cloud");
            hub.revoke_device(&d.key)?;
        }
    }
    for (g, _) in grants.iter().zip(known).filter(|(_, k)| !k) {
        tracing::warn!(grant_id = g.grant_id, "unknown grant from cloud ignored");
    }
    Ok(())
}

enum Next {
    /// Seen for the first time without a host nonce: send ours.
    Offer(Box<Request>),
    /// Refuse (lost our nonce in a restart, tampered with, or the commit didn't match).
    Deny(String),
}

/// Keeps the pending access requests in step with the cloud (§4.2 B steps 4 and 6).
async fn sync_requests(hub: &Hub, base: &str, list: Vec<StateRequest>) {
    let mut next = vec![];
    let mut changed = false;
    let mut throttled = false;
    {
        let mut mine = hub.cloud.requests.locked();
        let before = mine.len();
        mine.retain(|r| {
            let kept = list.iter().any(|s| s.request_id == r.id);
            if !kept && r.code.is_none() {
                // Still counted against the hourly nonce budget.
                tracing::warn!(request_id = r.id, "access request vanished without revealing its nonce");
            }
            kept
        });
        changed |= mine.len() != before;
        let mut budget = nonce_budget(&hub.store);
        for s in &list {
            let (Ok(dk), Ok(commit)) = (crypto::unb64_n::<32>(&s.device_key), crypto::unb64_n::<32>(&s.commit)) else {
                next.push(Next::Deny(s.request_id.clone()));
                continue;
            };
            let Some(i) = mine.iter().position(|r| r.id == s.request_id) else {
                // Our nonce is only in memory: after a restart this request can't be finished.
                if s.host_nonce.is_some() {
                    next.push(Next::Deny(s.request_id.clone()));
                } else if mine.iter().any(|r| r.device_key == dk && r.code.is_none())
                    || next.iter().any(|n| matches!(n, Next::Offer(r) if r.device_key == dk))
                {
                    // One unrevealed request per device at a time; this one waits its turn.
                } else if budget.len() >= SAS_NONCES_PER_HOUR {
                    throttled = true;
                } else {
                    budget.push(now_ms());
                    next.push(Next::Offer(Box::new(Request {
                        id: s.request_id.clone(),
                        device_key: dk,
                        device_name: s.device_name.clone(),
                        platform: s.platform.clone(),
                        email: s.email.clone(),
                        commit,
                        host_nonce: crypto::random(),
                        code: None,
                        created_at: s.created_at,
                        expires_at: s.expires_at,
                    })));
                }
                continue;
            };
            let r = &mut mine[i];
            let ours = b64(&r.host_nonce);
            if r.device_key != dk || r.commit != commit || s.host_nonce.as_deref() != Some(ours.as_str()) {
                tracing::warn!(request_id = r.id, "access request changed in the cloud; denying it");
                next.push(Next::Deny(r.id.clone()));
                mine.remove(i);
                changed = true;
                continue;
            }
            if r.code.is_some() {
                continue;
            }
            let Some(nd) = s.device_nonce.as_deref() else { continue };
            match crypto::unb64_n::<32>(nd) {
                Ok(nd) if crypto::sas_commit(&dk, &nd) == r.commit => {
                    r.code = Some(crypto::sas_code(&hub.identity.sign_pub(), &dk, &nd, &r.host_nonce));
                    changed = true;
                }
                _ => {
                    tracing::warn!(request_id = r.id, "device nonce doesn't match its commit; denying the request");
                    next.push(Next::Deny(r.id.clone()));
                    mine.remove(i);
                    changed = true;
                }
            }
        }
        // Saved before any nonce leaves, so a restart can't reset the budget.
        set_nonce_budget(&hub.store, &budget);
    }
    if throttled {
        tracing::warn!("accessRequests throttled: too many account access requests this hour");
        update_status(hub, |s| {
            s.last_error = Some("Too many requests to use this computer. New ones wait up to an hour.".into());
        });
    }
    for n in next {
        match n {
            Next::Offer(r) => {
                let path = format!("/v1/host/access-requests/{}/nonce", r.id);
                match call(hub, base, Method::POST, &path, Some(&json!({"nonce": b64(&r.host_nonce)}))).await {
                    Ok(_) => {
                        hub.cloud.requests.locked().push(*r);
                        changed = true;
                    }
                    // Someone else set a nonce: not one we can stand behind.
                    Err(e) if cloud_status_of(&e) == Some(409) => deny(hub, base, &r.id).await,
                    Err(e) => {
                        tracing::warn!(
                            error = format!("{e:#}"),
                            request_id = r.id,
                            "couldn't answer an access request"
                        );
                    }
                }
            }
            Next::Deny(id) => deny(hub, base, &id).await,
        }
    }
    if changed {
        emit_requests(hub);
    }
}

/// The host nonces sent in the last hour.
fn nonce_budget(store: &Store) -> Vec<i64> {
    let since = now_ms() - HOUR_MS;
    let sent: Vec<i64> = store.kv_get(SAS_NONCES).and_then(|v| serde_json::from_str(&v).ok()).unwrap_or_default();
    sent.into_iter().filter(|t| *t > since).collect()
}

fn set_nonce_budget(store: &Store, sent: &[i64]) {
    let saved = serde_json::to_string(sent).map_err(anyhow::Error::from).and_then(|v| store.kv_set(SAS_NONCES, &v));
    if let Err(e) = saved {
        tracing::warn!(error = format!("{e:#}"), "couldn't save the access request budget");
    }
}

async fn decision(hub: &Hub, base: &str, id: &str, approve: bool) -> Result<Value> {
    let path = format!("/v1/host/access-requests/{id}/decision");
    let decision = if approve { "approve" } else { "deny" };
    call(hub, base, Method::POST, &path, Some(&json!({"decision": decision}))).await
}

async fn deny(hub: &Hub, base: &str, id: &str) {
    if let Err(e) = decision(hub, base, id, false).await {
        tracing::warn!(error = format!("{e:#}"), request_id = id, "couldn't deny an access request");
    }
}

/// `decideAccessRequest`: approving adds the device the user saw (its key, the SAS they
/// compared) with the grant the cloud returns; nothing else can add an account device.
/// `how`: the user compared the code, or the computer approves on its own (`Approval::Auto`).
pub async fn decide(hub: &Hub, id: &str, approve: bool, how: Approval) -> Result<()> {
    let base = codync_url(hub)?;
    let r =
        hub.cloud.requests.locked().iter().find(|r| r.id == id).cloned().ok_or_else(|| anyhow!("unknown request"))?;
    if approve && r.code.is_none() {
        return Err(Conflict("The device hasn't shown its code yet.").into());
    }
    let answer = decision(hub, &base, id, approve).await;
    hub.cloud.requests.locked().retain(|x| x.id != id);
    emit_requests(hub);
    let answer = answer?;
    if !approve {
        return Ok(());
    }
    let grant = answer["grantId"]
        .as_str()
        .filter(|g| g.starts_with("grt_"))
        .ok_or_else(|| anyhow!("the cloud gave no grant"))?;
    let device = Device {
        key: b64(&r.device_key),
        name: r.device_name.chars().take(100).collect(),
        platform: r.platform,
        source: DeviceSource::Account,
        grant_id: Some(grant.to_owned()),
        scopes: vec![Scope::Control, Scope::Screen],
        lease_until: Some(now_ms() + LEASE_MS),
        created_at: now_ms(),
        last_seen_at: None,
        // Approved through the account the computer is in.
        account: super::owner::owner(&hub.store).map_or(DeviceAccount::Unrecorded, |o| DeviceAccount::Of(o.user_id)),
    };
    hub.store.put_device(&device)?;
    tracing::info!(grant_id = grant, "account device approved");
    crate::analytics::device_paired(
        hub,
        crate::analytics::Via::Account,
        &device.platform,
        how == Approval::Auto,
        crate::analytics::PairedAccount::Joined,
    );
    hub.auth_changed();
    Ok(())
}

// MARK: grants removed here

fn pending_revokes(store: &Store) -> Vec<String> {
    store.kv_get(PENDING_REVOKES).and_then(|v| serde_json::from_str(&v).ok()).unwrap_or_default()
}

fn set_pending_revokes(store: &Store, ids: &[String]) {
    let saved = serde_json::to_string(ids).map_err(anyhow::Error::from).and_then(|v| store.kv_set(PENDING_REVOKES, &v));
    if let Err(e) = saved {
        tracing::warn!(error = format!("{e:#}"), "couldn't remember a grant to revoke");
    }
}

/// Tells the cloud an account device was removed here. Local removal already took effect;
/// a failure is retried on later state pulls.
pub async fn revoke_grant(hub: Arc<Hub>, grant_id: String) {
    let mut pending = pending_revokes(&hub.store);
    if !pending.contains(&grant_id) {
        pending.push(grant_id);
        set_pending_revokes(&hub.store, &pending);
    }
    // On a private relay it waits for the next pull from the Codync cloud.
    if let Ok(base) = codync_url(&hub) {
        retry_revokes(&hub, &base).await;
    }
}

async fn retry_revokes(hub: &Hub, base: &str) {
    let pending = pending_revokes(&hub.store);
    if pending.is_empty() {
        return;
    }
    let mut left = vec![];
    for id in pending {
        match call(hub, base, Method::POST, &format!("/v1/host/grants/{id}/revoke"), Some(&json!({}))).await {
            Ok(_) => {}
            Err(e) if cloud_status_of(&e) == Some(404) => {}
            Err(e) => {
                tracing::warn!(error = format!("{e:#}"), grant_id = id, "couldn't revoke a grant in the cloud");
                left.push(id);
            }
        }
    }
    set_pending_revokes(&hub.store, &left);
}

// MARK: loopback methods

/// `unclaim`: takes this computer out of its account.
pub async fn unclaim(hub: &Hub) -> Result<()> {
    let base = codync_url(hub)?;
    call(hub, &base, Method::POST, "/v1/host/unclaim", Some(&json!({}))).await?;
    tracing::info!("left the account");
    pull(hub, &base).await
}

/// `setApproval`: whether account devices need the 6-digit check. Local only.
pub fn set_approval(hub: &Hub, approval: Approval) -> Result<CloudStatus> {
    require_accounts(&hub.store)?;
    hub.store.kv_set(APPROVAL, if approval == Approval::Auto { "auto" } else { "code" })?;
    tracing::info!(?approval, "account approval changed");
    update_status(hub, |s| s.approval = approval);
    Ok(hub.cloud.status())
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used)]
    use super::*;
    use crate::chat::team::assistant;
    use crate::remote::identity::Identity;

    fn temp_hub() -> Arc<Hub> {
        let dir = std::env::temp_dir().join(format!("codync-cloud-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let store = Store::open(&dir.join("t.db")).unwrap();
        Hub::new(store, "host".into(), Identity::load_or_create(&dir).unwrap(), "token".into(), 0)
    }

    fn account_device(key: &str, grant: &str) -> Device {
        Device {
            key: key.into(),
            name: "Phone".into(),
            platform: "ios".into(),
            source: DeviceSource::Account,
            grant_id: Some(grant.into()),
            scopes: vec![Scope::Control, Scope::Screen],
            lease_until: Some(now_ms() + 1000),
            created_at: now_ms(),
            last_seen_at: None,
            account: DeviceAccount::Unrecorded,
        }
    }

    #[test]
    fn state_only_renews_or_removes() {
        let hub = temp_hub();
        hub.store.put_device(&account_device("kept", "grt_a")).unwrap();
        hub.store.put_device(&account_device("gone", "grt_b")).unwrap();
        hub.store.put_device(&account_device("swapped", "grt_c")).unwrap();
        let mut local = account_device("qr", "x");
        (local.source, local.grant_id, local.lease_until) = (DeviceSource::Local, None, None);
        hub.store.put_device(&local).unwrap();
        let grant = |g: &str, k: &str| Grant { grant_id: g.into(), device_key: k.into() };
        apply_grants(&hub, &[grant("grt_a", "kept"), grant("grt_c", "another key"), grant("grt_injected", "attacker")])
            .unwrap();
        let keys: Vec<String> = hub.store.devices().unwrap().into_iter().map(|d| d.key).collect();
        assert_eq!(keys, ["kept", "qr"], "unknown grants add nothing; a grant naming another key doesn't count");
        assert!(hub.store.device("kept").unwrap().lease_until.unwrap() > now_ms() + LEASE_MS - 5000, "renewed");
        assert_eq!(hub.store.device("qr").unwrap().lease_until, None, "QR devices aren't touched");
    }

    #[test]
    fn signed_authority_omits_the_default_port() {
        assert_eq!(authority("https://Codync.Example.dev").unwrap(), "codync.example.dev");
        assert_eq!(authority("http://127.0.0.1:8787").unwrap(), "127.0.0.1:8787");
        assert_eq!(authority("https://x.dev:443").unwrap(), "x.dev", "default port is left out");
    }

    #[derive(Default)]
    struct FakeCloud {
        state: Mutex<Value>,
        posts: Mutex<Vec<(String, Value)>>,
    }

    async fn fake_cloud(fake: Arc<FakeCloud>) -> String {
        use axum::extract::{OriginalUri, State as Ax};
        use axum::routing::{get, post};
        let app = axum::Router::new()
            .route("/v1/host/state", get(|Ax(f): Ax<Arc<FakeCloud>>| async move { axum::Json(f.state.lock().unwrap().clone()) }))
            .route(
                "/v1/host/access-requests/{id}/{action}",
                post(|Ax(f): Ax<Arc<FakeCloud>>, OriginalUri(uri): OriginalUri, axum::Json(b): axum::Json<Value>| async move {
                    let path = uri.path().to_owned();
                    f.posts.lock().unwrap().push((path.clone(), b.clone()));
                    axum::Json(if path.ends_with("/decision") && b["decision"] == "approve" {
                        json!({"status": "approved", "grantId": "grt_new"})
                    } else {
                        json!({})
                    })
                }),
            )
            .with_state(fake);
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let base = format!("http://{}", listener.local_addr().unwrap());
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        base
    }

    #[tokio::test]
    async fn access_request_needs_a_matching_reveal_before_approval() {
        let hub = temp_hub();
        let fake = Arc::new(FakeCloud::default());
        let base = fake_cloud(fake.clone()).await;
        hub.store.kv_set("test_cloud_url", &base).unwrap();
        let (dk, other_dk): ([u8; 32], [u8; 32]) = (crypto::random(), crypto::random());
        let (nd, wrong): ([u8; 32], [u8; 32]) = (crypto::random(), crypto::random());
        let request = |id: &str, host_nonce: Option<String>, device_nonce: Option<[u8; 32]>| {
            let dk = if id == "req_bad" { other_dk } else { dk };
            json!({"requestId": id, "deviceKey": b64(&dk), "deviceName": "Kevin's iPhone", "platform": "ios",
                "email": "[email protected]", "commit": b64(&crypto::sas_commit(&dk, &nd)),
                "hostNonce": host_nonce, "deviceNonce": device_nonce.map(|n| b64(&n)),
                "createdAt": 1, "expiresAt": 2})
        };
        *fake.state.lock().unwrap() = json!({"owner": {"userId": "user_1", "email": "[email protected]"}, "grants": [],
                "requests": [request("req_ok", None, None), request("req_bad", None, None)]});
        pull(&hub, &base).await.unwrap();
        let nonces: Vec<(String, Value)> = fake.posts.lock().unwrap().clone();
        assert_eq!(nonces.len(), 2, "the host sends one nonce per request");
        let nh =
            |id: &str| nonces.iter().find(|(p, _)| p.contains(id)).unwrap().1["nonce"].as_str().unwrap().to_owned();
        assert_eq!(hub.cloud.requests_json()[0]["code"], Value::Null, "no code before the reveal");
        let early = decide(&hub, "req_ok", true, Approval::Code).await.unwrap_err();
        assert!(early.is::<Conflict>(), "approve before the reveal is refused");
        assert_eq!(hub.cloud.status().owner.unwrap().user_id, "user_1");

        *fake.state.lock().unwrap() = json!({"owner": null, "grants": [], "requests": [
            request("req_ok", Some(nh("req_ok")), Some(nd)),
            request("req_bad", Some(nh("req_bad")), Some(wrong)),
        ]});
        pull(&hub, &base).await.unwrap();
        let denied =
            fake.posts.lock().unwrap().iter().any(|(p, b)| p.ends_with("req_bad/decision") && b["decision"] == "deny");
        assert!(denied, "a reveal that doesn't match its commit is denied");
        let list = hub.cloud.requests_json();
        assert_eq!(list.len(), 1);
        let nh_ok: [u8; 32] = crypto::unb64_n(&nh("req_ok")).unwrap();
        assert_eq!(list[0]["code"], crypto::sas_code(&hub.identity.sign_pub(), &dk, &nd, &nh_ok));

        decide(&hub, "req_ok", true, Approval::Code).await.unwrap();
        let d = hub.store.device(&b64(&dk)).unwrap();
        assert_eq!((d.source, d.grant_id.as_deref()), (DeviceSource::Account, Some("grt_new")));
        assert_eq!(hub.cloud.requests_json().len(), 0);
    }

    #[tokio::test]
    async fn auto_approval_still_needs_a_matching_reveal() {
        let hub = temp_hub();
        let fake = Arc::new(FakeCloud::default());
        let base = fake_cloud(fake.clone()).await;
        hub.store.kv_set("test_cloud_url", &base).unwrap();
        set_approval(&hub, Approval::Auto).unwrap();
        let (dk, nd): ([u8; 32], [u8; 32]) = (crypto::random(), crypto::random());
        let request = |host_nonce: Option<String>, device_nonce: Option<[u8; 32]>| {
            json!({"owner": {"userId": "user_1"}, "grants": [], "requests": [{"requestId": "req_1", "deviceKey": b64(&dk),
                "commit": b64(&crypto::sas_commit(&dk, &nd)), "hostNonce": host_nonce,
                "deviceNonce": device_nonce.map(|n| b64(&n))}]})
        };
        *fake.state.lock().unwrap() = request(None, None);
        pull(&hub, &base).await.unwrap();
        assert!(hub.store.device(&b64(&dk)).is_none(), "nothing is approved before the reveal");
        let nh = fake.posts.lock().unwrap()[0].1["nonce"].as_str().unwrap().to_owned();
        *fake.state.lock().unwrap() = request(Some(nh), Some(nd));
        pull(&hub, &base).await.unwrap();
        assert_eq!(hub.store.device(&b64(&dk)).unwrap().source, DeviceSource::Account, "approved without the user");
    }

    #[tokio::test]
    async fn a_pull_that_works_clears_an_earlier_error() {
        let hub = temp_hub();
        let fake = Arc::new(FakeCloud::default());
        let base = fake_cloud(fake.clone()).await;
        *fake.state.lock().unwrap() = json!({"owner": null, "grants": [], "requests": []});
        update_status(&hub, |s| s.last_error = Some("couldn't get the cloud state".into()));
        pull(&hub, &base).await.unwrap();
        assert_eq!(hub.cloud.status().last_error, None, "Settings stops showing a relay error that's gone");
    }

    #[tokio::test]
    async fn a_phone_that_signed_out_gets_no_more_alerts() {
        let hub = temp_hub();
        let fake = Arc::new(FakeCloud::default());
        let base = fake_cloud(fake.clone()).await;
        hub.store.add_push_ticket("t1", "signed out", None, Some("ctx"), "iPhone").unwrap();
        hub.store.add_push_ticket("t2", "still in", None, Some("ctx"), "Pixel").unwrap();
        let state = |signed_out_ago: i64| {
            json!({"owner": {"userId": "user_1"}, "grants": [], "requests": [],
                   "signedOutDevices": [{"deviceKey": "signed out", "signedOutAgo": signed_out_ago}]})
        };
        *fake.state.lock().unwrap() = state(0);
        pull(&hub, &base).await.unwrap();
        let tickets = || hub.store.push_tickets().into_iter().map(|t| t.ticket).collect::<Vec<_>>();
        assert_eq!(tickets(), ["t2"]);
        // Signed out a minute ago; signing back in, the new ticket can reach this computer before the cloud clears
        // the mark.
        hub.store.add_push_ticket("t3", "signed out", None, Some("ctx"), "iPhone").unwrap();
        *fake.state.lock().unwrap() = state(60_000);
        pull(&hub, &base).await.unwrap();
        assert_eq!(tickets(), ["t2", "t3"], "a ticket sent after signing out keeps its alerts");
    }

    #[tokio::test]
    async fn leaving_the_account_turns_cloud_assistants_off_and_the_check_on() {
        let hub = temp_hub();
        let fake = Arc::new(FakeCloud::default());
        let base = fake_cloud(fake.clone()).await;
        *fake.state.lock().unwrap() = json!({"owner": {"userId": "user_1"}, "grants": [], "requests": []});
        pull(&hub, &base).await.unwrap();
        assistant::set_enabled(&hub, true).unwrap();
        set_approval(&hub, Approval::Auto).unwrap();
        *fake.state.lock().unwrap() = json!({"owner": null, "grants": [], "requests": []});
        pull(&hub, &base).await.unwrap();
        assert!(!assistant::enabled(&hub.store) && !hub.cloud.status().assistants, "the next account is asked again");
        assert_eq!(hub.cloud.status().approval, Approval::Code, "the next account's devices show a code");
        assert_eq!(approval(&hub.store), Approval::Code);
    }

    #[tokio::test]
    async fn host_nonces_are_rationed() {
        let hub = temp_hub();
        let fake = Arc::new(FakeCloud::default());
        let base = fake_cloud(fake.clone()).await;
        let request =
            |id: String, dk: [u8; 32]| json!({"requestId": id, "deviceKey": b64(&dk), "commit": b64(&[0u8; 32])});
        let one_key: [u8; 32] = crypto::random();
        let mut requests: Vec<Value> = (0..2).map(|i| request(format!("same_{i}"), one_key)).collect();
        requests.extend((0..8).map(|i| request(format!("req_{i}"), crypto::random())));
        *fake.state.lock().unwrap() = json!({"owner": null, "grants": [], "requests": requests});
        pull(&hub, &base).await.unwrap();
        let sent = fake.posts.lock().unwrap().len();
        assert_eq!(sent, SAS_NONCES_PER_HOUR, "a compromised cloud gets a few guesses an hour, not a million");
        assert!(!fake.posts.lock().unwrap().iter().any(|(p, _)| p.contains("same_1")), "one open request per key");

        // Requests that never reveal still used up the budget, across a restart too.
        *fake.state.lock().unwrap() =
            json!({"owner": null, "grants": [], "requests": [request("late".into(), crypto::random())]});
        hub.cloud.requests.locked().clear();
        pull(&hub, &base).await.unwrap();
        assert_eq!(fake.posts.lock().unwrap().len(), SAS_NONCES_PER_HOUR);
        assert!(hub.cloud.status().last_error.is_some(), "the Mac hears about it, though the pull worked");
    }

    #[tokio::test]
    async fn a_private_relay_takes_no_account_change() {
        let hub = temp_hub();
        hub.store.kv_set("test_cloud_url", "http://127.0.0.1:9").unwrap();
        hub.store.kv_set("test_cloud_token", "s3cret-token-value").unwrap();
        assert!(set_approval(&hub, Approval::Auto).unwrap_err().to_string().contains("private relay"));
        assert_eq!(approval(&hub.store), Approval::Code);
        assert!(unclaim(&hub).await.unwrap_err().to_string().contains("private relay"));
        let screen = screen_ice(&hub, "dk").await.err().expect("no TURN on a private relay");
        assert!(screen.to_string().contains("private relay"), "{screen}");
        let claim = json!({"claimId": "clm_1", "nonce": "n", "userId": "user_1"});
        let local = crate::api::devices::Caller::Local;
        let refused = crate::api::dispatch(&hub, &local, "claimSign", claim.clone()).await.unwrap_err();
        assert!(refused.to_string().contains("private relay"), "{refused}");
        hub.store.kv_delete("test_cloud_token").unwrap();
        assert!(crate::api::dispatch(&hub, &local, "claimSign", claim).await.is_ok(), "a Codync cloud signs claims");
        assert!(require_accounts(&hub.store).is_ok(), "a Codync cloud has accounts");
    }
}