web_push/
lib.rs

1//! # Web Push
2//!
3//! A library for creating and sending push notifications to a web browser. For
4//! content payload encryption it uses [RFC8188](https://datatracker.ietf.org/doc/html/rfc8188).
5//! The client is asynchronous and can run on any executor. An optional [`hyper`](https://crates.io/crates/hyper) based client is
6//! available with the feature `hyper-client`.
7//!
8//! # Example
9//!
10//! ```no_run
11//! # use web_push::*;
12//! # use base64::URL_SAFE;
13//! # use std::fs::File;
14//! # #[tokio::main]
15//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
16//! let endpoint = "https://updates.push.services.mozilla.com/wpush/v1/...";
17//! let p256dh = "key_from_browser_as_base64";
18//! let auth = "auth_from_browser_as_base64";
19//!
20//! //You would likely get this by deserializing a browser `pushSubscription` object.  
21//! let subscription_info = SubscriptionInfo::new(
22//!     endpoint,
23//!     p256dh,
24//!     auth
25//! );
26//!
27//! //Read signing material for payload.
28//! let file = File::open("private.pem").unwrap();
29//! let mut sig_builder = VapidSignatureBuilder::from_pem(file, &subscription_info)?.build()?;
30//!
31//! //Now add payload and encrypt.
32//! let mut builder = WebPushMessageBuilder::new(&subscription_info);
33//! let content = "Encrypted payload to be sent in the notification".as_bytes();
34//! builder.set_payload(ContentEncoding::Aes128Gcm, content);
35//! builder.set_vapid_signature(sig_builder);
36//!
37//! let client = IsahcWebPushClient::new()?;
38//!
39//! //Finally, send the notification!
40//! client.send(builder.build()?).await?;
41//! # Ok(())
42//! # }
43//! ```
44
45#[macro_use]
46extern crate log;
47#[macro_use]
48extern crate serde_derive;
49
50pub use crate::clients::request_builder;
51pub use crate::clients::WebPushClient;
52
53#[cfg(feature = "hyper-client")]
54pub use crate::clients::hyper_client::HyperWebPushClient;
55#[cfg(feature = "isahc-client")]
56pub use crate::clients::isahc_client::IsahcWebPushClient;
57
58pub use crate::error::WebPushError;
59pub use crate::http_ece::ContentEncoding;
60pub use crate::message::{
61    SubscriptionInfo, SubscriptionKeys, Urgency, WebPushMessage, WebPushMessageBuilder, WebPushPayload,
62};
63pub use crate::vapid::builder::PartialVapidSignatureBuilder;
64pub use crate::vapid::{VapidSignature, VapidSignatureBuilder};
65pub use base64::{Config, BCRYPT, BINHEX, CRYPT, IMAP_MUTF7, STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD};
66
67mod clients;
68mod error;
69mod http_ece;
70mod message;
71mod vapid;