diff --git a/services/identity/src/websockets/errors.rs b/services/identity/src/websockets/errors.rs --- a/services/identity/src/websockets/errors.rs +++ b/services/identity/src/websockets/errors.rs @@ -5,6 +5,7 @@ )] pub enum WebsocketError { InvalidMessage, + InvalidSearchQuery, UnauthorizedDevice, SendError, SearchError, diff --git a/services/identity/src/websockets/mod.rs b/services/identity/src/websockets/mod.rs --- a/services/identity/src/websockets/mod.rs +++ b/services/identity/src/websockets/mod.rs @@ -11,7 +11,7 @@ use hyper_tungstenite::HyperWebsocket; use identity_search_messages::{ ConnectionInitializationResponse, ConnectionInitializationStatus, - SearchResult, User, + SearchQuery, SearchResult, User, }; use serde::{Deserialize, Serialize}; use tokio::net::TcpListener; @@ -23,7 +23,6 @@ use crate::config::CONFIG; use crate::constants::IDENTITY_SERVICE_WEBSOCKET_ADDR; use send::{send_error_response, send_message, WebsocketSink}; - pub mod errors; #[derive(Serialize, Deserialize)] @@ -141,6 +140,35 @@ } } +fn create_json_query( + request_message: &str, +) -> Result { + let search_request: SearchQuery = match serde_json::from_str(request_message) + { + Ok(search_request) => search_request, + Err(_) => { + return Err(errors::WebsocketError::InvalidSearchQuery); + } + }; + + match search_request { + SearchQuery::Prefix(prefix_request) => { + let prefix_query = Query { + query: Prefix { + prefix: Username { + username: prefix_request.prefix.trim().to_string(), + }, + }, + }; + + return serde_json::to_string(&prefix_query).map_err(|e| { + error!("Error serializing prefix query: {}", e); + errors::WebsocketError::SerializationError + }); + } + } +} + async fn accept_connection(hyper_ws: HyperWebsocket, addr: SocketAddr) { debug!("Incoming WebSocket connection from {}", addr); @@ -215,23 +243,11 @@ } } Ok(Message::Text(text)) => { - let prefix_query = Query { - query: Prefix { - prefix: Username { - username: text.trim().to_string(), - }, - }, - }; - - let json_body = match serde_json::to_string(&prefix_query) { + let json_body = match create_json_query(&text) { Ok(json_body) => json_body, Err(e) => { - error!("Error serializing prefix query: {}", e); - send_error_response( - errors::WebsocketError::SerializationError, - outgoing.clone(), - ) - .await; + error!("Error creating query: {}", e); + send_error_response(e, outgoing.clone()).await; continue; } }; diff --git a/shared/identity_search_messages/src/messages/mod.rs b/shared/identity_search_messages/src/messages/mod.rs --- a/shared/identity_search_messages/src/messages/mod.rs +++ b/shared/identity_search_messages/src/messages/mod.rs @@ -1,9 +1,11 @@ //! Messages sent from Identity Search server to client pub mod auth_messages; +pub mod search_query; pub mod search_result; pub use auth_messages::*; +pub use search_query::*; pub use search_result::*; use serde::{Deserialize, Serialize}; @@ -15,6 +17,7 @@ #[serde(untagged)] pub enum Messages { AuthMessage(AuthMessage), + SearchQuery(SearchQuery), Heartbeat(Heartbeat), ConnectionInitializationStatus(ConnectionInitializationStatus), ConnectionInitializationResponse(ConnectionInitializationResponse), diff --git a/shared/identity_search_messages/src/messages/search_query.rs b/shared/identity_search_messages/src/messages/search_query.rs new file mode 100644 --- /dev/null +++ b/shared/identity_search_messages/src/messages/search_query.rs @@ -0,0 +1,14 @@ +//! Search Request Messages sent by Client to Identity Search via WebSocket. + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Prefix { + pub prefix: String, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum SearchQuery { + Prefix(Prefix), +}