barbed 0.0.2

Twitch Helix, EventSub, OAuth, and signing helpers
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
use anyhow::{Context, Result, anyhow, bail};
use reqwest::Client;
use serde::Deserialize;
use thiserror::Error;
use time::OffsetDateTime;
use tokio::time::sleep;
use tracing::{debug, info, trace};

use crate::TwitchIdentity;
use crate::eventsub::{
    EventSubMessageType, EventSubStreamEvent, EventSubWebSocketSession,
    channel_chat_message_delete_subscription_request, channel_chat_message_subscription_request,
    decode_eventsub_websocket_message,
};
use crate::helix::{self, TwitchTokenExchange};
use crate::http::{HttpMethod, PreparedRequest, RawResponse};
use crate::oauth::{
    TokenValidation, TwitchAuthConfig, TwitchAuthOutcome, TwitchDeviceAuthorization,
    TwitchTokenState, device_code_request_with_scope, device_token_request, normalize_scopes,
    refresh_token_request, validate_token_request,
};

#[derive(Clone)]
pub struct TwitchAuthClient {
    config: TwitchAuthConfig,
    http: Client,
}

impl TwitchAuthClient {
    pub fn new(config: TwitchAuthConfig) -> Result<Self> {
        let http = Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .context("failed to construct reqwest client for Twitch auth")?;

        Ok(Self { config, http })
    }

    pub fn config(&self) -> &TwitchAuthConfig {
        &self.config
    }

    pub async fn start_device_code_flow(
        &self,
        scopes: &[String],
    ) -> Result<TwitchDeviceAuthorization> {
        let scope_string = normalize_scopes(scopes, self.config.default_scopes());
        let response = send_prepared_request(
            &self.http,
            device_code_request_with_scope(self.config.client_id(), &scope_string),
        )
        .await?;

        if response.status != 200 {
            bail!(
                "twitch device code request failed with status {}: {}",
                response.status,
                response.body
            );
        }

        let payload: DeviceCodeResponse = serde_json::from_str(&response.body)
            .context("failed to parse Twitch device code response")?;

        let now = OffsetDateTime::now_utc();
        TwitchDeviceAuthorization::new(
            payload.user_code,
            payload.verification_uri,
            payload.verification_uri_complete,
            payload.expires_in,
            payload.interval,
            payload.device_code,
            now,
        )
        .map_err(|err| anyhow!("failed to construct Twitch device authorization: {err}"))
    }

    pub async fn complete_device_code_flow(
        &self,
        authorization: &TwitchDeviceAuthorization,
    ) -> Result<TwitchAuthOutcome> {
        let mut delay = authorization.interval;

        loop {
            if OffsetDateTime::now_utc() >= authorization.expires_at {
                bail!("device authorization expired before Twitch approval");
            }

            let response = send_prepared_request(
                &self.http,
                device_token_request(self.config.client_id(), &authorization.device_code),
            )
            .await?;

            if response.status == 200 {
                let exchange: TwitchTokenExchange = serde_json::from_str(&response.body)
                    .context("failed to parse Twitch token exchange response")?;
                let identity = fetch_authenticated_user(
                    &self.http,
                    &exchange.access_token,
                    self.config.client_id(),
                )
                .await?;
                return helix::build_auth_outcome(identity, exchange, now_ms())
                    .map_err(anyhow::Error::from);
            }

            let error =
                serde_json::from_str::<DeviceCodeErrorResponse>(&response.body).unwrap_or_default();
            let code = error.error.as_deref().or(error.message.as_deref());
            match code {
                Some("authorization_pending") => {
                    info!(
                        delay_secs = delay.as_secs(),
                        "waiting for Twitch authorization"
                    );
                    sleep(delay).await;
                }
                Some("slow_down") => {
                    debug!("received slow_down from Twitch; increasing poll interval");
                    delay += std::time::Duration::from_secs(5);
                    sleep(delay).await;
                }
                Some("access_denied") => bail!("Twitch authorization was denied by the user"),
                Some("expired_token") => {
                    bail!("device authorization expired before Twitch approval")
                }
                Some(other) => {
                    let description = error.description();
                    bail!("twitch token request failed: {other}{description}");
                }
                None => {
                    bail!(
                        "twitch token request failed (status {}): {}",
                        response.status,
                        error.description_or_body(&response.body)
                    );
                }
            }
        }
    }
}

pub async fn send_prepared_request(
    http: &Client,
    prepared: PreparedRequest,
) -> Result<RawResponse> {
    let PreparedRequest {
        url,
        method,
        headers,
        body,
    } = prepared;

    let mut request = match method {
        HttpMethod::Get => http.get(&url),
        HttpMethod::Post => http.post(&url),
        HttpMethod::Delete => http.delete(&url),
    };
    for (key, value) in headers {
        request = request.header(key, value);
    }
    if let Some(body) = body {
        request = request.body(body);
    }
    let response = request
        .send()
        .await
        .with_context(|| format!("failed to send request to {}", url))?;
    let status = response.status().as_u16();
    let body = response.text().await.unwrap_or_default();
    Ok(RawResponse { status, body })
}

pub async fn validate_access_token(
    http: &Client,
    access_token: &str,
) -> Result<Option<TokenValidation>> {
    let response = send_prepared_request(http, validate_token_request(access_token)).await?;

    if response.status == 200 {
        let payload = serde_json::from_str::<TokenValidation>(&response.body)
            .context("failed to parse Twitch token validation response")?;
        return Ok(Some(payload));
    }

    if response.status == 401 {
        return Ok(None);
    }

    bail!(
        "Twitch token validation failed with status {}: {}",
        response.status,
        response.body
    );
}

pub async fn refresh_access_token(
    http: &Client,
    client_id: &str,
    refresh_token: &str,
) -> Result<Option<TwitchTokenState>> {
    let response =
        send_prepared_request(http, refresh_token_request(client_id, refresh_token)).await?;

    if response.status == 200 {
        let exchange: TwitchTokenExchange = serde_json::from_str(&response.body)
            .context("failed to parse Twitch refresh token response")?;
        return Ok(Some(TwitchTokenState {
            access_token: exchange.access_token,
            refresh_token: exchange
                .refresh_token
                .unwrap_or_else(|| refresh_token.to_string()),
            expires_in_seconds: exchange.expires_in,
            scope: exchange.scope.unwrap_or_default(),
            token_type: exchange.token_type.unwrap_or_else(|| "bearer".to_string()),
            linked_at_ms: now_ms(),
        }));
    }

    if response.status == 400 || response.status == 401 {
        return Ok(None);
    }

    bail!(
        "Twitch refresh token request failed with status {}: {}",
        response.status,
        response.body
    );
}

pub async fn fetch_authenticated_user(
    http: &Client,
    access_token: &str,
    client_id: &str,
) -> Result<TwitchIdentity> {
    let raw =
        send_prepared_request(http, helix::user_lookup_request(access_token, client_id)).await?;
    helix::parse_user_lookup(raw).map_err(anyhow::Error::from)
}

pub async fn fetch_user_by_login(
    http: &Client,
    access_token: &str,
    client_id: &str,
    login: &str,
) -> Result<TwitchIdentity> {
    let raw = send_prepared_request(
        http,
        helix::user_lookup_by_login_request(access_token, client_id, login),
    )
    .await?;
    helix::parse_user_lookup(raw).map_err(anyhow::Error::from)
}

#[derive(Clone)]
pub struct ChatSubscriptionConfig {
    pub client_id: String,
    pub access_token: String,
    pub broadcaster_id: String,
    pub user_id: String,
}

#[derive(Debug, Error)]
#[error("Twitch rejected the provided EventSub credentials")]
pub struct EventSubAuthError;

#[cfg(feature = "tokio-eventsub")]
use futures_util::{SinkExt, StreamExt};
#[cfg(feature = "tokio-eventsub")]
use tokio::net::TcpStream;
#[cfg(feature = "tokio-eventsub")]
use tokio_tungstenite::WebSocketStream;
#[cfg(feature = "tokio-eventsub")]
use tokio_tungstenite::connect_async;
#[cfg(feature = "tokio-eventsub")]
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
#[cfg(feature = "tokio-eventsub")]
use tokio_tungstenite::tungstenite::{Message as WsMessage, http::Uri};

#[cfg(feature = "tokio-eventsub")]
const EVENTSUB_WEBSOCKET_URL: &str = "wss://eventsub.wss.twitch.tv/ws";

#[cfg(feature = "tokio-eventsub")]
pub struct EventSubChatStream {
    ws: WebSocketStream<tokio_tungstenite::MaybeTlsStream<TcpStream>>,
    session: EventSubWebSocketSession,
}

#[cfg(feature = "tokio-eventsub")]
impl EventSubChatStream {
    pub fn session_id(&self) -> &str {
        &self.session.id
    }

    pub async fn next_event(&mut self) -> Result<Option<EventSubStreamEvent>> {
        while let Some(message) = self.ws.next().await {
            let message = message.context("EventSub WebSocket error")?;
            match message {
                WsMessage::Text(text) => {
                    let envelope = decode_eventsub_websocket_message(&text)
                        .context("failed to parse EventSub payload")?;
                    if let Some(event) = envelope
                        .stream_event()
                        .context("failed to decode EventSub stream event")?
                    {
                        return Ok(Some(event));
                    }
                }
                WsMessage::Ping(payload) => {
                    self.ws
                        .send(WsMessage::Pong(payload))
                        .await
                        .context("failed to reply to EventSub ping")?;
                }
                WsMessage::Pong(_) => {}
                WsMessage::Binary(_) => {
                    debug!("dropping unexpected binary EventSub payload");
                }
                WsMessage::Close(frame) => {
                    debug!(?frame, "EventSub WebSocket closed by server");
                    return Ok(None);
                }
                WsMessage::Frame(_) => {}
            }
        }

        Ok(None)
    }

    pub async fn close(&mut self) -> Result<()> {
        self.ws
            .close(None)
            .await
            .context("failed to close EventSub WebSocket")
    }
}

#[cfg(feature = "tokio-eventsub")]
pub async fn connect_chat_stream(
    http: &Client,
    config: ChatSubscriptionConfig,
) -> Result<EventSubChatStream> {
    let mut ws = connect_eventsub_websocket(
        EVENTSUB_WEBSOCKET_URL,
        &config.client_id,
        &config.access_token,
    )
    .await?;
    let session = wait_for_session_welcome(&mut ws).await?;
    debug!(
        session_id = %session.id,
        keepalive = session.keepalive_timeout_seconds,
        "received EventSub session welcome"
    );

    create_chat_subscription(http, &config, &session.id).await?;

    Ok(EventSubChatStream { ws, session })
}

#[cfg(feature = "tokio-eventsub")]
async fn connect_eventsub_websocket(
    endpoint: &str,
    client_id: &str,
    access_token: &str,
) -> Result<WebSocketStream<tokio_tungstenite::MaybeTlsStream<TcpStream>>> {
    ensure_rustls_crypto_provider()?;

    let uri: Uri = endpoint
        .parse()
        .context("failed to parse EventSub WebSocket URL")?;
    let mut request = uri
        .into_client_request()
        .context("failed to build EventSub websocket request")?;
    request.headers_mut().insert(
        "Client-Id",
        client_id.parse().context("invalid client id header")?,
    );
    request.headers_mut().insert(
        "Authorization",
        format!("Bearer {access_token}")
            .parse()
            .context("invalid authorization header")?,
    );

    let (ws, response) = connect_async(request)
        .await
        .context("failed to connect to EventSub WebSocket")?;
    trace!(status = ?response.status(), endpoint, "connected to EventSub");
    Ok(ws)
}

#[cfg(feature = "tokio-eventsub")]
fn ensure_rustls_crypto_provider() -> Result<()> {
    if rustls::crypto::CryptoProvider::get_default().is_some() {
        return Ok(());
    }

    if rustls::crypto::ring::default_provider()
        .install_default()
        .is_err()
        && rustls::crypto::CryptoProvider::get_default().is_none()
    {
        bail!("failed to install the Rustls process-level crypto provider");
    }

    Ok(())
}

#[cfg(feature = "tokio-eventsub")]
async fn wait_for_session_welcome(
    ws: &mut WebSocketStream<tokio_tungstenite::MaybeTlsStream<TcpStream>>,
) -> Result<EventSubWebSocketSession> {
    while let Some(message) = ws.next().await {
        let message = message.context("EventSub WebSocket error during welcome")?;
        match message {
            WsMessage::Text(text) => {
                let envelope = decode_eventsub_websocket_message(&text)
                    .context("failed to parse EventSub welcome payload")?;
                match envelope.message_type()? {
                    EventSubMessageType::SessionWelcome => {
                        return envelope
                            .session()
                            .cloned()
                            .ok_or_else(|| anyhow!("EventSub welcome payload missing session"));
                    }
                    EventSubMessageType::SessionReconnect => {
                        let url = envelope
                            .session()
                            .and_then(|session| session.reconnect_url.clone())
                            .unwrap_or_else(|| "<missing reconnect URL>".to_string());
                        bail!("EventSub requested reconnect before welcome: {url}");
                    }
                    EventSubMessageType::SessionDisconnect => {
                        let status = envelope
                            .session()
                            .and_then(|session| session.status.clone())
                            .unwrap_or_else(|| "disconnected".to_string());
                        let reason = envelope
                            .session()
                            .and_then(|session| session.disconnect_reason.clone());
                        bail!("EventSub disconnected before welcome (status {status}): {reason:?}");
                    }
                    _ => {}
                }
            }
            WsMessage::Ping(payload) => {
                ws.send(WsMessage::Pong(payload))
                    .await
                    .context("failed to reply to EventSub ping")?;
            }
            WsMessage::Close(frame) => {
                bail!("EventSub WebSocket closed before welcome (frame: {frame:?})");
            }
            _ => {}
        }
    }

    bail!("EventSub WebSocket closed before welcome message was received")
}

#[cfg(feature = "tokio-eventsub")]
async fn create_chat_subscription(
    http: &Client,
    config: &ChatSubscriptionConfig,
    session_id: &str,
) -> Result<()> {
    create_subscription(
        http,
        config,
        &channel_chat_message_subscription_request(
            &config.broadcaster_id,
            &config.user_id,
            session_id,
        ),
    )
    .await?;
    if let Err(err) = create_subscription(
        http,
        config,
        &channel_chat_message_delete_subscription_request(
            &config.broadcaster_id,
            &config.user_id,
            session_id,
        ),
    )
    .await
    {
        debug!(
            ?err,
            "failed to subscribe to channel.chat.message_delete; continuing without delete events"
        );
    }
    Ok(())
}

#[cfg(feature = "tokio-eventsub")]
async fn create_subscription(
    http: &Client,
    config: &ChatSubscriptionConfig,
    subscription: &crate::eventsub::CreateEventSubSubscriptionRequest,
) -> Result<()> {
    let prepared = helix::create_eventsub_subscription_request(
        &config.client_id,
        &config.access_token,
        subscription,
    )
    .context("failed to serialize EventSub subscription request")?;
    let response = send_prepared_request(http, prepared).await?;

    if response.status == 202 {
        return Ok(());
    }

    match response.status {
        401 => Err(EventSubAuthError.into()),
        403 => {
            bail!(
                "Twitch reported insufficient permissions for EventSub subscription: {}",
                response.body
            );
        }
        _ => {
            bail!(
                "Twitch EventSub subscription request failed (status {}): {}",
                response.status,
                response.body
            );
        }
    }
}

fn now_ms() -> i64 {
    OffsetDateTime::now_utc().unix_timestamp() * 1_000
}

#[derive(Debug, Deserialize)]
struct DeviceCodeResponse {
    device_code: String,
    user_code: String,
    verification_uri: String,
    #[serde(default)]
    verification_uri_complete: Option<String>,
    expires_in: u64,
    #[serde(default)]
    interval: Option<u64>,
}

#[derive(Debug, Default, Deserialize)]
struct DeviceCodeErrorResponse {
    error: Option<String>,
    #[serde(default)]
    error_description: Option<String>,
    #[serde(default)]
    message: Option<String>,
}

impl DeviceCodeErrorResponse {
    fn description(&self) -> String {
        if let Some(desc) = &self.error_description {
            format!(" ({desc})")
        } else if let Some(message) = &self.message {
            format!(" ({message})")
        } else {
            String::new()
        }
    }

    fn description_or_body(&self, body: &str) -> String {
        let description = self.description();
        if description.is_empty() {
            body.to_string()
        } else {
            description
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "tokio-eventsub")]
    #[test]
    fn rustls_provider_init_is_idempotent() {
        ensure_rustls_crypto_provider().expect("first provider init should succeed");
        ensure_rustls_crypto_provider().expect("second provider init should succeed");
        assert!(rustls::crypto::CryptoProvider::get_default().is_some());
    }

    #[cfg(feature = "tokio-eventsub")]
    #[tokio::test(flavor = "current_thread")]
    async fn eventsub_connect_error_path_does_not_panic_for_missing_provider() {
        let result = tokio::time::timeout(
            std::time::Duration::from_secs(2),
            connect_eventsub_websocket("wss://127.0.0.1:1/ws", "test-client-id", "test-token"),
        )
        .await
        .expect("local websocket connect test should fail quickly");

        assert!(
            result.is_err(),
            "expected connect failure against a closed localhost port"
        );
    }

    #[test]
    fn prepared_request_send_supports_form_posts() {
        let request = crate::oauth::device_token_request("client", "device");
        assert_eq!(request.method, HttpMethod::Post);
        assert!(
            request
                .body
                .expect("body should exist")
                .contains("device_code=device")
        );
    }
}