diff --git a/src/config.rs b/src/config.rs index 9a8137de513ecdd470e7bdb6e0d0fcc40689942e..b70ed6fb05e141b51e165fbf4254284ce9172c5e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -73,16 +73,3 @@ pub fn get_optional<'de, T: Deserialize<'de>>(conf: &config::Config, key: &str) }, } } - -pub fn get_or_error<'de, T: Deserialize<'de>, E>(config: &config::Config, key: &str, error_value: E) -> Result<T, E> { - match get_optional(config, key) { - Some(v) => { - debug!("Configuration key found: {}", key); - return Ok(v); - }, - None => { - error!("Configuration key not found: {}", key); - return Err(error_value); - }, - } -} diff --git a/src/oauth.rs b/src/oauth.rs index 0b6715674e856a8a381dc0ebb958d54c9edf16fd..a875b63b23482b0cf13c4124557ab7723050618e 100644 --- a/src/oauth.rs +++ b/src/oauth.rs @@ -14,7 +14,6 @@ */ use crate::config::{ - get_or_error, get_optional }; @@ -52,13 +51,22 @@ fn full_key(parts: Vec<&str>) -> String { parts.join(".") } +/// Construct an OAuth2 client ready to exchange credentials +/// +/// Arguments +/// --------- +/// +/// `conf` - reference to the config object +/// `prefix` - current prefix (probably `nss` or `pam`) +/// `error_value` - The value to return as Err result on error fn get_client<E: Copy>(conf: &Config, prefix: &str, error_value: E) -> Result<BasicClient, E> { - let client_id = ClientId::new(get_or_error(&conf, &full_key(vec![prefix, "client_id"]), error_value)?); + // Get all the configuration parameters + let client_id = ClientId::new(get_optional(&conf, &full_key(vec![prefix, "client_id"])).ok_or(error_value)?); let client_secret = match get_optional(&conf, &full_key(vec![prefix, "client_secret"])) { Some(v) => Some(ClientSecret::new(v)), None => None, }; - let auth_url = match AuthUrl::new(get_or_error(&conf, &full_key(vec![prefix, "auth_url"]), error_value)?) { + let auth_url = match AuthUrl::new(get_optional(&conf, &full_key(vec![prefix, "auth_url"])).ok_or(error_value)?) { Ok(u) => u, _ => { error!("Could not parse authorization URL"); @@ -76,6 +84,7 @@ fn get_client<E: Copy>(conf: &Config, prefix: &str, error_value: E) -> Result<Ba None => None, }; + // Construct an OAuth 2 client ready to exchange credentials let client = BasicClient::new(client_id, client_secret, auth_url, token_url); return Ok(client); } @@ -148,7 +157,7 @@ fn get_data(conf: &Config, prefix: &str, endpoint: &str, param: String, token: & let token_type = "Bearer".to_string(); // FIXME Probably we need to handle other token types // Retreieve endpoint URL from configuration - let mut endpoint_url: String = get_or_error(&conf, &full_key(vec![prefix, "urls", endpoint]), "")?; + let mut endpoint_url: String = get_optional(&conf, &full_key(vec![prefix, "urls", endpoint])).ok_or("")?; endpoint_url = endpoint_url.replace("{}", ¶m); debug!("Loading text data from {}", endpoint_url);