Skip to main content

ic_auth/portable/
revocation.rs

1//! Bounded volatile revocation bookkeeping, not an authentication endpoint.
2
3use super::{VerifiedPortableClaims, browser_principal, validate_shape};
4use crate::admission_budget::{AdmissionBudgetError, SessionAdmissionBudget};
5use ic_auth_protocol_types::{AudienceId, PortableClaims, Principal};
6use std::collections::{BTreeMap, BTreeSet};
7use thiserror::Error;
8
9#[cfg(feature = "sessions")]
10const RECORD_DOMAIN: &[u8] = b"IC-AUTH-LOGIN-RECORD\0";
11#[cfg(feature = "sessions")]
12const MAX_RECORD_BYTES: usize = 1024;
13
14/// A refusal leaves records unchanged; trusted clock observations are retained.
15#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
16pub enum LoginRevocationError {
17    #[error("logout requires an authenticated self-authenticating caller")]
18    InvalidCaller,
19    #[error("invalid revocation limits or authenticated login deadline")]
20    InvalidPolicy,
21    #[error("login is locally revoked")]
22    Revoked,
23    #[error("revocation capacity is exhausted")]
24    Capacity,
25    #[error("revocation clock rolled back or retention arithmetic overflowed")]
26    Clock,
27    #[error("host session/replay transaction failed")]
28    Storage,
29}
30
31#[derive(Clone, Copy)]
32#[cfg_attr(feature = "sessions", derive(serde::Serialize))]
33#[cfg_attr(feature = "sessions", serde(into = "RecordFields"))]
34struct Record {
35    until_ns: u64,
36    state: LoginRecordState,
37}
38
39// A reservation always owns admitted capacity; a tombstone retains that pool
40// exactly when it retains an authenticated login binding.
41/// Ledger state projected without inferring state from independent flags.
42#[derive(Clone, Copy, Debug, Eq, PartialEq)]
43pub enum LoginRecordState {
44    Reserved(LoginBinding),
45    Revoked(Option<LoginBinding>),
46}
47
48impl Record {
49    fn login(self) -> Option<LoginBinding> {
50        match self.state {
51            LoginRecordState::Reserved(login) => Some(login),
52            LoginRecordState::Revoked(login) => login,
53        }
54    }
55
56    fn revoked(self) -> bool {
57        matches!(self.state, LoginRecordState::Revoked(_))
58    }
59
60    fn admitted_pool(self) -> bool {
61        self.login().is_some()
62    }
63}
64
65// The current storage fields and ordering are frozen independently of the
66// internal state representation. This is the sole record format, not a fallback.
67#[cfg(feature = "sessions")]
68#[derive(serde::Serialize)]
69#[cfg_attr(feature = "session-restore", derive(serde::Deserialize))]
70#[serde(deny_unknown_fields)]
71struct RecordFields {
72    until_ns: u64,
73    login: Option<LoginBinding>,
74    revoked: bool,
75    admitted_pool: bool,
76}
77
78#[cfg(feature = "sessions")]
79impl From<Record> for RecordFields {
80    fn from(record: Record) -> Self {
81        Self {
82            until_ns: record.until_ns,
83            login: record.login(),
84            revoked: record.revoked(),
85            admitted_pool: record.admitted_pool(),
86        }
87    }
88}
89
90#[derive(Clone, Copy, Debug, Eq, PartialEq)]
91#[cfg_attr(feature = "sessions", derive(serde::Serialize))]
92#[cfg_attr(feature = "session-restore", derive(serde::Deserialize))]
93#[cfg_attr(feature = "sessions", serde(deny_unknown_fields))]
94/// Authenticated login association retained by a reservation or known tombstone.
95/// This value alone is not authentication or admission evidence.
96pub struct LoginBinding {
97    pub issuer: Principal,
98    pub subject: Principal,
99    pub started_at_ns: u64,
100    pub deadline_ns: u64,
101}
102
103/// Proposed ledger record from a serialized commit callback or trusted storage.
104/// A durable host must persist this with its session/replay changes before
105/// returning success. This is not a credential or reusable authorization result.
106pub struct LoginRevocationChange {
107    audience: AudienceId,
108    caller: Principal,
109    record: Record,
110}
111
112impl LoginRevocationChange {
113    /// Encode the proposed record for the host's atomic durable commit.
114    #[cfg(feature = "sessions")]
115    pub fn encode(&self) -> Result<Vec<u8>, LoginRevocationError> {
116        crate::storage_codec::encode(
117            &(self.audience, self.caller, self.record),
118            RECORD_DOMAIN,
119            MAX_RECORD_BYTES,
120            LoginRevocationError::Storage,
121            LoginRevocationError::InvalidPolicy,
122        )
123    }
124
125    /// Decode trusted storage only; never accept these bytes from a client.
126    /// This checks shape, not provenance, completeness, rollback or live authority.
127    #[cfg(feature = "session-restore")]
128    pub fn decode(bytes: &[u8]) -> Result<Self, LoginRevocationError> {
129        let (audience, caller, fields): (_, Principal, RecordFields) =
130            crate::storage_codec::decode(
131                bytes,
132                RECORD_DOMAIN,
133                MAX_RECORD_BYTES,
134                || LoginRevocationError::InvalidPolicy,
135                LoginRevocationError::InvalidPolicy,
136            )?;
137        let caller_bytes = caller.as_slice();
138        if caller_bytes.len() != 29 || caller_bytes.last() != Some(&2) {
139            return Err(LoginRevocationError::InvalidCaller);
140        }
141        let state = match fields.login {
142            Some(login)
143                if fields.admitted_pool
144                    && login.issuer != Principal::anonymous()
145                    && login.subject != Principal::anonymous()
146                    && login.started_at_ns < login.deadline_ns
147                    && login.deadline_ns - login.started_at_ns <= crate::limits::MAX_LOGIN_NS =>
148            {
149                if fields.revoked {
150                    LoginRecordState::Revoked(Some(login))
151                } else {
152                    LoginRecordState::Reserved(login)
153                }
154            }
155            None if !fields.admitted_pool && fields.revoked => LoginRecordState::Revoked(None),
156            _ => return Err(LoginRevocationError::InvalidPolicy),
157        };
158        let change = Self {
159            audience,
160            caller,
161            record: Record {
162                until_ns: fields.until_ns,
163                state,
164            },
165        };
166        Ok(change)
167    }
168
169    /// Protected trust domain owning the record.
170    pub fn audience(&self) -> AudienceId {
171        self.audience
172    }
173    /// Authenticated runtime caller whose record changes.
174    pub fn caller(&self) -> Principal {
175        self.caller
176    }
177    /// Exclusive retention deadline, including the configured clock margin.
178    pub fn until_ns(&self) -> u64 {
179        self.record.until_ns
180    }
181    /// Typed state from this checked ledger record. Storage codecs retain their
182    /// existing frozen representation independently of this Rust projection.
183    pub fn state(&self) -> LoginRecordState {
184        self.record.state
185    }
186}
187
188/// Bounded reference ledger for one host-installed trust domain. Callers are
189/// runtime principals, never request fields; this ledger cannot authenticate them.
190/// One admitted slot survives lease expiry until the login deadline plus skew.
191/// Unknown callers have a separate pool and cannot consume admitted capacity.
192///
193/// The host MUST serialize these operations with proof/session/replay state and
194/// authority changes. Commit callbacks receive the exact proposed login record
195/// and must atomically write it with that other state in durable backends, or
196/// return an error without effects. No await/reentrancy may split the operation.
197/// This ledger commits its staged record only after callback success, with no
198/// further fallible policy/storage checks. Callbacks must not return success
199/// before their commit, or commit and then return an error.
200///
201/// This is not portable admission or a logout endpoint. No proof verification,
202/// startup/enrollment readiness or durable storage is supplied here. Hosts MUST
203/// apply those gates and a qualified clock-skew bound separately. Fresh unknown
204/// revocations consume an explicit host-retained budget before the commit callback.
205/// Losing this ledger requires the protocol's fresh login/reset floors; restoring
206/// sessions alone is unsafe. Never recreate it per request or share across domains.
207pub struct MemoryLoginRevocations {
208    audience: AudienceId,
209    records: BTreeMap<Principal, Record>,
210    expiry: BTreeSet<(u64, Principal)>,
211    max_admitted: usize,
212    max_unknown: usize,
213    admitted: usize,
214    unknown: usize,
215    skew_ns: u64,
216    observed_ns: u64,
217}
218
219impl MemoryLoginRevocations {
220    /// Stream the complete physical ledger under a serialized host checkpoint.
221    /// Persist these records with occupancy, clock and session/replay/authority
222    /// state atomically; expired entries count until explicit pruning.
223    #[cfg(feature = "session-restore")]
224    pub fn records(&self) -> impl ExactSizeIterator<Item = LoginRevocationChange> + '_ {
225        self.records
226            .iter()
227            .map(|(&caller, &record)| LoginRevocationChange {
228                audience: self.audience,
229                caller,
230                record,
231            })
232    }
233
234    /// Rebuild a fresh ledger from a complete, trusted atomic storage generation.
235    /// `self` must be newly constructed at current trusted time. The host supplies
236    /// that generation's persisted clock high-water mark and physical pool counts,
237    /// including expired records, from the same transaction as the records.
238    /// Failure consumes the partial ledger; no usable partial state is returned.
239    /// Counts detect missing entries, not malicious rollback: hosts MUST protect
240    /// snapshot provenance/generation and restore reset/authority/session/replay
241    /// state together, keeping admission closed until fresh enrollment is checked.
242    #[cfg(feature = "session-restore")]
243    pub fn restore_records(
244        mut self,
245        observed_ns: u64,
246        expected_counts: (usize, usize),
247        records: impl IntoIterator<Item = LoginRevocationChange>,
248    ) -> Result<Self, LoginRevocationError> {
249        if !self.records.is_empty()
250            || observed_ns > self.observed_ns
251            || expected_counts.0 > self.max_admitted
252            || expected_counts.1 > self.max_unknown
253        {
254            return Err(LoginRevocationError::InvalidPolicy);
255        }
256        let max_until = retention(observed_ns, self.skew_ns)?;
257        for change in records {
258            if change.audience != self.audience || self.records.contains_key(&change.caller) {
259                return Err(LoginRevocationError::InvalidPolicy);
260            }
261            let record = change.record;
262            if record.until_ns == 0 || record.until_ns > max_until {
263                return Err(LoginRevocationError::InvalidPolicy);
264            }
265            if let Some(login) = record.login() {
266                let minimum = login
267                    .deadline_ns
268                    .checked_add(self.skew_ns)
269                    .ok_or(LoginRevocationError::Clock)?;
270                if login.started_at_ns > observed_ns
271                    || record.until_ns < minimum
272                    || (!record.revoked() && record.until_ns != minimum)
273                {
274                    return Err(LoginRevocationError::InvalidPolicy);
275                }
276            }
277            // Stop at the persisted counts, even for an unbounded input iterator.
278            let (used, expected) = if record.admitted_pool() {
279                (self.admitted, expected_counts.0)
280            } else {
281                (self.unknown, expected_counts.1)
282            };
283            if used >= expected {
284                return Err(LoginRevocationError::Capacity);
285            }
286            self.install(change.caller, record);
287        }
288        if self.occupancy() != expected_counts {
289            return Err(LoginRevocationError::InvalidPolicy);
290        }
291        Ok(self)
292    }
293
294    /// Fixed record counts bound memory; no caller-supplied blobs are retained.
295    /// `skew_ns` is deployment-qualified protected configuration, not a default.
296    pub fn new(
297        audience: AudienceId,
298        max_admitted: usize,
299        max_unknown: usize,
300        skew_ns: u64,
301        now_ns: u64,
302    ) -> Result<Self, LoginRevocationError> {
303        if max_admitted == 0 || max_unknown == 0 {
304            return Err(LoginRevocationError::InvalidPolicy);
305        }
306        retention(now_ns, skew_ns)?;
307        Ok(Self {
308            audience,
309            records: BTreeMap::new(),
310            expiry: BTreeSet::new(),
311            max_admitted,
312            max_unknown,
313            admitted: 0,
314            unknown: 0,
315            skew_ns,
316            observed_ns: now_ns,
317        })
318    }
319
320    #[cfg(feature = "sessions")]
321    pub(crate) fn audience(&self) -> AudienceId {
322        self.audience
323    }
324
325    /// A restored session requires its matching live reservation, not just absence
326    /// of a tombstone. Losing the ledger must never revive a retained session.
327    #[cfg(feature = "sessions")]
328    pub(crate) fn check_session(
329        &mut self,
330        caller: Principal,
331        issuer: Principal,
332        subject: Principal,
333        started_at_ns: u64,
334        deadline_ns: u64,
335        now_ns: u64,
336    ) -> Result<(), LoginRevocationError> {
337        self.observe(caller, now_ns)?;
338        let record = self
339            .records
340            .get(&caller)
341            .filter(|record| record.until_ns > now_ns)
342            .ok_or(LoginRevocationError::InvalidPolicy)?;
343        if record.revoked() {
344            return Err(LoginRevocationError::Revoked);
345        }
346        if record.login()
347            != Some(LoginBinding {
348                issuer,
349                subject,
350                started_at_ns,
351                deadline_ns,
352            })
353        {
354            return Err(LoginRevocationError::InvalidPolicy);
355        }
356        Ok(())
357    }
358
359    /// Physical pool occupancy, including expired entries until bounded pruning.
360    pub fn occupancy(&self) -> (usize, usize) {
361        (self.admitted, self.unknown)
362    }
363
364    /// Clock high-water mark for an atomic host checkpoint, including refusals.
365    /// Durable hosts retain this with pool counts and the complete record set.
366    pub fn observed_at_ns(&self) -> u64 {
367        self.observed_ns
368    }
369
370    /// Cheap refusal before proof verification. This does not authenticate claims,
371    /// reserve capacity or permit a session commit; success is only permission to
372    /// continue checking. Untrusted claims can cause refusal, never admission.
373    /// Records and pool occupancy stay unchanged; trusted clock observations remain.
374    /// The host must serialize this check, verification and `with_reservation`
375    /// with authority/session/replay changes. The final reservation rechecks state.
376    pub fn check_reservation(
377        &mut self,
378        caller: Principal,
379        claims: &PortableClaims,
380        now_ns: u64,
381    ) -> Result<(), LoginRevocationError> {
382        self.reservation(caller, claims, now_ns).map(|_| ())
383    }
384
385    /// Reserve before committing a fully verified admission's session/replay pair.
386    /// The issuer/account and original login window stay unchanged on renewal.
387    /// Verified claims must match this ledger domain, caller and current clock.
388    /// The host still enforces current authority and replay under the same lock.
389    /// A live tombstone rejects even an exact admission retry before its callback.
390    /// No reservation is installed if session/replay storage in `commit` fails.
391    /// The callback receives the proposed record for that same atomic commit.
392    /// Its error is returned unchanged; `E` also carries ledger refusals through
393    /// `From<LoginRevocationError>`. No separate storage-error channel is needed.
394    pub fn with_reservation<T, E: From<LoginRevocationError>>(
395        &mut self,
396        caller: Principal,
397        verified: &VerifiedPortableClaims,
398        now_ns: u64,
399        commit: impl FnOnce(&LoginRevocationChange) -> Result<T, E>,
400    ) -> Result<T, E> {
401        let record = self.reservation(caller, verified.claims(), now_ns)?;
402        if verified.verified_at_ns() != now_ns {
403            return Err(LoginRevocationError::InvalidPolicy.into());
404        }
405        let value = commit(&LoginRevocationChange {
406            audience: self.audience,
407            caller,
408            record,
409        })?;
410        self.install(caller, record);
411        Ok(value)
412    }
413
414    /// Stage local denial, then atomically remove this caller's host sessions in
415    /// `commit`. Return the denial deadline only after that callback commits.
416    /// An existing live tombstone keeps its deadline; the callback still runs so
417    /// retry never falsely acknowledges a failed host operation. No proof needed.
418    /// An admitted reservation converts in its own pool even if unknowns fill theirs.
419    /// A caller without a live record consumes one unknown-logout budget credit
420    /// before the callback; exhaustion returns Capacity, never false success.
421    /// Keep that receiver-wide budget separate from fresh proof verification so
422    /// unknown logout traffic cannot spend admission credit. Failure retains the
423    /// charge; hosts must return normal errors rather than trap/roll back credit.
424    /// Live reservations and tombstone retries do not compete for this budget.
425    /// The callback receives the proposed tombstone, including retained binding.
426    /// Its error is returned unchanged; `E` also carries ledger refusals through
427    /// `From<LoginRevocationError>`. A failed callback never installs denial.
428    pub fn with_revocation<E: From<LoginRevocationError>>(
429        &mut self,
430        unknown_budget: &mut SessionAdmissionBudget,
431        caller: Principal,
432        now_ns: u64,
433        commit: impl FnOnce(&LoginRevocationChange) -> Result<(), E>,
434    ) -> Result<u64, E> {
435        self.observe(caller, now_ns)?;
436        let old = self
437            .records
438            .get(&caller)
439            .copied()
440            .filter(|r| r.until_ns > now_ns);
441        let record = if let Some(old) = old.filter(|r| r.revoked()) {
442            old
443        } else {
444            let until_ns = retention(now_ns, self.skew_ns)?;
445            if old.is_some_and(|r| r.until_ns > until_ns) {
446                return Err(LoginRevocationError::Clock.into());
447            }
448            Record {
449                until_ns,
450                state: LoginRecordState::Revoked(old.and_then(Record::login)),
451            }
452        };
453        self.capacity(caller, record)?;
454        if old.is_none() {
455            unknown_budget
456                .consume(now_ns)
457                .map_err(|error| match error {
458                    AdmissionBudgetError::Exhausted => LoginRevocationError::Capacity,
459                    AdmissionBudgetError::ClockRollback => LoginRevocationError::Clock,
460                    AdmissionBudgetError::InvalidPolicy => LoginRevocationError::InvalidPolicy,
461                })?;
462        }
463        commit(&LoginRevocationChange {
464            audience: self.audience,
465            caller,
466            record,
467        })?;
468        self.install(caller, record);
469        Ok(record.until_ns)
470    }
471
472    /// Consult local denial before any session use, including exact retries.
473    /// Absence is not authentication or authorization; other gates still apply.
474    pub fn is_revoked(
475        &mut self,
476        caller: Principal,
477        now_ns: u64,
478    ) -> Result<bool, LoginRevocationError> {
479        self.observe(caller, now_ns)?;
480        Ok(self
481            .records
482            .get(&caller)
483            .is_some_and(|r| r.revoked() && r.until_ns > now_ns))
484    }
485
486    /// Explicit maintenance, at most 128 expired entries; never evict live state.
487    /// The host clock must be monotonic, including across failed operations.
488    pub fn prune(&mut self, now_ns: u64, budget: usize) -> Result<usize, LoginRevocationError> {
489        self.prune_with_commit(now_ns, budget, |_, _| Ok(()))
490    }
491    /// Commit bounded expired-login deletion and resulting admitted/unknown counts
492    /// before heap changes. Empty work skips the callback. Failure preserves all
493    /// records but retains the trusted clock observation, as other refusals do.
494    /// Hosts persist clock/authority metadata separately even when no rows expire.
495    pub fn prune_with_commit(
496        &mut self,
497        now_ns: u64,
498        budget: usize,
499        commit: impl FnOnce(&[Principal], (usize, usize)) -> Result<(), LoginRevocationError>,
500    ) -> Result<usize, LoginRevocationError> {
501        self.clock(now_ns)?;
502        let callers: Vec<_> = self
503            .expiry
504            .iter()
505            .take_while(|(until, _)| *until <= now_ns)
506            .take(budget.min(128))
507            .map(|(_, caller)| *caller)
508            .collect();
509        if callers.is_empty() {
510            return Ok(0);
511        }
512        let mut counts = self.occupancy();
513        for caller in &callers {
514            if self.records[caller].admitted_pool() {
515                counts.0 -= 1;
516            } else {
517                counts.1 -= 1;
518            }
519        }
520        commit(&callers, counts)?;
521        for caller in &callers {
522            self.remove(*caller);
523        }
524        Ok(callers.len())
525    }
526
527    fn reservation(
528        &mut self,
529        caller: Principal,
530        claims: &PortableClaims,
531        now_ns: u64,
532    ) -> Result<Record, LoginRevocationError> {
533        self.observe(caller, now_ns)?;
534        validate_shape(claims).map_err(|_| LoginRevocationError::InvalidPolicy)?;
535        let login_deadline_ns = claims.login_deadline_ns;
536        if claims.audience != self.audience
537            || browser_principal(&claims.browser_public_key) != caller
538        {
539            return Err(LoginRevocationError::InvalidPolicy);
540        }
541        let until_ns = login_deadline_ns
542            .checked_add(self.skew_ns)
543            .ok_or(LoginRevocationError::Clock)?;
544        let login = LoginBinding {
545            issuer: claims.issuer,
546            subject: claims.account_subject,
547            started_at_ns: claims.login_started_at_ns,
548            deadline_ns: login_deadline_ns,
549        };
550        if let Some(old) = self.records.get(&caller).filter(|r| r.until_ns > now_ns) {
551            if old.revoked() {
552                return Err(LoginRevocationError::Revoked);
553            }
554            if old.login() != Some(login) {
555                return Err(LoginRevocationError::InvalidPolicy);
556            }
557        }
558        let record = Record {
559            until_ns,
560            state: LoginRecordState::Reserved(login),
561        };
562        self.capacity(caller, record)?;
563        Ok(record)
564    }
565
566    fn clock(&mut self, now_ns: u64) -> Result<(), LoginRevocationError> {
567        if now_ns < self.observed_ns {
568            return Err(LoginRevocationError::Clock);
569        }
570        self.observed_ns = now_ns;
571        Ok(())
572    }
573
574    fn observe(&mut self, caller: Principal, now_ns: u64) -> Result<(), LoginRevocationError> {
575        let bytes = caller.as_slice();
576        if bytes.len() != 29 || bytes.last() != Some(&2) {
577            return Err(LoginRevocationError::InvalidCaller);
578        }
579        self.clock(now_ns)
580    }
581
582    fn capacity(&self, caller: Principal, record: Record) -> Result<(), LoginRevocationError> {
583        let reuses_pool = self
584            .records
585            .get(&caller)
586            .is_some_and(|old| old.admitted_pool() == record.admitted_pool());
587        let (count, limit) = if record.admitted_pool() {
588            (self.admitted, self.max_admitted)
589        } else {
590            (self.unknown, self.max_unknown)
591        };
592        if !reuses_pool && count >= limit {
593            return Err(LoginRevocationError::Capacity);
594        }
595        Ok(())
596    }
597
598    fn remove(&mut self, caller: Principal) {
599        if let Some(old) = self.records.remove(&caller) {
600            self.expiry.remove(&(old.until_ns, caller));
601            if old.admitted_pool() {
602                self.admitted -= 1;
603            } else {
604                self.unknown -= 1;
605            }
606        }
607    }
608
609    fn install(&mut self, caller: Principal, record: Record) {
610        self.remove(caller);
611        if record.admitted_pool() {
612            self.admitted += 1;
613        } else {
614            self.unknown += 1;
615        }
616        self.expiry.insert((record.until_ns, caller));
617        self.records.insert(caller, record);
618    }
619}
620
621fn retention(now_ns: u64, skew_ns: u64) -> Result<u64, LoginRevocationError> {
622    skew_ns
623        .checked_mul(2)
624        .and_then(|skew| crate::limits::MAX_LOGIN_NS.checked_add(skew))
625        .and_then(|ttl| now_ns.checked_add(ttl))
626        .ok_or(LoginRevocationError::Clock)
627}