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    #[serde(skip)]
15    pub issued_at: Option<u64>,
16}
17
18impl TokenData {
19    pub fn is_expired(&self) -> bool {
20        let Some(issued_at) = self.issued_at else {
21            return false;
22        };
23        let now = std::time::SystemTime::now()
24            .duration_since(std::time::UNIX_EPOCH)
25            .map(|d| d.as_secs())
26            .unwrap_or(0);
27        // Treat as expired 60 seconds before actual expiry
28        now >= issued_at + self.expires_in.saturating_sub(60)
29    }
30
31    pub fn mark_issued_now(&mut self) {
32        self.issued_at = std::time::SystemTime::now()
33            .duration_since(std::time::UNIX_EPOCH)
34            .map(|d| d.as_secs())
35            .ok();
36    }
37}