How to add auth to your Rust CLI using WorkOS
Authenticate users in your Rust command-line tool with a secure OAuth 2.0 Device Code flow using WorkOS. This tutorial shows how to implement login via the terminal, step by step.
Modern CLI apps often need to authenticate users, and using a simple web-based OAuth flow is the cleanest way to do it.
In this tutorial, we'll implement a CLI login flow using WorkOS CLI Auth to authenticate users from the command line by using a web browser. Based on the OAuth 2.0 Device Authorization Grant, this method is perfect for CLIs where embedding a browser isn't feasible.
In this guide, we'll walk through how to add auth to your Rust CLI using WorkOS, step by step.
What we will build
You'll create a Rust CLI that:
- Requests a device code from WorkOS: The CLI starts the login flow by contacting WorkOS to get a
device_code(for itself) and auser_code(for the user), along with a URL the user should visit. - Displays a user-friendly prompt: The CLI prints the
user_codeand verification URL in the terminal, and optionally opens the browser automatically for the user to log in and approve. - Polls WorkOS while the user logs in: As the user completes authentication in the browser, the CLI quietly checks in with WorkOS every few seconds to see if the user has completed the login.
- Exchanges the device code for tokens: Once authorized, the CLI receives an access token ready to make authenticated API calls.
This flow is ideal for tools running in environments that can't easily embed a browser window (like the terminal), and it avoids the need to paste auth tokens manually.
Let's dive in.
Prerequisites
Before starting, make sure you have:
- A WorkOS account (first 1 million active users free, no credit card required).
- A CLI app registered as a public app. You can configure one in the WorkOS dashboard > Applications. Note down the Client ID.
- Rust 1.75+ installed (with
cargo). - You must use AuthKit for your authentication UI (for info on how to customize the look-and-feel see our docs).
Project setup
Create a new project and move into it:
Add these dependencies to your Cargo.toml:
We'll use reqwest for HTTP, tokio for the async runtime, serde for JSON deserialization, anyhow for ergonomic error handling, and webbrowser to open the verification URL.
Step 1: Start the auth flow from the CLI
Start by initiating the device authorization flow from your CLI tool. This will generate a user-facing code and a verification URL.
This function hits the WorkOS Device Authorization endpoint and retrieves:
- device_code: For your CLI only.
- user_code: To show the user.
- verification_uri_complete: A URL users can click to authorize.
- interval: How often to poll for the token.
Modeling the response with a serde-derived struct gives you compile-time guarantees that your code matches the API shape, and lets the rest of the program access fields by name (e.g. data.user_code) instead of through stringly-typed map lookups.
Step 2: Prompt the user to authenticate
Display the user code and link in the terminal. Optionally, open the browser automatically.
The webbrowser crate is cross-platform, it picks the right command (open on macOS, xdg-open on Linux, start on Windows) so you don't have to shell out manually. We discard the result with let _ = because failing to open the browser is non-fatal: the user can still copy the URL.
Note the following:
- Always display the user_code and verification_uri to the user.
- Never show the device_code, it’s only used internally for polling.
Step 3: Poll for access tokens
While the user authenticates in their browser, your app should poll the token endpoint.
Note the following:
- Poll at the interval specified in the authorization response (every 5 seconds).
- Respect slow_down errors by increasing your polling interval.
- Stop polling when you receive access_denied or expired_token errors.
- Implement a reasonable timeout to avoid infinite polling.
tokio::time::sleep is non-blocking; while we wait, the runtime is free to do other work. Using match on the error string makes each terminal condition explicit and exhaustive, which the compiler will help enforce.
The response looks like this:
Putting it all together
Wire the three steps into main. The #[tokio::main] attribute sets up the async runtime, and because poll_for_tokens is already an async function we can simply .await it.
If you want the polling to happen concurrently with other work, spawn it as a task with tokio::spawn(poll_for_tokens(...)) and .await the resulting JoinHandle when you need the result. For a single-purpose login command, the straight-line .await above is simpler and equivalent.
Run it with:
That’s it! You’ve just added OAuth-powered login to your CLI.
Your users can now authenticate securely through the browser, without ever pasting tokens by hand.