1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use chrono::{offset::TimeZone, DateTime, Utc};
#[derive(Clone, PartialEq, Debug, serde::Deserialize)]
pub struct Token {
pub access_token: String,
pub refresh_token: String,
pub token_type: String,
pub expires_in: Option<i64>,
pub expires_in_timestamp: Option<i64>,
}
impl Token {
pub fn has_expired(&self) -> bool {
self.access_token.is_empty() || self.expiry_date() <= Utc::now()
}
pub fn expiry_date(&self) -> DateTime<Utc> {
match self.expires_in_timestamp {
Some(ts) => Utc.timestamp(ts, 0),
None => Utc::now(),
}
}
}
impl std::convert::TryInto<http::header::HeaderValue> for Token {
type Error = crate::Error;
fn try_into(self) -> Result<http::header::HeaderValue, crate::Error> {
let auth_header_val = format!("{} {}", self.token_type, self.access_token);
http::header::HeaderValue::from_str(&auth_header_val)
.map_err(|e| crate::Error::from(http::Error::from(e)))
}
}