use hmac::{Hmac, Mac};
use serde_json::json;
use sha2::Sha256;
use crate::webpush::MAX_PLAINTEXT;
use crate::{Error, Result};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Notice {
pub h: String,
pub kind: String,
pub msg: String,
pub ts: u64,
pub text: String,
}
const MAC_LEN: usize = 16;
impl Notice {
pub fn mac(&self, mk: &[u8]) -> Result<String> {
let mut m = <Hmac<Sha256> as Mac>::new_from_slice(mk).map_err(|_| Error::Key("bad MAC key"))?;
m.update(self.canonical().as_bytes());
Ok(crate::hex(&m.finalize().into_bytes()[..MAC_LEN]))
}
fn canonical(&self) -> String {
format!("vector-push/1\n{}\n{}\n{}\n{}\n{}", self.h, self.kind, self.msg, self.ts, self.text)
}
pub fn message(&self, origin: &str, mk_hex: &str) -> Result<Vec<u8>> {
let mk = crate::unhex(mk_hex)?;
let mut notice = self.clone();
loop {
let out = notice.render(origin, &mk)?;
if out.len() <= MAX_PLAINTEXT {
return Ok(out);
}
let chars = notice.text.chars().count();
if chars == 0 {
return Err(Error::Format("notification does not fit".into()));
}
let over = out.len() - MAX_PLAINTEXT;
notice.text = notice.text.chars().take(chars.saturating_sub(over.max(chars / 8)).saturating_sub(1)).collect();
notice.text.push('…');
}
}
fn render(&self, origin: &str, mk: &[u8]) -> Result<Vec<u8>> {
let body = match self.kind.as_str() {
"call" | "video_call" => "Incoming call",
"missed_call" => "Missed call",
_ => "New message",
};
let msg = json!({
"web_push": 8030,
"mutable": true,
"notification": {
"title": "Vector",
"body": body,
"navigate": format!("{}/", origin.trim_end_matches('/')),
"mutable": true,
"data": {
"vp": 1,
"h": self.h,
"k": self.kind,
"m": self.msg,
"t": self.ts,
"x": self.text,
"a": self.mac(mk)?,
},
},
});
Ok(msg.to_string().into_bytes())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn notice(text: &str) -> Notice {
Notice { h: "a1b2c3d4e5f60718".into(), kind: "dm".into(), msg: "ab".repeat(32), ts: 1_790_000_000_000, text: text.into() }
}
#[test]
fn the_mac_covers_every_field() {
let mk = [9u8; 32];
let base = notice("hi").mac(&mk).unwrap();
assert_ne!(notice("hj").mac(&mk).unwrap(), base);
assert_ne!(Notice { ts: 1, ..notice("hi") }.mac(&mk).unwrap(), base);
assert_ne!(Notice { kind: "file".into(), ..notice("hi") }.mac(&mk).unwrap(), base);
assert_ne!(notice("hi").mac(&[8u8; 32]).unwrap(), base);
}
#[test]
fn the_fallback_names_a_call_and_nothing_the_sender_wrote() {
let mk = crate::hex(&[3u8; 32]);
let body = |kind: &str| {
let out = Notice { kind: kind.into(), ..notice("ignore this") }.message("https://web.vectorapp.io", &mk).unwrap();
serde_json::from_slice::<serde_json::Value>(&out).unwrap()["notification"]["body"].as_str().unwrap().to_string()
};
assert_eq!(body("call"), "Incoming call");
assert_eq!(body("video_call"), "Incoming call");
assert_eq!(body("missed_call"), "Missed call");
assert_eq!(body("dm"), "New message");
}
#[test]
fn long_text_is_cut_to_fit_and_still_verifies() {
let mk = crate::hex(&[3u8; 32]);
let out = notice(&"é\"".repeat(4000)).message("https://web.vectorapp.io", &mk).unwrap();
assert!(out.len() <= MAX_PLAINTEXT);
let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
let data = &v["notification"]["data"];
let cut = Notice { text: data["x"].as_str().unwrap().into(), ..notice("") };
assert!(cut.text.ends_with('…'));
assert_eq!(data["a"], cut.mac(&[3u8; 32]).unwrap());
assert_eq!(v["notification"]["navigate"], "https://web.vectorapp.io/");
}
}