Repository navigation
fix(server-sdk): throw on failed auth requests instead of returning the error body #2278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
albertoelias-crossmint
wants to merge
3
commits into
main
Choose a base branch
from
albertoe/server-auth-getuser-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9f4f319
Throw on failed server auth requests instead of returning the error body
AlbertoElias 26b19f8
test(common-auth): cover auth error message formats; encode user id i…
AlbertoElias 4bdbca8
Kebab-case the auth error test file and document the 5xx error on get…
AlbertoElias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@crossmint/server-sdk": patch | ||
| "@crossmint/common-sdk-auth": patch | ||
| --- | ||
|
|
||
| `CrossmintAuth.getUser` now throws a `CrossmintAuthenticationError` when Crossmint rejects the request, instead of returning the API's error body as if it were the user. The error carries the HTTP `status` and the API's message, and on a 401 or 403 it says that `getUser` needs a server API key with the `users.read` scope. `getUser` now returns `SDKExternalUser` instead of `any`, and the server SDK exports `CrossmintAuthenticationError` and `SDKExternalUser`. Failed session refreshes and logouts also report the status and the API's message. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| export class CrossmintAuthenticationError extends Error {} | ||
| export class CrossmintAuthenticationError extends Error { | ||
| /** HTTP status of the Crossmint API response that caused the error, when there was one. */ | ||
| public readonly status?: number; | ||
|
|
||
| constructor(message?: string, options?: { status?: number }) { | ||
| super(message); | ||
| this.status = options?.status; | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
packages/common/auth/src/utils/auth-error-from-response.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { authErrorFromResponse } from "./authErrorFromResponse"; | ||
|
|
||
| describe("authErrorFromResponse", () => { | ||
| const cases: { name: string; response: Response; hint?: string; message: string }[] = [ | ||
| { | ||
| name: "uses the API's message", | ||
| response: new Response(JSON.stringify({ message: "User not found" }), { status: 404 }), | ||
| message: "Failed to get user (404: User not found)", | ||
| }, | ||
| { | ||
| name: "joins a list of validation messages", | ||
| response: new Response(JSON.stringify({ message: ["refresh must be a string", "refresh is required"] }), { | ||
| status: 400, | ||
| }), | ||
| message: "Failed to get user (400: refresh must be a string, refresh is required)", | ||
| }, | ||
| { | ||
| name: "falls back to the error field", | ||
| response: new Response(JSON.stringify({ error: "Bad Gateway" }), { status: 502 }), | ||
| message: "Failed to get user (502: Bad Gateway)", | ||
| }, | ||
| { | ||
| name: "falls back to the status text when the body is not JSON", | ||
| response: new Response("<html>nope</html>", { status: 503, statusText: "Service Unavailable" }), | ||
| message: "Failed to get user (503: Service Unavailable)", | ||
| }, | ||
| { | ||
| name: "reports only the status when there is no body and no status text", | ||
| response: new Response(null, { status: 500 }), | ||
| message: "Failed to get user (500)", | ||
| }, | ||
| { | ||
| name: "appends the hint", | ||
| response: new Response(JSON.stringify({ message: "Forbidden" }), { status: 403 }), | ||
| hint: "needs users.read", | ||
| message: "Failed to get user (403: Forbidden). needs users.read", | ||
| }, | ||
| ]; | ||
|
|
||
| it.each(cases)("$name", async ({ response, hint, message }) => { | ||
| const error = await authErrorFromResponse(response, "Failed to get user", hint); | ||
|
|
||
| expect(error.message).toBe(message); | ||
| expect(error.status).toBe(response.status); | ||
| }); | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { CrossmintAuthenticationError } from "../types/errors"; | ||
|
|
||
| /** | ||
| * Build a `CrossmintAuthenticationError` from a failed Crossmint API response. The error keeps the HTTP status | ||
| * and the API's own message, so the caller sees why the request failed. `hint` is appended to the message. | ||
| */ | ||
| export async function authErrorFromResponse( | ||
| response: Response, | ||
| action: string, | ||
| hint?: string | ||
| ): Promise<CrossmintAuthenticationError> { | ||
| const details = (await readApiErrorMessage(response)) ?? response.statusText; | ||
| const message = `${action} (${response.status}${details ? `: ${details}` : ""})${hint ? `. ${hint}` : ""}`; | ||
| return new CrossmintAuthenticationError(message, { status: response.status }); | ||
| } | ||
|
|
||
| async function readApiErrorMessage(response: Response): Promise<string | undefined> { | ||
| try { | ||
| const body = JSON.parse(await response.text()); | ||
| const message = body?.message ?? body?.error; | ||
| if (typeof message === "string") { | ||
| return message; | ||
| } | ||
| if (Array.isArray(message)) { | ||
| return message.join(", "); | ||
| } | ||
| } catch (_) { | ||
| // The body is missing or not JSON: fall back to the status text. | ||
| } | ||
| return undefined; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from "./constants"; | ||
| export * from "./isEmailValid"; | ||
| export * from "./authErrorFromResponse"; |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { CrossmintAuthServer as CrossmintAuth, type CrossmintAuthServerOptions } from "./CrossmintAuthServer"; | ||
| export type { GenericRequest, GenericResponse } from "./types/request"; | ||
| export * from "./utils"; | ||
| export { CrossmintAuthenticationError, type SDKExternalUser } from "@crossmint/common-sdk-auth"; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.