diff --git a/services/tunnelbroker/src/main.rs b/services/tunnelbroker/src/main.rs --- a/services/tunnelbroker/src/main.rs +++ b/services/tunnelbroker/src/main.rs @@ -13,7 +13,6 @@ use crate::notifs::NotifClient; use anyhow::{anyhow, Result}; use config::CONFIG; -use std::str::FromStr; use tracing::{self, error, info, Level}; use tracing_subscriber::EnvFilter; diff --git a/services/tunnelbroker/src/notifs/apns/mod.rs b/services/tunnelbroker/src/notifs/apns/mod.rs --- a/services/tunnelbroker/src/notifs/apns/mod.rs +++ b/services/tunnelbroker/src/notifs/apns/mod.rs @@ -6,7 +6,6 @@ use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION}; use reqwest::StatusCode; use serde::{Deserialize, Serialize}; -use serde_json::Value; use std::time::Duration; use tracing::debug; @@ -109,7 +108,7 @@ } pub async fn send(&self, notif: APNsNotif) -> Result<(), error::Error> { - debug!("Sending notif to {}", notif.device_token); + debug!("Sending APNs notif to {}", notif.device_token); let headers = self.build_headers(notif.headers.clone()).await?; diff --git a/services/tunnelbroker/src/notifs/fcm/mod.rs b/services/tunnelbroker/src/notifs/fcm/mod.rs --- a/services/tunnelbroker/src/notifs/fcm/mod.rs +++ b/services/tunnelbroker/src/notifs/fcm/mod.rs @@ -14,7 +14,7 @@ pub mod config; mod error; -mod firebase_message; +pub mod firebase_message; mod response; mod token; diff --git a/services/tunnelbroker/src/websockets/session.rs b/services/tunnelbroker/src/websockets/session.rs --- a/services/tunnelbroker/src/websockets/session.rs +++ b/services/tunnelbroker/src/websockets/session.rs @@ -30,6 +30,9 @@ use crate::identity; use crate::notifs::apns::headers::NotificationHeaders; use crate::notifs::apns::APNsNotif; +use crate::notifs::fcm::firebase_message::{ + AndroidConfig, AndroidMessagePriority, FCMMessage, +}; use crate::notifs::NotifClient; pub struct DeviceInfo { @@ -64,6 +67,7 @@ PersistenceError(SdkError), DatabaseError(comm_lib::database::Error), MissingAPNsClient, + MissingFCMClient, MissingDeviceToken, } @@ -403,6 +407,64 @@ Err(SessionError::MissingAPNsClient), )) } + Messages::FCMNotif(notif) => { + // unauthenticated clients cannot send notifs + if !self.device_info.is_authenticated { + debug!( + "Unauthenticated device {} tried to send text notif. Aborting.", + self.device_info.device_id + ); + return Some(MessageSentStatus::Unauthenticated); + } + debug!("Received FCM notif for {}", notif.device_id); + + let Some(priority) = AndroidMessagePriority::from_str(¬if.priority) + else { + return Some(MessageSentStatus::SerializationError(notif.priority)); + }; + + let Ok(data) = serde_json::from_str(¬if.data) else { + return Some(MessageSentStatus::SerializationError(notif.data)); + }; + + let device_token = + match self.db_client.get_device_token(¬if.device_id).await { + Ok(db_token) => { + let Some(token) = db_token else { + return Some(self.get_message_to_device_status( + ¬if.client_message_id, + Err(SessionError::MissingDeviceToken), + )); + }; + token + } + Err(e) => { + return Some(self.get_message_to_device_status( + ¬if.client_message_id, + Err(SessionError::DatabaseError(e)), + )); + } + }; + + let fcm_message = FCMMessage { + data, + token: device_token.to_string(), + android: AndroidConfig { priority }, + }; + + if let Some(fcm) = self.notif_client.fcm.clone() { + let response = fcm.send(fcm_message).await; + return Some( + self + .get_message_to_device_status(¬if.client_message_id, response), + ); + } + + Some(self.get_message_to_device_status( + ¬if.client_message_id, + Err(SessionError::MissingFCMClient), + )) + } _ => { error!("Client sent invalid message type"); Some(MessageSentStatus::InvalidRequest)