From 6c9c97d17ea89edafb6f9052902a227ce8dca377 Mon Sep 17 00:00:00 2001 From: toto-xoxo <85445598+toto-xoxo@users.noreply.github.com> Date: Fri, 4 Aug 2023 19:56:46 +0200 Subject: [PATCH] fix cargo check + rewrite config + add check url --- .env.template | 3 --- src/api/push.rs | 3 +-- src/config.rs | 46 +++++++++++++++++++++++++++++++++------------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/.env.template b/.env.template index 029ab36b..0a7cbed3 100644 --- a/.env.template +++ b/.env.template @@ -81,9 +81,6 @@ # PUSH_INSTALLATION_ID=CHANGEME # PUSH_INSTALLATION_KEY=CHANGEME # PUSH_RELAY_REGION=us -## Don't change this unless you know what you're doing. -# PUSH_RELAY_URI=https://push.bitwarden.com -# IDENTITY_URI=https://identity.bitwarden.com ## Controls whether users are allowed to create Bitwarden Sends. ## This setting applies globally to all users. diff --git a/src/api/push.rs b/src/api/push.rs index b6c52abb..31fe97aa 100644 --- a/src/api/push.rs +++ b/src/api/push.rs @@ -50,8 +50,7 @@ async fn get_auth_push_token() -> ApiResult { ("client_secret", &client_secret), ]; - let res = match get_reqwest_client().post(CONFIG.identity_uri()).form(¶ms).send().await - { + let res = match get_reqwest_client().post(CONFIG.push_identity_uri()).form(¶ms).send().await { Ok(r) => r, Err(e) => err!(format!("Error getting push token from bitwarden server: {e}")), }; diff --git a/src/config.rs b/src/config.rs index 758aff83..87635e81 100644 --- a/src/config.rs +++ b/src/config.rs @@ -380,25 +380,25 @@ make_config! { push { /// Enable push notifications push_enabled: bool, false, def, false; - /// Push relay region |> The data region from https://bitwarden.com/host + /// Push relay region push_relay_region: String, false, def, "us".to_string(); /// Push relay uri push_relay_uri: String, false, auto, |c| { - let relay_region = match c.push_relay_region.as_str() { - "us" => "com", - "eu" => "eu", - _ => "com", // Default to US if the region is not recognized + let push_relay_uri = match c.push_relay_region.as_str() { + "us" => "https://push.bitwarden.com".to_string(), + "eu" => "https://push.bitwarden.eu".to_string(), + _ => "https://push.bitwarden.com".to_string(), // Default to "us" region }; - format!("https://push.bitwarden.{}", relay_region) + return push_relay_uri; }; - /// Identity uri - identity_uri: String, false, auto, |c| { - let relay_region = match c.push_relay_region.as_str() { - "us" => "com", - "eu" => "eu", - _ => "com", // Default to US if the region is not recognized + /// Push identity uri + push_identity_uri: String, false, auto, |c| { + let push_identity_uri = match c.push_relay_region.as_str() { + "us" => "https://identity.bitwarden.com".to_string(), + "eu" => "https://identity.bitwarden.eu".to_string(), + _ => "https://identity.bitwarden.com".to_string(), // Default to "us" region }; - format!("https://identity.bitwarden.{}", relay_region) + return push_identity_uri; }; /// Installation id |> The installation id from https://bitwarden.com/host push_installation_id: Pass, false, def, String::new(); @@ -769,6 +769,26 @@ fn validate_config(cfg: &ConfigItems) -> Result<(), Error> { ) } + if cfg.push_enabled { + let push_relay_uri = cfg.push_relay_uri.to_lowercase(); + if !push_relay_uri.starts_with("https://") { + err!("`PUSH_RELAY_URI` must start with 'https://'.") + } + + if let Err(_) = Url::parse(&push_relay_uri) { + err!("Invalid URL format for `PUSH_RELAY_URI`."); + } + + let push_identity_uri = cfg.push_identity_uri.to_lowercase(); + if !push_identity_uri.starts_with("https://") { + err!("`PUSH_IDENTITY_URI` must start with 'https://'.") + } + + if let Err(_) = Url::parse(&push_identity_uri) { + err!("Invalid URL format for `PUSH_IDENTITY_URI`."); + } + } + if cfg._enable_duo && (cfg.duo_host.is_some() || cfg.duo_ikey.is_some() || cfg.duo_skey.is_some()) && !(cfg.duo_host.is_some() && cfg.duo_ikey.is_some() && cfg.duo_skey.is_some())