twitch_oauth_token

A Rust library for Twitch OAuth 2.0 authentication with compile-time safety and comprehensive scope support.
- Token management - Refresh, validate, and revoke tokens
- Type-safe OAuth flows - Compile-time prevention of invalid operations using the type-state pattern
- HMAC-based CSRF protection - Cryptographically secure state validation using HMAC-SHA256 with timestamp validation
- Full Twitch scope support - All Twitch API scopes with convenient helper methods
- Pre-configured HTTP client - Includes an optimized authentication client preset from asknothingx2-util
- Twitch mock API support - Built-in support for the Twitch CLI mock API for testing and certification
Installation
[dependencies]
twitch_oauth_token = "3"
tokio = { version = "1", features = ["full"] }
Quick Start
App access token
use twitch_oauth_token::TwitchOauth;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let oauth = TwitchOauth::new("client_id", "client_secret");
let resp = oauth.app_access_token().await?;
let token = resp.app_token().await?;
println!("App token: {}", token.access_token.secret());
println!("Expires in: {} seconds", token.expires_in);
Ok(())
}
User access token
use std::str::FromStr;
use twitch_oauth_token::{scope::ChatScopes, RedirectUrl, TwitchOauth};
fn main() {
let oauth = TwitchOauth::new("client_id", "client_secret")
.set_redirect_uri(RedirectUrl::from_str("http://example.com/auth/callback").unwrap());
let mut auth_request = oauth.authorization_url();
auth_request.scopes_mut().chat_api_as_user();
let auth_url = auth_request.url();
println!("Visit: {}", auth_url);
}
Handling OAuth Callback
use twitch_oauth_token::{OAuthCallbackQuery, TwitchOauth, UserAuth};
async fn handle_callback(
oauth: &TwitchOauth<UserAuth>,
oauth_callback: OAuthCallbackQuery,
) -> Result<(), twitch_oauth_token::Error> {
let response = oauth
.user_access_token(oauth_callback.code, oauth_callback.state)
.await?;
let token = response.user_token().await?;
println!("Access token: {}", token.access_token.secret());
println!("Refresh token: {}", token.refresh_token.secret());
println!("Scopes: {:?}", token.scope);
println!("Expires in: {} seconds", token.expires_in);
Ok(())
}
Feature Flags
oneshot-server - Built-in development server for handling OAuth callbacks
test - Testing utilities and mock server support
License
Licensed under either of:
- Apache License, Version 2.0
- MIT license