use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone)]
pub struct AccessToken {
pub token: String,
pub expires_at: u64,
}
impl AccessToken {
pub fn new(token: String, expires_in: u64) -> Self {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
Self {
token,
expires_at: now + expires_in,
}
}
pub fn is_expired(&self) -> bool {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
now >= self.expires_at
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessTokenRequest {
pub grant_type: String,
pub appid: String,
pub secret: String,
}
impl AccessTokenRequest {
pub fn new(appid: String, secret: String) -> Self {
Self {
grant_type: "client_credential".to_string(),
appid,
secret,
}
}
}