[][src]Struct web_push::VapidSignatureBuilder

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

... or a base64 encoded string, which the client should convert into byte form before using:

openssl ec -in private.pem -pubout -outform DER|tail -c 65|base64|tr '/+' '_-'|tr -d '\n'

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]

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]

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]

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.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.