Repository navigation
fix(api): Allow Bearer token auth for GET /api/0/auth/ - #112853
Merged
Merged
Conversation
BYK
marked this pull request as ready for review
April 13, 2026 20:55
BYK
force-pushed
the
BYK/fix/auth-index-bearer-token
branch
from
April 13, 2026 20:55
fb6c27e to
08792d1
Compare
BaseAuthIndexEndpoint restricts authentication_classes to only QuietBasicAuthentication and SessionAuthentication, which excludes UserAuthTokenAuthentication. This causes GET /api/0/auth/ to return 400 for valid Bearer tokens since DRF treats the request as anonymous. Override initialize_request on AuthIndexEndpoint to conditionally add UserAuthTokenAuthentication for GET requests only. POST/PUT/DELETE retain the original session-only auth since they are session management operations where Bearer token auth is inappropriate. Fixes GH-112778 Co-Authored-By: Claude Sonnet 4 <[email protected]>
BYK
force-pushed
the
BYK/fix/auth-index-bearer-token
branch
from
April 13, 2026 21:10
08792d1 to
7291acc
Compare
armenzg
approved these changes
Apr 20, 2026
3 tasks
BYK
added a commit
to getsentry/cli
that referenced
this pull request
Apr 25, 2026
The server-side fix (getsentry/sentry#112853) is rolled out — OAuth and user PAT 400s on /auth/ are no longer happening. The only persistent 400 case is org auth tokens, which we already short-circuit before the API call. The 400 → AuthError translation was both unhelpful (the 'rolling out' claim is no longer accurate) and unreachable in normal operation. Anomalous future 400s remain diagnosable via the response-header telemetry context added to apiRequestToRegion in this PR.
BYK
added a commit
to getsentry/cli
that referenced
this pull request
Apr 25, 2026
## Summary Fix CLI-1AZ: `sentry auth whoami` throws a confusing empty-detail `ApiError: API request failed: 400 Bad Request` when the active token is an organization auth token (`sntrys_...`). The Sentry backend's `/auth/` endpoint (`AuthIndexEndpoint`) doesn't accept org auth tokens — `UserAuthTokenAuthentication.accepts_auth` explicitly rejects the `sntrys_` prefix, and `OrgAuthTokenAuthentication` was never added to this endpoint (getsentry/sentry#112853 added user-token auth only). Even if it did, `whoami` is semantically meaningless for org tokens — they aren't tied to a single user. The CLI should detect this client-side and tell the user clearly, not let them hit a 400. ## What this PR does 1. **Org-token short-circuit** (`src/commands/auth/whoami.ts`): before any API call, inspect the effective token via `getAuthToken()`. If it's `sntrys_...`, throw a `ResolutionError` pointing to `sentry auth status` and `sentry org list`. No request, no confusing 400, no auto-login loop. 2. **Response-header telemetry** (`src/lib/api/infrastructure.ts`): on every 4xx/5xx in `apiRequestToRegion`, attach an allow-listed subset of response headers (`content-type`, `content-length`, `server`, `cf-ray`, `x-sentry-error`, `www-authenticate`) to the Sentry scope as `api_response_headers` context. Lets future empty-`detail` 4xx events be triaged without user-side repro — Sentry-app 4xx (JSON body) vs CDN / WAF / proxy 4xx (HTML or empty body) is visible at a glance. Useful diagnostic for any future auth-class anomaly. 3. **New `classifySentryToken()` helper** (`src/lib/token-type.ts`): literal-prefix classification returning `"org-auth-token"` / `"user-auth-token"` / `"oauth-or-legacy"`. Tiny, reusable, property-tested. ## Background Most events on CLI-1AZ pre-date the server-side rollout of getsentry/sentry#112853 (merged Apr 20, shipped 26.4.1). That PR added `UserAuthTokenAuthentication` to the `/auth/` endpoint, fixing 400s for OAuth and user PATs (`sntryu_...`). Now that the rollout is complete, the only persistent 400 cause on `/auth/` is org auth tokens — addressed by the short-circuit above. ## Verification - `bun x tsc --noEmit` — clean. - `bun run script/check-error-patterns.ts` — no anti-patterns. - `bun test test/commands/auth/whoami.test.ts` — 13/13 pass (org-token short-circuit + error message + `sntryu_` passes through, plus existing happy-path coverage). - `bun test test/lib/token-type.property.test.ts` — 10/10 pass (fast-check properties + case sensitivity + boundary cases). - `bun test test/lib/api/infrastructure.test.ts` — 5/5 pass (telemetry addition doesn't regress). ### Manual repro ``` # Org-token short-circuit (no API call, clear message) SENTRY_AUTH_TOKEN='sntrys_fake' bun run src/bin.ts auth whoami # Normal happy path (unchanged) bun run src/bin.ts auth whoami ``` ## Refs - Sentry issue: [CLI-1AZ](https://sentry.sentry.io/issues/7425666334/) - Server-side context: getsentry/sentry#112853 (`6c28450`, merged Apr 20 2026, shipped 26.4.1)
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GET /api/0/auth/returns 400 for validAuthorization: Bearer <token>requestsbecause
BaseAuthIndexEndpointoverridesauthentication_classesto only(QuietBasicAuthentication, SessionAuthentication), excludingUserAuthTokenAuthentication.This adds an
initialize_requestoverride onAuthIndexEndpointthat conditionallyprepends
UserAuthTokenAuthenticationfor GET requests only. POST (login), PUT (sudo),and DELETE (logout) retain session-only auth since they are session-management operations
where Bearer token auth is inappropriate.
Fixes #112778