Skip to main content

iroh_services/
token.rs

1use std::time::{SystemTime, UNIX_EPOCH};
2
3use iroh_services_proto::caps::Caps;
4use rcan::{Expires, Rcan};
5
6/// An opaque capability token issued by iroh-services.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct ApiToken(Rcan<Caps>);
9
10impl ApiToken {
11    pub(crate) fn new(token: Rcan<Caps>) -> Self {
12        Self(token)
13    }
14
15    pub(crate) fn into_rcan(self) -> Rcan<Caps> {
16        self.0
17    }
18
19    /// Returns the encoded token.
20    pub fn encode(&self) -> Vec<u8> {
21        self.0.encode()
22    }
23
24    /// Returns the token's expiration time, or `None` if it does not expire.
25    pub fn expires_at(&self) -> Option<SystemTime> {
26        match self.0.expires() {
27            Expires::Never => None,
28            Expires::At(seconds) => {
29                UNIX_EPOCH.checked_add(std::time::Duration::from_secs(*seconds))
30            }
31        }
32    }
33}