Struct web_push::VapidSignatureBuilder [−][src]
pub struct VapidSignatureBuilder<'a> { /* fields omitted */ }
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
pub fn from_pem<R: Read>(
pk_pem: R,
subscription_info: &'a SubscriptionInfo
) -> Result<VapidSignatureBuilder<'a>, WebPushError>
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.
pub fn from_pem_no_sub<R: Read>(
pk_pem: R
) -> Result<PartialVapidSignatureBuilder, WebPushError>
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.
pub fn from_der<R: Read>(
pk_der: R,
subscription_info: &'a SubscriptionInfo
) -> Result<VapidSignatureBuilder<'a>, WebPushError>
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.
pub fn from_der_no_sub<R: Read>(
pk_der: R
) -> Result<PartialVapidSignatureBuilder, WebPushError>
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.
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
impl<'a> RefUnwindSafe for VapidSignatureBuilder<'a>
impl<'a> Send for VapidSignatureBuilder<'a>
impl<'a> Sync for VapidSignatureBuilder<'a>
impl<'a> Unpin for VapidSignatureBuilder<'a>
impl<'a> UnwindSafe for VapidSignatureBuilder<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V
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