1pub mod auto_refresh;
2pub mod current;
3pub mod output;
4pub mod refresh;
5pub mod sync;
6pub mod use_secret;
7
8use anyhow::Result;
9use std::path::Path;
10
11use crate::json;
12use crate::jwt;
13
14pub fn identity_from_auth_file(path: &Path) -> Result<Option<String>> {
15 let value = json::read_json(path)?;
16 let token = token_from_auth_json(&value);
17 let payload = token
18 .and_then(|tok| jwt::decode_payload_json(&tok))
19 .and_then(|payload| jwt::identity_from_payload(&payload));
20 Ok(payload)
21}
22
23pub fn email_from_auth_file(path: &Path) -> Result<Option<String>> {
24 let value = json::read_json(path)?;
25 let token = token_from_auth_json(&value);
26 let payload = token
27 .and_then(|tok| jwt::decode_payload_json(&tok))
28 .and_then(|payload| jwt::email_from_payload(&payload));
29 Ok(payload)
30}
31
32pub fn account_id_from_auth_file(path: &Path) -> Result<Option<String>> {
33 let value = json::read_json(path)?;
34 let account = json::string_at(&value, &["tokens", "account_id"])
35 .or_else(|| json::string_at(&value, &["account_id"]));
36 Ok(account)
37}
38
39pub fn last_refresh_from_auth_file(path: &Path) -> Result<Option<String>> {
40 let value = json::read_json(path)?;
41 Ok(json::string_at(&value, &["last_refresh"]))
42}
43
44pub fn identity_key_from_auth_file(path: &Path) -> Result<Option<String>> {
45 let identity = identity_from_auth_file(path)?;
46 let identity = match identity {
47 Some(value) => value,
48 None => return Ok(None),
49 };
50 let account_id = account_id_from_auth_file(path)?;
51 let key = match account_id {
52 Some(account) => format!("{}::{}", identity, account),
53 None => identity,
54 };
55 Ok(Some(key))
56}
57
58fn token_from_auth_json(value: &serde_json::Value) -> Option<String> {
59 json::string_at(value, &["tokens", "id_token"])
60 .or_else(|| json::string_at(value, &["tokens", "access_token"]))
61}