pub struct VapidSignatureBuilder<'a> { /* private fields */ }
Expand description

A VAPID signature builder for generating an optional signature to the request. This encryption is required for payloads in all current and future browsers.

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 for 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'

The above commands can be done in code using PartialVapidSignatureBuilder::get_public_key, then base64 URL safe encoding as well.

To create a VAPID signature:

//You would get this as a `pushSubscription` object from the client. They need your public key to get that object.
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();

//These fields are optional, and likely unneeded for most uses.
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();

Implementations

Creates a new builder from a PEM formatted private key.

Details

The input can be either a pkcs8 formatted PEM, denoted by a —–BEGIN PRIVATE KEY—— header, or a SEC1 formatted PEM, denoted by a —–BEGIN EC PRIVATE KEY—— header.

Creates a new builder from a PEM formatted private key. This function doesn’t take a subscription, allowing the reuse of one builder for multiple messages by cloning the resulting builder.

Details

The input can be either a pkcs8 formatted PEM, denoted by a —–BEGIN PRIVATE KEY—— header, or a SEC1 formatted PEM, denoted by a —–BEGIN EC PRIVATE KEY—— header.

Creates a new builder from a DER formatted private key.

Creates a new builder from a DER formatted private key. This function doesn’t take a subscription, allowing the reuse of one builder for multiple messages by cloning the resulting builder.

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.

Builds a signature to be used in WebPushMessageBuilder.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more