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
//! Twitch EventSub Webhook verification and utilities.
//!
//! # Features
//!
//! - `webhook-axum`: [`HeaderAccess`] implementation for axum via `axum-core`, `IntoResponse` for [`VerificationError`]
//! - `webhook-actix`: [`HeaderAccess`] implementation for actix-web via `actix-http`
//!
//! # Framework Integration
//!
//! The webhook verification works with any framework by implementing the [`HeaderAccess`] trait.
//! Built-in implementations are provided for common frameworks:
//!
//! ## Axum Example
//!
//! ```ignore
//! use std::sync::Arc;
//!
//! use axum::{
//! body::{Body, Bytes},
//! extract::State,
//! http::{HeaderMap, StatusCode},
//! response::Response,
//! routing::post,
//! Router,
//! };
//! use tokio::sync::RwLock;
//! use twitch_highway::eventsub::{
//! events::channels::follow::ChannelFollow,
//! webhook::{
//! get_message_type, get_subscription_type, verify_event_message, Challenge, MessageType,
//! Notification, Revoke, VerificationError,
//! },
//! SubscriptionType,
//! };
//!
//! #[derive(Clone)]
//! struct AppState {
//! pub secret: Arc<RwLock<String>>,
//! }
//!
//! async fn webhook_handler(
//! headers: HeaderMap,
//! State(state): State<AppState>,
//! body: Bytes,
//! ) -> Result<Response, VerificationError> {
//! let secret = state.secret.read().await;
//!
//! verify_event_message(&headers, &body, &secret)?;
//!
//! if let Ok(message_type) = get_message_type(&headers) {
//! match message_type {
//! MessageType::Verification => {
//! let challenge: Challenge = serde_json::from_slice(&body).unwrap();
//!
//! Ok(Response::builder()
//! .status(StatusCode::OK)
//! .header("Content-Type", "text/plain")
//! .body(Body::from(challenge.challenge))
//! .unwrap())
//! },
//! MessageType::Notification => {
//! let subscription_type = get_subscription_type(&headers);
//! if let Some(subscription_type) = subscription_type {
//!
//! match subscription_type {
//! SubscriptionType::ChannelFollow => {
//! let _notification: Notification<ChannelFollow> = serde_json::from_slice(&body).unwrap();
//! }
//! SubscriptionType::ChannelSubscribe => {/* */}
//! _ => {}
//! }
//! }
//!
//! Ok(Response::builder()
//! .status(StatusCode::NO_CONTENT)
//! .body(Body::empty())
//! .unwrap())
//! },
//! MessageType::Revocation => {
//! let _revocation: Revoke = serde_json::from_slice(&body).unwrap();
//!
//! Ok(Response::builder()
//! .status(StatusCode::NO_CONTENT)
//! .body(Body::empty())
//! .unwrap())
//! },
//! }
//! } else {
//! Ok(Response::builder()
//! .status(StatusCode::NO_CONTENT)
//! .body(Body::empty())
//! .unwrap())
//! }
//! }
//!
//! # async fn example() {
//! let state = AppState {
//! secret: Arc::new(RwLock::new("".to_string())),
//! };
//!
//! let app = Router::new()
//! .route("/webhook", post(webhook_handler))
//! .with_state(state);
//!
//! # let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
//! # axum::serve(listener, app);
//! # }
//! ```
//!
//! ## Actix-web Example
//!
//! ```ignore
//! use actix_web::{http::header, mime, post, web, HttpRequest, HttpResponse};
//! use twitch_highway::eventsub::{
//! events::channels::raid::Raid,
//! webhook::{
//! get_message_type, get_subscription_type, verify_event_message, Challenge, MessageType,
//! Notification, Revoke,
//! },
//! SubscriptionType,
//! };
//!
//! #[post("/webhook")]
//! async fn webhook_handler(req: HttpRequest, body: web::Bytes) -> HttpResponse {
//! let secret = "your-webhook-secret";
//!
//! if verify_event_message(req.headers(), &body, secret).is_err() {
//! return HttpResponse::Forbidden().finish();
//! }
//!
//! if let Ok(msg_type) = get_message_type(req.headers()) {
//! match msg_type {
//! MessageType::Notification => {
//! let sub_type = get_subscription_type(req.headers());
//! if let Some(sub_type) = sub_type {
//! match sub_type {
//! SubscriptionType::ChannelRaid => {
//! let _channel_raid: Notification<Raid> =
//! serde_json::from_slice(&body).unwrap();
//! }
//! SubscriptionType::ChannelSuspiciousUserMessage => {}
//! _ => {}
//! }
//! }
//!
//! HttpResponse::NoContent().finish()
//! }
//! MessageType::Verification => {
//! let challenge: Challenge = serde_json::from_slice(&body).unwrap();
//!
//! HttpResponse::Ok()
//! .insert_header(header::ContentType(mime::TEXT_PLAIN))
//! .body(challenge.challenge)
//! }
//! MessageType::Revocation => {
//! let _revocation: Revoke = serde_json::from_slice(&body).unwrap();
//!
//! HttpResponse::NoContent().finish()
//! }
//! }
//! } else {
//! HttpResponse::NoContent().finish()
//! }
//! }
//! ```
//!
//! ## Custom Framework
//!
//! Implement [`HeaderAccess`] for your framework's header type:
//!
//! ```ignore
//! use std::collections::HashMap;
//! use twitch_highway::eventsub::webhook::HeaderAccess;
//!
//! struct MyHeaders {
//! inner: HashMap<String, String>,
//! }
//!
//! impl MyHeaders {
//! pub fn get(&self, name: &str) -> Option<&str> {
//! self.inner.get(name).map(|x| x.as_str())
//! }
//! }
//!
//! impl HeaderAccess for MyHeaders {
//! fn get_header(&self, name: &str) -> Option<&str> {
//! self.get(name)
//! }
//! }
//! ```
pub use VerificationError;
pub use HeaderAccess;
pub use ;
pub use verify_event_message;
use FromStr;
use crate::;