Device Flow
The Device Authorization Flow (RFC 8628) enables authentication on devices with limited input capabilities. like smart TVs, CLI tools, or IoT devices.
Overview
When a device can't easily handle browser redirects or has no keyboard, the Device Flow lets users authenticate by entering a short code on a separate device (like their phone or computer).
How It Works
- Device requests authorization from the provider
- Provider returns a user code and verification URL
- Device displays the code and URL to the user
- User visits the URL on another device and enters the code
- User authenticates and authorizes
- Device polls the token endpoint until authorization completes
- Provider returns access tokens to the device
Provider Support
Not all OAuth providers support Device Flow. GitHub, Google, and Microsoft are common providers that do. Check your provider's documentation.
Implementation
The Device Flow involves three main steps: initiating the request, prompting the user, and polling for the token.
use authkestra_flow::DeviceFlow;
let flow = DeviceFlow::new(client_id, device_auth_url, token_url);
// 1. Initiate authorization
let device_resp = flow.initiate_device_authorization(&["user"]).await?;
// 2. Prompt user (example CLI output)
println!("Go to {} and enter code: {}",
device_resp.verification_uri,
device_resp.user_code
);
// 3. Poll for the token
let token = flow.poll_for_token(&device_resp.device_code, device_resp.interval).await?;Full Example
For a complete working implementation of a CLI-based device flow, see the example in the repository:
Polling Interval
The device_resp.interval contains the minimum polling interval
(in seconds) specified by the provider. Polling more frequently may result in
rate limiting errors.
Use Cases
Device Flow is ideal for:
- CLI Applications: Authenticate users in terminal-based tools
- Smart TVs: Login on devices with limited input
- IoT Devices: Secure authentication for connected devices
- Gaming Consoles: User authentication without a keyboard
- Kiosk Mode: Public terminals or shared devices
// Example: CLI tool authentication
fn main() {
println!("Welcome to MyCLI!");
println!("You need to authenticate to continue.\n");
// Start device flow
let token = authenticate_with_device_flow().await?;
// Save token for future use
save_token_to_config(&token)?;
println!("Authenticated! You can now use the CLI.");
}