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
use std::time::{SystemTime, UNIX_EPOCH};
pub use config_file::*;
pub use login_flow::google_login;
pub use refresh_flow::refresh_google_login;
pub use crate::errors::LibError;
mod config_file;
mod errors;
mod login_flow;
mod refresh_flow;
pub async fn get_access_token_from_config(
config_name: &str,
config_base_path: &ConfigBasePath,
) -> Result<Token, LibError> {
let mut config = ConfigFile::read_config(config_name, config_base_path)?;
check_token(config.access_token.clone(), &mut config, config_base_path).await?;
match &config.access_token {
Some(access_token) => Ok(access_token.clone()),
None => Err(LibError::CouldNotReadConfigCorrupt(config.name)),
}
}
pub async fn check_token(
token: Option<Token>,
config: &mut ConfigFile,
config_base_path: &ConfigBasePath,
) -> Result<(), LibError> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let token_expiration = match token {
Some(token) => token.exp,
None => 0,
};
if token_expiration < now {
refresh_google_login(config, config_base_path).await?;
}
Ok(())
}