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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! This crate provides the tools necessary for your application to send Web Push messages
//! in accordance with [`RFC 8030`].
//!
//! In addition to sending Push messages, this crate also handles:
//! * the generation of valid Voluntary Application Server Identification (VAPID) tokens,
//! as required, in accordance with [`RFC 8292`] for any messages you send; and,
//! * the proper encryption of message content you send, using AES128GCM, in accordance with
//! [`RFC 8291`].
//!
//! # Setup a Push Service
//!
//! Before your application can accept subscriptions from web browsers and send messages, you must
//! first initialize a [`PushService`] with an ECDSA private key and metadata that will be used to
//! generate VAPID tokens. The ECDSA private key and metadata you use represents your
//! application's "identity" to servers that deliver your push message: this means you should use
//! the same private key across restarts of your application and across multiple running
//! instances of your application.
//!
//! The OpenSSL suite can be used to generate an ECDSA private key for use with this library as
//! follows:
//!
//! ```bash
//! openssl ecparam -name secp256r1 -genkey -noout -out vapid.pem
//! ```
//!
//! This should have created a file called `vapid.pem` containing the private key. Keep it secret:
//! losing it could allow an attacker to send push messages as if they were coming from your
//! application.
//!
//! Then, create a [`PushService`] as follows:
//!
//! ```rust
//! use pushicino::{PushService, Vapid, Subject, VapidKey};
//! let service = PushService::new(Vapid::new(
//! Subject::parse("mailto:vapid@yourapplication.com")?,
//! VapidKey::load_from_file("vapid.pem")?,
//! ));
//!
//! // If you're using Axum, you should put the PushService into your application's state object
//! ```
//!
//! If you're using a web framework, like Axum, you should put the [`PushService`] that we created
//! here into your application's state and that can be accessed by your application's route
//! handlers.
//!
//! # VAPID Endpoint
//!
//! Frontend applications running on the browser will require your [`PushService`]'s public key.
//! You should create an endpoint that serves this public key, in Base64 encoding, to the frontend.
//! For example, in Axum, you could do:
//!
//! ```rust
//! use std::sync::Arc;
//! use axum::http::StatusCode;
//! use axum::extract::State;
//! use axum::Json;
//! use serde::Serialize;
//! use pushicino::PushService;
//!
//! struct ApplicationState {
//! push_service: PushService,
//! }
//!
//! #[derive(Serialize)]
//! struct ApplicationConfiguration {
//! vapid_public_key: String,
//! }
//!
//! // Create a request handler that will serve the push service's public key in Base64 encoding
//! pub async fn application_configuration(State(state): State<Arc<ApplicationState>>)
//! -> Result<Json<ApplicationConfiguration>, StatusCode> {
//! Ok(Json(ApplicationConfiguration {
//! vapid_public_key: state.push_service.vapid_public_key_base64(),
//! }))
//! }
//!
//! // Don't forget to register the `application_configuration` handler with your Axum router!
//! ```
//!
//! Feel free to use any other method, instead of the one suggested above, to transfer the public
//! key to the frontend.
//!
//! # Subscriptions Endpoints
//!
//! When the user's browser confirms they would like to receive push messages from your application,
//! they will need to send the subscription details to your application. You can receive this
//! information by setting up an endpoint to receive this information.
//!
//! [`Subscription`] implements the [`serde::Deserialize`] trait (from the `serde` crate) and it matches
//! the exact object returned by the [`PushSubscription.toJSON()`] method of the user agent. This
//! allows you to accept a [`Subscription`] directly from your request handler.
//!
//! For example, in Axum, you could do:
//!
//! ```rust
//! use std::sync::Arc;
//! use axum::http::StatusCode;
//! use axum::extract::State;
//! use axum::Json;
//! use serde::Serialize;
//! use pushicino::{PushService, Subscription};
//!
//! struct ApplicationState {
//! push_service: PushService,
//! }
//!
//! async fn subscribe(
//! State(application): State<Arc<ApplicationState>>,
//! Json(request): Json<Subscription>,
//! ) -> Result<(), StatusCode> {
//! // At this point, you should persist the Subscription into a database or some other storage
//!
//! // Send a Web Push message immediately to the user, thanking them for subscribing!
//! application.push_service.send(&request, "Hello, thanks for subscribing!".as_bytes())
//! .await.unwrap();
//!
//! Ok(())
//! }
//!
//! // Don't forget to register the `subscribe` handler with your Axum router!
//! ```
//!
//! **It is your responsibility to persist the [`Subscription`] into storage** ([`Subscription`]
//! implements the [`serde::Serialize`] trait). It is highly recommended that you associate the user's
//! identity alongside the [`Subscription`] in case you wish to send Web Push messages to specific
//! users.
//!
//! [`RFC 8030`]: https://datatracker.ietf.org/doc/html/rfc8030
//! [`RFC 8292`]: https://datatracker.ietf.org/doc/html/rfc8292
//! [`RFC 8291`]: https://datatracker.ietf.org/doc/html/rfc8291
//! [`PushSubscription.toJSON()`]: https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription/toJSON
//!
//!
use Engine;
use BASE64_URL_SAFE_NO_PAD;
use VerifyingKey;
use HeaderMap;
pub use ;
pub use ;
/// The main entrypoint for sending Web Push messages.
///
/// Each Push Service should be configured with a [`Vapid`] object that uniquely identifies your
/// application. It is important that the same [`Vapid`] configuration (private key and subject)
/// is reused across application restarts and across multiple running instances of your application.