Return 401 for expired OAuth access tokens#768
Merged
rickyrombo merged 2 commits intomainfrom Apr 17, 2026
Merged
Conversation
When an OAuth access token expired, requests asserting a caller identity (via ?user_id= or :wallet) returned 403 because the bearer token failed to resolve to a wallet and the authorization check ran with an empty wallet. 403 implies the caller is authenticated but unauthorized, which prevents clients from realizing they need to refresh their token. Return 401 when a bearer token was supplied but no auth path could resolve it, so clients can refresh and retry. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
A PKCE token belonging to user A used in a request for user B should still return 403 (the credential is valid, just unauthorized for the target). Track whether any auth path successfully decoded the bearer token; only return 401 when no path could validate it (expired, revoked, or undecodable). Fixes TestAuthMiddleware_PKCEToken_UserIDMismatch which expects 403 for valid-but-mismatched tokens. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
POST /v1/tracks?user_id=…) because the bearer token failed to resolve to a wallet and the downstream authorization check ran with an empty wallet.?user_id=or:wallet).Test plan
go test ./api/ -run "TestAuthMiddlewareInvalidBearerReturns401|TestAuthorized|TestRequireAuthMiddleware|TestRequireWriteScope|TestGetApiSigner"passesTestAuthMiddlewareInvalidBearerReturns401covers the three cases: invalid bearer +myId→ 401, invalid bearer +:wallet→ 401, invalid bearer with no identity assertion → pass-throughPOST /v1/tracksreturns 401🤖 Generated with Claude Code