use std::{fs, path::PathBuf};
use config::Config;
use google_gmail1::yup_oauth2::{ApplicationSecret, ConsoleApplicationSecret};
use crate::Result;
mod config_root;
use config_root::ConfigRoot;
#[derive(Debug)]
pub struct ClientConfig {
secret: ApplicationSecret,
config_root: ConfigRoot,
persist_path: String,
}
impl ClientConfig {
pub fn builder() -> ConfigBuilder {
ConfigBuilder::default()
}
pub fn new_from_configuration(configs: Config) -> Result<Self> {
log::debug!("Configurations: {configs:#?}");
let root = configs.get_string("config_root")?;
let config_root = ConfigRoot::parse(&root);
log::trace!("Configs are: {configs:#?}");
let secret = if let Ok(client_id) = configs.get_string("client_id")
&& let Ok(client_secret) = configs.get_string("client_secret")
&& let Ok(token_uri) = configs.get_string("token_uri")
&& let Ok(auth_uri) = configs.get_string("auth_uri")
{
log::info!("Generating the application secret from the environment!");
ApplicationSecret {
client_id,
client_secret,
token_uri,
auth_uri,
project_id: None,
redirect_uris: Vec::new(),
client_email: None,
auth_provider_x509_cert_url: None,
client_x509_cert_url: None,
}
} else {
log::info!("Generating the application secret from the credential file!");
let credential_file = configs.get_string("credential_file")?;
log::info!("root: {config_root}");
let path = config_root.full_path().join(credential_file);
log::info!("path: {}", path.display());
let json_str = fs::read_to_string(path).expect("could not read path");
let console: ConsoleApplicationSecret =
serde_json::from_str(&json_str).expect("could not convert to struct");
console.installed.unwrap()
};
let persist_path = format!("{}/gmail1", config_root.full_path().display());
Ok(ClientConfig {
config_root,
secret,
persist_path,
})
}
pub fn secret(&self) -> &ApplicationSecret {
&self.secret
}
pub fn persist_path(&self) -> &str {
&self.persist_path
}
pub fn config_root(&self) -> &ConfigRoot {
&self.config_root
}
pub fn full_path(&self) -> String {
self.config_root.full_path().display().to_string()
}
}
#[derive(Debug)]
pub struct ConfigBuilder {
secret: ApplicationSecret,
config_root: ConfigRoot,
}
impl Default for ConfigBuilder {
fn default() -> Self {
let secret = ApplicationSecret {
auth_uri: "https://accounts.google.com/o/oauth2/auth".to_string(),
token_uri: "https://oauth2.googleapis.com/token".to_string(),
..Default::default()
};
Self {
secret,
config_root: Default::default(),
}
}
}
impl ConfigBuilder {
pub fn with_config_base(&mut self, value: &config_root::RootBase) -> &mut Self {
self.config_root.set_root_base(value);
self
}
pub fn with_config_path(&mut self, value: &str) -> &mut Self {
self.config_root.set_path(value);
self
}
pub fn with_credential_file(&mut self, credential_file: &str) -> &mut Self {
let path = PathBuf::from(self.config_root.to_string()).join(credential_file);
log::info!("path: {}", path.display());
let json_str = fs::read_to_string(path).expect("could not read path");
let console: ConsoleApplicationSecret =
serde_json::from_str(&json_str).expect("could not convert to struct");
self.secret = console.installed.unwrap();
self
}
pub fn with_client_id(&mut self, value: &str) -> &mut Self {
self.secret.client_id = value.to_string();
self
}
pub fn with_client_secret(&mut self, value: &str) -> &mut Self {
self.secret.client_secret = value.to_string();
self
}
pub fn with_token_uri(&mut self, value: &str) -> &mut Self {
self.secret.token_uri = value.to_string();
self
}
pub fn with_auth_uri(&mut self, value: &str) -> &mut Self {
self.secret.auth_uri = value.to_string();
self
}
pub fn add_redirect_uri(&mut self, value: &str) -> &mut Self {
self.secret.redirect_uris.push(value.to_string());
self
}
pub fn with_project_id(&mut self, value: &str) -> &mut Self {
self.secret.project_id = Some(value.to_string());
self
}
pub fn with_client_email(&mut self, value: &str) -> &mut Self {
self.secret.client_email = Some(value.to_string());
self
}
pub fn with_auth_provider_x509_cert_url(&mut self, value: &str) -> &mut Self {
self.secret.auth_provider_x509_cert_url = Some(value.to_string());
self
}
pub fn with_client_x509_cert_url(&mut self, value: &str) -> &mut Self {
self.secret.client_x509_cert_url = Some(value.to_string());
self
}
fn full_path(&self) -> String {
self.config_root.full_path().display().to_string()
}
pub fn build(&self) -> ClientConfig {
let persist_path = format!("{}/gmail1", self.full_path());
ClientConfig {
secret: self.secret.clone(),
config_root: self.config_root.clone(),
persist_path,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::get_test_logger;
use config::Config;
use std::env;
use std::fs;
use tempfile::TempDir;
fn create_test_credential_file(temp_dir: &TempDir, filename: &str, content: &str) -> String {
let file_path = temp_dir.path().join(filename);
fs::write(&file_path, content).expect("Failed to write test file");
file_path.to_string_lossy().to_string()
}
fn sample_valid_credential() -> &'static str {
r#"{
"installed": {
"client_id": "123456789-test.googleusercontent.com",
"project_id": "test-project",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "test-client-secret",
"redirect_uris": ["http://localhost"]
}
}"#
}
#[test]
fn test_config_builder_defaults() {
let builder = ConfigBuilder::default();
assert_eq!(
builder.secret.auth_uri,
"https://accounts.google.com/o/oauth2/auth"
);
assert_eq!(
builder.secret.token_uri,
"https://oauth2.googleapis.com/token"
);
assert!(builder.secret.client_id.is_empty());
assert!(builder.secret.client_secret.is_empty());
}
#[test]
fn test_builder_pattern_direct_oauth2() {
let config = ClientConfig::builder()
.with_client_id("test-client-id")
.with_client_secret("test-client-secret")
.with_auth_uri("https://auth.example.com")
.with_token_uri("https://token.example.com")
.add_redirect_uri("http://localhost:8080")
.add_redirect_uri("http://localhost:3000")
.with_project_id("test-project")
.with_client_email("test@example.com")
.with_auth_provider_x509_cert_url("https://certs.example.com")
.with_client_x509_cert_url("https://client-cert.example.com")
.build();
assert_eq!(config.secret().client_id, "test-client-id");
assert_eq!(config.secret().client_secret, "test-client-secret");
assert_eq!(config.secret().auth_uri, "https://auth.example.com");
assert_eq!(config.secret().token_uri, "https://token.example.com");
assert_eq!(
config.secret().redirect_uris,
vec!["http://localhost:8080", "http://localhost:3000"]
);
assert_eq!(config.secret().project_id, Some("test-project".to_string()));
assert_eq!(
config.secret().client_email,
Some("test@example.com".to_string())
);
assert_eq!(
config.secret().auth_provider_x509_cert_url,
Some("https://certs.example.com".to_string())
);
assert_eq!(
config.secret().client_x509_cert_url,
Some("https://client-cert.example.com".to_string())
);
assert!(config.persist_path().contains("gmail1"));
}
#[test]
fn test_builder_with_config_path() {
let config = ClientConfig::builder()
.with_client_id("test-id")
.with_config_path(".test-config")
.build();
let full_path = config.full_path();
assert_eq!(full_path, ".test-config");
assert!(config.persist_path().contains(".test-config/gmail1"));
}
#[test]
fn test_builder_with_config_base_home() {
let config = ClientConfig::builder()
.with_client_id("test-id")
.with_config_base(&config_root::RootBase::Home)
.with_config_path(".test-config")
.build();
let expected_path = env::home_dir()
.unwrap_or_default()
.join(".test-config")
.display()
.to_string();
assert_eq!(config.full_path(), expected_path);
}
#[test]
fn test_builder_with_config_base_root() {
let config = ClientConfig::builder()
.with_client_id("test-id")
.with_config_base(&config_root::RootBase::Root)
.with_config_path("etc/test-config")
.build();
assert_eq!(config.full_path(), "/etc/test-config");
}
#[test]
fn test_config_from_direct_oauth2_params() {
let app_config = Config::builder()
.set_default("client_id", "direct-client-id")
.unwrap()
.set_default("client_secret", "direct-client-secret")
.unwrap()
.set_default("token_uri", "https://token.direct.com")
.unwrap()
.set_default("auth_uri", "https://auth.direct.com")
.unwrap()
.set_default("config_root", "h:.test-direct")
.unwrap()
.build()
.unwrap();
let config = ClientConfig::new_from_configuration(app_config).unwrap();
assert_eq!(config.secret().client_id, "direct-client-id");
assert_eq!(config.secret().client_secret, "direct-client-secret");
assert_eq!(config.secret().token_uri, "https://token.direct.com");
assert_eq!(config.secret().auth_uri, "https://auth.direct.com");
assert_eq!(config.secret().project_id, None);
assert!(config.secret().redirect_uris.is_empty());
}
#[test]
fn test_config_from_credential_file() {
get_test_logger();
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let _cred_file =
create_test_credential_file(&temp_dir, "test_creds.json", sample_valid_credential());
let config_root = format!("c:{}", temp_dir.path().display());
let app_config = Config::builder()
.set_default("credential_file", "test_creds.json")
.unwrap()
.set_default("config_root", config_root.as_str())
.unwrap()
.build()
.unwrap();
let config = ClientConfig::new_from_configuration(app_config).unwrap();
assert_eq!(
config.secret().client_id,
"123456789-test.googleusercontent.com"
);
assert_eq!(config.secret().client_secret, "test-client-secret");
assert_eq!(config.secret().project_id, Some("test-project".to_string()));
assert_eq!(config.secret().redirect_uris, vec!["http://localhost"]);
}
#[test]
fn test_config_missing_required_params() {
let app_config = Config::builder()
.set_default("client_id", "test-id")
.unwrap()
.build()
.unwrap();
let result = ClientConfig::new_from_configuration(app_config);
assert!(result.is_err());
}
#[test]
fn test_config_incomplete_oauth2_params() {
let app_config = Config::builder()
.set_default("client_id", "test-id")
.unwrap()
.set_default("client_secret", "test-secret")
.unwrap()
.set_default("config_root", "h:.test")
.unwrap()
.build()
.unwrap();
let result = ClientConfig::new_from_configuration(app_config);
assert!(result.is_err());
}
#[test]
#[should_panic(expected = "could not read path")]
fn test_config_invalid_credential_file() {
let app_config = Config::builder()
.set_default("credential_file", "nonexistent.json")
.unwrap()
.set_default("config_root", "h:.test")
.unwrap()
.build()
.unwrap();
let _result = ClientConfig::new_from_configuration(app_config);
}
#[test]
#[should_panic(expected = "could not convert to struct")]
fn test_config_malformed_credential_file() {
get_test_logger();
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let _cred_file = create_test_credential_file(&temp_dir, "malformed.json", "{ invalid json");
let config_root = format!("c:{}", temp_dir.path().display());
let app_config = Config::builder()
.set_default("credential_file", "malformed.json")
.unwrap()
.set_default("config_root", config_root.as_str())
.unwrap()
.build()
.unwrap();
let _result = ClientConfig::new_from_configuration(app_config);
}
#[test]
#[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
fn test_config_credential_file_wrong_structure() {
get_test_logger();
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let wrong_structure = r#"{"wrong": "structure"}"#;
let _cred_file = create_test_credential_file(&temp_dir, "wrong.json", wrong_structure);
let config_root = format!("c:{}", temp_dir.path().display());
let app_config = Config::builder()
.set_default("credential_file", "wrong.json")
.unwrap()
.set_default("config_root", config_root.as_str())
.unwrap()
.build()
.unwrap();
let _result = ClientConfig::new_from_configuration(app_config);
}
#[test]
fn test_persist_path_generation() {
let config = ClientConfig::builder()
.with_client_id("test")
.with_config_path("/custom/path")
.build();
assert_eq!(config.persist_path(), "/custom/path/gmail1");
}
#[test]
fn test_config_accessor_methods() {
let config = ClientConfig::builder()
.with_client_id("accessor-test-id")
.with_client_secret("accessor-test-secret")
.with_config_path("/test/path")
.build();
let secret = config.secret();
assert_eq!(secret.client_id, "accessor-test-id");
assert_eq!(secret.client_secret, "accessor-test-secret");
assert_eq!(config.persist_path(), "/test/path/gmail1");
assert_eq!(config.full_path(), "/test/path");
let config_root = config.config_root();
assert_eq!(config_root.full_path().display().to_string(), "/test/path");
}
#[test]
fn test_builder_method_chaining() {
let config = ClientConfig::builder()
.with_client_id("chain-test")
.with_client_secret("chain-secret")
.with_auth_uri("https://auth.chain.com")
.with_token_uri("https://token.chain.com")
.add_redirect_uri("http://redirect1.com")
.add_redirect_uri("http://redirect2.com")
.with_project_id("chain-project")
.with_client_email("chain@test.com")
.with_auth_provider_x509_cert_url("https://cert1.com")
.with_client_x509_cert_url("https://cert2.com")
.with_config_base(&config_root::RootBase::Home)
.with_config_path(".chain-test")
.build();
assert_eq!(config.secret().client_id, "chain-test");
assert_eq!(config.secret().redirect_uris.len(), 2);
}
#[test]
fn test_configuration_priority() {
get_test_logger();
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let _cred_file =
create_test_credential_file(&temp_dir, "priority.json", sample_valid_credential());
let config_root = format!("c:{}", temp_dir.path().display());
let app_config = Config::builder()
.set_default("client_id", "priority-client-id")
.unwrap()
.set_default("client_secret", "priority-client-secret")
.unwrap()
.set_default("token_uri", "https://priority.token.com")
.unwrap()
.set_default("auth_uri", "https://priority.auth.com")
.unwrap()
.set_default("credential_file", "priority.json")
.unwrap()
.set_default("config_root", config_root.as_str())
.unwrap()
.build()
.unwrap();
let config = ClientConfig::new_from_configuration(app_config).unwrap();
assert_eq!(config.secret().client_id, "priority-client-id");
assert_eq!(config.secret().client_secret, "priority-client-secret");
assert_eq!(config.secret().token_uri, "https://priority.token.com");
assert_ne!(
config.secret().client_id,
"123456789-test.googleusercontent.com"
); }
#[test]
fn test_empty_redirect_uris() {
let config = ClientConfig::builder().with_client_id("test-id").build();
assert!(config.secret().redirect_uris.is_empty());
}
#[test]
fn test_multiple_redirect_uris() {
let config = ClientConfig::builder()
.with_client_id("test-id")
.add_redirect_uri("http://localhost:8080")
.add_redirect_uri("http://localhost:3000")
.add_redirect_uri("https://example.com/callback")
.build();
assert_eq!(config.secret().redirect_uris.len(), 3);
assert!(
config
.secret()
.redirect_uris
.contains(&"http://localhost:8080".to_string())
);
assert!(
config
.secret()
.redirect_uris
.contains(&"http://localhost:3000".to_string())
);
assert!(
config
.secret()
.redirect_uris
.contains(&"https://example.com/callback".to_string())
);
}
#[test]
fn test_optional_fields() {
let config = ClientConfig::builder()
.with_client_id("optional-test")
.build();
assert_eq!(config.secret().project_id, None);
assert_eq!(config.secret().client_email, None);
assert_eq!(config.secret().auth_provider_x509_cert_url, None);
assert_eq!(config.secret().client_x509_cert_url, None);
}
#[test]
fn test_unicode_in_configuration() {
let config = ClientConfig::builder()
.with_client_id("unicode-客戶端-🔐-test")
.with_client_secret("secret-with-unicode-密碼")
.with_project_id("project-項目-id")
.with_config_path(".unicode-配置")
.build();
assert_eq!(config.secret().client_id, "unicode-客戶端-🔐-test");
assert_eq!(config.secret().client_secret, "secret-with-unicode-密碼");
assert_eq!(
config.secret().project_id,
Some("project-項目-id".to_string())
);
assert!(config.full_path().contains(".unicode-配置"));
}
}