ic-auth 0.39.4

Portable authentication, delegation proof verification and local session machinery
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! One portable session lifecycle for both complete proof mechanisms, with atomic replay
//! consumption. Hosts own protected context, generation changes and durable
//! transactions. Application resource policy and IC ingress delegation are separate.

mod memory;
mod model;
mod refusal;
mod scopes;

pub use memory::{MemorySessionStore, SessionCommit, SessionPrune, SessionPruned, SessionWrite};
pub use model::{Replay, Session, SessionLimits, SessionPolicy};
pub use scopes::RequiredScopes;

use crate::{
    admission_budget::SessionAdmissionBudget,
    canonical::CanonicalAuthError,
    portable::{
        LoginRevocationChange, LoginRevocationError, MemoryLoginRevocations, PortableAuthority,
        PortableClaimsError, PortableEncodingError,
    },
    session_request::{SessionRequestError, SessionTtlLimit},
};
use ic_auth_protocol_types::{AuthRole, LogoutResult, LogoutUnavailable, Principal};
use std::borrow::Cow;
#[cfg(any(
    feature = "portable-signature-verification",
    feature = "certified-proof-verification"
))]
use std::ops::ControlFlow;
use thiserror::Error;

/// Protected current authority, operation scopes and live session lifetime bound.
/// Proof admission limits and default TTL are absent from ordinary authorization.
/// Enrollment, caller and clock are host-authenticated inputs. Advance generation
/// atomically with authority changes; never derive them from submitted claims.
pub struct SessionContext<'a> {
    pub authority: PortableAuthority<'a>,
    pub max_ttl: SessionTtlLimit,
    pub role: &'a AuthRole,
    pub required_scopes: &'a RequiredScopes,
}
impl SessionContext<'_> {
    pub(crate) fn validate(
        &self,
        policy: SessionPolicy,
        audience: ic_auth_protocol_types::AudienceId,
    ) -> Result<(), SessionError> {
        if !policy.enabled || !policy.subject_admissible {
            return Err(SessionError::Disabled);
        }
        if audience != *self.authority.audience || !self.authority.authority_shape_valid() {
            return Err(SessionError::InvalidPolicyOrRecord);
        }
        Ok(())
    }
}

/// Physical occupancy: expired records count until explicit bounded pruning.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct SessionOccupancy {
    pub sessions: usize,
    pub replays: usize,
    pub replays_for_account: usize,
}

/// One host-isolated read snapshot. No writes or whole-store clones are permitted.
/// A reader cannot publish a write:
/// ```compile_fail
/// use ic_auth::session::{SessionReader, Session, Replay};
/// use ic_auth::portable::LoginRevocationChange;
/// fn write(reader: &dyn SessionReader, session: &Session, replay: &Replay, login: &LoginRevocationChange) {
///     reader.admit(session, replay, login);
/// }
/// ```
pub trait SessionReader {
    fn generation(&self) -> u64;
    fn limits(&self) -> SessionLimits;
    /// Borrow in-memory records; storage adapters may return an owned decode.
    /// The value cannot escape the host's read snapshot without copying it.
    fn session(&self, caller: Principal) -> Result<Option<Cow<'_, Session>>, SessionError>;
    fn replay_exists(&self, fingerprint: [u8; 32]) -> Result<bool, SessionError>;
    /// Account-wide replay occupancy, independent of device keys and sessions.
    fn occupancy(&self, account: (Principal, Principal)) -> Result<SessionOccupancy, SessionError>;
}

/// Host-owned atomic storage boundary. No await/reentrancy may split an engine
/// operation or change authority. Refusals and commit errors preserve prior state.
/// Admission validates generation, pair association, replay, login reservation,
/// record sizes and capacity before atomically persisting the pair and login.
/// Retain at most one session per `(issuer, subject, device_slot)` and per caller.
/// Admission atomically removes `login.displaced_caller()` and its old login row,
/// retaining replay history. Only a verified higher generation can displace it.
/// Logout validates a revocation, removes only its caller's session and retains
/// replay history, atomically with the login tombstone.
pub trait SessionStore {
    fn read<T>(
        &mut self,
        operation: impl FnOnce(&dyn SessionReader) -> Result<T, SessionError>,
    ) -> Result<T, SessionError>;
    /// Consumes the validated pair so the backend can install it without copying.
    /// Returns true when replacing an existing session; false when creating one.
    fn admit(
        &mut self,
        session: Session,
        replay: Replay,
        login: &LoginRevocationChange,
    ) -> Result<bool, SessionError>;
    fn logout(&mut self, login: &LoginRevocationChange) -> Result<(), SessionError>;
}

/// Compact result of admission or an exact retry, never authorization evidence.
/// Full records remain in the host store. Authorize every later operation using
/// current protected authority, clock and generation; this receipt grants nothing.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SessionReceipt {
    caller: Principal,
    issuer: Principal,
    subject: Principal,
    expires_at_ns: u64,
}
impl From<&Session> for SessionReceipt {
    fn from(session: &Session) -> Self {
        Self {
            caller: session.caller(),
            issuer: session.issuer(),
            subject: session.subject(),
            expires_at_ns: session.expires_at_ns(),
        }
    }
}
impl SessionReceipt {
    pub fn caller(&self) -> Principal {
        self.caller
    }
    pub fn issuer(&self) -> Principal {
        self.issuer
    }
    pub fn subject(&self) -> Principal {
        self.subject
    }
    pub fn expires_at_ns(&self) -> u64 {
        self.expires_at_ns
    }
}

/// Session creation, replacement, or the unchanged receipt of an exact retry.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SessionEstablished {
    Created(SessionReceipt),
    Replaced(SessionReceipt),
    ExactRetry(SessionReceipt),
}

impl SessionEstablished {
    pub fn receipt(self) -> SessionReceipt {
        match self {
            Self::Created(receipt) | Self::Replaced(receipt) | Self::ExactRetry(receipt) => receipt,
        }
    }
}

/// Session/replay admission and local authorization failures.
#[derive(Debug, Error, Eq, PartialEq)]
pub enum SessionError {
    #[error("session capability is disabled or subject is inadmissible")]
    Disabled,
    #[error("session policy or stored record is invalid")]
    InvalidPolicyOrRecord,
    #[error("session authority is no longer current")]
    StaleAuthority,
    #[error("session is missing or expired")]
    MissingOrExpired,
    #[error("proof was already consumed or retry request differs")]
    ReplayConflict,
    #[error("requested or required scope is not granted")]
    ScopeRejected,
    #[error("session/replay capacity is exhausted")]
    Capacity,
    #[error("encoded record budget is exhausted")]
    ByteCapacity,
    #[error("authority generation is exhausted")]
    GenerationExhausted,
    #[error("host transaction could not commit")]
    StorageFailure,
    #[error(transparent)]
    Canonical(#[from] CanonicalAuthError),
    #[cfg(any(
        feature = "portable-signature-verification",
        feature = "certified-proof-verification"
    ))]
    #[error(transparent)]
    Credential(#[from] crate::portable_credential::PortableCredentialError),
    #[error(transparent)]
    Claims(#[from] PortableClaimsError),
    #[error(transparent)]
    Encoding(#[from] PortableEncodingError),
    #[error(transparent)]
    Revocation(#[from] LoginRevocationError),
}

// Stored record checks retain the engine's existing rejection surface.
impl From<SessionRequestError> for SessionError {
    fn from(error: SessionRequestError) -> Self {
        match error {
            SessionRequestError::InvalidRequest | SessionRequestError::InvalidLifetime => {
                Self::InvalidPolicyOrRecord
            }
            SessionRequestError::ScopeRejected => Self::ScopeRejected,
            SessionRequestError::Canonical(error) => Self::Canonical(error),
        }
    }
}

/// Fresh admission inputs. The selected verifier is the single source of current
/// authority and proof constraints. Authorization needs only SessionContext.
#[cfg(any(
    feature = "portable-signature-verification",
    feature = "certified-proof-verification"
))]
pub struct SessionAdmissionContext<'a> {
    pub verification: crate::portable_credential::PortableVerificationContext<'a>,
    pub role: &'a AuthRole,
    pub ttl: crate::session_request::SessionTtlPolicy,
}

/// Admit the exact credential with the protected verifier and shared budget.
/// The engine owns decoding and verification; no caller-supplied verification
/// callback can substitute claims or a different authority snapshot. Exact retries
/// and cheap refusals do not verify proofs or consume verification credit.
/// Hosts serialize the entire call across store, ledger, authority and budget.
/// No await/reentrancy is permitted, and charges survive ordinary storage errors.
#[cfg(any(
    feature = "portable-signature-verification",
    feature = "certified-proof-verification"
))]
pub fn establish_session(
    store: &mut impl SessionStore,
    ledger: &mut MemoryLoginRevocations,
    budget: &mut SessionAdmissionBudget,
    credential: &ic_auth_protocol_types::PortableCredential,
    request: crate::session_request::SessionRequest,
    admission: &SessionAdmissionContext<'_>,
    policy: SessionPolicy,
) -> Result<SessionEstablished, SessionError> {
    let claims_context = admission.verification.claims();
    SessionContext {
        authority: claims_context.authority,
        max_ttl: admission.ttl.maximum(),
        role: admission.role,
        required_scopes: RequiredScopes::NONE,
    }
    .validate(policy, ledger.audience())?;
    let decoded = crate::portable::DecodedPortableClaims::decode(&credential.claims)?;
    establish_decoded_session(
        store,
        ledger,
        budget,
        (decoded, &credential.proof),
        request,
        admission,
        policy,
    )
}

// Persistent receivers use the same owned decode to select their bounded login
// snapshot. All protected-context, replay and cryptographic checks stay here.
#[cfg(any(
    feature = "portable-signature-verification",
    feature = "certified-proof-verification"
))]
pub(crate) fn establish_decoded_session(
    store: &mut impl SessionStore,
    ledger: &mut MemoryLoginRevocations,
    budget: &mut SessionAdmissionBudget,
    credential: (
        crate::portable::DecodedPortableClaims,
        &ic_auth_protocol_types::PortableProof,
    ),
    request: crate::session_request::SessionRequest,
    admission: &SessionAdmissionContext<'_>,
    policy: SessionPolicy,
) -> Result<SessionEstablished, SessionError> {
    let claims_context = admission.verification.claims();
    let context = SessionContext {
        authority: claims_context.authority,
        max_ttl: admission.ttl.maximum(),
        role: admission.role,
        required_scopes: RequiredScopes::NONE,
    };
    context.validate(policy, ledger.audience())?;
    let authority = &context.authority;
    let (decoded, proof) = credential;
    let claims = &decoded.claims;
    let fingerprint = decoded.fingerprint;
    let login = ledger.observe_login(authority.caller, authority.now_ns)?;
    let admission_expires_at_ns = claims.admission_expires_at_ns;
    let slot_caller = ledger.slot_caller(claims);
    let checked = store.read(|tx| {
        if tx.generation() != policy.generation {
            return Err(SessionError::StaleAuthority);
        }
        let previous = tx.session(authority.caller)?;
        let replacing = previous.is_some()
            || slot_caller
                .map(|caller| tx.session(caller))
                .transpose()?
                .flatten()
                .is_some();
        if let Some(session) = previous
            && session.fingerprint() == fingerprint
        {
            if session.request_hash() != request.hash() {
                return Err(SessionError::ReplayConflict);
            }
            session.check_live(&context, policy)?;
            session.check_revocation(login)?;
            return Ok(ControlFlow::Break(SessionReceipt::from(session.as_ref())));
        }
        if tx.replay_exists(fingerprint)? {
            return Err(SessionError::ReplayConflict);
        }
        let prepared = admission.verification.prepare(decoded)?;
        let requested_expiry = request.evaluate(
            &prepared.decoded().claims.grants,
            admission.role,
            authority.now_ns,
            admission.ttl,
        )?;
        let limits = tx.limits();
        limits.check_capacity(
            tx.occupancy((
                prepared.decoded().claims.issuer,
                prepared.decoded().claims.account_subject,
            ))?,
            replacing,
        )?;
        ledger.check_prepared_reservation(
            authority.caller,
            prepared.contextual(),
            authority.now_ns,
        )?;
        Ok(ControlFlow::Continue((prepared, requested_expiry)))
    })?;
    let (prepared, requested_expiry) = match checked {
        ControlFlow::Break(session) => return Ok(SessionEstablished::ExactRetry(session)),
        ControlFlow::Continue(fresh) => fresh,
    };
    let verified = prepared.verify(budget, proof)?;
    let session = Session::from_admission(&verified, request, &context, policy, requested_expiry);
    let replay = Replay::new(&session, admission_expires_at_ns);
    let receipt = SessionReceipt::from(&session);
    ledger.with_reservation(authority.caller, &verified, authority.now_ns, |login| {
        let replacing = store.admit(session, replay, login)?;
        Ok(if replacing {
            SessionEstablished::Replaced(receipt)
        } else {
            SessionEstablished::Created(receipt)
        })
    })
}

/// Check the caller's local session against current enrollment, generation,
/// revocation and required scopes. No signature verification or lease extension.
/// Project the required result inside the host read snapshot without cloning a
/// full session. The callback runs only after every authorization check passes.
/// Applications separately enforce resource ownership and effects after awaits.
pub fn authorize_session<T>(
    store: &mut impl SessionStore,
    ledger: &mut MemoryLoginRevocations,
    context: &SessionContext<'_>,
    policy: SessionPolicy,
    operation: impl FnOnce(&Session) -> T,
) -> Result<T, SessionError> {
    context.validate(policy, ledger.audience())?;
    let login = ledger.observe_login(context.authority.caller, context.authority.now_ns)?;
    store.read(|tx| authorize_snapshot(tx, login, context, policy, operation))
}

// One common post-storage authorization path for volatile and persistent stores.
// Callers first validate protected context and observe the current login/clock.
pub(crate) fn authorize_snapshot<T>(
    tx: &dyn SessionReader,
    login: Option<crate::portable::LoginBinding>,
    context: &SessionContext<'_>,
    policy: SessionPolicy,
    operation: impl FnOnce(&Session) -> T,
) -> Result<T, SessionError> {
    if tx.generation() != policy.generation {
        return Err(SessionError::StaleAuthority);
    }
    let session = tx
        .session(context.authority.caller)?
        .ok_or(SessionError::MissingOrExpired)?;
    session.check_live(context, policy)?;
    session.check_revocation(login)?;
    if !crate::certificate_issuance::scopes_are_subset(
        context.required_scopes.as_slice(),
        session.scopes(),
    ) {
        return Err(SessionError::ScopeRejected);
    }
    Ok(operation(&session))
}

/// Authenticate logout with the runtime caller even if proof/enrollment expired
/// or admission is disabled. Commit session removal before acknowledging denial;
/// replay records remain. Only a retained authenticated login window permits
/// `Revoked`; an unseen login gets bounded temporary denial and `Unconfirmed`.
/// Host startup readiness, complete safety state and serialization still apply.
pub fn logout_session(
    store: &mut impl SessionStore,
    ledger: &mut MemoryLoginRevocations,
    unknown_budget: &mut SessionAdmissionBudget,
    caller: Principal,
    now_ns: u64,
) -> LogoutResult {
    let mut confirmed = false;
    let result = ledger.with_revocation(unknown_budget, caller, now_ns, |change| {
        store
            .logout(change)
            .map_err(|_| LoginRevocationError::Storage)?;
        confirmed = matches!(
            change.state(),
            crate::portable::LoginRecordState::Revoked(Some(_))
        );
        Ok::<_, LoginRevocationError>(())
    });
    // Backend diagnostics stay host-side; never acknowledge a failed commit.
    match result {
        Ok(deny_until_ns) if confirmed => LogoutResult::Revoked { deny_until_ns },
        Ok(deny_until_ns) => LogoutResult::Unconfirmed { deny_until_ns },
        Err(LoginRevocationError::InvalidCaller) => LogoutResult::InvalidCaller,
        Err(LoginRevocationError::Capacity) => {
            LogoutResult::Unavailable(LogoutUnavailable::Capacity)
        }
        Err(LoginRevocationError::Clock) => LogoutResult::Unavailable(LogoutUnavailable::Clock),
        Err(
            LoginRevocationError::InvalidPolicy
            | LoginRevocationError::Revoked
            | LoginRevocationError::Storage,
        ) => LogoutResult::Unavailable(LogoutUnavailable::Storage),
    }
}