wechat_api_rs/
auth.rs

1//! Authentication related functionality
2
3use serde::{Deserialize, Serialize};
4use std::time::{SystemTime, UNIX_EPOCH};
5
6/// Access token holder
7#[derive(Debug, Clone)]
8pub struct AccessToken {
9    pub token: String,
10    pub expires_at: u64,
11}
12
13impl AccessToken {
14    pub fn new(token: String, expires_in: u64) -> Self {
15        let now = SystemTime::now()
16            .duration_since(UNIX_EPOCH)
17            .unwrap()
18            .as_secs();
19
20        Self {
21            token,
22            expires_at: now + expires_in,
23        }
24    }
25
26    pub fn is_expired(&self) -> bool {
27        let now = SystemTime::now()
28            .duration_since(UNIX_EPOCH)
29            .unwrap()
30            .as_secs();
31
32        now >= self.expires_at
33    }
34}
35
36/// Access token request parameters
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct AccessTokenRequest {
39    pub grant_type: String,
40    pub appid: String,
41    pub secret: String,
42}
43
44impl AccessTokenRequest {
45    pub fn new(appid: String, secret: String) -> Self {
46        Self {
47            grant_type: "client_credential".to_string(),
48            appid,
49            secret,
50        }
51    }
52}