1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::error::WebPushError;
use hyper::Uri;
use crate::message::SubscriptionInfo;
use openssl::ec::EcKey;
use openssl::pkey::Private;
use serde_json::Value;
use std::collections::BTreeMap;
use std::io::Read;
use crate::vapid::{VapidKey, VapidSignature, VapidSigner};
pub struct VapidSignatureBuilder<'a> {
claims: BTreeMap<&'a str, Value>,
key: VapidKey,
subscription_info: &'a SubscriptionInfo,
}
impl<'a> VapidSignatureBuilder<'a> {
pub fn from_pem<R: Read>(
mut pk_pem: R,
subscription_info: &'a SubscriptionInfo,
) -> Result<VapidSignatureBuilder<'a>, WebPushError> {
let mut pem_key: Vec<u8> = Vec::new();
pk_pem.read_to_end(&mut pem_key)?;
Ok(Self::from_ec(
EcKey::private_key_from_pem(&pem_key)?,
subscription_info,
))
}
pub fn from_der<R: Read>(
mut pk_der: R,
subscription_info: &'a SubscriptionInfo,
) -> Result<VapidSignatureBuilder<'a>, WebPushError> {
let mut der_key: Vec<u8> = Vec::new();
pk_der.read_to_end(&mut der_key)?;
Ok(Self::from_ec(
EcKey::private_key_from_der(&der_key)?,
subscription_info,
))
}
pub fn add_claim<V>(&mut self, key: &'a str, val: V)
where
V: Into<Value>,
{
self.claims.insert(key, val.into());
}
pub fn build(self) -> Result<VapidSignature, WebPushError> {
let endpoint: Uri = self.subscription_info.endpoint.parse()?;
let signature = VapidSigner::sign(self.key, &endpoint, self.claims)?;
Ok(signature)
}
fn from_ec(
ec_key: EcKey<Private>,
subscription_info: &'a SubscriptionInfo,
) -> VapidSignatureBuilder<'a> {
VapidSignatureBuilder {
claims: BTreeMap::new(),
key: VapidKey::new(ec_key),
subscription_info: subscription_info,
}
}
}
#[cfg(test)]
mod tests {
use crate::message::SubscriptionInfo;
use serde_json;
use std::fs::File;
use crate::vapid::VapidSignatureBuilder;
lazy_static! {
static ref PRIVATE_PEM: File = File::open("resources/vapid_test_key.pem").unwrap();
static ref PRIVATE_DER: File = File::open("resources/vapid_test_key.der").unwrap();
}
lazy_static! {
static ref SUBSCRIPTION_INFO: SubscriptionInfo =
serde_json::from_value(
json!({
"endpoint": "https://updates.push.services.mozilla.com/wpush/v2/gAAAAABaso4Vajy4STM25r5y5oFfyN451rUmES6mhQngxABxbZB5q_o75WpG25oKdrlrh9KdgWFKdYBc-buLPhvCTqR5KdsK8iCZHQume-ndtZJWKOgJbQ20GjbxHmAT1IAv8AIxTwHO-JTQ2Np2hwkKISp2_KUtpnmwFzglLP7vlCd16hTNJ2I",
"keys": {
"auth": "sBXU5_tIYz-5w7G2B25BEw",
"p256dh": "BH1HTeKM7-NwaLGHEqxeu2IamQaVVLkcsFHPIHmsCnqxcBHPQBprF41bEMOr3O1hUQ2jU1opNEm1F_lZV_sxMP8"
}
})
).unwrap();
}
#[test]
fn test_builder_from_pem() {
let builder = VapidSignatureBuilder::from_pem(&*PRIVATE_PEM, &*SUBSCRIPTION_INFO).unwrap();
let signature = builder.build().unwrap();
assert_eq!(
"BMo1HqKF6skMZYykrte9duqYwBD08mDQKTunRkJdD3sTJ9E-yyN6sJlPWTpKNhp-y2KeS6oANHF-q3w37bClb7U",
&signature.auth_k
);
assert!(!signature.auth_t.is_empty());
}
#[test]
fn test_builder_from_der() {
let builder = VapidSignatureBuilder::from_der(&*PRIVATE_DER, &*SUBSCRIPTION_INFO).unwrap();
let signature = builder.build().unwrap();
assert_eq!(
"BMo1HqKF6skMZYykrte9duqYwBD08mDQKTunRkJdD3sTJ9E-yyN6sJlPWTpKNhp-y2KeS6oANHF-q3w37bClb7U",
&signature.auth_k
);
assert!(!signature.auth_t.is_empty());
}
}