From db9dc91a6a882a3b3f406d5473f2f30e65156072 Mon Sep 17 00:00:00 2001 From: k-taro56 <121674121+k-taro56@users.noreply.github.com> Date: Mon, 20 May 2024 01:15:12 +0900 Subject: [PATCH] normalize `AuthError` message --- .../src/lib/actions/callback/oauth/checks.ts | 20 +++++++++---------- packages/core/src/lib/utils/assert.ts | 16 +++++++-------- packages/core/src/lib/utils/web.ts | 2 +- packages/core/src/lib/utils/webauthn-utils.ts | 2 +- packages/core/src/providers/webauthn.ts | 2 +- packages/core/test/assert-config.test.ts | 2 +- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/core/src/lib/actions/callback/oauth/checks.ts b/packages/core/src/lib/actions/callback/oauth/checks.ts index 9269ab1224..6a301f4710 100644 --- a/packages/core/src/lib/actions/callback/oauth/checks.ts +++ b/packages/core/src/lib/actions/callback/oauth/checks.ts @@ -74,7 +74,7 @@ export const pkce = { const codeVerifier = cookies?.[options.cookies.pkceCodeVerifier.name] if (!codeVerifier) - throw new InvalidCheck("PKCE code_verifier cookie was missing.") + throw new InvalidCheck("PKCE code_verifier cookie was missing") const value = await decode({ ...options.jwt, @@ -83,7 +83,7 @@ export const pkce = { }) if (!value?.value) - throw new InvalidCheck("PKCE code_verifier value could not be parsed.") + throw new InvalidCheck("PKCE code_verifier value could not be parsed") // Clear the pkce code verifier cookie after use resCookies.push({ @@ -117,7 +117,7 @@ export const state = { if (!provider.checks.includes("state")) { if (data) { throw new InvalidCheck( - "State data was provided but the provider is not configured to use state." + "State data was provided but the provider is not configured to use state" ) } return @@ -155,7 +155,7 @@ export const state = { const state = cookies?.[options.cookies.state.name] - if (!state) throw new InvalidCheck("State cookie was missing.") + if (!state) throw new InvalidCheck("State cookie was missing") // IDEA: Let the user do something with the returned state const encodedState = await decode({ @@ -165,12 +165,12 @@ export const state = { }) if (!encodedState?.value) - throw new InvalidCheck("State (cookie) value could not be parsed.") + throw new InvalidCheck("State (cookie) value could not be parsed") const decodedState = decodeState(encodedState.value) if (!decodedState) - throw new InvalidCheck("State (encoded) value could not be parsed.") + throw new InvalidCheck("State (encoded) value could not be parsed") if (decodedState.random !== paramRandom) throw new InvalidCheck( @@ -214,7 +214,7 @@ export const nonce = { if (!provider?.checks?.includes("nonce")) return const nonce = cookies?.[options.cookies.nonce.name] - if (!nonce) throw new InvalidCheck("Nonce cookie was missing.") + if (!nonce) throw new InvalidCheck("Nonce cookie was missing") const value = await decode({ ...options.jwt, @@ -223,7 +223,7 @@ export const nonce = { }) if (!value?.value) - throw new InvalidCheck("Nonce value could not be parsed.") + throw new InvalidCheck("Nonce value could not be parsed") // Clear the nonce cookie after use resCookies.push({ @@ -294,7 +294,7 @@ export const webauthnChallenge = { ): Promise { const challenge = cookies?.[options.cookies.webauthnChallenge.name] - if (!challenge) throw new InvalidCheck("Challenge cookie missing.") + if (!challenge) throw new InvalidCheck("Challenge cookie missing") const value = await decode({ ...options.jwt, @@ -303,7 +303,7 @@ export const webauthnChallenge = { }) if (!value?.value) - throw new InvalidCheck("Challenge value could not be parsed.") + throw new InvalidCheck("Challenge value could not be parsed") // Clear the pkce code verifier cookie after use const cookie = { diff --git a/packages/core/src/lib/utils/assert.ts b/packages/core/src/lib/utils/assert.ts index d4ae83be35..bec5322c4a 100644 --- a/packages/core/src/lib/utils/assert.ts +++ b/packages/core/src/lib/utils/assert.ts @@ -98,7 +98,7 @@ export function assertConfig( } if (!options.secret?.length) { - return new MissingSecret("Please define a `secret`.") + return new MissingSecret("Please define a `secret`") } const callbackUrlParam = request.query?.callbackUrl as string | undefined @@ -141,7 +141,7 @@ export function assertConfig( if (key) { return new InvalidEndpoints( - `Provider "${provider.id}" is missing both \`issuer\` and \`${key}\` endpoint config. At least one of them is required.` + `Provider "${provider.id}" is missing both \`issuer\` and \`${key}\` endpoint config. At least one of them is required` ) } } @@ -165,7 +165,7 @@ export function assertConfig( // Make sure only one webauthn provider has "enableConditionalUI" set to true if (hasConditionalUIProvider) { return new DuplicateConditionalUI( - `Multiple webauthn providers have 'enableConditionalUI' set to True. Only one provider can have this option enabled at a time.` + `Multiple webauthn providers have 'enableConditionalUI' set to True. Only one provider can have this option enabled at a time` ) } hasConditionalUIProvider = true @@ -177,7 +177,7 @@ export function assertConfig( ) if (!hasWebauthnFormField) { return new MissingWebAuthnAutocomplete( - `Provider "${provider.id}" has 'enableConditionalUI' set to True, but none of its formFields have 'webauthn' in their autocomplete param.` + `Provider "${provider.id}" has 'enableConditionalUI' set to True, but none of its formFields have 'webauthn' in their autocomplete param` ) } } @@ -217,11 +217,11 @@ export function assertConfig( ) { if (hasEmail) { if (!adapter) - return new MissingAdapter("Email login requires an adapter.") + return new MissingAdapter("Email login requires an adapter") requiredMethods.push(...emailMethods) } else { if (!adapter) - return new MissingAdapter("Database session requires an adapter.") + return new MissingAdapter("Database session requires an adapter") requiredMethods.push(...sessionMethods) } } @@ -232,11 +232,11 @@ export function assertConfig( warnings.push("experimental-webauthn") } else { return new ExperimentalFeatureNotEnabled( - "WebAuthn is an experimental feature. To enable it, set `experimental.enableWebAuthn` to `true` in your config." + "WebAuthn is an experimental feature. To enable it, set `experimental.enableWebAuthn` to `true` in your config" ) } - if (!adapter) return new MissingAdapter("WebAuthn requires an adapter.") + if (!adapter) return new MissingAdapter("WebAuthn requires an adapter") requiredMethods.push(...webauthnMethods) } diff --git a/packages/core/src/lib/utils/web.ts b/packages/core/src/lib/utils/web.ts index 42fd2b7168..41e341d7de 100644 --- a/packages/core/src/lib/utils/web.ts +++ b/packages/core/src/lib/utils/web.ts @@ -28,7 +28,7 @@ export async function toInternalRequest( ): Promise { try { if (req.method !== "GET" && req.method !== "POST") - throw new UnknownAction("Only GET and POST requests are supported.") + throw new UnknownAction("Only GET and POST requests are supported") // Defaults are usually set in the `init` function, but this is needed below config.basePath ??= "/auth" diff --git a/packages/core/src/lib/utils/webauthn-utils.ts b/packages/core/src/lib/utils/webauthn-utils.ts index eaa3b0febb..6a1a601d03 100644 --- a/packages/core/src/lib/utils/webauthn-utils.ts +++ b/packages/core/src/lib/utils/webauthn-utils.ts @@ -262,7 +262,7 @@ export async function verifyAuthenticate( // Make sure the response was verified if (!verified) { throw new WebAuthnVerificationError( - "WebAuthn authentication response could not be verified." + "WebAuthn authentication response could not be verified" ) } diff --git a/packages/core/src/providers/webauthn.ts b/packages/core/src/providers/webauthn.ts index f0e66f6b4a..2cbc56bd38 100644 --- a/packages/core/src/providers/webauthn.ts +++ b/packages/core/src/providers/webauthn.ts @@ -241,7 +241,7 @@ const getUserInfo: GetUserInfo = async (options, request) => { const { adapter } = options if (!adapter) throw new MissingAdapter( - "WebAuthn provider requires a database adapter to be configured." + "WebAuthn provider requires a database adapter to be configured" ) // Get email address from the query. diff --git a/packages/core/test/assert-config.test.ts b/packages/core/test/assert-config.test.ts index b1f338f6ce..ee2ce079ed 100644 --- a/packages/core/test/assert-config.test.ts +++ b/packages/core/test/assert-config.test.ts @@ -29,7 +29,7 @@ describe("Assert user config correctness", () => { "There was a problem with the server configuration. Check the server logs for more information.", }) expect(logger?.error).toHaveBeenCalledWith( - new MissingSecret("Please define a `secret`.") + new MissingSecret("Please define a `secret`") ) })