Struct web_push::VapidSignatureBuilder
[−]
[src]
pub struct VapidSignatureBuilder<'a> { /* fields omitted */ }A VAPID signature builder for generating an optional signature to the request. With a given signature, one can pass the registration to Google's FCM service. And prevent unauthorized notifications to be sent to the client.
To communicate with the site, one needs to generate a private key to keep in the server and derive a public key from the generated private key to the client.
Private key generation:
openssl ecparam -name prime256v1 -genkey -noout -out private.pem
To derive a public key out of generated private key:
openssl ec -in private.pem -pubout -out vapid_public.pem
To get the byte form of the public key for the JavaScript client:
openssl ec -in private.pem -text -noout -conv_form uncompressed
To create a VAPID signature:
let subscription_info = SubscriptionInfo { keys: SubscriptionKeys { p256dh: String::from("something"), auth: String::from("secret"), }, endpoint: String::from("https://mozilla.rules/something"), }; let file = File::open("private.pem").unwrap(); let mut sig_builder = VapidSignatureBuilder::from_pem(file, &subscription_info).unwrap(); sig_builder.add_claim("sub", "mailto:test@example.com"); sig_builder.add_claim("foo", "bar"); sig_builder.add_claim("omg", 123); let signature = sig_builder.build().unwrap();
Methods
impl<'a> VapidSignatureBuilder<'a>[src]
pub fn from_pem<R: Read>(
pk_pem: R,
subscription_info: &'a SubscriptionInfo
) -> Result<VapidSignatureBuilder<'a>, WebPushError>[src]
pk_pem: R,
subscription_info: &'a SubscriptionInfo
) -> Result<VapidSignatureBuilder<'a>, WebPushError>
Creates a new builder from a PEM formatted private key.
pub fn from_der<R: Read>(
pk_der: R,
subscription_info: &'a SubscriptionInfo
) -> Result<VapidSignatureBuilder<'a>, WebPushError>[src]
pk_der: R,
subscription_info: &'a SubscriptionInfo
) -> Result<VapidSignatureBuilder<'a>, WebPushError>
Creates a new builder from a DER formatted private key.
pub fn add_claim<V>(&mut self, key: &'a str, val: V) where
V: Into<Value>, [src]
V: Into<Value>,
Add a claim to the signature. Claims aud and exp are automatically
added to the signature. Add them manually to override the default
values.
The function accepts any value that can be converted into a type JSON supports.
pub fn build(self) -> Result<VapidSignature, WebPushError>[src]
Builds a signature to be used in WebPushMessageBuilder.