A runnable demo of browser-based OAuth authentication flows in command-line applications, powered by WorkOS AuthKit.
- 🔐 Secure browser-based OAuth authentication
- 🔒 WorkOS AuthKit integration
- 💾 Persistent token storage in system keychain
- 🎨 Stylized authentication successful page
Running npm start login will start the authentication flow:
╭───────────────────────────────╮
│ │
│ WorkOS CLI Authentication │
│ │
╰───────────────────────────────╯
⠋ Starting authentication flow...
→ Local server started
→ Opening browser for authentication
⠋ Waiting for authentication...
→ Received authorization code
⠼ Processing authentication...
About to save token...
→ Successfully stored credentials in system keychain
Token saved successfully
Token Storage Details:
🔐 Credentials saved to system keychain
🔍 View stored credentials with:
npm start keychainThis will:
- Start a local server
- Open your browser for WorkOS authentication
- Process the authentication response
- Save your access token locally
Following successful authentication, you can fetch a secure resource using your stored access token:
npm start meExample output:
🧑 User Profile:
Email: booker@example.com
First Name: Booker
Last Name: DeWitt
ID: user_02JCQ1E9ZV4JQXNCT0TD4V7DJ3
Created At: 11/14/2024, 11:30:56 PM
This demonstrates:
- The OAuth authentication flow with WorkOS AuthKit
- Secure token storage in your system keychain
- Using the stored credentials to make authenticated API calls
View the contents of your stored credentials:
npm start keychainExample output:
🔐 Keychain Contents:
Service: workos-cli
Account: default
Status: Entry found
Contents: {
"accessToken": "eyJhbGc...X_YphjyXXXXX",
"userId": "user_02XXXXXXXXXXXXX"
}
The CLI implements a secure, multi-tiered storage strategy for authentication credentials:
-
System Keychain (Primary):
- First attempts to store credentials in the system's native keychain
- Uses
@napi-rs/keyringfor cross-platform keychain access - macOS: Keychain Access
- Windows: Credential Manager
- Linux: libsecret/Secret Service API
-
Encrypted File Fallback:
- If keychain access fails, falls back to encrypted file storage
- Files are encrypted using industry-standard AES-256-GCM
- Encryption key is derived from machine-specific factors
- Similar to GitHub CLI's credential storage approach
This approach mirrors the GitHub CLI (gh) implementation, which is considered an industry best practice for CLI tools. The multi-tiered strategy ensures:
- Maximum security by preferring system keychains
- Broad compatibility across different environments
- Graceful fallback when keychain access is unavailable
- Zero configuration required from users
# Clone the repository
git clone https://github.com/zackproser-workos/cli-auth-example.git
cd oauth-cli-demo
# Install dependencies
npm install
# Set environment variables
cp .env.example .env.localAdd your WorkOS credentials to .env.local:
WORKOS_CLIENT_ID=client_xxxxxxxxxxxx
WORKOS_API_KEY=sk_xxxxxxxxxxxx
# Optional: Set a custom token directory or leave unset to use default: ~/.workos
WORKOS_TOKEN_DIR=/path/to/token/directory
# Run in development mode
npm run dev
# Build the project
npm run build
# Run the built version
npm startThis CLI demonstrates the common pattern used by tools like GitHub's CLI (gh) for authentication:
- The CLI initiates an OAuth flow by starting a local server
- It launches the user's browser to the WorkOS AuthKit authentication page
- After successful authentication, WorkOS redirects to the local server
- The CLI exchanges the authorization code for an access token
- The token is securely stored in the system keychain for future use
- OAuth tokens are stored securely using system keychain integration
- Environment variables are strictly validated
- Local server only accepts connections from localhost
- HTTPS is used for all OAuth token exchanges
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
The CLI can be configured using environment variables:
| Variable | Required | Default | Description |
|---|---|---|---|
| WORKOS_API_KEY | Yes | - | Your WorkOS API Key |
| WORKOS_CLIENT_ID | Yes | - | Your WorkOS Client ID |
| WORKOS_TOKEN_DIR | No | ~/.workos | Directory where the authentication token will be stored |
By default, the CLI stores the authentication token in ~/.workos/token. You can customize this location by setting the WORKOS_TOKEN_DIR environment variable.
To use a custom directory for token storage:
export WORKOS_TOKEN_DIR=/custom/path/to/token/directoryThe project includes a comprehensive test suite using Jest. You can run tests using the following commands:
# Run all tests
npm test
# Run tests in watch mode (useful during development)
npm run test:watch
# Run tests with coverage report
npm run test:coverageTest files are located in src/__tests__/ and follow the naming convention *.test.ts. The suite includes tests for:
- OAuth authentication flow
- Token storage and retrieval
- Environment variable validation
- UI components

