You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/auth.mdx
+188Lines changed: 188 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,194 @@ Some API endpoints require an authentication token that's associated with your u
27
27
28
28
The endpoints that require a user authentication token are specific to your user, such as [Retrieve an Organization](/api/organizations/retrieve-an-organization/).
29
29
30
+
## OAuth2
31
+
32
+
For third-party applications that need to access Sentry on behalf of users, Sentry supports OAuth2 with the authorization code grant type. This allows users to authorize your application without sharing their credentials.
Exchange the authorization code for an access token:
62
+
63
+
```bash
64
+
curl -X POST https://sentry.io/oauth/token/ \
65
+
-d client_id={CLIENT_ID} \
66
+
-d client_secret={CLIENT_SECRET} \
67
+
-d grant_type=authorization_code \
68
+
-d code={AUTHORIZATION_CODE} \
69
+
-d code_verifier={CODE_VERIFIER}
70
+
```
71
+
72
+
The `code_verifier` parameter is required if you used PKCE in the authorization request.
73
+
74
+
**Response:**
75
+
```json
76
+
{
77
+
"access_token": "{ACCESS_TOKEN}",
78
+
"refresh_token": "{REFRESH_TOKEN}",
79
+
"expires_in": 2591999,
80
+
"expires_at": "2024-11-27T23:20:21.054320Z",
81
+
"token_type": "bearer",
82
+
"scope": "org:read project:read",
83
+
"user": {
84
+
"id": "123",
85
+
"name": "Jane Doe",
86
+
"email": "jane@example.com"
87
+
}
88
+
}
89
+
```
90
+
91
+
### Refreshing Tokens
92
+
93
+
Access tokens expire after 30 days. Use the refresh token to obtain new tokens:
94
+
95
+
```bash
96
+
curl -X POST https://sentry.io/oauth/token/ \
97
+
-d client_id={CLIENT_ID} \
98
+
-d client_secret={CLIENT_SECRET} \
99
+
-d grant_type=refresh_token \
100
+
-d refresh_token={REFRESH_TOKEN}
101
+
```
102
+
103
+
### Using Access Tokens
104
+
105
+
Include the access token in API requests using the Authorization header:
106
+
107
+
```bash
108
+
curl -H 'Authorization: Bearer {ACCESS_TOKEN}' \
109
+
https://sentry.io/api/0/organizations/
110
+
```
111
+
112
+
### Organization Scoping
113
+
114
+
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.
115
+
116
+
### PKCE (Proof Key for Code Exchange)
117
+
118
+
PKCE protects against authorization code interception attacks and is strongly recommended for all OAuth clients.
119
+
120
+
**How it works:** For each authorization request, generate a unique random secret called the `code_verifier`. Create a `code_challenge` by hashing this verifier. The challenge is sent with the authorization request, while the original verifier is sent when exchanging the code for a token. Sentry verifies they match, ensuring the same client that started the flow is completing it.
121
+
122
+
**Generating PKCE values (generate fresh for each authorization request):**
123
+
124
+
```python
125
+
import base64
126
+
import hashlib
127
+
import secrets
128
+
129
+
# Generate a random code_verifier (43-128 URL-safe characters)
130
+
code_verifier = secrets.token_urlsafe(64)
131
+
132
+
# Create code_challenge by hashing the verifier with SHA256
133
+
code_challenge = base64.urlsafe_b64encode(
134
+
hashlib.sha256(code_verifier.encode()).digest()
135
+
).rstrip(b'=').decode()
136
+
137
+
# Store code_verifier securely - you'll need it for the token exchange
138
+
```
139
+
140
+
### Error Handling
141
+
142
+
| Status Code | Meaning | Action |
143
+
|-------------|---------|--------|
144
+
| 401 | Token expired or revoked | Refresh the token, or prompt user to reconnect |
Some API endpoints may allow DSN-based authentication. This is generally very limited and an endpoint will describe if its supported. This works similar to Bearer token authentication, but uses your DSN (Client Key).
0 commit comments