From a698ac16bc77475d53bd4c0dafffd1ec25d98a72 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Mon, 10 May 2021 00:11:23 +0200
Subject: [PATCH] [Config] Make scopes optional

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

diff --git a/src/config.rs b/src/config.rs
index ca3b3be..9a8137d 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -58,12 +58,16 @@ pub fn argv_to_config(argv: &Vec<String>) -> config::Config {
 }
 
 pub fn get_optional<'de, T: Deserialize<'de>>(conf: &config::Config, key: &str) -> Option<T> {
+    debug!("Looking up key {} in config", key);
     match conf.get(key) {
         Ok(v) => Some(v),
         Err(_) => {
             // Try falling back to parent block
             match key.find('.') {
-                Some(i) => get_optional(conf, &key[i+1..]),
+                Some(i) => {
+                    debug!("Key {} not found in config, trying parent block", key);
+                    get_optional(conf, &key[i+1..])
+                },
                 None => None,
             }
         },
diff --git a/src/oauth.rs b/src/oauth.rs
index c5d140b..ebdd5f3 100644
--- a/src/oauth.rs
+++ b/src/oauth.rs
@@ -71,7 +71,10 @@ fn get_client<E: Copy>(conf: Config, prefix: &str, error_value: E) -> Result<Bas
 }
 
 pub fn get_access_token<E: Copy>(conf: Config, prefix: &str, error_value: E, unauth_value: E) -> Result<BasicTokenResponse, E> {
-    let scopes: Vec<&str> = get_or_error(&conf, &full_key(prefix, "scopes"), error_value)?;
+    let scopes: Vec<String> = match get_optional(&conf, &full_key(prefix, "scopes")) {
+        Some(v) => v,
+        None => vec![]
+    };
 
     let client = get_client(conf, prefix, error_value)?;
     let mut request = client.exchange_client_credentials();
@@ -96,7 +99,10 @@ pub fn get_access_token<E: Copy>(conf: Config, prefix: &str, error_value: E, una
 }
 
 pub fn get_access_token_password<E: Copy>(conf: Config, prefix: &str, username: String, password: String, error_value: E, unauth_value: E) -> Result<BasicTokenResponse, E> {
-    let scopes: Vec<&str> = get_or_error(&conf, &full_key(prefix, "scopes"), error_value)?;
+    let scopes: Vec<String> = match get_optional(&conf, &full_key(prefix, "scopes")) {
+        Some(v) => v,
+        None => vec![]
+    };
 
     let res_username = ResourceOwnerUsername::new(username);
     let res_password = ResourceOwnerPassword::new(password);
-- 
GitLab