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
#![allow(dead_code)]
use serde_json;
use std::fs;
use std::io::{self, Read};
use std::path::Path;
use crate::service_account::ServiceAccountKey;
use crate::types::{ApplicationSecret, ConsoleApplicationSecret};
pub fn read_application_secret(path: &Path) -> io::Result<ApplicationSecret> {
let mut secret = String::new();
let mut file = fs::OpenOptions::new().read(true).open(path)?;
file.read_to_string(&mut secret)?;
parse_application_secret(&secret)
}
pub fn parse_application_secret<S: AsRef<str>>(secret: S) -> io::Result<ApplicationSecret> {
let result: serde_json::Result<ConsoleApplicationSecret> =
serde_json::from_str(secret.as_ref());
match result {
Err(e) => Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Bad application secret: {}", e),
)),
Ok(decoded) => {
if decoded.web.is_some() {
Ok(decoded.web.unwrap())
} else if decoded.installed.is_some() {
Ok(decoded.installed.unwrap())
} else {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"Unknown application secret format",
))
}
}
}
}
pub fn service_account_key_from_file<S: AsRef<Path>>(path: S) -> io::Result<ServiceAccountKey> {
let mut key = String::new();
let mut file = fs::OpenOptions::new().read(true).open(path)?;
file.read_to_string(&mut key)?;
match serde_json::from_str(&key) {
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, format!("{}", e))),
Ok(decoded) => Ok(decoded),
}
}