twitch_oauth_token 2.0.7

Type-safe Twitch OAuth 2.0 authentication library with CSRF protection and full scope support
docs.rs failed to build twitch_oauth_token-2.0.7
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: twitch_oauth_token-2.0.6

twitch_oauth_token

crates.io Documentation license: MIT

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
  • Stateless CSRF protection - Cryptographically secure state validation without server-side storage
  • 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 = "2"
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();

    // Create authorization URL for the user to visit
    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