Cotton code
96 lines · 5.0 KB · RustRaw
//! What a presented refresh generation is allowed to be — the ONE decision,//! shared by every Mac of a household.//!//! A refresh token is signed rather than stored//! ([`crate::vault::tokens`]), so the only state behind it is the grant row in//! the person's own file: `refresh_generation`, the chain's position, and//! `refresh_rotated_at`, the instant it last moved. Both travel. Both settle a//! merge by `Policy::MaxNonNull` rather than by the clock, so the highest//! generation anybody reached is the one every device ends on and a Mac that//! had not heard cannot wind the chain back.//!//! **TWO ANSWERS, and each one is a different thing that happened.**//!//! | presented | verdict | why |//! |---|---|---|//! | `= current` | [`Verdict::Rotate`] | the ordinary refresh |//! | `> current` | [`Verdict::Rotate`] | this Mac's copy of the row is behind — another Mac advanced the chain and the op has not folded yet. The token is MAC-verified, so only a Mac holding the workspace key can have minted it |//! | `< current` | [`Verdict::Replay`] | a token that was already spent is in circulation. The family dies — unless it is a RETRY (below) |//!//! **A RETRY IS NOT A REPLAY.** A client that sent generation `n`, whose//! answer carrying `n + 1` was lost on a slow link, sends `n` again. Revoking//! the grant for that signed the owner out (coordinator item 8). So a//! [`Verdict::Replay`] of EXACTLY the predecessor, while the chain still stands//! at `n + 1` and inside [`RETRY_WINDOW_MS`] of the rotation, may be answered//! with the very pair the lost answer carried — [`retry_window_open`] is that//! arithmetic. It is only the NUMBERS: the server honours the retry only when//! it can also re-derive the successor and prove it is the one the chain//! stands at, and when no device was revoked and no key rotated since//! (`cotton_server::oauth::grant`). A presentation after `n + 1` has itself//! been spent — the chain at `n + 2` — or outside the window is reuse, and the//! family dies (OAuth 2.1 §4.3.1).//!//! The successor of generation `g` is a pure function of the grant, `g + 1`//! and the token presented at `g` (whose bytes seed the successor's nonce —//! [`crate::vault::tokens::successor_nonce`]), so two presentations of one//! token — in flight together, or a retry — converge on ONE successor instead//! of forking into two lineages.//!//! PURE: numbers in, a verdict out. Nothing here reads a row, a clock or a key./// What to do about a presented refresh generation.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Verdict {    /// Advance the chain to `next` and mint that generation's token.    Rotate { next: u32 },    /// A token that was already spent is in circulation. Revoke the grant —    /// unless [`retry_window_open`] and the server proves a retry.    Replay,}/// How long after a rotation the predecessor, presented again, may be a/// RETRY rather than a replay.////// Sixty seconds: a client re-sends a refresh whose answer was lost within/// its own request timeout, which is seconds; a token turning up later than a/// minute is a token somebody kept. It is also the bound on how long a stolen/// predecessor could share the live successor — the window exists for the/// lost answer, and is no longer than that needs.pub const RETRY_WINDOW_MS: i64 = 60_000;/// Whether presenting `presented` against a chain at `current` that last/// moved at `rotated_at_ms` is, by the numbers, a retry of that very rotation:/// the predecessor EXACTLY, and `now_ms` within [`RETRY_WINDOW_MS`] of the/// rotation on EITHER side — the stamp is another Mac's clock, and a stamp/// wound forward must not hold the window open (it closes a minute past it).pub fn retry_window_open(current: u32, presented: u32, rotated_at_ms: i64, now_ms: i64) -> bool {    current != u32::MAX        && presented.checked_add(1) == Some(current)        && now_ms.abs_diff(rotated_at_ms) <= RETRY_WINDOW_MS.unsigned_abs()}/// The decision, given where the row says the chain stands and what was/// presented.pub fn judge(current: u32, presented: u32) -> Verdict {    // **THE CEILING IS A DEAD END, NOT A RESTING PLACE.** Saturating at    // `u32::MAX` used to look like the safe direction, and it is the opposite:    // the chain would stop advancing while the token for `MAX` stayed a pure    // function of the grant, so that one string could be replayed for ever and    // every legitimate lower generation read as reuse — replay detection off,    // permanently, with `MaxNonNull` making it unrecoverable. Four billion    // refreshes of one consent is not a thing that happens; a row that says so    // is a row somebody wrote, and the grant dies rather than going quiet.    if presented == u32::MAX || current == u32::MAX {        return Verdict::Replay;    }    if presented >= current {        return Verdict::Rotate {            next: presented + 1,        };    }    Verdict::Replay}#[cfg(test)]#[path = "oauth_grants_tests.rs"]mod oauth_grants_tests;