Skip to content

Commit 3489ddf

Browse files
dcramerclaude
andauthored
docs(api): add OAuth device authorization flow documentation (#15977)
Add documentation for the OAuth 2.0 Device Authorization Grant (RFC 8628) to the API authentication docs. This documents the device code flow implemented in getsentry/sentry@d4e4b74, which enables headless clients (CLI tools, CI/CD pipelines, Docker containers) to authenticate without a browser on the device. The new section covers: - When to use the device flow vs standard OAuth - Requesting device codes from `/oauth/device/code/` - Displaying user codes and verification URLs - Polling the token endpoint with proper interval handling - Error responses (`authorization_pending`, `slow_down`, `access_denied`, `expired_token`) - Complete Python example implementation --------- Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent ce5f79a commit 3489ddf

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

docs/api/auth.mdx

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,179 @@ curl -H 'Authorization: Bearer {ACCESS_TOKEN}' \
113113

114114
A Sentry user can belong to multiple organizations. The access token only provides access to the specific organization the user selected during the OAuth flow. The `/api/0/organizations/` endpoint will only return the connected organization.
115115

116+
### Device Authorization Flow
117+
118+
The device authorization grant ([RFC 8628](https://datatracker.ietf.org/doc/html/rfc8628)) enables applications on devices without a browser or with limited input capabilities to obtain authorization. This is ideal for CLI tools, CI/CD pipelines, Docker containers, and other headless environments where redirecting to a browser on the same device isn't practical.
119+
120+
**How it works:** Your application requests a device code, displays a short user code to the user, and polls for authorization. The user visits Sentry in their browser (on any device), enters the code, and approves the request. Once approved, your application receives an access token.
121+
122+
#### Step 1: Request Device Code
123+
124+
Request a device code from the device authorization endpoint:
125+
126+
```bash
127+
curl -X POST https://sentry.io/oauth/device/code/ \
128+
-d client_id={CLIENT_ID} \
129+
-d scope=org:read%20project:read
130+
```
131+
132+
**Parameters:**
133+
| Parameter | Required | Description |
134+
|-----------|----------|-------------|
135+
| `client_id` | Yes | Your registered client ID |
136+
| `scope` | No | Space-separated list of [permissions](/api/permissions/) |
137+
138+
**Response:**
139+
```json
140+
{
141+
"device_code": "a1b2c3d4e5f6...",
142+
"user_code": "ABCD-EFGH",
143+
"verification_uri": "https://sentry.io/oauth/device/",
144+
"verification_uri_complete": "https://sentry.io/oauth/device/?user_code=ABCD-EFGH",
145+
"expires_in": 600,
146+
"interval": 5
147+
}
148+
```
149+
150+
| Field | Description |
151+
|-------|-------------|
152+
| `device_code` | Secret code your application uses to poll for the token |
153+
| `user_code` | Short code the user enters to authorize (format: `XXXX-XXXX`) |
154+
| `verification_uri` | URL where the user should go to enter the code |
155+
| `verification_uri_complete` | URL with user code pre-filled (useful for QR codes or clickable links) |
156+
| `expires_in` | Seconds until the codes expire (default: 600 / 10 minutes) |
157+
| `interval` | Minimum seconds between polling requests (default: 5) |
158+
159+
#### Step 2: Display Instructions to User
160+
161+
Display the user code and verification URL to your user:
162+
163+
```
164+
To authenticate, visit: https://sentry.io/oauth/device/
165+
Enter code: ABCD-EFGH
166+
```
167+
168+
The user code uses an unambiguous character set (no 0/O, 1/I/L confusion) for easy entry.
169+
170+
#### Step 3: Poll for Token
171+
172+
While the user authorizes in their browser, poll the token endpoint:
173+
174+
```bash
175+
curl -X POST https://sentry.io/oauth/token/ \
176+
-d client_id={CLIENT_ID} \
177+
-d device_code={DEVICE_CODE} \
178+
-d grant_type=urn:ietf:params:oauth:grant-type:device_code
179+
```
180+
181+
Poll at the `interval` specified in the device authorization response (default: 5 seconds). While waiting for the user, you'll receive:
182+
183+
```json
184+
{
185+
"error": "authorization_pending",
186+
"error_description": "The authorization request is still pending."
187+
}
188+
```
189+
190+
Continue polling until you receive a token or an error.
191+
192+
#### Step 4: Receive Access Token
193+
194+
Once the user approves, the token endpoint returns:
195+
196+
```json
197+
{
198+
"access_token": "{ACCESS_TOKEN}",
199+
"refresh_token": "{REFRESH_TOKEN}",
200+
"expires_in": 2591999,
201+
"expires_at": "2024-11-27T23:20:21.054320Z",
202+
"token_type": "bearer",
203+
"scope": "org:read project:read",
204+
"user": {
205+
"id": "123",
206+
"name": "Jane Doe",
207+
"email": "[email protected]"
208+
}
209+
}
210+
```
211+
212+
#### Device Flow Error Responses
213+
214+
| Error | Description | Action |
215+
|-------|-------------|--------|
216+
| `authorization_pending` | User hasn't completed authorization yet | Continue polling |
217+
| `slow_down` | Polling too frequently | Increase interval by 5 seconds |
218+
| `access_denied` | User denied the authorization request | Stop polling, notify user |
219+
| `expired_token` | Device code has expired | Restart the flow from step 1 |
220+
221+
#### Device Flow Example
222+
223+
```python
224+
import time
225+
import requests
226+
227+
CLIENT_ID = 'your-client-id'
228+
DEVICE_AUTH_URL = 'https://sentry.io/oauth/device/code/'
229+
TOKEN_URL = 'https://sentry.io/oauth/token/'
230+
231+
def authenticate():
232+
# Step 1: Request device code
233+
response = requests.post(DEVICE_AUTH_URL, data={
234+
'client_id': CLIENT_ID,
235+
'scope': 'org:read project:read'
236+
})
237+
data = response.json()
238+
239+
device_code = data['device_code']
240+
user_code = data['user_code']
241+
verification_uri = data['verification_uri']
242+
verification_uri_complete = data.get('verification_uri_complete')
243+
interval = data.get('interval', 5)
244+
expires_in = data['expires_in']
245+
246+
# Step 2: Display instructions
247+
print(f"\nTo authenticate, visit: {verification_uri}")
248+
print(f"Enter code: {user_code}")
249+
if verification_uri_complete:
250+
print(f"\nOr open this link directly: {verification_uri_complete}")
251+
print()
252+
253+
# Step 3: Poll for token
254+
deadline = time.time() + expires_in
255+
while time.time() < deadline:
256+
time.sleep(interval)
257+
258+
response = requests.post(TOKEN_URL, data={
259+
'client_id': CLIENT_ID,
260+
'device_code': device_code,
261+
'grant_type': 'urn:ietf:params:oauth:grant-type:device_code'
262+
})
263+
result = response.json()
264+
265+
if 'access_token' in result:
266+
# Step 4: Success
267+
print("Authentication successful!")
268+
return result['access_token'], result['refresh_token']
269+
270+
error = result.get('error')
271+
if error == 'authorization_pending':
272+
continue
273+
elif error == 'slow_down':
274+
interval += 5
275+
elif error == 'access_denied':
276+
raise Exception("User denied authorization")
277+
elif error == 'expired_token':
278+
raise Exception("Device code expired")
279+
else:
280+
raise Exception(f"Unexpected error: {error}")
281+
282+
raise Exception("Authorization timed out")
283+
284+
if __name__ == '__main__':
285+
access_token, refresh_token = authenticate()
286+
print(f"Access token: {access_token[:20]}...")
287+
```
288+
116289
### PKCE (Proof Key for Code Exchange)
117290

118291
PKCE protects against authorization code interception attacks and is strongly recommended for all OAuth clients.

0 commit comments

Comments
 (0)