ic_auth_client/util/
arc_identity.rs

1use ic_agent::identity::{AnonymousIdentity, BasicIdentity, DelegatedIdentity, Identity};
2use std::{fmt, sync::Arc};
3
4/// Arc-wrapped identity that can be one of several identity types.
5///
6/// This enum provides a way to work with different identity types in a uniform manner
7/// while maintaining reference counting through [`Arc`](std::sync::Arc) for efficient cloning and sharing.
8#[derive(Clone)]
9pub enum ArcIdentity {
10    /// An anonymous identity that provides no authentication.
11    Anonymous(Arc<AnonymousIdentity>),
12    /// An Ed25519-based identity using a basic cryptographic key pair.
13    Ed25519(Arc<BasicIdentity>),
14    /// A delegated identity that uses delegation chains for authentication.
15    Delegated(Arc<DelegatedIdentity>),
16}
17
18impl Default for ArcIdentity {
19    fn default() -> Self {
20        ArcIdentity::Anonymous(Arc::new(AnonymousIdentity))
21    }
22}
23
24impl fmt::Debug for ArcIdentity {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            ArcIdentity::Anonymous(_) => write!(f, "ArcIdentity::Anonymous"),
28            ArcIdentity::Ed25519(_) => write!(f, "ArcIdentity::Ed25519"),
29            ArcIdentity::Delegated(_) => write!(f, "ArcIdentity::Delegated"),
30        }
31    }
32}
33
34impl ArcIdentity {
35    /// Returns the underlying identity as an [`Arc<dyn Identity>`](https://docs.rs/ic-agent/latest/ic_agent/identity/trait.Identity.html#impl-Identity-for-Arc%3Cdyn+Identity%3E).
36    ///
37    /// [`Arc<dyn Identity>`](https://docs.rs/ic-agent/latest/ic_agent/identity/trait.Identity.html#impl-Identity-for-Arc%3Cdyn+Identity%3E) implements the [`Identity`](ic_agent::identity::Identity) trait, making it directly usable for tasks like login.
38    pub fn as_arc_identity(&self) -> Arc<dyn Identity> {
39        match self {
40            ArcIdentity::Anonymous(id) => id.clone(),
41            ArcIdentity::Ed25519(id) => id.clone(),
42            ArcIdentity::Delegated(id) => id.clone(),
43        }
44    }
45
46    /// Returns the public key associated with this identity, if available.
47    ///
48    /// This method delegates to the underlying identity's [`public_key()`](ic_agent::identity::Identity::public_key) method.
49    /// The availability and format of the public key depends on the specific identity type.
50    pub fn public_key(&self) -> Option<Vec<u8>> {
51        match self {
52            ArcIdentity::Anonymous(id) => id.public_key(),
53            ArcIdentity::Ed25519(id) => id.public_key(),
54            ArcIdentity::Delegated(id) => id.public_key(),
55        }
56    }
57}
58
59impl From<AnonymousIdentity> for ArcIdentity {
60    fn from(identity: AnonymousIdentity) -> Self {
61        ArcIdentity::Anonymous(Arc::new(identity))
62    }
63}
64
65impl From<ic_agent::identity::BasicIdentity> for ArcIdentity {
66    fn from(identity: ic_agent::identity::BasicIdentity) -> Self {
67        ArcIdentity::Ed25519(Arc::new(identity))
68    }
69}
70
71impl From<DelegatedIdentity> for ArcIdentity {
72    fn from(identity: DelegatedIdentity) -> Self {
73        ArcIdentity::Delegated(Arc::new(identity))
74    }
75}