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
use http::uri::Uri;
use std::fmt::{Display, Formatter};
use crate::error::WebPushError;
use crate::http_ece::{ContentEncoding, HttpEce};
use crate::vapid::VapidSignature;
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
pub struct SubscriptionKeys {
pub p256dh: String,
pub auth: String,
}
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
pub struct SubscriptionInfo {
pub endpoint: String,
pub keys: SubscriptionKeys,
}
impl SubscriptionInfo {
pub fn new<S>(endpoint: S, p256dh: S, auth: S) -> SubscriptionInfo
where
S: Into<String>,
{
SubscriptionInfo {
endpoint: endpoint.into(),
keys: SubscriptionKeys {
p256dh: p256dh.into(),
auth: auth.into(),
},
}
}
}
#[derive(Debug, PartialEq)]
pub struct WebPushPayload {
pub content: Vec<u8>,
pub crypto_headers: Vec<(&'static str, String)>,
pub content_encoding: &'static str,
}
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)]
#[serde(rename_all = "kebab-case")]
pub enum Urgency {
VeryLow,
Low,
#[default]
Normal,
High,
}
impl Display for Urgency {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let str = match self {
Urgency::VeryLow => "very-low",
Urgency::Low => "low",
Urgency::Normal => "normal",
Urgency::High => "high",
};
f.write_str(str)
}
}
#[derive(Debug)]
pub struct WebPushMessage {
pub endpoint: Uri,
pub ttl: u32,
pub urgency: Option<Urgency>,
pub payload: Option<WebPushPayload>,
}
struct WebPushPayloadBuilder<'a> {
pub content: &'a [u8],
pub encoding: ContentEncoding,
}
pub struct WebPushMessageBuilder<'a> {
subscription_info: &'a SubscriptionInfo,
payload: Option<WebPushPayloadBuilder<'a>>,
ttl: u32,
urgency: Option<Urgency>,
vapid_signature: Option<VapidSignature>,
}
impl<'a> WebPushMessageBuilder<'a> {
pub fn new(subscription_info: &'a SubscriptionInfo) -> Result<WebPushMessageBuilder<'a>, WebPushError> {
Ok(WebPushMessageBuilder {
subscription_info,
ttl: 2_419_200,
urgency: None,
payload: None,
vapid_signature: None,
})
}
pub fn set_ttl(&mut self, ttl: u32) {
self.ttl = ttl;
}
pub fn set_urgency(&mut self, urgency: Urgency) {
self.urgency = Some(urgency);
}
pub fn set_vapid_signature(&mut self, vapid_signature: VapidSignature) {
self.vapid_signature = Some(vapid_signature);
}
pub fn set_payload(&mut self, encoding: ContentEncoding, content: &'a [u8]) {
self.payload = Some(WebPushPayloadBuilder { content, encoding });
}
pub fn build(self) -> Result<WebPushMessage, WebPushError> {
let endpoint: Uri = self.subscription_info.endpoint.parse()?;
if let Some(payload) = self.payload {
let p256dh = base64::decode_config(&self.subscription_info.keys.p256dh, base64::URL_SAFE)?;
let auth = base64::decode_config(&self.subscription_info.keys.auth, base64::URL_SAFE)?;
let http_ece = HttpEce::new(payload.encoding, &p256dh, &auth, self.vapid_signature);
Ok(WebPushMessage {
endpoint,
ttl: self.ttl,
urgency: self.urgency,
payload: Some(http_ece.encrypt(payload.content)?),
})
} else {
Ok(WebPushMessage {
endpoint,
ttl: self.ttl,
urgency: self.urgency,
payload: None,
})
}
}
}