Changeset View
Changeset View
Standalone View
Standalone View
shared/tunnelbroker_messages/src/messages/session.rs
// Messages sent between tunnelbroker and a device | // Messages sent between tunnelbroker and a device | ||||
use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||
/// The workflow when estabilishing a tunnelbroker connection: | /// The workflow when estabilishing a tunnelbroker connection: | ||||
/// - Client sends SessionRequest | /// - Client sends ConnectionInitializationMessage | ||||
/// - Tunnelbroker validates access_token with identity service | /// - Tunnelbroker validates access_token with identity service | ||||
/// - Tunnelbroker emits an AMQP message declaring that it has opened a new | /// - Tunnelbroker emits an AMQP message declaring that it has opened a new | ||||
/// connection with a given device, so that the respective tunnelbroker | /// connection with a given device, so that the respective tunnelbroker | ||||
/// instance can close the existing connection. | /// instance can close the existing connection. | ||||
/// - Tunnelbroker returns a session_id representing that the connection was | /// - Tunnelbroker returns a session_id representing that the connection was | ||||
/// accepted | /// accepted | ||||
/// - Tunnelbroker will flush all messages related to device from RabbitMQ. | /// - Tunnelbroker will flush all messages related to device from RabbitMQ. | ||||
/// This must be done first before flushing DynamoDB to prevent duplicated | /// This must be done first before flushing DynamoDB to prevent duplicated | ||||
Show All 11 Lines | pub enum DeviceTypes { | ||||
Keyserver, | Keyserver, | ||||
} | } | ||||
/// Message sent by a client to tunnelbroker to initiate a websocket | /// Message sent by a client to tunnelbroker to initiate a websocket | ||||
/// session. Tunnelbroker will then validate the access token with identity | /// session. Tunnelbroker will then validate the access token with identity | ||||
/// service before continuing with the request. | /// service before continuing with the request. | ||||
#[derive(Serialize, Deserialize)] | #[derive(Serialize, Deserialize)] | ||||
#[serde(tag = "type", rename_all = "camelCase")] | #[serde(tag = "type", rename_all = "camelCase")] | ||||
pub struct SessionRequest { | pub struct ConnectionInitializationMessage { | ||||
pub device_id: String, | pub device_id: String, | ||||
pub user_id: String, | |||||
pub access_token: String, | pub access_token: String, | ||||
pub notify_token: Option<String>, | pub notify_token: Option<String>, | ||||
pub device_type: DeviceTypes, | pub device_type: DeviceTypes, | ||||
pub device_app_version: Option<String>, | pub device_app_version: Option<String>, | ||||
pub device_os: Option<String>, | pub device_os: Option<String>, | ||||
} | } | ||||
#[derive(Serialize, Deserialize)] | #[derive(Serialize, Deserialize)] | ||||
pub struct SessionResponse { | pub struct ConnectionInitializationResponse { | ||||
pub session_id: String, | pub session_id: String, | ||||
} | } | ||||
#[cfg(test)] | #[cfg(test)] | ||||
mod session_tests { | mod session_tests { | ||||
use super::*; | use super::*; | ||||
#[test] | #[test] | ||||
fn test_session_deserialization() { | fn test_session_deserialization() { | ||||
let example_payload = r#"{ | let example_payload = r#"{ | ||||
"type": "sessionRequest", | "type": "sessionRequest", | ||||
"accessToken": "xkdeifjsld", | "accessToken": "xkdeifjsld", | ||||
"deviceId": "foo", | "deviceId": "foo", | ||||
"deviceType": "keyserver" | "deviceType": "keyserver" | ||||
}"#; | }"#; | ||||
let request = | let request = | ||||
serde_json::from_str::<SessionRequest>(example_payload).unwrap(); | serde_json::from_str::<ConnectionInitializationMessage>(example_payload) | ||||
.unwrap(); | |||||
assert_eq!(request.device_id, "foo"); | assert_eq!(request.device_id, "foo"); | ||||
assert_eq!(request.access_token, "xkdeifjsld"); | assert_eq!(request.access_token, "xkdeifjsld"); | ||||
assert_eq!(request.device_os, None); | assert_eq!(request.device_os, None); | ||||
assert_eq!(request.device_type, DeviceTypes::Keyserver); | assert_eq!(request.device_type, DeviceTypes::Keyserver); | ||||
} | } | ||||
} | } |