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.

63.2 KB
Raw Blame
//! Who may call the API. Browsers may only call it from kwnt's own page (Origin allowlist), and
//! requests that don't come from this machine need the API token as a bearer header, or a session
//! in the `kwnt_token` cookie, which the UI gets by posting the token or a one-time sign-in link
//! code to `/api/auth/session` (see `signin`).
//!
//! "From this machine" means a loopback peer, a loopback `Host` and no proxy forwarding headers.
//! The `Host` check is what keeps a DNS-rebinding page (same peer, foreign host) out.

use super::signin::{self, Poll, Redeem, Via};
use super::AppState;
use crate::config::Config;
use axum::{
    extract::{ConnectInfo, Request, State},
    http::{header, HeaderMap, HeaderValue, Method, StatusCode},
    middleware::Next,
    response::{IntoResponse, Response},
    Json,
};
use serde::Deserialize;
use serde_json::json;
use std::net::{IpAddr, SocketAddr};
use std::path::{Path, PathBuf};

pub const COOKIE: &str = "kwnt_token";
const TOKEN_FILE: &str = "api-token";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Require {
    /// Requests from other machines need the token (default).
    Auto,
    /// Every API request needs the token, local ones too.
    Always,
    /// No token at all (only for setups behind their own auth).
    Never,
}

pub struct ApiToken {
    token: String,
    pub require: Require,
    /// Extra browser origins allowed besides kwnt's own (`KWNT_ALLOWED_ORIGINS`).
    extra_origins: Vec<String>,
    port: u16,
    /// Sessions and one-time sign-in links.
    pub signin: signin::SignIn,
    /// `KWNT_SIGNIN_HINT`: where this install's token is, as an app store or host words it.
    signin_hint: Option<String>,
}

impl std::fmt::Debug for ApiToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ApiToken")
            .field("require", &self.require)
            .finish_non_exhaustive()
    }
}

impl ApiToken {
    /// `KWNT_API_TOKEN`, else `<data_dir>/api-token` (created with a random token, mode 0600).
    pub fn load(config: &Config) -> Self {
        let require = match std::env::var("KWNT_REQUIRE_TOKEN")
            .unwrap_or_default()
            .to_lowercase()
            .as_str()
        {
            "always" | "1" | "true" => Require::Always,
            "never" | "0" | "false" => Require::Never,
            _ => Require::Auto,
        };
        let extra_origins = std::env::var("KWNT_ALLOWED_ORIGINS")
            .unwrap_or_default()
            .split(',')
            .map(|o| o.trim().trim_end_matches('/').to_string())
            .filter(|o| !o.is_empty())
            .collect();
        let token = env_token().unwrap_or_else(|| load_or_create(&config.data_dir));
        let raw_hint = std::env::var("KWNT_SIGNIN_HINT").ok();
        let signin_hint = signin_hint_from(raw_hint.as_deref(), &token);
        if raw_hint.is_some_and(|h| !h.trim().is_empty()) && signin_hint.is_none() {
            tracing::warn!("KWNT_SIGNIN_HINT contains the API token, so the sign-in screen leaves it out");
        }
        Self {
            token,
            require,
            extra_origins,
            port: config.port,
            signin: signin::SignIn::load(&config.data_dir),
            signin_hint,
        }
    }

    pub fn for_tests(token: &str, require: Require, port: u16) -> Self {
        Self {
            token: token.into(),
            require,
            extra_origins: Vec::new(),
            port,
            signin: signin::SignIn::in_memory(),
            signin_hint: None,
        }
    }

    /// A test server whose sign-in screen shows `hint` (as `KWNT_SIGNIN_HINT` would).
    pub fn with_signin_hint(mut self, hint: &str) -> Self {
        self.signin_hint = signin_hint_from(Some(hint), &self.token);
        self
    }

    /// The store's or host's words for where the token is, for the sign-in screen. Public: it is
    /// shown before anyone signs in.
    pub fn signin_hint(&self) -> Option<&str> {
        self.signin_hint.as_deref()
    }

    /// kwnt's public address: the first `KWNT_ALLOWED_ORIGINS` entry (the HTTPS address in front
    /// of it), for sign-in links that open as they are.
    pub fn public_base(&self) -> Option<&str> {
        self.extra_origins.first().map(String::as_str)
    }

    /// The token, for `kwnt token` and the startup URL only.
    pub fn reveal(&self) -> &str {
        &self.token
    }

    pub fn matches(&self, candidate: &str) -> bool {
        constant_time_eq(self.token.as_bytes(), candidate.as_bytes())
    }

    fn origin_allowed(&self, origin: &str, host: Option<&str>) -> bool {
        let origin = origin.trim_end_matches('/');
        let own = [self.port, 5199]
            .iter()
            .flat_map(|p| {
                [
                    format!("http://127.0.0.1:{p}"),
                    format!("http://localhost:{p}"),
                    format!("http://[::1]:{p}"),
                ]
            })
            .any(|o| o == origin);
        let same_host = host
            .is_some_and(|h| origin == format!("http://{h}") || origin == format!("https://{h}"));
        own || same_host || self.extra_origins.iter().any(|o| o == origin)
    }
}

/// `KWNT_API_TOKEN` when it is set to a usable token (16+ characters).
pub fn env_token() -> Option<String> {
    std::env::var("KWNT_API_TOKEN")
        .ok()
        .map(|t| t.trim().to_string())
        .filter(|t| t.len() >= 16)
}

/// Longest `KWNT_SIGNIN_HINT` the sign-in screen shows, in characters.
pub const SIGNIN_HINT_MAX: usize = 300;

/// `KWNT_SIGNIN_HINT` as the sign-in screen shows it: one line of plain text (line breaks and other
/// control characters become spaces, runs of spaces one), at most [`SIGNIN_HINT_MAX`] characters.
/// None when it is empty, or when it holds the API token itself: the hint is public, so a store
/// that pasted the token into it would show the token to anyone who opens the page.
pub fn signin_hint_from(raw: Option<&str>, token: &str) -> Option<String> {
    let raw = raw?;
    if !token.is_empty() && raw.contains(token) {
        return None;
    }
    let spaced: String = raw.chars().map(|c| if c.is_control() { ' ' } else { c }).collect();
    let line = spaced.split_whitespace().collect::<Vec<_>>().join(" ");
    let hint: String = line.chars().take(SIGNIN_HINT_MAX).collect();
    (!hint.is_empty()).then_some(hint)
}

/// Why the token file could not be read. `kwnt token` reports these instead of writing a new
/// token: one made by another user or for another data dir is a token the server rejects.
#[derive(Debug)]
pub enum ReadTokenError {
    Missing(PathBuf),
    Unreadable(PathBuf, std::io::Error),
    Invalid(PathBuf),
}

impl std::fmt::Display for ReadTokenError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Missing(p) => write!(f, "no token file at {}", p.display()),
            Self::Unreadable(p, e) => write!(f, "can't read {}: {e}", p.display()),
            Self::Invalid(p) => write!(f, "{} doesn't hold a valid token", p.display()),
        }
    }
}

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

/// Reads `<data_dir>/api-token` without ever creating or changing it.
pub fn read_existing(data_dir: &Path) -> Result<String, ReadTokenError> {
    let path = data_dir.join(TOKEN_FILE);
    match std::fs::read_to_string(&path) {
        Ok(t) if t.trim().len() >= 16 => Ok(t.trim().to_string()),
        Ok(_) => Err(ReadTokenError::Invalid(path)),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Err(ReadTokenError::Missing(path)),
        Err(e) => Err(ReadTokenError::Unreadable(path, e)),
    }
}

/// Reads the token file, or writes a new random one. Falls back to an in-memory token.
/// Only the server calls this; `kwnt token` uses [`read_existing`].
pub fn load_or_create(data_dir: &Path) -> String {
    if let Ok(t) = read_existing(data_dir) {
        return t;
    }
    let path = data_dir.join(TOKEN_FILE);
    let token = format!(
        "{}{}",
        uuid::Uuid::new_v4().simple(),
        uuid::Uuid::new_v4().simple()
    );
    let _ = std::fs::create_dir_all(data_dir);
    if let Err(e) = write_private(&path, &token) {
        tracing::warn!(error = %e, "could not save the API token; it changes on restart");
    }
    token
}

/// Whether `kwnt serve --host <host>` listens on this machine only.
pub fn is_loopback_bind(host: &str) -> bool {
    host.eq_ignore_ascii_case("localhost")
        || host
            .trim_matches(['[', ']'])
            .parse::<IpAddr>()
            .is_ok_and(|ip| ip.is_loopback())
}

/// The link that signs a browser in: `http://<host>:<port>/#kwnt_token=<token or code>`. The UI
/// posts what follows `#kwnt_token=` to `/api/auth/session` and clears it from the address bar;
/// a fragment never reaches access logs or a `Referer`.
pub fn sign_in_link(host: &str, port: u16, token: &str) -> String {
    let bare = host.trim_matches(['[', ']']);
    let host = match bare.parse::<IpAddr>() {
        Ok(IpAddr::V6(_)) => format!("[{bare}]"),
        _ => bare.to_string(),
    };
    format!("http://{host}:{port}/#kwnt_token={token}")
}

/// A one-time link: on `base` (kwnt's public address) when known, else on the bound host
/// (`<this-host>` when kwnt listens on every address).
pub fn one_time_link(host: &str, port: u16, base: Option<&str>, code: &str) -> String {
    match base {
        Some(b) => format!("{}/#kwnt_token={code}", b.trim_end_matches('/')),
        None => token_command_link(host, port, code),
    }
}

/// Whether `kwnt serve` should print a sign-in link: not when nobody needs one (loopback, unless
/// `KWNT_REQUIRE_TOKEN=always`, or `never`).
pub fn startup_link_needed(host: &str, require: Require) -> bool {
    match require {
        Require::Never => false,
        Require::Auto => !is_loopback_bind(host),
        Require::Always => true,
    }
}

/// What `kwnt serve` and `kwnt signin-link` say about a one-time link, for stdout only (never
/// through tracing into log files): the link, a QR code for a phone when the address is real, and
/// how to get another.
pub fn one_time_notice(link: &str, qr: bool) -> Vec<String> {
    let minutes = signin::LINK_TTL.as_secs() / 60;
    let mut lines = vec![format!(
        "🔒 To sign in from another device, open this link. It works once, for {minutes} minutes (keep it private): {link}"
    )];
    if link.contains(THIS_HOST) {
        lines.push(format!("   Replace {THIS_HOST} with this server's address. Set KWNT_ALLOWED_ORIGINS to your HTTPS address for a ready link and a QR code."));
    } else if qr {
        let code = signin::qr_lines(link);
        if !code.is_empty() {
            lines.push("   Or scan it with your phone:".into());
            lines.extend(code);
        }
    }
    lines.push("   Used or expired? `kwnt signin-link` makes a new one (Docker: docker compose exec kwnt kwnt signin-link).".into());
    lines
}

/// The link and its QR code alone, indented, for install.sh (`kwnt signin-link --bare`), which says
/// the rest in its own words (Mira's S1 lines): no "works once" or "Used or expired?" line here.
pub fn bare_notice(link: &str, qr: bool) -> Vec<String> {
    let mut lines = vec![format!("  {link}")];
    if link.contains(THIS_HOST) {
        lines.push(format!("  Replace {THIS_HOST} with this server's address. Set KWNT_ALLOWED_ORIGINS to your HTTPS address for a ready link and a QR code."));
    } else if qr {
        let code = signin::qr_lines(link);
        if !code.is_empty() {
            lines.push(String::new());
            lines.extend(code.into_iter().map(|l| format!("  {l}")));
        }
    }
    lines
}

/// Stands in for the server's address when kwnt listens on every address.
pub const THIS_HOST: &str = "<this-host>";

/// `0.0.0.0` or `::`: every address, so the bound host says nothing about how to reach it.
fn is_any_bind(host: &str) -> bool {
    host.is_empty()
        || host
            .trim_matches(['[', ']'])
            .parse::<IpAddr>()
            .is_ok_and(|ip| ip.is_unspecified())
}

/// A sign-in link on the configured bind (`<this-host>` when kwnt listens on every address).
pub fn token_command_link(host: &str, port: u16, token: &str) -> String {
    sign_in_link(if is_any_bind(host) { THIS_HOST } else { host }, port, token)
}

pub(crate) fn write_private(path: &Path, contents: &str) -> std::io::Result<()> {
    use std::io::Write;
    let mut opts = std::fs::OpenOptions::new();
    opts.write(true).create(true).truncate(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        opts.mode(0o600);
    }
    let mut f = opts.open(path)?;
    f.write_all(contents.as_bytes())?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?;
    }
    Ok(())
}

fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
}

fn is_loopback_host(host: &str) -> bool {
    let name = match host.strip_prefix('[') {
        Some(rest) => rest.split(']').next().unwrap_or(""),
        None => host.rsplit_once(':').map(|(h, _)| h).unwrap_or(host),
    };
    name.eq_ignore_ascii_case("localhost")
        || name.parse::<IpAddr>().is_ok_and(|ip| ip.is_loopback())
}

/// Paths that never need the token: health, the login endpoints themselves, the OAuth redirect
/// (the provider's page sends the browser there), the OAuth client document and the MCP endpoints, which carry their own
/// per-turn tokens.
fn exempt(path: &str) -> bool {
    path == "/api/health"
        || path.starts_with("/api/auth/")
        || path == "/api/mcp/oauth/callback"
        // The client ID metadata document authorization servers fetch (public by design)
        || path == crate::mcp::oauth::CLIENT_DOCUMENT_PATH
        || path.starts_with("/api/mcp/permission/")
        || path.starts_with("/api/mcp/gateway")
        || path == "/api/mcp/self"
        // Connected coding agents: the handler checks its own `kag_` bearer on every call
        || path == super::agents_connect::ENDPOINT
}

/// Routes only the person may call: saved passwords and the password vault (Bitwarden), changing app-wide settings, what bots wait on the person for, answering an approval card, Loose ends, Connections and Parallel Universe, the
/// approvals switch, team and channel instructions, notes written for a bot, skills, and deleting a bot or team or undoing a bot action or import (those delete saved passwords too). A bot's turn token (or any
/// bearer that isn't the API token) is refused with 403 everywhere. From another machine they need the API token (401);
/// from this machine they don't, unless `KWNT_REQUIRE_TOKEN=always` (`person_token_needed`).
pub fn person_only(method: &Method, path: &str) -> bool {
    let segs: Vec<&str> = path.trim_end_matches('/').split('/').collect();
    match segs.as_slice() {
        ["", "api", "sandbox", _, "passwords", ..] => true,
        // Folders isolated bots used (#446 item 6): forgetting one lets host CLIs load its files
        // again, so only the person reads or changes the list
        ["", "api", "isolated-folders", ..] => true,
        // P5 tool notes: bots file them with `note_tool`; only the person reads, edits, fixes or
        // deletes them, and their text is what every bot with the tool sees
        ["", "api", "tool-notes", ..] => true,
        // Raw clicks by position are the person's live view: a bot clicking by coordinates would
        // skip the payment floor, which reads the element under a label (bots use /act)
        ["", "api", "sandbox", _, "interact"] => true,
        ["", "api", "passwords", ..] => true,
        // Connected coding agents (0.7.4): only the person creates, lists and revokes their tokens
        ["", "api", "agent-connections", ..] => true,
        // Loose ends read the person's mail: bots never see or change them
        ["", "api", "zombie", ..] => true,
        // Connections list saved logins and add Composio, which holds the person's app sign-ins
        ["", "api", "connections", ..] => true,
        // Parallel Universe starts containers on this machine from a directory the person names
        ["", "api", "parallel", ..] => true,
        // Camera-to-Completed: the person's letters
        ["", "api", "documents", ..] => true,
        // Setup installs software on this machine and shows what is signed in
        ["", "api", "setup", ..] => true,
        // Signing the machine's own AI accounts in or out (0.7.6: Claude, ChatGPT, Grok)
        ["", "api", "engines", _, "login", ..] | ["", "api", "engines", _, "logout"] => true,
        // Sign-in links, device approvals and sessions: who else may sign in as the person
        ["", "api", "signin", ..] => true,
        // Web Push: where the person's notifications go
        ["", "api", "push", ..] => true,
        // What the bots wait on the person for: their questions, across every chat
        ["", "api", "waiting", ..] => true,
        // The review inbox (0.9 V1): what the person approves, sends back or rolls back, with held actions
        ["", "api", "inbox", ..] => true,
        // The desktop island's glance (0.9.13): every space's Needs you, and a quick ask as the person
        ["", "api", "glance", ..] => true,
        // Starting work-ahead runs now spends the person's tokens
        ["", "api", "work-ahead", ..] => true,
        // The idea jar is the person's: what they drop in, Look now, Do it and the shelf
        ["", "api", "ideas", ..] => true,
        // Show it once (0.7.9): the person's recorded steps, Write and Try it once, every method
        ["", "api", "show-once", ..] => true,
        // Mission Control: approving plans, verifying, rejecting, retrying and approving checks are
        // the person's calls; bots act on tasks only through their task tools
        ["", "api", "missions" | "tasks" | "checks", ..] => *method != Method::GET && *method != Method::HEAD,
        // Goals: creating, approving, editing, pausing, deleting and Check now are the person's;
        // bots only propose drafts (the `goal_propose` tool)
        ["", "api", "goals", ..] => *method != Method::GET && *method != Method::HEAD,
        // AutoFix's proposed changes to routines, checks and tasks: only the person reads, approves
        // or rejects them; bots only propose (the `workflow_propose` tool)
        ["", "api", "workflow-changes", ..] => true,
        // The Performance Market's scoreboard is the person's report: bots never read it, so it
        // can't feed a bot's own routing or be played for (Phase D1)
        ["", "api", "performance", ..] => true,
        // Flight School: only the person gives or takes a bot's freedom (C3); reads stay open
        ["", "api", "capabilities", ..] => *method != Method::GET && *method != Method::HEAD,
        // App-wide settings, including where notifications go (`notify_webhook_url`), and the
        // sub-routes such as `/computer` (which machine the bots run on) and `/computer/window`:
        // bots change the allowed ones through their `settings_update` action, never these routes
        ["", "api", "settings", ..] => *method != Method::GET && *method != Method::HEAD,
        // Carrying on a chain that went over its token budget is the person's call
        ["", "api", "chains", _, "continue"] => *method == Method::POST,
        // Lifting today's spending stop spends the person's money: only they press Continue
        // A bot's monthly budget (G2): only the person sets, raises, continues or pauses it again;
        // a bot (Chief included) never changes its own budget or another bot's
        ["", "api", "kwants", _, "budget", ..] => *method != Method::GET && *method != Method::HEAD,
        ["", "api", "usage", "today", "continue"] => *method != Method::GET && *method != Method::HEAD,
        // Changing a choice a bot made is the person's call
        ["", "api", "messages", _, "decisions", ..] => *method != Method::GET && *method != Method::HEAD,
        // The person's own 👍/👎: a bot can't react for them (or start a turn with one)
        ["", "api", "messages", _, "reaction"] => *method != Method::GET && *method != Method::HEAD,
        // The Mac app's update: check and install (which swaps and relaunches the app) are the
        // person's; the status stays readable
        ["", "api", "app", "update", ..] => *method != Method::GET && *method != Method::HEAD,
        // Answering a bot's question saves the answer as the person's own message and resumes the
        // bot with it: a bot (the asker or any other) answering would put words in the person's
        // mouth (Rigel, #646 advisory 1). Telegram answers in-process, not through this route
        ["", "api", "questions", _, "answer"] => *method == Method::POST,
        ["", "api", "approvals", _, "resolve"] => *method == Method::POST,
        // Retry on a card that expired unanswered asks the person again: theirs to press (0.9.5)
        ["", "api", "approvals", _, "retry"] => *method == Method::POST,
        // Dev mode (0.7): the watched repo, its cards, Fix it and Dismiss, release notes and the
        // backup command are the person's; bots never read the desk either (D3)
        ["", "api", "spaces", _, "dev_mode" | "dev", ..] => true,
        // A space's documents are what its bots quote as fact (10c): only the person adds or
        // removes them, so a bot can't write the words it then cites; the list, cite and page
        // reads stay open
        ["", "api", "spaces", _, "docs"] => *method == Method::POST,
        ["", "api", "spaces", _, "docs", _] => *method == Method::DELETE,
        ["", "api", "dev", ..] => true,
        ["", "api", "spaces" | "kwants", _, "approvals"] => *method != Method::GET && *method != Method::HEAD,
        // A team's instructions go into every member's prompt (PUT and PATCH both set them), and a
        // channel's PATCH sets its instructions and members: bots change these through their
        // `team_update` / `channel_update` actions, which the person can undo
        // Ready-made teams and discovery (0.7.7, next-077/api.md): making a team, reading a
        // project and the Undo preview are the person's; the catalogue stays readable for agents
        ["", "api", "team-templates", _, "preview"] => true,
        ["", "api", "teams", "from-template" | "suggest"] => true,
        ["", "api", "teams", _, "template"] => true,
        ["", "api", "teams", _] => matches!(*method, Method::DELETE | Method::PUT | Method::PATCH),
        ["", "api", "channels", _] => *method == Method::PATCH,
        ["", "api", "kwants", _] => *method == Method::DELETE,
        // 25e: what a bot may do without asking, and taking it back, are the person's
        ["", "api", "kwants", _, "always-allowed"] => true,
        // A bot's lessons (its failures and the person's send-backs): the person reads them; bots
        // never read their own record, any method
        ["", "api", "kwants", _, "lessons", ..] => true,
        // A note written here is saved as the person's ("written by the person"), for any bot:
        // bots keep their own notes with the memory tools
        ["", "api", "kwants", _, "memories", ..] => *method != Method::GET && *method != Method::HEAD,
        // Skills go into prompts. A skill made or switched on here is live at once, and approving
        // or rejecting one is the person's call: bots only propose (`skill_propose`)
        // Skill Foundry clusters and drafts: the person's review queue, read too
        ["", "api", "skills", "foundry", ..] | ["", "api", "skills", _, "foundry"] => true,
        // Automation Discovery reads every board task the person made in the space
        ["", "api", "automation-discovery", ..] => true,
        // Truth Ledger (C1): the person reads facts here and accepts or rejects them; bots get fact
        // tools in C2
        ["", "api", "facts", ..] => true,
        ["", "api", "skills", ..] => *method != Method::GET && *method != Method::HEAD,
        ["", "api", "bot-actions" | "imports", _, "undo"] => *method == Method::POST,
        // Import from this computer reads the person's home folder (other apps' bots, memories and
        // skills), and a GitHub link makes the server fetch from the internet: both the person's
        // (0.8.0 A2/A3), the list included
        ["", "api", "import", "local" | "github", ..] => true,
        _ => false,
    }
}

fn header_str(headers: &HeaderMap, name: impl header::AsHeaderName) -> Option<&str> {
    headers.get(name).and_then(|v| v.to_str().ok())
}

/// Whether the request comes from this machine, not through a proxy, to a loopback name.
pub fn is_local(headers: &HeaderMap, peer: Option<IpAddr>) -> bool {
    let forwarded = [
        "forwarded",
        "x-forwarded-for",
        "x-forwarded-host",
        "x-real-ip",
    ]
    .iter()
    .any(|h| headers.contains_key(*h));
    let host_ok = header_str(headers, header::HOST).is_none_or(is_loopback_host);
    peer.is_none_or(|ip| ip.is_loopback() || ip.to_canonical().is_loopback())
        && !forwarded
        && host_ok
}

pub(super) fn cookie_token(headers: &HeaderMap) -> Option<&str> {
    headers
        .get_all(header::COOKIE)
        .iter()
        .filter_map(|v| v.to_str().ok())
        .flat_map(|v| v.split(';'))
        .find_map(|c| {
            c.trim()
                .strip_prefix(COOKIE)
                .and_then(|r| r.strip_prefix('='))
        })
}

fn bearer(headers: &HeaderMap) -> Option<&str> {
    header_str(headers, header::AUTHORIZATION)
        .and_then(|v| v.strip_prefix("Bearer "))
        .map(str::trim)
}

/// The API token as a bearer, or a cookie holding a live session (or the API token itself, as
/// browsers signed in before sessions still have).
fn presented_token_ok(api: &ApiToken, headers: &HeaderMap) -> bool {
    bearer(headers).is_some_and(|t| api.matches(t))
        || cookie_token(headers).is_some_and(|t| api.matches(t) || api.signin.session_ok(t))
}

fn needs_token(api: &ApiToken, headers: &HeaderMap, peer: Option<IpAddr>) -> bool {
    match api.require {
        Require::Never => false,
        Require::Always => true,
        Require::Auto => !is_local(headers, peer),
    }
}

/// Whether a person-only route needs the API token for this request. Not from this machine:
/// the person there shouldn't be asked for a token to answer their own approval cards. A bot's
/// shell there could reach these routes with a bare `curl`, but it could read the token file just
/// as well; `KWNT_REQUIRE_TOKEN=always` asks for the token on loopback too.
fn person_token_needed(api: &ApiToken, headers: &HeaderMap, peer: Option<IpAddr>) -> bool {
    api.require != Require::Never && needs_token(api, headers, peer)
}

fn deny(status: StatusCode, msg: &str) -> Response {
    (status, Json(json!({ "error": msg }))).into_response()
}

/// 401 for a person-only route without the token: `{error, person_only: true}`.
/// For a handler guarding something person-only that `person_only` can't see from the path (a
/// letter's pages under `/api/attachments`): the API token must be presented where
/// `person_token_needed` says so.
#[allow(clippy::result_large_err)]
pub(super) fn require_person_token(state: &AppState, headers: &HeaderMap, peer: Option<IpAddr>) -> Result<(), Response> {
    if person_token_needed(&state.api_token, headers, peer) && !presented_token_ok(&state.api_token, headers) {
        return Err(person_token_required());
    }
    Ok(())
}

fn person_token_required() -> Response {
    (
        StatusCode::UNAUTHORIZED,
        Json(json!({ "error": "this needs the API token: open the sign-in link kwant printed when it started, or run `kwnt token`", "person_only": true, "code": "person_only" })),
    )
        .into_response()
}

/// Origin check, CORS for allowed origins, and the token check, for every `/api` request.
pub async fn guard(State(state): State<AppState>, req: Request, next: Next) -> Response {
    let path = req.uri().path().to_string();
    if !path.starts_with("/api/") && path != "/api" {
        return next.run(req).await;
    }
    let api = state.api_token.clone();
    let headers = req.headers().clone();
    let peer = req
        .extensions()
        .get::<ConnectInfo<SocketAddr>>()
        .map(|c| c.0.ip());
    let host = header_str(&headers, header::HOST);
    let origin = header_str(&headers, header::ORIGIN).map(str::to_string);

    if let Some(o) = &origin {
        if !api.origin_allowed(o, host) {
            return deny(StatusCode::FORBIDDEN, "origin not allowed");
        }
    }
    let preflight = req.method() == Method::OPTIONS
        && headers.contains_key(header::ACCESS_CONTROL_REQUEST_METHOD);
    let mut resp = if preflight {
        StatusCode::NO_CONTENT.into_response()
    } else if path != super::agents_connect::ENDPOINT && super::agents_connect::is_agent_token(&headers) {
        // A connected agent's token only ever works on its own endpoint, from loopback too, and
        // never counts as the person (whatever `KWNT_REQUIRE_TOKEN` says)
        deny(StatusCode::FORBIDDEN, "this token only works for /api/mcp/agents")
    } else if !exempt(&path)
        && needs_token(&api, &headers, peer)
        && !presented_token_ok(&api, &headers)
    {
        deny(StatusCode::UNAUTHORIZED, "API token required")
    } else if api.require != Require::Never && person_only(req.method(), &path) {
        // A bot's turn token (or any bearer that isn't the API token) is a 403, from anywhere;
        // no token a 401 where the token is needed
        if super::mcp_suggest::refuse_agent_tokens(&state, &headers, "only the person can do this").await.is_err() {
            (StatusCode::FORBIDDEN, Json(json!({ "error": "only the person can do this", "code": "person_only" }))).into_response()
        } else if person_token_needed(&api, &headers, peer) && !presented_token_ok(&api, &headers) {
            person_token_required()
        } else {
            next.run(req).await
        }
    } else {
        next.run(req).await
    };
    if let Some(o) = origin.and_then(|o| HeaderValue::from_str(&o).ok()) {
        let h = resp.headers_mut();
        h.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, o);
        h.insert(
            header::ACCESS_CONTROL_ALLOW_CREDENTIALS,
            HeaderValue::from_static("true"),
        );
        h.append(header::VARY, HeaderValue::from_static("Origin"));
        if preflight {
            h.insert(
                header::ACCESS_CONTROL_ALLOW_METHODS,
                HeaderValue::from_static("GET, POST, PUT, PATCH, DELETE, OPTIONS"),
            );
            h.insert(
                header::ACCESS_CONTROL_ALLOW_HEADERS,
                HeaderValue::from_static("authorization, content-type"),
            );
            h.insert(
                header::ACCESS_CONTROL_MAX_AGE,
                HeaderValue::from_static("600"),
            );
        }
    }
    resp
}

/// `GET /api/auth/status`: `{auth_required, authenticated, person_auth_required,
/// person_authenticated}` for this request.
pub async fn status(State(state): State<AppState>, req: Request) -> Response {
    let peer = req
        .extensions()
        .get::<ConnectInfo<SocketAddr>>()
        .map(|c| c.0.ip());
    let headers = req.headers();
    let required = needs_token(&state.api_token, headers, peer);
    let has_token = presented_token_ok(&state.api_token, headers);
    let authenticated = !required || has_token;
    // Person-only actions (passwords, approvals) need it from another machine, or with `always`
    let person_required = person_token_needed(&state.api_token, headers, peer);
    let mut status = json!({
        "auth_required": required,
        "authenticated": authenticated,
        "person_auth_required": person_required,
        "person_authenticated": !person_required || has_token,
        // This server can let a new browser in from a signed-in device (`/api/auth/device`)
        "device_login": true,
    });
    // Where an app store or host shows this install's token (`KWNT_SIGNIN_HINT`). Left out
    // when unset, so the reply keeps the shape existing clients already know.
    if let Some(hint) = state.api_token.signin_hint() {
        status["signin_hint"] = json!(hint);
    }
    Json(status).into_response()
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SessionBody {
    token: String,
}

fn session_cookie(value: &str, max_age: u64, secure: bool) -> HeaderValue {
    let secure = if secure { "; Secure" } else { "" };
    HeaderValue::from_str(&format!(
        "{COOKIE}={value}; HttpOnly; SameSite=Strict; Path=/; Max-Age={max_age}{secure}"
    ))
    .unwrap_or_else(|_| HeaderValue::from_static("kwnt_token=; Max-Age=0; Path=/"))
}

fn is_https(headers: &HeaderMap) -> bool {
    header_str(headers, "x-forwarded-proto").is_some_and(|p| p.eq_ignore_ascii_case("https"))
}

fn peer_ip(peer: &Option<ConnectInfo<SocketAddr>>) -> Option<IpAddr> {
    peer.as_ref().map(|c| c.0.ip())
}

/// A response that signs this browser in with a new session's cookie (HttpOnly, SameSite=Strict),
/// which EventSource and the browser WebSocket then send.
pub(super) fn with_session_cookie(mut resp: Response, secret: &str, headers: &HeaderMap) -> Response {
    resp.headers_mut().insert(
        header::SET_COOKIE,
        session_cookie(secret, signin::SESSION_SECS, is_https(headers)),
    );
    resp.headers_mut()
        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
    resp
}

/// `POST /api/auth/session {token}`: `token` is the API token or a one-time sign-in link code.
/// Either starts a new session for this browser (the cookie never holds the API token). 401
/// `{error, reason}`: `wrong_token`, or `link_used_or_expired` for a link that worked once.
pub async fn create_session(
    State(state): State<AppState>,
    peer: Option<ConnectInfo<SocketAddr>>,
    headers: HeaderMap,
    Json(body): Json<SessionBody>,
) -> Response {
    let token = body.token.trim();
    let client = signin::client(&headers, peer_ip(&peer));
    let secret = if state.api_token.matches(token) {
        state.api_token.signin.create_session(Via::Token, &client)
    } else {
        match state.api_token.signin.redeem_link(token, &client) {
            Redeem::Ok(secret) => secret,
            refused => {
                // A wrong guess costs a little time
                tokio::time::sleep(std::time::Duration::from_millis(300)).await;
                let (msg, reason) = if refused == Redeem::Spent {
                    ("this sign-in link was already used or has expired", "link_used_or_expired")
                } else {
                    ("wrong token", "wrong_token")
                };
                return (StatusCode::UNAUTHORIZED, Json(json!({ "error": msg, "reason": reason }))).into_response();
            }
        }
    };
    with_session_cookie(StatusCode::NO_CONTENT.into_response(), &secret, &headers)
}

/// `DELETE /api/auth/session`: ends this browser's session and clears the cookie.
pub async fn delete_session(State(state): State<AppState>, headers: HeaderMap) -> Response {
    if let Some(secret) = cookie_token(&headers) {
        if let Some(id) = state.api_token.signin.session_id(secret) {
            state.push.drop_session(&id);
        }
        state.api_token.signin.revoke_secret(secret);
    }
    let mut resp = StatusCode::NO_CONTENT.into_response();
    resp.headers_mut().insert(
        header::SET_COOKIE,
        session_cookie("", 0, is_https(&headers)),
    );
    resp
}

/// `POST /api/signin/links` (person only): a one-time sign-in link for another device, live for
/// 15 minutes: `{code, url, expires_at, expires_in}`. `url` is null when kwnt doesn't know its
/// public address (no `KWNT_ALLOWED_ORIGINS`); the page then builds it from its own address.
pub async fn create_link(State(state): State<AppState>) -> Response {
    let code = state.api_token.signin.create_link(signin::LINK_TTL);
    let ttl = signin::LINK_TTL.as_secs();
    let url = state
        .api_token
        .public_base()
        .map(|b| one_time_link("", 0, Some(b), &code));
    let mut resp = Json(json!({
        "code": code,
        "url": url,
        "expires_in": ttl,
        "expires_at": chrono::Utc::now() + chrono::TimeDelta::seconds(ttl as i64),
    }))
    .into_response();
    resp.headers_mut()
        .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
    resp
}

/// `POST /api/auth/device {}` (open to anyone): a new browser asks to be let in.
/// `{device_code, user_code, expires_in, interval}`; the browser keeps `device_code` to itself,
/// shows `user_code`, and polls. 429 `{error, retry_after}` (and `Retry-After`) when this address
/// asked too often or too many requests are waiting.
pub async fn device_start(
    State(state): State<AppState>,
    peer: Option<ConnectInfo<SocketAddr>>,
    headers: HeaderMap,
) -> Response {
    let client = signin::client(&headers, peer_ip(&peer));
    match state.api_token.signin.start_device(&client) {
        Ok((device_code, user_code)) => {
            let mut resp = Json(json!({
                "device_code": device_code,
                "user_code": user_code,
                "expires_in": signin::DEVICE_TTL.as_secs(),
                "interval": signin::DEVICE_INTERVAL_SECS,
            }))
            .into_response();
            resp.headers_mut()
                .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
            resp
        }
        Err(wait) => {
            let mut resp = (
                StatusCode::TOO_MANY_REQUESTS,
                Json(json!({ "error": "too many sign-in requests; try again shortly", "retry_after": wait })),
            )
                .into_response();
            if let Ok(v) = HeaderValue::from_str(&wait.to_string()) {
                resp.headers_mut().insert(header::RETRY_AFTER, v);
            }
            resp
        }
    }
}

#[derive(Deserialize)]
pub struct DevicePollBody {
    device_code: String,
}

/// `POST /api/auth/device/poll {device_code}`: 200 `{status: "pending"}`; 200
/// `{status: "approved"}` with the session cookie (once); 403 `{status: "denied"}` (once); 410
/// `{status: "expired"}` for one that ran out, was collected, or never existed.
pub async fn device_poll(
    State(state): State<AppState>,
    headers: HeaderMap,
    Json(body): Json<DevicePollBody>,
) -> Response {
    match state.api_token.signin.poll_device(body.device_code.trim()) {
        Poll::Pending => Json(json!({ "status": "pending" })).into_response(),
        Poll::Approved(secret) => with_session_cookie(Json(json!({ "status": "approved" })).into_response(), &secret, &headers),
        Poll::Denied => (StatusCode::FORBIDDEN, Json(json!({ "status": "denied" }))).into_response(),
        Poll::Expired => (StatusCode::GONE, Json(json!({ "status": "expired" }))).into_response(),
    }
}

/// `GET /api/signin/requests` (person only): devices waiting to be let in, oldest first.
pub async fn device_requests(State(state): State<AppState>) -> Response {
    Json(state.api_token.signin.pending_devices()).into_response()
}

#[derive(Deserialize)]
pub struct UserCodeBody {
    user_code: String,
}

async fn decide(state: &AppState, body: UserCodeBody, approve: bool) -> Response {
    if state.api_token.signin.decide_device(&body.user_code, approve) {
        StatusCode::NO_CONTENT.into_response()
    } else {
        deny(StatusCode::NOT_FOUND, "no waiting sign-in request with that code (it may have expired)")
    }
}

/// `POST /api/signin/requests/approve {user_code}` (person only): lets that device in. 404 when no
/// request with that code is waiting.
pub async fn device_approve(State(state): State<AppState>, Json(body): Json<UserCodeBody>) -> Response {
    decide(&state, body, true).await
}

/// `POST /api/signin/requests/deny {user_code}` (person only).
pub async fn device_deny(State(state): State<AppState>, Json(body): Json<UserCodeBody>) -> Response {
    decide(&state, body, false).await
}

/// `GET /api/signin/sessions` (person only): signed-in browsers, newest first, `current` for this one.
pub async fn sessions(State(state): State<AppState>, headers: HeaderMap) -> Response {
    let current = cookie_token(&headers).and_then(|c| state.api_token.signin.session_id(c));
    let list: Vec<_> = state
        .api_token
        .signin
        .sessions()
        .into_iter()
        .map(|s| {
            json!({
                "id": s.id,
                "browser": s.browser,
                "ip": s.ip,
                "via": s.via,
                "created_at": s.created_at,
                "last_seen": s.last_seen,
                "current": current.as_deref() == Some(s.id.as_str()),
            })
        })
        .collect();
    Json(list).into_response()
}

/// `DELETE /api/signin/sessions/:id` (person only): signs that browser out. 404 when unknown.
pub async fn revoke_session(State(state): State<AppState>, axum::extract::Path(id): axum::extract::Path<String>) -> Response {
    // A signed-out phone stops getting notifications too
    state.push.drop_session(&id);
    if state.api_token.signin.revoke(&id) {
        StatusCode::NO_CONTENT.into_response()
    } else {
        deny(StatusCode::NOT_FOUND, "no such session")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn headers(pairs: &[(&'static str, &str)]) -> HeaderMap {
        let mut h = HeaderMap::new();
        for (k, v) in pairs {
            h.append(*k, HeaderValue::from_str(v).unwrap());
        }
        h
    }

    #[test]
    fn local_needs_loopback_peer_and_host_and_no_proxy() {
        let lo: Option<IpAddr> = Some("127.0.0.1".parse().unwrap());
        let remote: Option<IpAddr> = Some("203.0.113.9".parse().unwrap());
        assert!(is_local(&headers(&[("host", "127.0.0.1:3000")]), lo));
        assert!(is_local(&headers(&[("host", "localhost:3000")]), lo));
        assert!(is_local(
            &headers(&[("host", "[::1]:3000")]),
            Some("::1".parse().unwrap())
        ));
        assert!(is_local(&headers(&[]), None));
        assert!(!is_local(&headers(&[("host", "127.0.0.1:3000")]), remote));
        // DNS rebinding: loopback peer, foreign name
        assert!(!is_local(&headers(&[("host", "evil.example:3000")]), lo));
        // A reverse proxy on the same machine
        assert!(!is_local(
            &headers(&[
                ("host", "localhost:3000"),
                ("x-forwarded-for", "203.0.113.9")
            ]),
            lo
        ));
        assert!(!is_local(
            &headers(&[("host", "127.0.0.1.evil.example")]),
            lo
        ));
    }

    #[test]
    fn signin_hint_is_one_short_line_and_never_the_token() {
        let token = "abcdefghijklmnopqrstuvwxyz012345";
        assert_eq!(signin_hint_from(None, token), None);
        assert_eq!(signin_hint_from(Some("  \n\t "), token), None);
        assert_eq!(
            signin_hint_from(Some("  Use the password\r\nUmbrel shows\tfor kwnt. "), token).as_deref(),
            Some("Use the password Umbrel shows for kwnt.")
        );
        // Plain text stays as typed: the UI renders it as text, not HTML
        assert_eq!(
            signin_hint_from(Some("Files → AppData/kwnt/data/api-token <b>"), token).as_deref(),
            Some("Files → AppData/kwnt/data/api-token <b>")
        );
        let long = "é".repeat(SIGNIN_HINT_MAX + 50);
        let cut = signin_hint_from(Some(&long), token).unwrap();
        assert_eq!(cut.chars().count(), SIGNIN_HINT_MAX);
        // A hint holding the token would publish it
        assert_eq!(signin_hint_from(Some(&format!("Your token is {token}")), token), None);
        let api = ApiToken::for_tests(token, Require::Auto, 3000);
        assert_eq!(api.signin_hint(), None);
        let api = api.with_signin_hint(" Copy the KWNT_API_TOKEN value from Render. ");
        assert_eq!(api.signin_hint(), Some("Copy the KWNT_API_TOKEN value from Render."));
        assert!(!format!("{api:?}").contains("Render"), "Debug stays short");
    }

    #[test]
    fn origins() {
        let api = ApiToken::for_tests("t".repeat(32).as_str(), Require::Auto, 3000);
        assert!(api.origin_allowed("http://localhost:3000", None));
        assert!(api.origin_allowed("http://127.0.0.1:5199/", None));
        assert!(api.origin_allowed("https://kwnt.example.com", Some("kwnt.example.com")));
        assert!(!api.origin_allowed("https://evil.example", Some("kwnt.example.com")));
        assert!(!api.origin_allowed("http://localhost:3001", Some("localhost:3000")));
        assert!(!api.origin_allowed("null", None));
    }

    #[test]
    fn tokens_and_cookies() {
        let api = ApiToken::for_tests("abcdefghijklmnopqrstuvwxyz012345", Require::Auto, 3000);
        assert!(presented_token_ok(
            &api,
            &headers(&[("authorization", "Bearer abcdefghijklmnopqrstuvwxyz012345")])
        ));
        assert!(presented_token_ok(
            &api,
            &headers(&[("cookie", "a=b; kwnt_token=abcdefghijklmnopqrstuvwxyz012345")])
        ));
        assert!(!presented_token_ok(
            &api,
            &headers(&[("cookie", "kwnt_token=abcdefghijklmnopqrstuvwxyz01234")])
        ));
        assert!(!presented_token_ok(
            &api,
            &headers(&[("authorization", "Basic abc")])
        ));
        assert!(!format!("{api:?}").contains("abcdef"));
    }

    #[test]
    fn person_only_routes() {
        let (get, post, put, del) = (Method::GET, Method::POST, Method::PUT, Method::DELETE);
        assert!(person_only(&get, "/api/sandbox/t1/passwords"));
        assert!(person_only(&get, "/api/tool-notes"));
        assert!(person_only(&get, "/api/glance"));
        assert!(person_only(&post, "/api/glance/ask"));
        assert!(person_only(&post, "/api/questions/q1/answer"));
        assert!(person_only(&post, "/api/questions/q1/answer/"));
        // 22i: Dismiss in Needs attention is the person's alone
        assert!(person_only(&post, "/api/waiting/w1/dismiss"));
        assert!(!person_only(&get, "/api/questions/q1"));
        assert!(!person_only(&post, "/api/questions/q1/cancel"));
        assert!(person_only(&post, "/api/spaces/s1/docs"));
        assert!(person_only(&post, "/api/spaces/s1/docs/"));
        assert!(person_only(&del, "/api/spaces/s1/docs/faq.md"));
        assert!(!person_only(&get, "/api/spaces/s1/docs"));
        assert!(!person_only(&post, "/api/spaces/s1/docs/cite"));
        assert!(!person_only(&get, "/api/spaces/s1/docs/page"));
        assert!(person_only(&post, "/api/tool-notes"));
        assert!(person_only(&Method::PATCH, "/api/tool-notes/n1"));
        assert!(person_only(&del, "/api/tool-notes/n1/"));
        assert!(person_only(&post, "/api/engines/codex/login"));
        assert!(person_only(&get, "/api/engines/grok/login/login_1"));
        assert!(person_only(&post, "/api/engines/claude/login/login_1/code"));
        assert!(person_only(&post, "/api/engines/claude/logout"));
        assert!(!person_only(&get, "/api/engines"));
        assert!(!person_only(&post, "/api/engines/recheck"));
        assert!(person_only(&post, "/api/sandbox/t1/passwords/c1/reveal"));
        assert!(person_only(&del, "/api/sandbox/t1/passwords/c1"));
        assert!(person_only(&post, "/api/sandbox/t1/interact"));
        assert!(!person_only(&post, "/api/sandbox/t1/act"));
        assert!(person_only(&get, "/api/passwords/store"));
        assert!(person_only(&get, "/api/isolated-folders"));
        assert!(person_only(&post, "/api/isolated-folders/forget"));
        assert!(person_only(&Method::HEAD, "/api/isolated-folders/"));
        // 0.7.7 ready-made teams: everything but the catalogue
        assert!(person_only(&get, "/api/team-templates/inbox/preview"));
        assert!(person_only(&post, "/api/teams/from-template"));
        assert!(person_only(&post, "/api/teams/suggest"));
        assert!(person_only(&del, "/api/teams/suggest"));
        assert!(person_only(&get, "/api/teams/t1/template"));
        assert!(!person_only(&get, "/api/team-templates"));
        assert!(!person_only(&post, "/api/teams"));
        assert!(!person_only(&get, "/api/teams/t1"));
        assert!(person_only(&get, "/api/kwants/k1/always-allowed"));
        assert!(person_only(&del, "/api/kwants/k1/always-allowed/"));
        for (m, p) in [
            (&get, "/api/spaces/s1/dev_mode"),
            (&put, "/api/spaces/s1/dev_mode"),
            (&post, "/api/spaces/s1/dev_mode/check"),
            (&post, "/api/spaces/s1/dev_mode/seen"),
            (&get, "/api/spaces/s1/dev/landed?before=2026-11-05T00:00:00Z"),
            (&post, "/api/spaces/s1/dev/landed/seen"),
            (&get, "/api/spaces/s1/dev/reviews"),
            (&post, "/api/spaces/s1/dev/release_notes"),
            (&post, "/api/spaces/s1/dev/backup"),
            (&get, "/api/dev/cards/dvc_1"),
            (&post, "/api/dev/cards/dvc_1/findings/f1"),
            (&post, "/api/dev/cards/dvc_1/post"),
            (&Method::HEAD, "/api/spaces/s1/dev/reviews"),
        ] {
            assert!(person_only(m, p), "{m} {p}");
        }
        assert!(!person_only(&get, "/api/spaces/s1/developers"));
        assert!(person_only(&post, "/api/usage/today/continue"));
        assert!(person_only(&put, "/api/usage/today/continue/"));
        assert!(!person_only(&get, "/api/usage/today"));
        // G2: a bot's monthly budget
        assert!(person_only(&post, "/api/kwants/k1/budget"));
        assert!(person_only(&post, "/api/kwants/k1/budget/continue"));
        assert!(person_only(&Method::DELETE, "/api/kwants/k1/budget/continue"));
        assert!(!person_only(&get, "/api/kwants/k1/budget"));
        assert!(!person_only(&get, "/api/usage/budgets"));
        // 0.7.4: connected coding agents are made and revoked by the person only; the agent
        // endpoint itself checks its own token instead
        assert!(person_only(&get, "/api/agent-connections"));
        assert!(person_only(&post, "/api/agent-connections"));
        assert!(person_only(&del, "/api/agent-connections/agc_1"));
        assert!(person_only(&get, "/api/agent-connections/agc_1/activity"));
        assert!(!person_only(&post, "/api/mcp/agents"));
        assert!(exempt("/api/mcp/agents"));
        assert!(person_only(&post, "/api/messages/m1/decisions/0/change"));
        assert!(person_only(&put, "/api/messages/m1/reaction"));
        assert!(person_only(&post, "/api/messages/m1/reaction"));
        assert!(!person_only(&get, "/api/messages/m1/reaction"));
        assert!(person_only(&post, "/api/work-ahead/run"));
        assert!(person_only(&get, "/api/ideas"));
        assert!(person_only(&post, "/api/ideas"));
        assert!(person_only(&get, "/api/ideas/i1"));
        // Show it once: GET included (the steps are what the person did)
        assert!(person_only(&get, "/api/show-once/recordings"));
        assert!(person_only(&get, "/api/show-once/recordings/rec_1"));
        assert!(person_only(&post, "/api/show-once/recordings/rec_1/write"));
        assert!(person_only(&del, "/api/show-once/recordings/rec_1/steps/4"));
        assert!(person_only(&post, "/api/show-once/skills/s1/try"));
        assert!(person_only(&post, "/api/show-once/test/clock"));
        assert!(person_only(&post, "/api/ideas/i1/pick"));
        assert!(person_only(&post, "/api/ideas/i1/look"));
        assert!(person_only(&del, "/api/ideas/i1"));
        assert!(person_only(&get, "/api/skills/foundry"));
        assert!(person_only(&get, "/api/kwants/k1/lessons"));
        assert!(person_only(&Method::HEAD, "/api/kwants/k1/lessons"));
        assert!(person_only(&post, "/api/kwants/k1/lessons"));
        assert!(person_only(&del, "/api/kwants/k1/lessons/x"));
        assert!(!person_only(&get, "/api/kwants/k1/memories"));
        assert!(person_only(&post, "/api/skills/foundry/k1~release/draft"));
        assert!(person_only(&get, "/api/skills/s1/foundry"));
        assert!(person_only(&post, "/api/skills/s1/rollback"));
        assert!(person_only(&post, "/api/skills/s1/replay"));
        assert!(person_only(&post, "/api/automation-discovery/scan"));
        assert!(person_only(&get, "/api/automation-discovery/scan?space=s1"));
        assert!(person_only(&get, "/api/automation-discovery"));
        assert!(person_only(&post, "/api/automation-discovery/sug_1/accept"));
        assert!(person_only(&post, "/api/automation-discovery/sug_1/dismiss"));
        assert!(!person_only(&get, "/api/skills/s1"));
        assert!(person_only(&post, "/api/missions"));
        assert!(person_only(&post, "/api/missions/m1/approve"));
        assert!(person_only(&post, "/api/missions/m1/run"));
        assert!(person_only(&Method::PATCH, "/api/missions/m1"));
        assert!(person_only(&post, "/api/tasks/t1/verify"));
        assert!(person_only(&post, "/api/tasks/t1/checks/run"));
        assert!(person_only(&post, "/api/checks/c1/approve"));
        assert!(person_only(&post, "/api/checks/c1/wait-more"));
        assert!(person_only(&post, "/api/tasks/t1/fork"));
        assert!(person_only(&post, "/api/tasks/t1/run"));
        assert!(person_only(&post, "/api/tasks/t1/checkpoints"));
        assert!(person_only(&post, "/api/tasks/t1/checkpoints/c1/restore"));
        assert!(!person_only(&get, "/api/tasks/t1/checkpoints"));
        assert!(!person_only(&get, "/api/tasks/t1/checkpoints/c1/restore-preview"));
        assert!(person_only(&post, "/api/tasks/t1/flags/f1/resolve"));
        assert!(person_only(&get, "/api/workflow-changes"));
        assert!(person_only(&post, "/api/workflow-changes/w1/approve"));
        assert!(person_only(&post, "/api/workflow-changes/w1/reject"));
        assert!(person_only(&post, "/api/tasks/t1/autofix"));
        assert!(person_only(&post, "/api/tasks/t1/autofix/a1/cancel"));
        assert!(!person_only(&get, "/api/tasks/t1/failures"));
        assert!(person_only(&post, "/api/missions/m1/graph/scan"));
        assert!(!person_only(&get, "/api/missions/m1/graph"));
        // P1: only the person picks, removes or runs a mission's watcher; bots may read it
        assert!(person_only(&Method::PUT, "/api/missions/m1/watch"));
        assert!(person_only(&Method::DELETE, "/api/missions/m1/watch"));
        assert!(person_only(&post, "/api/missions/m1/watch/check"));
        assert!(!person_only(&get, "/api/missions/m1/watch"));
        assert!(!person_only(&get, "/api/missions/m1/watch/checks"));
        assert!(!person_only(&get, "/api/tasks/t1/flags"));
        assert!(!person_only(&get, "/api/tasks"));
        assert!(!person_only(&get, "/api/missions/insights"));
        assert!(person_only(&post, "/api/goals"));
        assert!(person_only(&post, "/api/goals/g1/approve"));
        assert!(person_only(&post, "/api/goals/g1/check"));
        assert!(person_only(&Method::PATCH, "/api/goals/g1"));
        assert!(person_only(&del, "/api/goals/g1"));
        assert!(!person_only(&get, "/api/goals"));
        assert!(!person_only(&get, "/api/goals/g1"));
        assert!(person_only(&get, "/api/performance"));
        assert!(person_only(&post, "/api/passwords/bitwarden/unlock"));
        assert!(person_only(&del, "/api/passwords/bitwarden"));
        assert!(person_only(&post, "/api/passwords/migrate"));
        assert!(person_only(&get, "/api/zombie/cases"));
        assert!(person_only(&post, "/api/zombie/sources/s1/test"));
        assert!(person_only(&get, "/api/connections"));
        assert!(person_only(&get, "/api/setup/status"));
        assert!(person_only(&post, "/api/setup/install"));
        assert!(person_only(&get, "/api/setup/jobs/setup-1"));
        assert!(person_only(&get, "/api/push/key"));
        assert!(person_only(&post, "/api/push/subscribe"));
        assert!(person_only(&post, "/api/push/unsubscribe"));
        assert!(person_only(&get, "/api/waiting"));
        assert!(person_only(&get, "/api/waiting/"));
        assert!(person_only(&get, "/api/inbox"));
        assert!(!person_only(&get, "/api/away"));
        assert!(person_only(&post, "/api/connections/composio"));
        assert!(person_only(&get, "/api/parallel/prices"));
        assert!(person_only(&post, "/api/parallel/scenarios"));
        assert!(person_only(&post, "/api/documents"));
        assert!(person_only(&get, "/api/documents/d1"));
        assert!(person_only(&post, "/api/approvals/a1/resolve"));
        assert!(person_only(&post, "/api/approvals/a1/retry"));
        assert!(person_only(&put, "/api/spaces/s1/approvals"));
        assert!(person_only(&put, "/api/kwants/k1/approvals"));
        assert!(!person_only(&get, "/api/kwants/k1/approvals"));
        assert!(person_only(&post, "/api/settings"));
        assert!(person_only(&post, "/api/chains/m1/continue"));
        assert!(person_only(&put, "/api/settings/"));
        assert!(!person_only(&get, "/api/settings"));
        assert!(person_only(&put, "/api/settings/computer"));
        assert!(person_only(&put, "/api/settings/computer/"));
        assert!(person_only(&put, "/api/settings/computer/window"));
        assert!(!person_only(&get, "/api/settings/computer"));
        assert!(person_only(&post, "/api/app/update/check"));
        assert!(person_only(&post, "/api/app/update/install"));
        assert!(person_only(&post, "/api/app/update/install/"));
        assert!(!person_only(&get, "/api/app/update"));
        assert!(!person_only(&Method::HEAD, "/api/app/update"));
        assert!(!person_only(&get, "/api/approvals"));
        assert!(!person_only(&get, "/api/sandbox/t1/status"));
        assert!(!person_only(&get, "/api/kwants"));
        assert!(person_only(&del, "/api/kwants/k1"));
        assert!(person_only(&del, "/api/teams/t1/"));
        assert!(person_only(&post, "/api/bot-actions/b1/undo"));
        assert!(person_only(&post, "/api/imports/i1/undo"));
        assert!(person_only(&get, "/api/import/local"));
        assert!(person_only(&post, "/api/import/local/preview"));
        assert!(person_only(&post, "/api/import/github/preview"));
        assert!(!person_only(&get, "/api/import/prompts"));
        assert!(!person_only(&put, "/api/kwants/k1"));
        assert!(!person_only(&get, "/api/teams/t1"));
        assert!(!person_only(&post, "/api/bot-actions/b1/confirm"));
        // C0: instructions, notes written for a bot, and skills
        let patch = Method::PATCH;
        assert!(person_only(&patch, "/api/teams/t1"));
        assert!(person_only(&put, "/api/teams/t1"));
        assert!(person_only(&patch, "/api/channels/c1"));
        assert!(!person_only(&get, "/api/channels/c1"));
        assert!(person_only(&post, "/api/kwants/k1/memories"));
        assert!(person_only(&patch, "/api/kwants/k1/memories/m1"));
        assert!(person_only(&del, "/api/kwants/k1/memories/m1"));
        assert!(!person_only(&get, "/api/kwants/k1/memories"));
        assert!(person_only(&post, "/api/skills/s1/approve"));
        assert!(person_only(&post, "/api/skills/s1/reject"));
        assert!(person_only(&post, "/api/skills"));
        assert!(person_only(&patch, "/api/skills/s1"));
        assert!(person_only(&del, "/api/skills/s1"));
        assert!(!person_only(&get, "/api/skills"));
        assert!(!person_only(&get, "/api/skills/s1"));
    }

    #[test]
    fn startup_link_only_where_someone_needs_it() {
        for host in ["127.0.0.1", "localhost", "::1", "[::1]"] {
            assert!(!startup_link_needed(host, Require::Auto), "{host}");
            assert!(!startup_link_needed(host, Require::Never), "{host}");
            assert!(startup_link_needed(host, Require::Always), "{host}");
        }
        for host in ["0.0.0.0", "::", "203.0.113.9", "kwnt.example.com"] {
            assert!(startup_link_needed(host, Require::Auto), "{host}");
            assert!(!startup_link_needed(host, Require::Never), "{host}");
        }
    }

    #[test]
    fn one_time_links_and_notice() {
        let code = "kl1.abc";
        assert_eq!(one_time_link("0.0.0.0", 8799, None, code), "http://<this-host>:8799/#kwnt_token=kl1.abc");
        assert_eq!(one_time_link("::1", 8899, None, code), "http://[::1]:8899/#kwnt_token=kl1.abc");
        assert_eq!(
            one_time_link("0.0.0.0", 8799, Some("https://kwant.example.com/"), code),
            "https://kwant.example.com/#kwnt_token=kl1.abc"
        );
        // A placeholder host: no QR code (it couldn't open), and how to get a real one
        let placeholder = one_time_notice(&one_time_link("0.0.0.0", 8799, None, code), true);
        assert!(placeholder[0].ends_with("http://<this-host>:8799/#kwnt_token=kl1.abc"), "{placeholder:?}");
        assert!(placeholder[0].contains("works once, for 15 minutes"), "{placeholder:?}");
        assert!(placeholder.iter().any(|l| l.contains("KWNT_ALLOWED_ORIGINS")));
        assert!(!placeholder.iter().any(|l| l.contains('█')));
        let real = one_time_notice("https://kwant.example.com/#kwnt_token=kl1.abc", true);
        assert!(real.iter().any(|l| l.contains('█')), "QR code");
        assert!(real.last().unwrap().contains("kwnt signin-link"));
        assert!(!one_time_notice("https://kwant.example.com/#kwnt_token=kl1.abc", false).iter().any(|l| l.contains('█')));
        // install.sh's bare form: the link (indented), then the QR code, and none of the notice's sentences
        let bare = bare_notice("https://kwant.example.com/#kwnt_token=kl1.abc", true);
        assert_eq!(bare[0], "  https://kwant.example.com/#kwnt_token=kl1.abc");
        assert!(bare.iter().any(|l| l.contains('█')), "QR code");
        assert!(bare.iter().skip(1).all(|l| l.is_empty() || l.starts_with("  ")), "{bare:?}");
        assert!(!bare.iter().any(|l| l.contains("works once") || l.contains("signin-link")), "{bare:?}");
        let bare_placeholder = bare_notice(&one_time_link("0.0.0.0", 8799, None, code), true);
        assert!(bare_placeholder.iter().any(|l| l.contains("KWNT_ALLOWED_ORIGINS")));
        assert!(!bare_placeholder.iter().any(|l| l.contains('█')));
        assert_eq!(bare_notice("https://kwant.example.com/#kwnt_token=kl1.abc", false).len(), 1);
        // The API token itself is never printed at startup any more
        assert!(!real.iter().chain(&placeholder).any(|l| l.contains("kwnt token")));
        assert_eq!(token_command_link("0.0.0.0", 8799, "tok"), "http://<this-host>:8799/#kwnt_token=tok");
        assert_eq!(token_command_link("127.0.0.1", 8899, "tok"), "http://127.0.0.1:8899/#kwnt_token=tok");
    }

    #[test]
    fn a_session_cookie_counts_as_the_token() {
        let api = ApiToken::for_tests("abcdefghijklmnopqrstuvwxyz012345", Require::Auto, 3000);
        let secret = api.signin.create_session(Via::Link, &signin::Client::default());
        assert!(presented_token_ok(&api, &headers(&[("cookie", &format!("kwnt_token={secret}"))])));
        // Only as a cookie: a bearer that isn't the API token is an agent's
        assert!(!presented_token_ok(&api, &headers(&[("authorization", &format!("Bearer {secret}"))])));
        let id = api.signin.session_id(&secret).unwrap();
        api.signin.revoke(&id);
        assert!(!presented_token_ok(&api, &headers(&[("cookie", &format!("kwnt_token={secret}"))])));
    }

    #[test]
    fn read_existing_never_creates_a_token() {
        let dir = tempfile::tempdir().unwrap();
        assert!(matches!(read_existing(dir.path()), Err(ReadTokenError::Missing(_))));
        assert!(!dir.path().join(TOKEN_FILE).exists());

        std::fs::write(dir.path().join(TOKEN_FILE), "short\n").unwrap();
        assert!(matches!(read_existing(dir.path()), Err(ReadTokenError::Invalid(_))));
        assert_eq!(std::fs::read_to_string(dir.path().join(TOKEN_FILE)).unwrap(), "short\n");

        let made = load_or_create(dir.path());
        assert_eq!(read_existing(dir.path()).unwrap(), made);
    }

    #[test]
    fn token_file_is_private_and_stable() {
        let dir = tempfile::tempdir().unwrap();
        let a = load_or_create(dir.path());
        assert_eq!(a.len(), 64);
        assert_eq!(load_or_create(dir.path()), a);
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(dir.path().join(TOKEN_FILE))
                .unwrap()
                .permissions()
                .mode();
            assert_eq!(mode & 0o777, 0o600);
        }
    }
}