Crate oauth_device_flows

Source
Expand description

§OAuth Device Flows

A specialized Rust library that implements the OAuth 2.0 Device Authorization Grant (RFC 8628), commonly known as the “device flow” or “device code flow”. This authentication flow is particularly useful for devices with limited input capabilities, headless systems, or CLI applications where redirecting to a browser is impractical.

§Features

  • Complete implementation of the OAuth 2.0 Device Authorization Grant
  • Support for multiple OAuth providers (Microsoft, Google, GitHub, etc.)
  • Customizable polling strategies with exponential backoff
  • Token caching and refresh mechanisms
  • Strong typing for OAuth responses
  • Comprehensive error handling and recovery
  • Minimal dependencies for embedded use
  • Async/await support
  • Custom URI handling for verification
  • QR code generation for device code display (optional feature)

§Quick Start

use oauth_device_flows::{DeviceFlow, DeviceFlowConfig, Provider};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Configure the device flow
    let config = DeviceFlowConfig::new()
        .client_id("your-client-id")
        .scopes(vec!["user.read", "offline_access"])
        .poll_interval(Duration::from_secs(5))
        .max_attempts(12);
     
    // Create a device flow instance for Microsoft
    let mut device_flow = DeviceFlow::new(Provider::Microsoft, config)?;
     
    // Start the device authorization flow
    let auth_response = device_flow.initialize().await?;
     
    // Display information to the user
    println!("To sign in, use a web browser to open the page {} and enter the code {} to authenticate.",
        auth_response.verification_uri(),
        auth_response.user_code());
     
    // Poll for the token
    println!("Waiting for authentication...");
    let token_response = device_flow.poll_for_token().await?;
     
    println!("Successfully authenticated!");
    Ok(())
}

Re-exports§

pub use config::DeviceFlowConfig;
pub use device_flow::DeviceFlow;
pub use error::DeviceFlowError;
pub use error::Result;
pub use provider::Provider;
pub use token::TokenManager;
pub use types::AuthorizationResponse;
pub use types::TokenResponse;

Modules§

config
Configuration for OAuth device flows
device_flow
Main device flow implementation
error
Error types for OAuth device flows
provider
OAuth provider configurations and implementations
token
Token management and refresh functionality
types
Core types for OAuth device flows