1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
use super::{helpers, MessageDirection, WampMessage};
use crate::roles::Roles;
use serde::{de::Visitor, Deserialize, Serialize};
use serde_json::Value;
use std::marker::PhantomData;
#[derive(Debug, Clone, PartialEq, Eq)]
/// # Authenticate - [wamp-proto](https://wamp-proto.org/wamp_latest_ietf.html#name-authenticate)
/// Represents an Authentication message in the WAMP protocol.
/// ## Examples
/// ```
/// use wamp_core::messages::Authenticate;
/// use wamp_core::authenticate;
/// use serde_json::json;
/// # let mut auth_message2 = authenticate!("signature");
///
/// let auth_message = Authenticate {
/// signature: "signature".to_string(),
/// details: json!({})
/// };
///
/// # assert_eq!(auth_message, auth_message2);
/// ```
/// ### Serializer
/// Implements serde Serialize trait for Authenticate
/// ```
/// use wamp_core::messages::Authenticate;
/// use serde_json::{json, to_string};
///
/// // Create an Authenticate message
/// let auth = Authenticate {
/// signature: "signature".to_string(),
/// details: json!({ "key": "value" })
/// };
///
/// // Establish raw json data string
/// let data = r#"[5,"signature",{"key":"value"}]"#;
///
/// // Here we convert it from an `Authenticate` frame, to a string representation.
/// let auth = to_string(&auth).unwrap();
///
/// // Confirm that our auth frame strings are equal to each other
/// assert_eq!(auth, data);
/// ```
/// ### Deserializer
/// Implements serde Deserialize trait for Authenticate
/// ```
/// use wamp_core::messages::Authenticate;
/// use serde_json::from_str;
///
/// // Here is our raw json data string
/// let data = r#"[5,"signature",{}]"#;
///
/// // Here we convert it to an `Authenticate` frame
/// let auth = from_str::<Authenticate>(data).unwrap();
///
/// // Confirm that our signature and details deserialized
/// assert_eq!(auth.signature, "signature");
/// assert_eq!(auth.details, serde_json::json!({}));
/// ```
pub struct Authenticate {
pub signature: String,
pub details: Value,
}
#[macro_export]
/// # Authenticate Macro - [wamp-proto](https://wamp-proto.org/wamp_latest_ietf.html#name-authenticate)
/// Macro that allows for default empty implementation of details object on Authenticate.
/// ## Examples
/// ```
/// use wamp_core::messages::{self, Authenticate};
/// use wamp_core::authenticate;
/// use serde_json::json;
///
/// // Construct with default empty details object
/// let mut auth_message = authenticate!("signature");
/// assert_eq!(auth_message.details, json!({}));
///
/// // Construct with custom details
/// let auth_message2 = authenticate!("signature", json!({
/// "key": "value"
/// }));
///
/// assert_ne!(auth_message, auth_message2);
/// auth_message.details = json!({ "key": "value" });
/// assert_eq!(auth_message, auth_message2);
///
/// // These macro invocations are the same as the following:
/// let auth_message3 = Authenticate {
/// signature: "signature".to_string(),
/// details: json!({
/// "key": "value"
/// })
/// };
///
/// assert_eq!(auth_message, auth_message3);
/// assert_eq!(auth_message2, auth_message3);
/// ```
macro_rules! authenticate {
($signature:expr) => {
authenticate! {$signature, serde_json::json!({})}
};
($signature:expr, $details:expr) => {
$crate::messages::Authenticate {
signature: $signature.to_string(),
details: $details,
}
};
}
impl WampMessage for Authenticate {
const ID: u64 = 5;
fn direction(role: Roles) -> &'static MessageDirection {
match role {
Roles::Callee => &MessageDirection {
receives: &false,
sends: &true,
},
Roles::Caller => &MessageDirection {
receives: &false,
sends: &true,
},
Roles::Publisher => &MessageDirection {
receives: &false,
sends: &true,
},
Roles::Subscriber => &MessageDirection {
receives: &false,
sends: &true,
},
Roles::Dealer => &MessageDirection {
receives: &true,
sends: &false,
},
Roles::Broker => &MessageDirection {
receives: &true,
sends: &false,
},
}
}
}
impl Serialize for Authenticate {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let details =
helpers::ser_value_is_object::<S, _>(&self.details, "Details must be object like.")?;
(Self::ID, &self.signature, details).serialize(serializer)
}
}
impl<'de> Deserialize<'de> for Authenticate {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct AuthenticateVisitor(PhantomData<u8>, PhantomData<String>, PhantomData<Value>);
impl<'vi> Visitor<'vi> for AuthenticateVisitor {
type Value = Authenticate;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("Wamp message containing authentication details")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'vi>,
{
let message_id: u64 = helpers::deser_seq_element(
&mut seq,
"Message ID must be present and type u8.",
)?;
helpers::validate_id::<Authenticate, A, _>(&message_id, "Authenticate")?;
let signature: String =
helpers::deser_seq_element(&mut seq, "Signature must be type String.")?;
let details: Value = helpers::deser_seq_element(
&mut seq,
"Details must be present and object like.",
)?;
helpers::deser_value_is_object::<A, _>(&details, "Value must be object like")?;
Ok(Authenticate { signature, details })
}
}
deserializer.deserialize_struct(
"Authenticate",
&["signature", "details"],
AuthenticateVisitor(PhantomData, PhantomData, PhantomData),
)
}
}