From 625b9b20b073775bf6fcc90f58579d3b727ab849 Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Wed, 1 Oct 2025 10:03:55 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(credential):=20implement=20cre?= =?UTF-8?q?dential=20loading=20and=20conversion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add struct `Credential` for deserializing Google API credentials - implement `load_json_file` to load credentials from a JSON file - implement `From for yup_oauth2::ApplicationSecret` for conversion --- src/credential.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/credential.rs diff --git a/src/credential.rs b/src/credential.rs new file mode 100644 index 0000000..e02df66 --- /dev/null +++ b/src/credential.rs @@ -0,0 +1,51 @@ +use std::{env, fs, path::PathBuf}; + +use google_gmail1::yup_oauth2; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Installed { + pub(crate) client_id: String, + pub(crate) project_id: Option, + pub(crate) auth_uri: String, + pub(crate) token_uri: String, + pub(crate) auth_provider_x509_cert_url: Option, + pub(crate) client_secret: String, + pub(crate) redirect_uris: Vec, +} + +/// Struct for google credentials +#[derive(Debug, Serialize, Deserialize)] +pub struct Credential { + installed: Option, +} + +impl Credential { + /// Load the credential from a file. + pub fn load_json_file(path: &str) -> Self { + let home_dir = env::home_dir().unwrap(); + + let path = PathBuf::new().join(home_dir).join(".config").join(path); + let json_str = fs::read_to_string(path).expect("could not read path"); + + serde_json::from_str(&json_str).expect("could not convert to struct") + } +} + +impl From for yup_oauth2::ApplicationSecret { + fn from(value: Credential) -> Self { + let mut out = Self::default(); + if let Some(installed) = value.installed { + out.client_id = installed.client_id; + out.client_secret = installed.client_secret; + out.token_uri = installed.token_uri; + out.auth_uri = installed.auth_uri; + out.redirect_uris = installed.redirect_uris; + out.project_id = installed.project_id; + // out.client_email = installed.client_email; + out.auth_provider_x509_cert_url = installed.auth_provider_x509_cert_url; + // out.client_x509_cert_url = installed.client_x509_cert_url; + } + out + } +}