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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use crate::{
custom_serde::{duration_second, space_separated_scopes},
ModelResult,
};
use std::{
collections::{HashMap, HashSet},
fs,
io::{Read, Write},
path::Path,
};
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Token {
pub access_token: String,
#[serde(with = "duration_second")]
pub expires_in: Duration,
pub expires_at: Option<DateTime<Utc>>,
pub refresh_token: Option<String>,
#[serde(default, with = "space_separated_scopes", rename = "scope")]
pub scopes: HashSet<String>,
}
impl Default for Token {
fn default() -> Self {
Token {
access_token: String::new(),
expires_in: Duration::seconds(0),
expires_at: Some(Utc::now()),
refresh_token: None,
scopes: HashSet::new(),
}
}
}
impl Token {
pub fn from_cache<T: AsRef<Path>>(path: T) -> ModelResult<Self> {
let mut file = fs::File::open(path)?;
let mut tok_str = String::new();
file.read_to_string(&mut tok_str)?;
let tok = serde_json::from_str::<Token>(&tok_str)?;
Ok(tok)
}
pub fn write_cache<T: AsRef<Path>>(&self, path: T) -> ModelResult<()> {
let token_info = serde_json::to_string(&self)?;
let mut file = fs::OpenOptions::new().write(true).create(true).open(path)?;
file.set_len(0)?;
file.write_all(token_info.as_bytes())?;
Ok(())
}
pub fn is_expired(&self) -> bool {
self.expires_at.map_or(true, |expiration| {
Utc::now() + Duration::seconds(10) >= expiration
})
}
pub fn auth_headers(&self) -> HashMap<String, String> {
let auth = "authorization".to_owned();
let value = format!("Bearer {}", self.access_token);
let mut headers = HashMap::new();
headers.insert(auth, value);
headers
}
}
#[cfg(test)]
mod test {
use crate::Token;
#[test]
fn test_bearer_auth() {
let tok = Token {
access_token: "access_token".to_string(),
..Default::default()
};
let headers = tok.auth_headers();
assert_eq!(headers.len(), 1);
assert_eq!(
headers.get("authorization"),
Some(&"Bearer access_token".to_owned())
);
}
}