1use super::{VerifiedPortableClaims, browser_principal, is_login_caller, 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 MAX_RECORD_BYTES: usize = 1024;
11
12#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
14pub enum LoginRevocationError {
15 #[error("logout requires an authenticated self-authenticating caller")]
16 InvalidCaller,
18 #[error("invalid revocation limits or authenticated login deadline")]
19 InvalidPolicy,
21 #[error("login is locally revoked")]
22 Revoked,
24 #[error("revocation capacity is exhausted")]
25 Capacity,
27 #[error("revocation clock rolled back or retention arithmetic overflowed")]
28 Clock,
30 #[error("host session/replay transaction failed")]
31 Storage,
33}
34
35#[derive(Clone, Copy)]
36#[cfg_attr(feature = "sessions", derive(serde::Serialize))]
37#[cfg_attr(feature = "session-restore", derive(serde::Deserialize))]
38struct Record {
39 until_ns: u64,
40 state: LoginRecordState,
41}
42
43#[derive(Clone, Copy, Debug, Eq, PartialEq)]
47#[cfg_attr(feature = "sessions", derive(serde::Serialize))]
48#[cfg_attr(feature = "session-restore", derive(serde::Deserialize))]
49pub enum LoginRecordState {
50 Reserved(LoginBinding),
52 Revoked(Option<LoginBinding>),
54}
55
56impl Record {
57 #[cfg(feature = "session-restore")]
58 fn check_stored(
59 &self,
60 observed_ns: u64,
61 max_until: u64,
62 maximum_ttl: u64,
63 devices: u8,
64 ) -> Result<(), LoginRevocationError> {
65 if self.until_ns == 0 || self.until_ns > max_until {
66 return Err(LoginRevocationError::InvalidPolicy);
67 }
68 if let Some(login) = self.login()
69 && (login
70 .deadline_ns
71 .checked_sub(login.started_at_ns)
72 .is_none_or(|ttl| ttl == 0 || ttl > maximum_ttl)
73 || login.device_slot >= devices
74 || login.started_at_ns > observed_ns
75 || self.until_ns < login.deadline_ns
76 || (!self.revoked() && self.until_ns != login.deadline_ns))
77 {
78 return Err(LoginRevocationError::InvalidPolicy);
79 }
80 Ok(())
81 }
82 #[cfg(feature = "sessions")]
83 fn live_binding(&self, now_ns: u64) -> Result<Option<LoginBinding>, LoginRevocationError> {
84 if self.until_ns <= now_ns {
85 return Ok(None);
86 }
87 if self.revoked() {
88 return Err(LoginRevocationError::Revoked);
89 }
90 Ok(self.login())
91 }
92 fn login(&self) -> Option<LoginBinding> {
93 match &self.state {
94 LoginRecordState::Reserved(login) => Some(*login),
95 LoginRecordState::Revoked(login) => *login,
96 }
97 }
98
99 fn revoked(&self) -> bool {
100 matches!(self.state, LoginRecordState::Revoked(_))
101 }
102
103 fn admitted_pool(&self) -> bool {
104 matches!(
105 self.state,
106 LoginRecordState::Reserved(_) | LoginRecordState::Revoked(Some(_))
107 )
108 }
109}
110
111#[derive(Clone, Copy, Debug, Eq, PartialEq)]
112#[cfg_attr(feature = "sessions", derive(serde::Serialize))]
113#[cfg_attr(feature = "session-restore", derive(serde::Deserialize))]
114#[cfg_attr(feature = "sessions", serde(deny_unknown_fields))]
115pub struct LoginBinding {
118 pub issuer: Principal,
120 pub subject: Principal,
122 pub device_slot: u8,
124 pub device_generation: u64,
126 pub started_at_ns: u64,
128 pub deadline_ns: u64,
130}
131
132pub struct LoginRevocationChange(LoginData, Option<Principal>);
136
137#[cfg_attr(feature = "sessions", derive(serde::Serialize))]
138#[cfg_attr(feature = "session-restore", derive(serde::Deserialize))]
139#[cfg_attr(feature = "sessions", serde(deny_unknown_fields))]
140struct LoginData {
141 #[cfg_attr(feature = "sessions", serde(with = "crate::storage_codec::audience"))]
142 audience: AudienceId,
143 caller: Principal,
144 record: Record,
145}
146
147impl LoginRevocationChange {
148 #[cfg(feature = "persistent")]
151 pub(crate) fn from_session(session: &crate::session::Session) -> Self {
152 let binding = session.login_binding();
153 Self(
154 LoginData {
155 audience: session.audience(),
156 caller: session.caller(),
157 record: Record {
158 until_ns: binding.deadline_ns,
159 state: LoginRecordState::Reserved(binding),
160 },
161 },
162 None,
163 )
164 }
165 #[cfg(feature = "persistent")]
166 pub(crate) fn check_stored(
167 &self,
168 observed_ns: u64,
169 maximum_ttl: u64,
170 devices: u8,
171 ) -> Result<(), LoginRevocationError> {
172 self.0.record.check_stored(
173 observed_ns,
174 retention(observed_ns, maximum_ttl)?,
175 maximum_ttl,
176 devices,
177 )
178 }
179 #[cfg(feature = "persistent")]
180 pub(crate) fn live_binding(
181 &self,
182 now_ns: u64,
183 ) -> Result<Option<LoginBinding>, LoginRevocationError> {
184 self.0.record.live_binding(now_ns)
185 }
186 pub fn check_replacement(
193 &self,
194 previous: &Self,
195 now_ns: u64,
196 ) -> Result<(), LoginRevocationError> {
197 if self.0.audience != previous.0.audience {
198 return Err(LoginRevocationError::InvalidPolicy);
199 }
200 if let Some(old) = previous.0.record.login() {
201 let new = self
202 .0
203 .record
204 .login()
205 .ok_or(LoginRevocationError::InvalidPolicy)?;
206 if slot(old) != slot(new) || new.device_generation < old.device_generation {
207 return Err(LoginRevocationError::InvalidPolicy);
208 }
209 if new.device_generation == old.device_generation {
210 if new != old
211 || self.caller() != previous.caller()
212 || (previous.0.record.revoked() && !self.0.record.revoked())
213 || self.until_ns() < previous.until_ns()
214 {
215 return Err(LoginRevocationError::InvalidPolicy);
216 }
217 } else if !matches!(self.state(), LoginRecordState::Reserved(_))
218 || self.caller() == previous.caller()
219 {
220 return Err(LoginRevocationError::InvalidPolicy);
221 }
222 } else if self.caller() != previous.caller()
223 || (previous.until_ns() > now_ns
224 && (self.0.record.login().is_some()
225 || !self.0.record.revoked()
226 || self.until_ns() < previous.until_ns()))
227 {
228 return Err(LoginRevocationError::InvalidPolicy);
229 }
230 Ok(())
231 }
232 #[cfg(feature = "sessions")]
234 pub fn encode(&self) -> Result<Vec<u8>, LoginRevocationError> {
235 crate::storage_codec::encode(&self.0, crate::storage_codec::LOGIN, MAX_RECORD_BYTES)
236 .map_err(|error| match error {
237 ic_wire::Error::Limit | ic_wire::Error::Allocation => {
238 LoginRevocationError::InvalidPolicy
239 }
240 _ => LoginRevocationError::Storage,
241 })
242 }
243
244 #[cfg(feature = "session-restore")]
247 pub fn decode(bytes: &[u8]) -> Result<Self, LoginRevocationError> {
248 let data: LoginData =
249 crate::storage_codec::decode(bytes, crate::storage_codec::LOGIN, MAX_RECORD_BYTES)
250 .map_err(|_| LoginRevocationError::InvalidPolicy)?;
251 if !is_login_caller(data.caller) {
252 return Err(LoginRevocationError::InvalidCaller);
253 }
254 if let Some(login) = data.record.login()
255 && (login.issuer == Principal::anonymous()
256 || login.subject == Principal::anonymous()
257 || login.device_slot >= ic_auth_protocol_types::limits::MAX_DEVICE_SLOTS
258 || login.device_generation == 0
259 || login.started_at_ns >= login.deadline_ns
260 || login.deadline_ns - login.started_at_ns > crate::limits::MAX_LOGIN_NS)
261 {
262 return Err(LoginRevocationError::InvalidPolicy);
263 }
264 Ok(Self(data, None))
265 }
266
267 #[must_use]
269 pub fn audience(&self) -> AudienceId {
270 self.0.audience
271 }
272 #[must_use]
274 pub fn caller(&self) -> Principal {
275 self.0.caller
276 }
277 #[must_use]
280 pub fn displaced_caller(&self) -> Option<Principal> {
281 self.1
282 }
283 #[must_use]
285 pub fn until_ns(&self) -> u64 {
286 self.0.record.until_ns
287 }
288 #[must_use]
291 pub fn state(&self) -> LoginRecordState {
292 self.0.record.state
293 }
294}
295
296pub struct MemoryLoginRevocations {
319 audience: AudienceId,
320 records: BTreeMap<Principal, Record>,
321 accounts: BTreeMap<(Principal, Principal, u8), Principal>,
324 expiry: BTreeSet<(u64, Principal)>,
325 max_admitted: usize,
326 max_device_slots: u8,
327 max_unknown: usize,
328 admitted: usize,
329 unknown: usize,
330 max_login_ttl_ns: u64,
331 observed_ns: u64,
332}
333
334impl MemoryLoginRevocations {
335 #[cfg(feature = "persistent")]
339 pub(crate) fn selected(
340 audience: AudienceId,
341 limits: (usize, usize, u64, u8),
342 now_ns: u64,
343 counts: (usize, usize),
344 records: Vec<LoginRevocationChange>,
345 ) -> Result<Self, LoginRevocationError> {
346 if counts.0 > limits.0 || counts.1 > limits.1 || records.len() > 2 {
347 return Err(LoginRevocationError::InvalidPolicy);
348 }
349 let admitted = records
350 .iter()
351 .filter(|row| row.0.record.admitted_pool())
352 .count();
353 let unknown = records.len() - admitted;
354 if admitted > counts.0 || unknown > counts.1 {
355 return Err(LoginRevocationError::InvalidPolicy);
356 }
357 let mut snapshot = Self::new(audience, limits.0, limits.1, limits.2, now_ns)?
358 .with_device_limit(limits.3)?
359 .restore_records(now_ns, (admitted, unknown), records)?;
360 snapshot.admitted = counts.0;
361 snapshot.unknown = counts.1;
362 Ok(snapshot)
363 }
364 #[cfg(feature = "session-restore")]
368 pub fn records(&self) -> impl ExactSizeIterator<Item = LoginRevocationChange> + '_ {
369 self.records.iter().map(|(&caller, &record)| {
370 LoginRevocationChange(
371 LoginData {
372 audience: self.audience,
373 caller,
374 record,
375 },
376 None,
377 )
378 })
379 }
380
381 #[cfg(feature = "session-restore")]
390 pub fn restore_records(
391 mut self,
392 observed_ns: u64,
393 expected_counts: (usize, usize),
394 records: impl IntoIterator<Item = LoginRevocationChange>,
395 ) -> Result<Self, LoginRevocationError> {
396 if !self.records.is_empty()
397 || observed_ns > self.observed_ns
398 || expected_counts.0 > self.max_admitted
399 || expected_counts.1 > self.max_unknown
400 {
401 return Err(LoginRevocationError::InvalidPolicy);
402 }
403 let max_until = retention(observed_ns, self.max_login_ttl_ns)?;
404 for change in records {
405 if change.0.audience != self.audience || self.records.contains_key(&change.0.caller) {
406 return Err(LoginRevocationError::InvalidPolicy);
407 }
408 let record = change.0.record;
409 record.check_stored(
410 observed_ns,
411 max_until,
412 self.max_login_ttl_ns,
413 self.max_device_slots,
414 )?;
415 if let Some(login) = record.login()
416 && self.accounts.contains_key(&slot(login))
417 {
418 return Err(LoginRevocationError::InvalidPolicy);
419 }
420 let (used, expected) = if record.admitted_pool() {
422 (self.admitted, expected_counts.0)
423 } else {
424 (self.unknown, expected_counts.1)
425 };
426 if used >= expected {
427 return Err(LoginRevocationError::Capacity);
428 }
429 self.install(change.0.caller, record);
430 }
431 if self.occupancy() != expected_counts {
432 return Err(LoginRevocationError::InvalidPolicy);
433 }
434 Ok(self)
435 }
436
437 pub fn new(
441 audience: AudienceId,
442 max_admitted: usize,
443 max_unknown: usize,
444 max_login_ttl_ns: u64,
445 now_ns: u64,
446 ) -> Result<Self, LoginRevocationError> {
447 if max_admitted == 0
448 || max_unknown == 0
449 || max_login_ttl_ns == 0
450 || max_login_ttl_ns > crate::limits::MAX_LOGIN_NS
451 {
452 return Err(LoginRevocationError::InvalidPolicy);
453 }
454 retention(now_ns, max_login_ttl_ns)?;
455 Ok(Self {
456 audience,
457 records: BTreeMap::new(),
458 accounts: BTreeMap::new(),
459 expiry: BTreeSet::new(),
460 max_admitted,
461 max_device_slots: ic_auth_protocol_types::limits::MAX_DEVICE_SLOTS,
462 max_unknown,
463 admitted: 0,
464 unknown: 0,
465 max_login_ttl_ns,
466 observed_ns: now_ns,
467 })
468 }
469
470 pub fn with_device_limit(mut self, maximum: u8) -> Result<Self, LoginRevocationError> {
474 if maximum == 0
475 || maximum > ic_auth_protocol_types::limits::MAX_DEVICE_SLOTS
476 || !self.records.is_empty()
477 {
478 return Err(LoginRevocationError::InvalidPolicy);
479 }
480 self.max_device_slots = maximum;
481 Ok(self)
482 }
483
484 #[cfg(feature = "sessions")]
485 pub(crate) fn audience(&self) -> AudienceId {
486 self.audience
487 }
488
489 #[cfg(all(
490 feature = "sessions",
491 any(
492 feature = "portable-signature-verification",
493 feature = "certified-proof-verification"
494 )
495 ))]
496 pub(crate) fn slot_caller(&self, claims: &PortableClaims) -> Option<Principal> {
497 self.accounts
498 .get(&(claims.issuer, claims.account_subject, claims.device_slot))
499 .copied()
500 }
501
502 #[cfg(feature = "sessions")]
506 pub(crate) fn observe_login(
507 &mut self,
508 caller: Principal,
509 now_ns: u64,
510 ) -> Result<Option<LoginBinding>, LoginRevocationError> {
511 self.observe(caller, now_ns)?;
512 self.records
513 .get(&caller)
514 .map(|record| record.live_binding(now_ns))
515 .unwrap_or(Ok(None))
516 }
517
518 #[must_use]
520 pub fn occupancy(&self) -> (usize, usize) {
521 (self.admitted, self.unknown)
522 }
523
524 #[must_use]
527 pub fn observed_at_ns(&self) -> u64 {
528 self.observed_ns
529 }
530
531 pub fn check_reservation(
538 &mut self,
539 caller: Principal,
540 claims: &PortableClaims,
541 now_ns: u64,
542 ) -> Result<(), LoginRevocationError> {
543 self.reservation(caller, claims, now_ns).map(|_| ())
544 }
545
546 #[cfg(all(
547 feature = "sessions",
548 any(
549 feature = "portable-signature-verification",
550 feature = "certified-proof-verification"
551 )
552 ))]
553 pub(crate) fn check_prepared_reservation(
554 &mut self,
555 caller: Principal,
556 prepared: &super::ContextualClaims<'_, '_>,
557 now_ns: u64,
558 ) -> Result<(), LoginRevocationError> {
559 self.observe(caller, now_ns)?;
560 self.reservation_checked(
561 caller,
562 &prepared.decoded().claims,
563 prepared.browser_caller(),
564 now_ns,
565 )
566 .map(|_| ())
567 }
568
569 pub fn with_reservation<T, E: From<LoginRevocationError>>(
579 &mut self,
580 caller: Principal,
581 verified: &VerifiedPortableClaims,
582 now_ns: u64,
583 commit: impl FnOnce(&LoginRevocationChange) -> Result<T, E>,
584 ) -> Result<T, E> {
585 self.observe(caller, now_ns)?;
586 let record =
587 self.reservation_checked(caller, verified.claims(), verified.browser_caller(), now_ns)?;
588 if verified.verified_at_ns() != now_ns {
589 return Err(LoginRevocationError::InvalidPolicy.into());
590 }
591 self.commit_record(caller, record, commit)
592 }
593
594 pub fn with_revocation<E: From<LoginRevocationError>>(
611 &mut self,
612 unknown_budget: &mut SessionAdmissionBudget,
613 caller: Principal,
614 now_ns: u64,
615 commit: impl FnOnce(&LoginRevocationChange) -> Result<(), E>,
616 ) -> Result<u64, E> {
617 self.observe(caller, now_ns)?;
618 let old = self
619 .records
620 .get(&caller)
621 .filter(|r| r.admitted_pool() || r.until_ns > now_ns);
622 let record = if let Some(old) = old {
623 Record {
624 until_ns: old.until_ns,
625 state: LoginRecordState::Revoked(old.login()),
626 }
627 } else {
628 let until_ns = retention(now_ns, self.max_login_ttl_ns)?;
629 Record {
630 until_ns,
631 state: LoginRecordState::Revoked(None),
632 }
633 };
634 self.capacity(caller, record.admitted_pool())?;
635 if old.is_none() {
636 unknown_budget
637 .consume(now_ns)
638 .map_err(|error| match error {
639 AdmissionBudgetError::Exhausted => LoginRevocationError::Capacity,
640 AdmissionBudgetError::ClockRollback => LoginRevocationError::Clock,
641 AdmissionBudgetError::InvalidPolicy => LoginRevocationError::InvalidPolicy,
642 })?;
643 }
644 self.commit_record(caller, record, commit)?;
645 Ok(record.until_ns)
646 }
647
648 fn commit_record<T, E: From<LoginRevocationError>>(
651 &mut self,
652 caller: Principal,
653 record: Record,
654 commit: impl FnOnce(&LoginRevocationChange) -> Result<T, E>,
655 ) -> Result<T, E> {
656 let displaced = record
657 .login()
658 .and_then(|login| self.accounts.get(&slot(login)).copied())
659 .filter(|old| *old != caller);
660 let change = LoginRevocationChange(
661 LoginData {
662 audience: self.audience,
663 caller,
664 record,
665 },
666 displaced,
667 );
668 let previous_caller = displaced.unwrap_or(caller);
669 if let Some(previous) = self.records.get(&previous_caller) {
670 change.check_replacement(
671 &LoginRevocationChange(
672 LoginData {
673 audience: self.audience,
674 caller: previous_caller,
675 record: *previous,
676 },
677 None,
678 ),
679 self.observed_ns,
680 )?;
681 }
682 let value = commit(&change)?;
683 if let Some(old) = displaced {
684 self.remove(old);
685 }
686 self.install(caller, record);
687 Ok(value)
688 }
689
690 pub fn is_revoked(
693 &mut self,
694 caller: Principal,
695 now_ns: u64,
696 ) -> Result<bool, LoginRevocationError> {
697 self.observe(caller, now_ns)?;
698 Ok(self
699 .records
700 .get(&caller)
701 .is_some_and(|r| r.revoked() && r.until_ns > now_ns))
702 }
703
704 pub fn prune(&mut self, now_ns: u64, budget: usize) -> Result<usize, LoginRevocationError> {
708 self.prune_with_commit(now_ns, budget, |_, _| Ok(()))
709 }
710 pub fn prune_with_commit(
715 &mut self,
716 now_ns: u64,
717 budget: usize,
718 commit: impl FnOnce(&[Principal], (usize, usize)) -> Result<(), LoginRevocationError>,
719 ) -> Result<usize, LoginRevocationError> {
720 self.clock(now_ns)?;
721 let mut callers = Vec::new();
722 let mut counts = self.occupancy();
723 for (_, caller) in self
724 .expiry
725 .iter()
726 .take_while(|(until, _)| *until <= now_ns)
727 .take(budget.min(128))
728 {
729 if self.records[caller].admitted_pool() {
730 counts.0 -= 1;
731 } else {
732 counts.1 -= 1;
733 }
734 callers.push(*caller);
735 }
736 if callers.is_empty() {
737 return Ok(0);
738 }
739 commit(&callers, counts)?;
740 for caller in &callers {
741 self.remove(*caller);
742 }
743 Ok(callers.len())
744 }
745
746 fn reservation(
747 &mut self,
748 caller: Principal,
749 claims: &PortableClaims,
750 now_ns: u64,
751 ) -> Result<Record, LoginRevocationError> {
752 self.observe(caller, now_ns)?;
753 validate_shape(claims).map_err(|_| LoginRevocationError::InvalidPolicy)?;
754 self.reservation_checked(
755 caller,
756 claims,
757 browser_principal(&claims.browser_public_key),
758 now_ns,
759 )
760 }
761
762 fn reservation_checked(
765 &self,
766 caller: Principal,
767 claims: &PortableClaims,
768 browser_caller: Principal,
769 now_ns: u64,
770 ) -> Result<Record, LoginRevocationError> {
771 let login_deadline_ns = claims.login_deadline_ns;
772 if claims.audience != self.audience
773 || claims.device_slot >= self.max_device_slots
774 || browser_caller != caller
775 || claims
776 .login_deadline_ns
777 .checked_sub(claims.login_started_at_ns)
778 .is_none_or(|ttl| ttl == 0 || ttl > self.max_login_ttl_ns)
779 {
780 return Err(LoginRevocationError::InvalidPolicy);
781 }
782 let until_ns = login_deadline_ns;
783 let login = LoginBinding {
784 issuer: claims.issuer,
785 subject: claims.account_subject,
786 device_slot: claims.device_slot,
787 device_generation: claims.device_generation,
788 started_at_ns: claims.login_started_at_ns,
789 deadline_ns: claims.login_deadline_ns,
790 };
791 if let Some(old) = self
792 .records
793 .get(&caller)
794 .filter(|r| r.admitted_pool() || r.until_ns > now_ns)
795 {
796 if old.revoked() {
797 return Err(LoginRevocationError::Revoked);
798 }
799 if old.login() != Some(login) {
800 return Err(LoginRevocationError::InvalidPolicy);
801 }
802 }
803 let owner = self.accounts.get(&slot(login));
806 if let Some(owner) = owner {
807 let old = self.records[owner];
808 let binding = old.login().expect("slot index has authenticated binding");
809 if login.device_generation < binding.device_generation {
810 return Err(LoginRevocationError::Revoked);
811 }
812 if login.device_generation == binding.device_generation
813 && (*owner != caller || binding != login || old.revoked())
814 {
815 return Err(LoginRevocationError::Revoked);
816 }
817 }
818 let record = Record {
819 until_ns,
820 state: LoginRecordState::Reserved(login),
821 };
822 if owner.is_none() {
823 self.capacity(caller, record.admitted_pool())?;
824 }
825 Ok(record)
826 }
827
828 fn clock(&mut self, now_ns: u64) -> Result<(), LoginRevocationError> {
829 if now_ns < self.observed_ns {
830 return Err(LoginRevocationError::Clock);
831 }
832 self.observed_ns = now_ns;
833 Ok(())
834 }
835
836 fn observe(&mut self, caller: Principal, now_ns: u64) -> Result<(), LoginRevocationError> {
837 if !is_login_caller(caller) {
838 return Err(LoginRevocationError::InvalidCaller);
839 }
840 self.clock(now_ns)
841 }
842
843 fn capacity(&self, caller: Principal, admitted_pool: bool) -> Result<(), LoginRevocationError> {
844 let reuses_pool = self
845 .records
846 .get(&caller)
847 .is_some_and(|old| old.admitted_pool() == admitted_pool);
848 let (count, limit) = if admitted_pool {
849 (self.admitted, self.max_admitted)
850 } else {
851 (self.unknown, self.max_unknown)
852 };
853 if !reuses_pool && count >= limit {
854 return Err(LoginRevocationError::Capacity);
855 }
856 Ok(())
857 }
858
859 fn remove(&mut self, caller: Principal) {
860 if let Some(old) = self.records.remove(&caller) {
861 self.unindex(caller, old);
862 }
863 }
864
865 fn unindex(&mut self, caller: Principal, old: Record) {
866 self.expiry.remove(&(old.until_ns, caller));
867 if let Some(login) = old.login() {
868 self.accounts.remove(&slot(login));
869 }
870 if old.admitted_pool() {
871 self.admitted -= 1;
872 } else {
873 self.unknown -= 1;
874 }
875 }
876
877 fn install(&mut self, caller: Principal, record: Record) {
878 if let Some(old) = self.records.insert(caller, record) {
879 self.unindex(caller, old);
880 }
881 if let Some(login) = record.login() {
882 self.accounts.insert(slot(login), caller);
883 }
884 if record.admitted_pool() {
885 self.admitted += 1;
886 } else {
887 self.unknown += 1;
888 }
889 if !record.admitted_pool() {
892 self.expiry.insert((record.until_ns, caller));
893 }
894 }
895}
896
897fn slot(login: LoginBinding) -> (Principal, Principal, u8) {
898 (login.issuer, login.subject, login.device_slot)
899}
900
901fn retention(now_ns: u64, max_login_ttl_ns: u64) -> Result<u64, LoginRevocationError> {
902 now_ns
903 .checked_add(max_login_ttl_ns)
904 .ok_or(LoginRevocationError::Clock)
905}
906
907#[cfg(test)]
908mod tests;