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
use crypto_mac::Mac;
use hmac::Hmac;
use sha2::Sha256;
const SLACK_TIMESTAMP_HEADER: &str = "X-Slack-Request-Timestamp";
const SLACK_SIGNATURE_HEADER: &str = "X-Slack-Signature";
type Sha256Hmac = Hmac<Sha256>;
mod error {
use http::header::ToStrError;
#[derive(Debug)]
pub struct InvalidKeyLengthError;
#[derive(Debug)]
pub enum VerificationError {
MissingTimestampHeader,
MissingSignatureHeader,
SignatureMismatch,
UnencodableHeader(ToStrError),
}
}
use error::*;
#[derive(Clone, Debug)]
pub struct SlackHTTPVerifier {
verifier: SlackVerifier,
}
impl SlackHTTPVerifier {
pub fn new<S: AsRef<[u8]>>(secret: S) -> Result<Self, InvalidKeyLengthError> {
let verifier = SlackVerifier::new(secret)?;
Ok(SlackHTTPVerifier { verifier })
}
pub fn verify<T>(&self, req: &http::Request<T>) -> Result<(), VerificationError>
where
T: AsRef<str>,
{
let ts = req
.headers()
.get(SLACK_TIMESTAMP_HEADER)
.ok_or(VerificationError::MissingTimestampHeader)?
.to_str()
.map_err(VerificationError::UnencodableHeader)?;
let exp_sig = req
.headers()
.get(SLACK_SIGNATURE_HEADER)
.ok_or(VerificationError::MissingSignatureHeader)?
.to_str()
.map_err(VerificationError::UnencodableHeader)?;
self.verifier.verify(ts, req.body().as_ref(), exp_sig)
}
}
unsafe impl Send for SlackHTTPVerifier {}
unsafe impl Sync for SlackHTTPVerifier {}
#[derive(Clone, Debug)]
pub struct SlackVerifier {
mac: Sha256Hmac,
}
impl SlackVerifier {
pub fn new<S: AsRef<[u8]>>(secret: S) -> Result<SlackVerifier, InvalidKeyLengthError> {
match Sha256Hmac::new_varkey(secret.as_ref()) {
Ok(mac) => Ok(SlackVerifier { mac }),
Err(_) => Err(InvalidKeyLengthError),
}
}
pub fn verify(&self, ts: &str, body: &str, exp_sig: &str) -> Result<(), VerificationError> {
let basestring = format!("v0:{}:{}", ts, body);
let mut mac = self.mac.clone();
mac.input(basestring.as_bytes());
let sig = format!("v0={}", hex::encode(mac.result().code().as_slice()));
match sig == exp_sig {
true => Ok(()),
false => Err(VerificationError::SignatureMismatch),
}
}
}
unsafe impl Send for SlackVerifier {}
unsafe impl Sync for SlackVerifier {}
#[cfg(test)]
mod tests {
use super::*;
const SLACK_SAMPLE_KEY: &str = "8f742231b10e8888abcd99yyyzzz85a5";
const SLACK_SAMPLE_TIMESTAMP: &str = "1531420618";
const SLACK_SAMPLE_BODY: &str =
"token=xyzz0WbapA4vBCDEFasx0q6G&team_id=T1DC2JH3J&team_domain=testteamnow&channel_id=G8PSS9T3V&channel_name=foobar&user_id=U2CERLKJA&user_name=roadrunner&command=%2Fwebhook-collect&text=&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT1DC2JH3J%2F397700885554%2F96rGlfmibIGlgcZRskXaIFfN&trigger_id=398738663015.47445629121.803a0bc887a14d10d2c447fce8b6703c";
const SLACK_SAMPLE_SIG: &str =
"v0=a2114d57b48eac39b9ad189dd8316235a7b4a8d21a10bd27519666489c69b503";
#[test]
fn site_example() {
let verifier = SlackVerifier::new(SLACK_SAMPLE_KEY).unwrap();
assert!(verifier
.verify(SLACK_SAMPLE_TIMESTAMP, SLACK_SAMPLE_BODY, SLACK_SAMPLE_SIG)
.is_ok());
}
#[test]
fn site_example_http_req() {
let verifier = SlackHTTPVerifier::new(SLACK_SAMPLE_KEY).unwrap();
let req = http::Request::builder()
.header(SLACK_TIMESTAMP_HEADER, SLACK_SAMPLE_TIMESTAMP)
.header(SLACK_SIGNATURE_HEADER, SLACK_SAMPLE_SIG)
.body(SLACK_SAMPLE_BODY)
.unwrap();
assert!(verifier.verify(&req).is_ok());
}
}