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§

source§

impl<'a> VapidSignatureBuilder<'a>

source

pub fn from_pem<R: Read>( pk_pem: R, subscription_info: &'a SubscriptionInfo ) -> Result<VapidSignatureBuilder<'a>, WebPushError>

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.

source

pub fn from_pem_no_sub<R: Read>( pk_pem: R ) -> Result<PartialVapidSignatureBuilder, WebPushError>

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.

source

pub fn from_der<R: Read>( pk_der: R, subscription_info: &'a SubscriptionInfo ) -> Result<VapidSignatureBuilder<'a>, WebPushError>

Creates a new builder from a DER formatted private key.

source

pub fn from_der_no_sub<R: Read>( pk_der: R ) -> Result<PartialVapidSignatureBuilder, WebPushError>

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.

source

pub fn from_base64( encoded: &str, config: Config, subscription_info: &'a SubscriptionInfo ) -> Result<VapidSignatureBuilder<'a>, WebPushError>

Creates a new builder from a raw base64 encoded private key. This isn’t the base64 from a key generated by openssl, but rather the literal bytes of the private key itself. This is the kind of key given to you by most VAPID key generator sites, and also the kind used in the API of other large web push libraries, such as PHP and Node.

Config

base64 has multiple encodings itself, the most common of which for web push is URL_SAFE_NO_PAD. This function does support other encodings however, if needed.

Example
// Use `from_base64` here if you have a sub
let builder = VapidSignatureBuilder::from_base64_no_sub("IQ9Ur0ykXoHS9gzfYX0aBjy9lvdrjx_PFUXmie9YRcY", base64::URL_SAFE_NO_PAD).unwrap();
source

pub fn from_base64_no_sub( encoded: &str, config: Config ) -> Result<PartialVapidSignatureBuilder, WebPushError>

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

source

pub fn add_claim<V>(&mut self, key: &'a str, val: V)
where 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.

source

pub fn build(self) -> Result<VapidSignature, WebPushError>

Builds a signature to be used in WebPushMessageBuilder.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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