Skip to main content

highlevel_api/auth/
token.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct TokenData {
5    pub access_token: String,
6    pub refresh_token: Option<String>,
7    pub token_type: String,
8    pub expires_in: u64,
9    pub scope: Option<String>,
10    pub user_type: Option<String>,
11    pub company_id: Option<String>,
12    pub location_id: Option<String>,
13    /// Unix timestamp (seconds) when this token was issued, for expiry tracking.
14    /// Serialized so restored tokens keep correct expiry. `None` means expiry is
15    /// unchecked until `mark_issued_now()` is called.
16    #[serde(default, skip_serializing_if = "Option::is_none")]
17    pub issued_at: Option<u64>,
18}
19
20impl TokenData {
21    pub fn is_expired(&self) -> bool {
22        let Some(issued_at) = self.issued_at else {
23            return false;
24        };
25        let now = std::time::SystemTime::now()
26            .duration_since(std::time::UNIX_EPOCH)
27            .map(|d| d.as_secs())
28            .unwrap_or(0);
29        // Treat as expired 60 seconds before actual expiry
30        now >= issued_at + self.expires_in.saturating_sub(60)
31    }
32
33    pub fn mark_issued_now(&mut self) {
34        self.issued_at = std::time::SystemTime::now()
35            .duration_since(std::time::UNIX_EPOCH)
36            .map(|d| d.as_secs())
37            .ok();
38    }
39}