Repository navigation
fix(server-sdk): throw on failed auth requests instead of returning the error body - #2278
Open
albertoelias-crossmint wants to merge 3 commits into
Open
albertoelias-crossmint wants to merge 3 commits into
albertoelias-crossmint wants to merge 3 commits into
Conversation
CrossmintAuth.getUser parsed the response without checking its status, so a 403 for a server key without users.read came back as the "user". It now throws CrossmintAuthenticationError with the HTTP status and the API's message, names the users.read scope on 401/403, and returns SDKExternalUser instead of any. Session refresh and logout also keep the status and API message on failure, and the server SDK exports CrossmintAuthenticationError and SDKExternalUser. The README documents which scopes each method needs.
🦋 Changeset detectedLatest commit: 4bdbca8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 14 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
7 of 8 tasks
Contributor
[Medium impact] The PR appears safe to merge; no new actionable issues remain. Reviews (2) · Last reviewed commit: "Kebab-case the auth error test file and ..." · Reviewed by Greptile |
Collaborator
Author
|
@greptileai review |
This branch has not been 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 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.
fix(server-sdk): throw with the HTTP status when a server auth call fails
Problem
A developer calls
crossmintAuth.getUser(userId)from the server SDK with a server API key that does not have theusers.readscope. Crossmint answers403withThe API key provided doesn't have the required scopes to perform this action: users.read.The SDK does not look at the status: it returns that error body as if it were the user. The developer gets{ statusCode: 403, message: "...", error: "Forbidden" }whereuser.emailshould be, and finds out only when the code readinguser.emailmisbehaves.Failed session refreshes and logouts have the same shape: a refresh that Crossmint rejects with
Invalid refresh tokensurfaces only as the HTTP status text (Unauthorized), and a failed logout is ignored without a trace. Nothing says which scope an API key needs.What this PR does
getUserthrows aCrossmintAuthenticationErrorwhen Crossmint rejects the request. The error hasstatusand the API's own message; on 401 or 403 it adds thatgetUserneeds a server API key withusers.read.getUserreturnsSDKExternalUserinstead ofany, and encodes the user ID in the request path.CrossmintAuthenticationErrorandSDKExternalUser.getUserexample that handles the error.flowchart LR subgraph Before A1[getUser, key without users.read] --> B1[API 403] --> C1[error body returned as the user] end subgraph After A2[getUser, key without users.read] --> B2[API 403] --> C2[throws CrossmintAuthenticationError, status 403, names users.read] endResult
getUserwith a key lackingusers.read{ statusCode: 403, message, error }Failed to get user (403: ...users.read...). getUser needs a server API key with the users.read scopegetUserfor an unknown userFailed to get user (404: User with id ... not found.),error.status === 404UnauthorizedFailed to refresh the session (401: Invalid refresh token)getUseranySDKExternalUserUnchanged: successful
getUser,getSessionandlogoutbehaviour (logout still clears the cookies and does not throw; a failed revoke call is logged with its status).What to check when reviewing
packages/common/auth/src/utils/authErrorFromResponse.ts: message format<action> (<status>: <API message>), API message frommessageorerror, status text as fallback.getUsercallers that relied on getting a body back for non-2xx responses now see a throw. That is the intended fix, but it is a behaviour change for anyone who checkeduser.error.getUsernow returnsSDKExternalUserinstead ofany. TypeScript callers that read fields outside that type will get a compile error.getSessionstill reportsFailed to get sessionto its caller (the cause is logged); onlygetUserandhandleCustomRefreshexpose the status in the response.CrossmintAuthServer.ts, its test andpackages/server/README.md; whichever merges second takes the other's change (different methods and README sections).Test plan
pnpm --filter @crossmint/server-sdk test:vitest: 70 passed.@crossmint/common-sdk-auth: 8 passed.getUserfails the 403, 404 and path-encoding tests; reverting the refresh error handling fails the rejected-refresh test; the message-format table fails without the helper.getUseragainst staging with a key withoutusers.read.Checklist
Co-Authored-BylinesRollback
Revert the PR. No data or API changes; callers go back to receiving the error body.
Closes WAL-13394
🤖 Generated with Claude Code