From 917fb507d6ff46be6816dbcebad1821241d6f1b2 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Fri, 27 Oct 2023 17:46:24 +0200 Subject: [PATCH] Addressed remarks given and some updates - Addressed comments given during review - Updated crates, including Rocket to the latest merged v0.5 changes - Removed an extra header which should not be sent for websocket connections --- .env.template | 4 ++-- src/api/admin.rs | 4 ++-- src/api/notifications.rs | 43 +++++++++++++++++++++------------------- src/config.rs | 4 ++-- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/.env.template b/.env.template index 3581c1afa6..8abf0e1cd9 100644 --- a/.env.template +++ b/.env.template @@ -84,8 +84,8 @@ ### WebSocket ### ################# -## Disable websocket notifications -# WEBSOCKET_DISABLED=false +## Enable websocket notifications +# ENABLE_WEBSOCKET=true ########################## ### Push notifications ### diff --git a/src/api/admin.rs b/src/api/admin.rs index dfd3e28fee..acaa36819f 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -316,7 +316,7 @@ async fn test_smtp(data: Json, _token: AdminToken) -> EmptyResult { #[get("/logout")] fn logout(cookies: &CookieJar<'_>) -> Redirect { - cookies.remove(Cookie::build(COOKIE_NAME).path(admin_path())); + cookies.remove(Cookie::build(COOKIE_NAME, "").path(admin_path()).finish()); Redirect::to(admin_path()) } @@ -797,7 +797,7 @@ impl<'r> FromRequest<'r> for AdminToken { if decode_admin(access_token).is_err() { // Remove admin cookie - cookies.remove(Cookie::build(COOKIE_NAME).path(admin_path())); + cookies.remove(Cookie::build(COOKIE_NAME, "").path(admin_path()).finish()); error!("Invalid or expired admin JWT. IP: {}.", &ip.ip); return Outcome::Error((Status::Unauthorized, "Session expired")); } diff --git a/src/api/notifications.rs b/src/api/notifications.rs index 119b4b1c2e..1f64b86edb 100644 --- a/src/api/notifications.rs +++ b/src/api/notifications.rs @@ -35,12 +35,14 @@ use super::{ push_send_update, push_user_update, }; +static NOTIFICATIONS_DISABLED: Lazy = Lazy::new(|| !CONFIG.enable_websocket() && !CONFIG.push_enabled()); + pub fn routes() -> Vec { - if CONFIG.websocket_disabled() { + if CONFIG.enable_websocket() { + routes![websockets_hub, anonymous_websockets_hub] + } else { info!("WebSocket are disabled, realtime sync functionality will not work!"); routes![] - } else { - routes![websockets_hub, anonymous_websockets_hub] } } @@ -339,7 +341,7 @@ impl WebSocketUsers { // NOTE: The last modified date needs to be updated before calling these methods pub async fn send_user_update(&self, ut: UpdateType, user: &User) { // Skip any processing if both WebSockets and Push are not active - if CONFIG.websocket_disabled() && !CONFIG.push_enabled() { + if *NOTIFICATIONS_DISABLED { return; } let data = create_update( @@ -348,7 +350,7 @@ impl WebSocketUsers { None, ); - if !CONFIG.websocket_disabled() { + if CONFIG.enable_websocket() { self.send_update(&user.uuid, &data).await; } @@ -359,7 +361,7 @@ impl WebSocketUsers { pub async fn send_logout(&self, user: &User, acting_device_uuid: Option) { // Skip any processing if both WebSockets and Push are not active - if CONFIG.websocket_disabled() && !CONFIG.push_enabled() { + if *NOTIFICATIONS_DISABLED { return; } let data = create_update( @@ -368,7 +370,7 @@ impl WebSocketUsers { acting_device_uuid.clone(), ); - if !CONFIG.websocket_disabled() { + if CONFIG.enable_websocket() { self.send_update(&user.uuid, &data).await; } @@ -385,7 +387,7 @@ impl WebSocketUsers { conn: &mut DbConn, ) { // Skip any processing if both WebSockets and Push are not active - if CONFIG.websocket_disabled() && !CONFIG.push_enabled() { + if *NOTIFICATIONS_DISABLED { return; } let data = create_update( @@ -398,7 +400,7 @@ impl WebSocketUsers { Some(acting_device_uuid.into()), ); - if !CONFIG.websocket_disabled() { + if CONFIG.enable_websocket() { self.send_update(&folder.user_uuid, &data).await; } @@ -417,7 +419,7 @@ impl WebSocketUsers { conn: &mut DbConn, ) { // Skip any processing if both WebSockets and Push are not active - if CONFIG.websocket_disabled() && !CONFIG.push_enabled() { + if *NOTIFICATIONS_DISABLED { return; } let org_uuid = convert_option(cipher.organization_uuid.clone()); @@ -445,7 +447,7 @@ impl WebSocketUsers { Some(acting_device_uuid.into()), ); - if !CONFIG.websocket_disabled() { + if CONFIG.enable_websocket() { for uuid in user_uuids { self.send_update(uuid, &data).await; } @@ -465,7 +467,7 @@ impl WebSocketUsers { conn: &mut DbConn, ) { // Skip any processing if both WebSockets and Push are not active - if CONFIG.websocket_disabled() && !CONFIG.push_enabled() { + if *NOTIFICATIONS_DISABLED { return; } let user_uuid = convert_option(send.user_uuid.clone()); @@ -480,7 +482,7 @@ impl WebSocketUsers { None, ); - if !CONFIG.websocket_disabled() { + if CONFIG.enable_websocket() { for uuid in user_uuids { self.send_update(uuid, &data).await; } @@ -498,7 +500,7 @@ impl WebSocketUsers { conn: &mut DbConn, ) { // Skip any processing if both WebSockets and Push are not active - if CONFIG.websocket_disabled() && !CONFIG.push_enabled() { + if *NOTIFICATIONS_DISABLED { return; } let data = create_update( @@ -506,7 +508,7 @@ impl WebSocketUsers { UpdateType::AuthRequest, Some(acting_device_uuid.to_string()), ); - if !CONFIG.websocket_disabled() { + if CONFIG.enable_websocket() { self.send_update(user_uuid, &data).await; } @@ -523,7 +525,7 @@ impl WebSocketUsers { conn: &mut DbConn, ) { // Skip any processing if both WebSockets and Push are not active - if CONFIG.websocket_disabled() && !CONFIG.push_enabled() { + if *NOTIFICATIONS_DISABLED { return; } let data = create_update( @@ -531,7 +533,7 @@ impl WebSocketUsers { UpdateType::AuthRequestResponse, approving_device_uuid.clone().into(), ); - if !CONFIG.websocket_disabled() { + if CONFIG.enable_websocket() { self.send_update(auth_response_uuid, &data).await; } @@ -557,14 +559,15 @@ impl AnonymousWebSocketSubscriptions { } pub async fn send_auth_response(&self, user_uuid: &String, auth_response_uuid: &str) { + if !CONFIG.enable_websocket() { + return; + } let data = create_anonymous_update( vec![("Id".into(), auth_response_uuid.to_owned().into()), ("UserId".into(), user_uuid.clone().into())], UpdateType::AuthRequestResponse, user_uuid.to_string(), ); - if !CONFIG.websocket_disabled() { - self.send_update(auth_response_uuid, &data).await; - } + self.send_update(auth_response_uuid, &data).await; } } diff --git a/src/config.rs b/src/config.rs index 9835f0018c..0d975e924d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -369,8 +369,8 @@ make_config! { web_vault_folder: String, false, def, "web-vault/".to_string(); }, ws { - /// Disable websocket notifications - websocket_disabled: bool, false, def, false; + /// Enable websocket notifications + enable_websocket: bool, false, def, true; }, push { /// Enable push notifications