Skip to content
Snippets Groups Projects
Verified Commit a0494398 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

[OAuth] Document get_client and remove get_or_error

parent 2a85c588
No related branches found
No related tags found
No related merge requests found
......@@ -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);
},
}
}
......@@ -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("{}", &param);
debug!("Loading text data from {}", endpoint_url);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment