Device Code Flow
The Device Code flow (also known as Device Authorization Grant) is designed for devices that lack a browser or have limited input capabilities, such as smart TVs, IoT devices, or CLI tools. It allows users to authorize devices using a secondary device with a browser.Overview
This flow is ideal for browserless or input-constrained devices. The device displays a user code and a verification URL, which the user enters on a separate device (like a smartphone or computer) to complete the authorization. The original device polls the authorization server until the user completes the authorization.Flow Steps
- Device Authorization Request: The device requests a device code and user code from the authorization server
- Display User Code: The device displays the user code and verification URL to the user
- User Authorization: The user navigates to the verification URL on another device and enters the user code
- User Authentication: The user authenticates and authorizes the application
- Polling: The device polls the authorization server to check if authorization is complete
- Token Exchange: Once authorized, the device receives an access token
- Access Resources: Use the access token to access protected resources
Implementation
Prerequisites
Before starting, you’ll need:- Your application’s
client_idandclient_secret - The device authorization endpoint:
https://login.camall.io/oauth/device/code - The token endpoint:
https://login.camall.io/oauth/token - The verification URL:
https://login.camall.io/activate
Step 1: Initiate Device Authorization Request
Request a device code and user code from the authorization server:client_id: Your application’s client IDscope: Requested permissions (space-separated)
device_code: The device verification code (keep this secret)user_code: The code the user must enter (display this to the user)verification_uri: The URL where the user should go to authorizeverification_uri_complete: Optional URL with the user code pre-filledexpires_in: How long the codes are valid (in seconds)interval: Minimum time between polling requests (in seconds)
Step 2: Display User Code to User
Show the user code and instructions to the user:- Display the
verification_urianduser_codeseparately - Display the
verification_uri_completeas a clickable link or QR code - Show a QR code that encodes the
verification_uri_complete
Step 3: Poll for Authorization
While the user is authorizing on another device, poll the token endpoint:grant_type=urn:ietf:params:oauth:grant-type:device_code: Specifies device code flowdevice_code: The device code received in step 1client_id: Your application’s client IDclient_secret: Your application’s client secret
interval seconds and poll again
2. Slow Down (Polling too quickly):
Step 4: Exchange Complete - Use Access Token
Once you receive the access token, make authenticated API requests:Step 5: Refresh the Access Token (Optional)
When the access token expires, use the refresh token to obtain a new one:Complete Example Script
Here’s a complete bash script demonstrating the entire flow:Security Best Practices
- Protect device_code: The device code should be kept secret and not displayed to users
- Display user_code clearly: Make the user code easy to read and enter
- Respect polling interval: Always wait at least
intervalseconds between polls - Handle slow_down: Increase polling interval when receiving slow_down errors
- Set appropriate expiration: Device codes should have limited lifetime (typically 10-15 minutes)
- Use HTTPS: All endpoints must use HTTPS in production
- Secure client secret: Protect your client secret on the device (consider using public clients for highly insecure devices)
- Implement timeout: Stop polling after the
expires_inperiod - User feedback: Provide clear feedback about authorization status
HTTP Request Examples
Raw HTTP Request - Device Authorization
Raw HTTP Request - Token Polling
Raw HTTP Request - API Call with Bearer Token
Use Cases
The Device Code flow is ideal for:- Smart TVs and streaming devices: Apps on TVs where typing is difficult
- CLI tools and scripts: Command-line applications that need user authorization
- IoT devices: Devices without screens or keyboards
- Gaming consoles: Applications on gaming platforms
- Printer/scanner applications: Devices with limited input capabilities
- CI/CD pipelines: Automated systems requiring user authorization
Comparison with Other Flows
| Feature | Device Code | Authorization Code | Client Credentials |
|---|---|---|---|
| User interaction | On separate device | Direct browser redirect | None (machine-to-machine) |
| Requires browser on device | No | Yes | No |
| User context | Yes | Yes | No |
| Best for | Input-constrained devices | Web/mobile apps | Server-to-server |
| Security | High (with polling) | Very high (with PKCE) | High (client credentials) |