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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#[cfg(any(feature = "crypto-use-rustcrypto", feature = "crypto-use-ring"))]
use hex::FromHex;
#[cfg(feature = "crypto-use-rustcrypto")]
use hmac::{Hmac, Mac};
#[cfg(feature = "crypto-use-ring")]
use ring::digest;
#[cfg(feature = "crypto-use-ring")]
use ring::hmac;
#[cfg(feature = "crypto-use-rustcrypto")]
use sha1::Sha1;
use std::sync::Arc;
use super::handler::Delivery;
#[cfg(feature = "crypto-use-rustcrypto")]
type HmacSha1 = Hmac<Sha1>;
#[macro_export]
macro_rules! unwrap_or_false {
($e:expr) => {
match $e {
Some(content) => content,
_ => return false,
}
};
}
pub trait HookFunc: Sync + Send {
fn run(&self, delivery: &Delivery);
}
#[derive(Clone)]
pub struct Hook {
pub event: &'static str,
pub secret: Option<String>,
pub func: Arc<HookFunc>,
}
impl<F> HookFunc for F
where
F: Fn(&Delivery) + Clone + Sync + Send + 'static,
{
fn run(&self, delivery: &Delivery) {
self(delivery)
}
}
impl Hook {
pub fn new(event: &'static str, secret: Option<String>, func: impl HookFunc + 'static) -> Self {
Self {
event,
secret,
func: Arc::new(func),
}
}
#[cfg(feature = "crypto-use-ring")]
pub fn auth(&self, delivery: &Delivery) -> bool {
if let Some(secret) = &self.secret {
let signature = unwrap_or_false!(&delivery.signature);
debug!("Signature: {}", signature);
let request_body = unwrap_or_false!(&delivery.request_body);
debug!("Request body: {}", &request_body);
let signature_hex = signature[5..signature.len()].as_bytes();
if let Ok(signature_bytes) = Vec::from_hex(signature_hex) {
let secret_bytes = secret.as_bytes();
let request_body_bytes = request_body.as_bytes();
let key = hmac::SigningKey::new(&digest::SHA1, &secret_bytes);
debug!("Validating payload with given secret");
return hmac::verify_with_own_key(&key, &request_body_bytes, &signature_bytes)
.is_ok();
}
debug!("Invalid signature");
return false;
} else {
debug!("No secret given, passing...");
return true;
}
}
#[cfg(feature = "crypto-use-rustcrypto")]
pub fn auth(&self, delivery: &Delivery) -> bool {
if let Some(secret) = &self.secret {
let signature = unwrap_or_false!(&delivery.signature);
debug!("Signature: {}", &signature);
let request_body = unwrap_or_false!(&delivery.request_body);
debug!("Request body: {}", &request_body);
let signature_hex = signature[5..signature.len()].as_bytes();
if let Ok(signature_bytes) = Vec::from_hex(signature_hex) {
let secret_bytes = secret.as_bytes();
let request_body_bytes = request_body.as_bytes();
let mut mac = unwrap_or_false!(HmacSha1::new_varkey(secret_bytes).ok());
mac.input(request_body_bytes);
debug!("Validating payload with given secret");
return mac.verify(&signature_bytes).is_ok();
}
debug!("Invalid signature");
return false;
} else {
debug!("No secret given, passing...");
return false;
}
}
#[cfg(all(not(feature = "crypto-use-rustcrypto"), not(feature = "crypto-use-ring")))]
pub fn auth(&self, _: &Delivery) -> bool {
info!("Payload authentication not enabled, passing...");
true
}
pub fn handle_delivery(self, delivery: &Delivery) {
if self.auth(delivery) {
debug!("Valid payload found");
self.func.run(delivery);
}
debug!("Invalid payload");
}
}
#[cfg(any(feature = "crypto-use-rustcrypto", feature = "crypto-use-ring"))]
#[cfg(test)]
mod tests {
use super::super::handler::ContentType;
#[cfg(feature = "crypto-use-rustcrypto")]
use super::HmacSha1;
use super::*;
use hex::ToHex;
#[cfg(feature = "crypto-use-ring")]
use ring::digest;
#[cfg(feature = "crypto-use-ring")]
use ring::hmac;
#[cfg(feature = "crypto-use-ring")]
#[test]
fn payload_authentication_ring() {
let secret = String::from("secret");
let hook = Hook::new("*", Some(secret.clone()), |_: &Delivery| {});
let payload = String::from(r#"{"zen": "Bazinga!"}"#);
let request_body = payload.clone();
let secret_bytes = secret.as_bytes();
let request_bytes = request_body.as_bytes();
let key = hmac::SigningKey::new(&digest::SHA1, &secret_bytes);
let mut signature = String::new();
hmac::sign(&key, &request_bytes)
.as_ref()
.write_hex(&mut signature)
.unwrap();
let signature_field = String::from(format!("sha1={}", signature));
let delivery = Delivery::new(
None,
Some(String::from("push")),
Some(signature_field),
ContentType::JSON,
Some(request_body),
);
assert!(hook.auth(&delivery));
}
#[cfg(feature = "crypto-use-rustcrypto")]
#[test]
fn payload_authentication_rustcrypto() {
let secret = String::from("secret");
let hook = Hook::new("*", Some(secret.clone()), |_: &Delivery| {});
let payload = String::from(r#"{"zen": "Bazinga!"}"#);
let request_body = payload.clone();
let secret_bytes = secret.as_bytes();
let request_bytes = request_body.as_bytes();
let mut mac = HmacSha1::new_varkey(&secret_bytes).expect("Invalid key");
mac.input(&request_bytes);
let mut signature = String::new();
mac.result()
.code()
.as_ref()
.write_hex(&mut signature)
.expect("Invalid signature");
let signature_field = String::from(format!("sha1={}", signature));
let delivery = Delivery::new(
None,
Some(String::from("push")),
Some(signature_field),
ContentType::JSON,
Some(request_body),
);
assert!(hook.auth(&delivery));
}
#[test]
fn payload_authentication_fail() {
let secret = String::from("secret");
let hook = Hook::new("*", Some(secret.clone()), |_: &Delivery| {});
let payload = String::from(r#"{"zen": "Another test!"}"#);
let request_body = payload.clone();
let signature = String::from("sha1=ec760ee6d10bf638089f078b5a0c23f6575821e7");
let delivery = Delivery::new(
None,
Some(String::from("push")),
Some(signature),
ContentType::JSON,
Some(request_body),
);
assert_eq!(hook.auth(&delivery), false);
}
}