Skip to main content

OAuth 2.0 Device Flow

GitHub-style device authorization for CLIs, installers, and limited-input devices

The OAuth 2.0 Device Authorization Grant (Device Flow) allows applications running on devices with limited input capabilities—such as CLIs, installers, smart TVs, or IoT devices—to obtain user authorization without requiring a web browser on the device itself.

How It Works​

  1. Device initiates: Your CLI/app requests a device code and user code
  2. User approves: User visits a URL and enters the short user code
  3. Device polls: Your app polls the token endpoint until approved
  4. Token issued: Once approved, your app receives an access token

This is the same flow used by GitHub CLI (gh auth login), Netlify CLI, and other developer tools.

Quick Start​

Step 1: Request Device and User Codes​

curl -X POST https://auth.api.us.23blocks.com/device/authorize \
-H "Content-Type: application/json" \
-d '{
"client_id": "your_client_id",
"scope": "read:user write:projects"
}'

Response:

{
"device_code": "NGU5OWE4YjgtM2E3My00ZTg3LWJhNmEtNjJhMmNi...",
"user_code": "ABCD-1234",
"verification_uri": "https://23blocks.com/device",
"verification_uri_complete": "https://23blocks.com/device?user_code=ABCD-1234",
"expires_in": 600,
"interval": 5
}

Step 2: Display Instructions to User​

Show the user the user_code and verification_uri:

To authorize this device, visit:
https://23blocks.com/device

And enter the code: ABCD-1234

This code expires in 10 minutes.

Tip: Use verification_uri_complete to generate a QR code or clickable link that pre-fills the user code.

Step 3: Poll for Authorization​

Poll the token endpoint every interval seconds (5 seconds by default) until the user approves or denies, or the code expires.

curl -X POST https://auth.api.us.23blocks.com/oauth/token \
-H "Content-Type: application/json" \
-d '{
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"device_code": "NGU5OWE4YjgtM2E3My00ZTg3LWJhNmEtNjJhMmNi..."
}'

Response (pending):

{
"error": "authorization_pending",
"error_description": "The authorization request is still pending."
}

Response (success):

{
"access_token": "at_live_abc123...",
"token_type": "Bearer",
"expires_in": 300,
"scope": "read:user write:projects"
}

Endpoint Reference​

POST /device/authorize​

Initiates a device authorization flow and returns a device code and user code.

Request:

FieldTypeRequiredDescription
client_idstringNoYour application's client ID (optional for public clients)
scopestringNoSpace-separated list of requested scopes

Response:

FieldTypeDescription
device_codestringDevice verification code (43-char urlsafe_base64, one-time use)
user_codestringShort code for user to enter (format: ABCD-1234)
verification_uristringURL where user enters the code
verification_uri_completestringURL with user_code pre-filled (for QR codes/links)
expires_innumberSeconds until codes expire (600 = 10 minutes)
intervalnumberMinimum seconds between polling requests (5)

Example:

const response = await fetch('https://auth.api.us.23blocks.com/device/authorize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
client_id: 'cli_app_123',
scope: 'read:user write:projects'
})
});

const { device_code, user_code, verification_uri, interval } = await response.json();

console.log(`Visit ${verification_uri} and enter code: ${user_code}`);

GET /apps/:url_id/device/authorizations/:user_code​

Show device authorization details to the user before they approve or deny.

Authentication: Requires Authorization: Bearer <user JWT> + X-API-KEY header

Path Parameters:

ParameterDescription
url_idMust match the tenant of the API key and JWT. Using a different tenant's url_id returns 404.
user_codeThe 8-character user code (e.g., ABCD-1234)

Headers:

HeaderRequiredDescription
AuthorizationYesBearer <user JWT> - Must be the user's JWT for the tenant that owns the API key
X-API-KEYYesYour application's API key

Important: Send the JWT in the Authorization header, not in the query string. Query parameters are logged and should never contain tokens.

Response: JSON:API format with authorization details (key on attributes.user_code, no data.id)

Error Responses:

StatusCodeDescription
401-Missing or invalid JWT
404GW-DEV-SHOW-002Invalid, expired, other-tenant code, or wrong url_id
429GW-DEV-SHOW-006Too many invalid codes (10 in 10 minutes per tenant+address)

Example:

const response = await fetch(
`https://auth.api.us.23blocks.com/apps/${tenantUrlId}/device/authorizations/${userCode}`,
{
headers: {
'Authorization': `Bearer ${userJWT}`,
'X-API-KEY': apiKey
}
}
);

if (response.status === 401) {
alert('Please log in to authorize this device');
return;
}

if (response.status === 404) {
alert('Invalid or expired code');
return;
}

const data = await response.json();
// Display authorization details to user

POST /apps/:url_id/device/verify​

Approve or deny the device authorization.

Authentication: Requires Authorization: Bearer <user JWT>

Path Parameters:

ParameterDescription
url_idMust match the tenant of the API key and JWT. Wrong tenant returns 400 Invalid Code.

Request Body:

FieldTypeRequiredDescription
user_codestringYesThe user code displayed by the device
action_typestringYesEither 'approve' or 'deny'

Response: JSON:API format with status

Error Responses:

StatusCodeDescription
400GW-DEV-VERInvalid code or wrong url_id
400GW-DEV-VERAlready denied (after 5 failed attempts)
401-Missing or invalid JWT
429GW-DEV-VER-429Too many invalid codes (10 in 10 minutes)

Example:

const response = await fetch(
`https://auth.api.us.23blocks.com/apps/${tenantUrlId}/device/verify`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${userAccessToken}`
},
body: JSON.stringify({
user_code: 'ABCD-1234',
action_type: 'approve'
})
}
);

if (response.status === 401) {
alert('Authentication required');
return;
}

if (response.status === 400) {
const data = await response.json();
alert(data.errors[0].detail); // "Invalid code" or "Already denied"
return;
}

POST /oauth/token​

Exchanges the device code for an access token. The device should poll this endpoint until the user approves, denies, or the code expires.

Request:

FieldTypeRequiredDescription
grant_typestringYesMust be 'urn:ietf:params:oauth:grant-type:device_code'
device_codestringYesThe device code from /device/authorize

Success Response:

FieldTypeDescription
access_tokenstringThe access token
token_typestringAlways 'Bearer'
expires_innumberSeconds until token expires (300 = 5 minutes)
scopestringGranted scopes

Example:

import time
import requests

# After getting device_code and showing user_code to user
while True:
response = requests.post('https://auth.api.us.23blocks.com/oauth/token', json={
'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',
'device_code': device_code
})

data = response.json()

if 'access_token' in data:
# Success! User approved
print(f"Authenticated! Token: {data['access_token']}")
break
elif data.get('error') == 'authorization_pending':
# Still waiting for user
time.sleep(interval)
elif data.get('error') == 'slow_down':
# Polling too fast
interval += 5
time.sleep(interval)
else:
# Denied or expired
print(f"Authorization failed: {data.get('error')}")
break

Error Codes​

All errors follow RFC 8628 (OAuth 2.0 Device Authorization Grant), plus additional 23blocks-specific error handling.

Token Endpoint Errors (POST /oauth/token)​

Error CodeDescriptionAction
authorization_pendingUser hasn't approved yetContinue polling
slow_downPolling too fastAdd 5 seconds to your interval
access_deniedUser denied the requestStop polling, show error to user
expired_tokenDevice code expired (>10 min)Restart flow with new device code
invalid_grantInvalid or already-used device codeRestart flow with new device code

Example Error Response:

{
"error": "slow_down",
"error_description": "You are polling too frequently. Please slow down."
}

Verification Endpoint Errors​

HTTP 401 - Unauthorized

The GET show and POST verify endpoints require a valid user JWT in the Authorization: Bearer header:

{
"errors": [{
"status": "401",
"title": "Unauthorized",
"detail": "Valid authentication token required"
}]
}

Important: Always send the JWT in the Authorization header, never in the query string (query parameters are logged).

HTTP 429 - Too Many Invalid Codes

When users enter 10 invalid user codes within 10 minutes (per tenant and client address), the verification endpoints return:

{
"errors": [{
"status": "429",
"source": "Device Authorization",
"code": "GW-DEV-SHOW-006",
"title": "Too Many Attempts",
"detail": "Too many invalid codes. Try again later."
}]
}

Response Headers:

  • Retry-After: 600 (10 minutes)

HTTP 404 - Invalid or Expired Code (GET show only)

All invalid codes on the GET show endpoint (unknown, expired, from another tenant, or wrong url_id) return:

{
"errors": [{
"status": "404",
"code": "GW-DEV-SHOW-002",
"title": "Invalid Code",
"detail": "Invalid or expired user code"
}]
}

Note: On POST verify, a wrong url_id returns 400 'Invalid Code' instead of 404.

HTTP 400 - Already Denied

After 5 failed verification attempts on a valid pending code, the code is automatically denied:

{
"errors": [{
"status": "400",
"code": "GW-DEV-VER",
"title": "Already Denied",
"detail": "This authorization has been denied"
}]
}

Rate Limits​

Invalid Code Protection​

  • 10 invalid user codes per 10 minutes per company and client address
  • Applies to GET /device/verify (show) and POST /device/verify endpoints
  • Returns HTTP 429 with Retry-After: 600 (10 minutes)
  • Error code: GW-DEV-SHOW-006 (show) or GW-DEV-VER-429 (verify)

Important: This limit protects against brute-force attacks on user codes. Display clear error messages to users when this limit is reached.

IP-Based Throttling (Rack::Attack)​

  • 60 requests per 5 minutes to GET /device/verify (show) per IP
  • 30 requests per 5 minutes to POST /device/verify per IP
  • Returns HTTP 429 with RateLimit-* headers
  • Error body: {"error": "Throttle limit reached. Retry later."}

General Limits​

  • 20 requests per 10 minutes per IP address (POST /device/authorize)
  • 30 requests per 10 minutes per API key/client_id

Exceeding any limit returns HTTP 429. Always check for Retry-After header and respect it.

Flow Diagram​

┌─────────┐                                  ┌────────────┐
│ CLI/App │ │ Auth API │
└────┬────┘ └─────┬──────┘
│ │
│ POST /device/authorize │
│─────────────────────────────────────────────>│
│ │
│ device_code, user_code, verification_uri │
│<─────────────────────────────────────────────│
│ │
│ │
┌─┴──────────────────────────────────┐ │
│ Show to user: │ │
│ Visit: https://23blocks.com/device │ │
│ Code: ABCD-1234 │ │
└─────────────────────────────────────┘ │
│ │
│ (User opens browser and logs in) │
│ ┌────┴─────┐
│ │ User │
│ │ Browser │
│ └────┬─────┘
│ │
│ │ GET /device?user_code=ABCD-1234
│ │
│ │ (Shows approval page)
│ │
│ │ POST /device/verify
│ │ {user_code, action_type: 'approve'}
│ │
│ │ ✓ Approved
│ │
│ POST /oauth/token (polling every 5s) │
│─────────────────────────────────────────────>│
│ │
│ {error: "authorization_pending"} │
│<─────────────────────────────────────────────│
│ │
│ ... wait 5s ... │
│ │
│ POST /oauth/token │
│─────────────────────────────────────────────>│
│ │
│ {access_token, expires_in, scope} │
│<─────────────────────────────────────────────│
│ │
┌─┴─────────────────┐ │
│ ✓ Authenticated! │ │
│ Store token │ │
└────────────────────┘ │

Code Examples​

JavaScript/Node.js​

async function authenticateDevice() {
// Step 1: Request device code
const authResponse = await fetch('https://auth.api.us.23blocks.com/device/authorize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
client_id: 'my_cli_app',
scope: 'read:user write:projects'
})
});

const { device_code, user_code, verification_uri, interval } = await authResponse.json();

// Step 2: Show user code
console.log('\nTo authorize this CLI, visit:');
console.log(` ${verification_uri}`);
console.log(`\nAnd enter the code: ${user_code}\n`);
console.log('Waiting for authorization...');

// Step 3: Poll for token
let pollInterval = interval * 1000; // Convert to ms

while (true) {
await new Promise(resolve => setTimeout(resolve, pollInterval));

const tokenResponse = await fetch('https://auth.api.us.23blocks.com/oauth/token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
device_code
})
});

const data = await tokenResponse.json();

if (data.access_token) {
console.log('✓ Authorization successful!');
return data.access_token;
} else if (data.error === 'authorization_pending') {
// Keep waiting
continue;
} else if (data.error === 'slow_down') {
// Increase interval by 5 seconds
pollInterval += 5000;
continue;
} else {
// Denied or expired
throw new Error(`Authorization failed: ${data.error}`);
}
}
}

// Usage
const token = await authenticateDevice();
// Now use token for authenticated API calls

Python​

import time
import requests

def authenticate_device():
# Step 1: Request device code
auth_response = requests.post(
'https://auth.api.us.23blocks.com/device/authorize',
json={
'client_id': 'my_cli_app',
'scope': 'read:user write:projects'
}
)
auth_data = auth_response.json()

device_code = auth_data['device_code']
user_code = auth_data['user_code']
verification_uri = auth_data['verification_uri']
interval = auth_data['interval']

# Step 2: Show user code
print('\nTo authorize this CLI, visit:')
print(f' {verification_uri}')
print(f'\nAnd enter the code: {user_code}\n')
print('Waiting for authorization...')

# Step 3: Poll for token
poll_interval = interval

while True:
time.sleep(poll_interval)

token_response = requests.post(
'https://auth.api.us.23blocks.com/oauth/token',
json={
'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',
'device_code': device_code
}
)
token_data = token_response.json()

if 'access_token' in token_data:
print('✓ Authorization successful!')
return token_data['access_token']
elif token_data.get('error') == 'authorization_pending':
# Keep waiting
continue
elif token_data.get('error') == 'slow_down':
# Increase interval by 5 seconds
poll_interval += 5
continue
else:
# Denied or expired
raise Exception(f"Authorization failed: {token_data.get('error')}")

# Usage
token = authenticate_device()
# Now use token for authenticated API calls

Go​

package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)

type DeviceAuthResponse struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationURI string `json:"verification_uri"`
VerificationURIComplete string `json:"verification_uri_complete"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
}

type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
Error string `json:"error"`
}

func AuthenticateDevice() (string, error) {
// Step 1: Request device code
authReq := map[string]string{
"client_id": "my_cli_app",
"scope": "read:user write:projects",
}
authBody, _ := json.Marshal(authReq)

authResp, err := http.Post(
"https://auth.api.us.23blocks.com/device/authorize",
"application/json",
bytes.NewBuffer(authBody),
)
if err != nil {
return "", err
}
defer authResp.Body.Close()

var authData DeviceAuthResponse
json.NewDecoder(authResp.Body).Decode(&authData)

// Step 2: Show user code
fmt.Println("\nTo authorize this CLI, visit:")
fmt.Printf(" %s\n", authData.VerificationURI)
fmt.Printf("\nAnd enter the code: %s\n\n", authData.UserCode)
fmt.Println("Waiting for authorization...")

// Step 3: Poll for token
pollInterval := time.Duration(authData.Interval) * time.Second

for {
time.Sleep(pollInterval)

tokenReq := map[string]string{
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"device_code": authData.DeviceCode,
}
tokenBody, _ := json.Marshal(tokenReq)

tokenResp, err := http.Post(
"https://auth.api.us.23blocks.com/oauth/token",
"application/json",
bytes.NewBuffer(tokenBody),
)
if err != nil {
return "", err
}

var tokenData TokenResponse
json.NewDecoder(tokenResp.Body).Decode(&tokenData)
tokenResp.Body.Close()

if tokenData.AccessToken != "" {
fmt.Println("✓ Authorization successful!")
return tokenData.AccessToken, nil
} else if tokenData.Error == "authorization_pending" {
// Keep waiting
continue
} else if tokenData.Error == "slow_down" {
// Increase interval by 5 seconds
pollInterval += 5 * time.Second
continue
} else {
// Denied or expired
return "", fmt.Errorf("authorization failed: %s", tokenData.Error)
}
}
}

func main() {
token, err := AuthenticateDevice()
if err != nil {
panic(err)
}
// Now use token for authenticated API calls
fmt.Printf("Token: %s\n", token)
}

Best Practices​

1. Handle HTTP 429 Rate Limiting​

Always check for HTTP 429 responses and respect the Retry-After header. Display a clear message to users when rate limits are hit.

const response = await fetch('https://auth.api.us.23blocks.com/oauth/token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
device_code: deviceCode
})
});

if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After');
const waitMinutes = Math.ceil(retryAfter / 60);
console.error(`Rate limit reached. Please wait ${waitMinutes} minutes before trying again.`);
return;
}

const data = await response.json();

For verification endpoints (showing/entering user codes):

// After user enters a code
const response = await fetch(`https://auth.api.us.23blocks.com/device/verify`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
user_code: userCode,
action_type: 'approve'
})
});

if (response.status === 429) {
const data = await response.json();
// Show: "Too many invalid codes. Please wait 10 minutes."
alert(data.errors[0].detail);
return;
}

if (response.status === 404) {
// Generic message for all invalid codes (expired, wrong tenant, etc.)
alert('Invalid or expired user code');
return;
}

2. Respect the Polling Interval​

Always wait at least interval seconds between token requests. If you receive a slow_down error, increase your interval by 5 seconds.

let pollInterval = interval;

if (data.error === 'slow_down') {
pollInterval += 5;
}

3. Handle Expiration Gracefully​

Device codes expire after 10 minutes. If the user doesn't complete authorization in time, restart the flow with a fresh code.

if (data.error === 'expired_token') {
console.log('Code expired. Please try again.');
// Restart flow
return authenticateDevice();
}

4. Use verification_uri_complete for Better UX​

Generate a QR code or clickable link using verification_uri_complete to skip the manual code entry step:

import qrcode from 'qrcode-terminal';

qrcode.generate(verification_uri_complete, { small: true });
console.log(`Or visit: ${verification_uri_complete}`);

5. Store Tokens Securely​

Never log tokens or store them in plain text. Use secure storage:

// Good: Use keychain/credential manager
const keytar = require('keytar');
await keytar.setPassword('my-cli-app', 'access-token', token);

// Bad: Don't do this
fs.writeFileSync('token.txt', token); // ❌
console.log(token); // ❌

6. Add a Timeout​

Don't poll forever. Set a maximum time or number of attempts:

const MAX_ATTEMPTS = 120; // 10 minutes at 5s intervals
let attempts = 0;

while (attempts < MAX_ATTEMPTS) {
// ... polling logic
attempts++;
}

throw new Error('Authorization timed out');

See Also​

Questions?​

For questions about this feature, contact support@23blocks.com or visit our support page.