From d2a97be689b034ed056f3ba41a9775635872a073 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 18 Jul 2023 03:39:59 -0400 Subject: fix websockets for self-hosted bitwarden --- README.md | 3 +++ src/bin/rbw-agent/actions.rs | 13 ++++++------- src/config.rs | 12 ++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7ea61ee..f327ade 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ configuration options: * `identity_url`: The URL of the Bitwarden identity server to use. If unset, will use the `/identity` path on the configured `base_url`, or `https://identity.bitwarden.com/` if no `base_url` is set. +* `notifications_url`: The URL of the Bitwarden notifications server to use. + If unset, will use the `/notifications` path on the configured `base_url`, + or `https://notifications.bitwarden.com/` if no `base_url` is set. * `lock_timeout`: The number of seconds to keep the master keys in memory for before requiring the password to be entered again. Defaults to `3600` (one hour). diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs index c44f601..6291680 100644 --- a/src/bin/rbw-agent/actions.rs +++ b/src/bin/rbw-agent/actions.rs @@ -697,13 +697,12 @@ pub async fn subscribe_to_notifications( let access_token = db.access_token.context("Error getting access token")?; - let mut websocket_url = config - .base_url - .clone() - .expect("config is missing base url") - .replace("https://", "wss://") - + "/notifications/hub?access_token="; - websocket_url.push_str(&access_token); + let websocket_url = format!( + "{}/hub?access_token={}", + config.notifications_url(), + access_token + ) + .replace("https://", "wss://"); let mut state = state.lock().await; let err = state diff --git a/src/config.rs b/src/config.rs index d70a21b..03a9641 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,6 +8,7 @@ pub struct Config { pub email: Option, pub base_url: Option, pub identity_url: Option, + pub notifications_url: Option, #[serde(default = "default_lock_timeout")] pub lock_timeout: u64, #[serde(default = "default_sync_interval")] @@ -26,6 +27,7 @@ impl Default for Config { email: None, base_url: None, identity_url: None, + notifications_url: None, lock_timeout: default_lock_timeout(), sync_interval: default_sync_interval(), pinentry: default_pinentry(), @@ -158,6 +160,16 @@ impl Config { }) } + #[must_use] + pub fn notifications_url(&self) -> String { + self.notifications_url.clone().unwrap_or_else(|| { + self.base_url.clone().map_or_else( + || "https://notifications.bitwarden.com".to_string(), + |url| format!("{}/notifications", url.trim_end_matches('/')), + ) + }) + } + #[must_use] pub fn client_cert_path(&self) -> Option<&std::path::Path> { self.client_cert_path.as_deref() -- cgit v1.2.3-54-g00ecf