Cotton code
76 lines · 3.5 KB · RustRaw
//! Whether a connector needs signing in again after a key rotation — the ONE//! decision the Assistants list reads.//!//! Revoking a device rotates the workspace key, and every `cta_`/`ctr_` signed//! under the old version stops verifying (`crate::vault::tokens`). The consent//! stays: the grant row is live and the Connected AIs row is not revoked, so//! without this the list shows a connector that can no longer reach the//! server as working. The server says so to the connector itself//! (`cotton-server`'s `oauth::signed_out`); this says it on the person's screen.//!//! **THE RULE.** An OAuth connector (a live grant names its row) whose last//! sign of life — the grant's creation, its last refresh, the row's last use —//! is older than the workspace's last key rotation holds only tokens signed//! under a retired key. It needs signing in again, since the rotation. Any//! sign of life after the rotation clears it: that token was signed under the//! current key. A workspace still on its first key (`vmk_version` 1) has never//! rotated, whatever its `rotated_at` says.//!//! **A GRANT THAT OUTLIVES THE ROTATION IS NOT SIGNED OUT.** Since store//! version 45 a grant carries the digest of the refresh token its chain stands//! at, and a Mac admits that one token after a rotation//! (`cotton-server`'s `oauth::grant::refresh_across_rotation`) — but only when//! the grant also names the Mac that minted it. Such a connector refreshes//! itself on its next call, so it is never listed as needing sign-in. Builds//! before this rule (Mac 0.23.0, phone 212) still list it; that label is wrong//! there and harmless.//!//! PURE: strings in, an answer out. Nothing here reads a row or a clock.use chrono::{DateTime, NaiveDate, Utc};/// The facts one connector's answer is read from, as the file stores them.#[derive(Debug, Clone, Copy, Default)]pub struct ConnectorFacts<'a> {    /// The workspace key's current version and when it last rotated    /// (`cotton_vault_state`).    pub vmk_version: Option<i64>,    pub rotated_at: Option<&'a str>,    /// The live grant naming this Connected AIs row, if there is one    /// (`cotton_oauth_grants` with `revoked_at` NULL).    pub grant_created_at: Option<&'a str>,    pub grant_refreshed_at: Option<&'a str>,    /// The Connected AIs row (`cotton_mcp_tokens`).    pub token_revoked: bool,    pub token_last_used_at: Option<&'a str>,    /// The live grant carries a refresh digest AND names its minter    /// (`cotton_oauth_grants.refresh_digest` and `minted_by`, version 45), so    /// its current refresh token is admitted across a rotation.    pub grant_outlives_rotation: bool,}/// The day of the rotation that signed this connector out, or `None` when it/// does not need signing in again.pub fn needs_sign_in_since(facts: &ConnectorFacts<'_>) -> Option<NaiveDate> {    if facts.token_revoked || facts.grant_outlives_rotation || facts.vmk_version.unwrap_or(1) <= 1 {        return None;    }    let created = instant(facts.grant_created_at?)?;    let rotated = instant(facts.rotated_at?)?;    let last_sign_of_life = [facts.grant_refreshed_at, facts.token_last_used_at]        .into_iter()        .flatten()        .filter_map(instant)        .fold(created, DateTime::max);    (last_sign_of_life < rotated).then(|| rotated.date_naive())}fn instant(value: &str) -> Option<DateTime<Utc>> {    DateTime::parse_from_rfc3339(value)        .ok()        .map(|at| at.with_timezone(&Utc))}#[cfg(test)]#[path = "connector_sign_in_tests.rs"]mod tests;