ic-auth 0.57.0

Canonical authentication, proof verification and bounded receiver/issuer state with optional stable upgrade checkpoints
Documentation
//! Private canonical input shared by admission and the complete verifiers.

use crate::claims::{ClaimsError, decode_claims};
use ic_auth_protocol_types::{PortableClaims, Principal};
use sha2::{Digest, Sha256};
use std::cell::OnceCell;

///
/// DecodedClaims
///
/// Keeps the original encoding and derived browser principal bound to immutable claims.
///

pub(crate) struct DecodedClaims<'a> {
    claims: PortableClaims,
    encoded: &'a [u8],
    caller: Principal,
    fingerprint: OnceCell<[u8; 32]>,
}

impl<'a> DecodedClaims<'a> {
    pub(crate) fn new(encoded: &'a [u8]) -> Result<Self, ClaimsError> {
        let claims = decode_claims(encoded)?;
        Ok(Self {
            caller: crate::browser_identity::browser_principal(&claims.browser_public_key),
            claims,
            encoded,
            fingerprint: OnceCell::new(),
        })
    }

    pub(crate) fn claims(&self) -> &PortableClaims {
        &self.claims
    }

    pub(super) fn into_claims(self) -> PortableClaims {
        self.claims
    }

    pub(crate) fn caller(&self) -> Principal {
        self.caller
    }

    pub(crate) fn fingerprint(&self) -> [u8; 32] {
        *self
            .fingerprint
            .get_or_init(|| Sha256::digest(self.encoded).into())
    }

    #[cfg(all(feature = "receiver-stable", feature = "certified-verification"))]
    pub(super) fn encoded_len(&self) -> usize {
        self.encoded.len()
    }
}