web_push/
message.rs

1use std::fmt::{Display, Formatter};
2
3use ct_codecs::{Base64UrlSafeNoPadding, Decoder};
4use http::uri::Uri;
5
6use crate::{
7    error::WebPushError,
8    http_ece::{ContentEncoding, HttpEce},
9    vapid::VapidSignature,
10};
11
12/// Encryption keys from the client.
13#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
14pub struct SubscriptionKeys {
15    /// The public key. Base64-encoded, URL-safe alphabet, no padding.
16    pub p256dh: String,
17    /// Authentication secret. Base64-encoded, URL-safe alphabet, no padding.
18    pub auth: String,
19}
20
21/// Client info for sending the notification. Maps the values from browser's
22/// subscription info JSON data (AKA pushSubscription object).
23///
24/// Client pushSubscription objects can be directly deserialized into this struct using serde.
25#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
26pub struct SubscriptionInfo {
27    /// The endpoint URI for sending the notification.
28    pub endpoint: String,
29    /// The encryption key and secret for payload encryption.
30    pub keys: SubscriptionKeys,
31}
32
33impl SubscriptionInfo {
34    /// A constructor function to create a new `SubscriptionInfo`, if not using
35    /// Serde's serialization.
36    pub fn new<S>(endpoint: S, p256dh: S, auth: S) -> SubscriptionInfo
37    where
38        S: Into<String>,
39    {
40        SubscriptionInfo {
41            endpoint: endpoint.into(),
42            keys: SubscriptionKeys {
43                p256dh: p256dh.into(),
44                auth: auth.into(),
45            },
46        }
47    }
48}
49
50/// The push content payload, already in an encrypted form.
51#[derive(Debug, PartialEq)]
52pub struct WebPushPayload {
53    /// Encrypted content data.
54    pub content: Vec<u8>,
55    /// Headers depending on the authorization scheme and encryption standard.
56    pub crypto_headers: Vec<(&'static str, String)>,
57    /// The encryption standard.
58    pub content_encoding: ContentEncoding,
59}
60
61#[derive(Debug, Deserialize, Serialize, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
62#[serde(rename_all = "kebab-case")]
63pub enum Urgency {
64    VeryLow,
65    Low,
66    #[default]
67    Normal,
68    High,
69}
70
71impl Display for Urgency {
72    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73        let str = match self {
74            Urgency::VeryLow => "very-low",
75            Urgency::Low => "low",
76            Urgency::Normal => "normal",
77            Urgency::High => "high",
78        };
79
80        f.write_str(str)
81    }
82}
83
84/// Everything needed to send a push notification to the user.
85#[derive(Debug)]
86pub struct WebPushMessage {
87    /// The endpoint URI where to send the payload.
88    pub endpoint: Uri,
89    /// Time to live, how long the message should wait in the server if user is
90    /// not online. Some services require this value to be set.
91    pub ttl: u32,
92    /// The urgency of the message (very-low | low | normal | high)
93    pub urgency: Option<Urgency>,
94    /// The topic of the mssage
95    pub topic: Option<String>,
96    /// The encrypted request payload, if sending any data.
97    pub payload: Option<WebPushPayload>,
98}
99
100struct WebPushPayloadBuilder<'a> {
101    pub content: &'a [u8],
102    pub encoding: ContentEncoding,
103}
104
105/// The main class for creating a notification payload.
106pub struct WebPushMessageBuilder<'a> {
107    subscription_info: &'a SubscriptionInfo,
108    payload: Option<WebPushPayloadBuilder<'a>>,
109    ttl: u32,
110    urgency: Option<Urgency>,
111    topic: Option<String>,
112    vapid_signature: Option<VapidSignature>,
113}
114
115impl<'a> WebPushMessageBuilder<'a> {
116    /// Creates a builder for generating the web push payload.
117    ///
118    /// All parameters are from the subscription info given by browser when
119    /// subscribing to push notifications.
120    pub fn new(subscription_info: &'a SubscriptionInfo) -> WebPushMessageBuilder<'a> {
121        WebPushMessageBuilder {
122            subscription_info,
123            ttl: 2_419_200,
124            urgency: None,
125            topic: None,
126            payload: None,
127            vapid_signature: None,
128        }
129    }
130
131    /// How long the server should keep the message if it cannot be delivered
132    /// currently. If not set, the message is deleted immediately on failed
133    /// delivery.
134    pub fn set_ttl(&mut self, ttl: u32) {
135        self.ttl = ttl;
136    }
137
138    /// Urgency indicates to the push service how important a message is to the
139    /// user. This can be used by the push service to help conserve the battery
140    /// life of a user's device by only waking up for important messages when
141    /// battery is low.
142    /// Possible values are 'very-low', 'low', 'normal' and 'high'.
143    pub fn set_urgency(&mut self, urgency: Urgency) {
144        self.urgency = Some(urgency);
145    }
146
147    /// Assign a topic to the push message. A message that has been stored
148    /// by the push service can be replaced with new content if the message
149    /// has been assigned a topic. If the user agent is offline during the
150    /// time that the push messages are sent, updating a push message avoid
151    /// the situation where outdated or redundant messages are sent to the
152    /// user agent. A message with a topic replaces any outstanding push
153    /// messages with an identical topic. It is an arbitrary string
154    /// consisting of at most 32 base64url characters.
155    pub fn set_topic(&mut self, topic: String) {
156        self.topic = Some(topic);
157    }
158
159    /// Add a VAPID signature to the request. To be generated with the
160    /// [VapidSignatureBuilder](struct.VapidSignatureBuilder.html).
161    pub fn set_vapid_signature(&mut self, vapid_signature: VapidSignature) {
162        self.vapid_signature = Some(vapid_signature);
163    }
164
165    /// If set, the client will get content in the notification. Has a maximum size of
166    /// 3800 characters.
167    ///
168    /// Aes128gcm is preferred, if the browser supports it.
169    pub fn set_payload(&mut self, encoding: ContentEncoding, content: &'a [u8]) {
170        self.payload = Some(WebPushPayloadBuilder { content, encoding });
171    }
172
173    /// Builds and if set, encrypts the payload.
174    pub fn build(self) -> Result<WebPushMessage, WebPushError> {
175        let endpoint: Uri = self.subscription_info.endpoint.parse()?;
176        let topic: Option<String> = self
177            .topic
178            .map(|topic| {
179                if topic.len() > 32 {
180                    Err(WebPushError::InvalidTopic)
181                } else if topic.chars().all(is_base64url_char) {
182                    Ok(topic)
183                } else {
184                    Err(WebPushError::InvalidTopic)
185                }
186            })
187            .transpose()?;
188
189        if let Some(payload) = self.payload {
190            let p256dh = Base64UrlSafeNoPadding::decode_to_vec(&self.subscription_info.keys.p256dh, None)
191                .map_err(|_| WebPushError::InvalidCryptoKeys)?;
192            let auth = Base64UrlSafeNoPadding::decode_to_vec(&self.subscription_info.keys.auth, None)
193                .map_err(|_| WebPushError::InvalidCryptoKeys)?;
194
195            let http_ece = HttpEce::new(payload.encoding, &p256dh, &auth, self.vapid_signature);
196
197            Ok(WebPushMessage {
198                endpoint,
199                ttl: self.ttl,
200                urgency: self.urgency,
201                topic,
202                payload: Some(http_ece.encrypt(payload.content)?),
203            })
204        } else {
205            Ok(WebPushMessage {
206                endpoint,
207                ttl: self.ttl,
208                urgency: self.urgency,
209                topic,
210                payload: None,
211            })
212        }
213    }
214}
215
216fn is_base64url_char(c: char) -> bool {
217    c.is_ascii_uppercase() || c.is_ascii_lowercase() || c.is_ascii_digit() || (c == '-' || c == '_')
218}