From a0494398fe91b745de2b104e0af7e853f3e00152 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Fri, 14 May 2021 11:11:56 +0200
Subject: [PATCH] [OAuth] Document get_client and remove get_or_error

---
 src/config.rs | 13 -------------
 src/oauth.rs  | 17 +++++++++++++----
 2 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/src/config.rs b/src/config.rs
index 9a8137d..b70ed6f 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 0b67156..a875b63 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("{}", &param);
     debug!("Loading text data from {}", endpoint_url);
 
-- 
GitLab