//! The two headers on every request: who this device is, and its signature.//!//! **There is no third spelling of the signed string here.** The seven//! newline-joined fields are `cotton_core::device::signature`'s, which is//! itself held byte-for-byte against the server's own `device_auth.rs` by//! `signature_tests.rs`. This module composes them, mints the nonce, reads the//! clock — and that is the whole of it.//!//! Three rules the composition carries, each of which is a defence://!//! * **A read carries an EMPTY nonce; a write carries 22 characters.** The//! server enforces exactly that (`NonceRule::None`), because it keeps a//! replay table for writes and none for reads.//! * **The clock is the SERVER's, per origin.** A phone whose own clock is//! two minutes out signs a timestamp the server refuses; it learns the//! offset from a `403 device_clock` and signs from then on with the server's//! idea of now. The offset is per origin: a Mac with a bad clock must not//! stop the phone talking to a relay.//! * **The body is hashed AS SENT.** The bytes handed to the socket, never a//! re-serialisation — two JSON encoders agreeing on a value do not agree on//! its bytes.use std::sync::Arc;use cotton_core::device::signature::{ device_signed_message, format_device_sig_header, DeviceSignedInput, DEVICE_HEADER, DEVICE_SIG_HEADER,};usecrate::identity::DeviceIdentity;usecrate::seams::{Clock, Random};/// How many random bytes a write's nonce is. 16 → 22 base64url characters,/// which is the length the server's grammar admits.pubconst NONCE_BYTES: usize = 16;/// What a request needs signed.#[derive(Debug, Clone, Copy)]pubstructSignRequest<'a> {/// The origin as it is stored and as the server rebuilds it — no trailing/// slash.pub origin: &'a str,pub method: &'a str,/// Path plus query, already in the shape [`signed_path`] answers.pub path: &'a str,pub body: Option<&'a [u8]>,}/// The signer, holding this device's key and the two seams a signature needs.#[derive(Clone)]pubstructRequestSigner { identity: Arc<DeviceIdentity>, clock: Arc<dynClock>, random: Arc<dynRandom>,}impl std::fmt::DebugforRequestSigner {fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("RequestSigner") .field("device_id", &self.identity.device_id()) .finish_non_exhaustive() }}implRequestSigner {pubfn new( identity: Arc<DeviceIdentity>, clock: Arc<dynClock>, random: Arc<dynRandom>, ) -> RequestSigner {RequestSigner { identity, clock, random, } }pubfn device_id(&self) -> String {self.identity.device_id() }/// The two headers, given the offset THIS origin's clock is at.pubfn headers( &self, request: &SignRequest<'_>, clock_offset_ms: i64, ) -> [(&'static str, String); 2] {let ts = (self.clock.now().timestamp_millis() + clock_offset_ms).div_euclid(1_000);let nonce = if is_read(request.method) {String::new() } else {self.nonce() };let message = device_signed_message(&DeviceSignedInput { origin: request.origin, method: request.method, path: request.path, ts, nonce: &nonce, body: request.body, });let sig = self.identity.sign(&message); [ (DEVICE_HEADER, self.identity.device_id()), ( DEVICE_SIG_HEADER, format_device_sig_header(ts, &nonce, &sig), ), ] }fn nonce(&self) -> String {letmut bytes = [0u8; NONCE_BYTES];self.random.fill(&mut bytes); cotton_core::device::base64url::base64url_encode(&bytes) }}/// A read signs no nonce. The server refuses a GET that carries one, so this/// is grammar rather than economy.pubfn is_read(method: &str) -> bool { method.eq_ignore_ascii_case("GET") || method.eq_ignore_ascii_case("HEAD")}#[cfg(test)]#[path = "signer_tests.rs"]mod tests;