Skip to content

Commit

Permalink
chore: normalize AuthError message
Browse files Browse the repository at this point in the history
  • Loading branch information
k-taro56 committed May 19, 2024
1 parent 7770f2b commit 668796b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
21 changes: 10 additions & 11 deletions packages/core/src/lib/actions/callback/oauth/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check warning on line 77 in packages/core/src/lib/actions/callback/oauth/checks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/actions/callback/oauth/checks.ts#L77

Added line #L77 was not covered by tests

const value = await decode<CheckPayload>({
...options.jwt,
Expand All @@ -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")

Check warning on line 86 in packages/core/src/lib/actions/callback/oauth/checks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/actions/callback/oauth/checks.ts#L86

Added line #L86 was not covered by tests

// Clear the pkce code verifier cookie after use
resCookies.push({
Expand Down Expand Up @@ -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"

Check warning on line 120 in packages/core/src/lib/actions/callback/oauth/checks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/actions/callback/oauth/checks.ts#L120

Added line #L120 was not covered by tests
)
}
return
Expand Down Expand Up @@ -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<CheckPayload>({
Expand All @@ -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")

Check warning on line 173 in packages/core/src/lib/actions/callback/oauth/checks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/actions/callback/oauth/checks.ts#L173

Added line #L173 was not covered by tests

if (decodedState.random !== paramRandom)
throw new InvalidCheck(
Expand Down Expand Up @@ -214,16 +214,15 @@ 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")

Check warning on line 217 in packages/core/src/lib/actions/callback/oauth/checks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/actions/callback/oauth/checks.ts#L217

Added line #L217 was not covered by tests

const value = await decode<CheckPayload>({
...options.jwt,
token: nonce,
salt: options.cookies.nonce.name,
})

if (!value?.value)
throw new InvalidCheck("Nonce value could not be parsed.")
if (!value?.value) throw new InvalidCheck("Nonce value could not be parsed")

Check warning on line 225 in packages/core/src/lib/actions/callback/oauth/checks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/actions/callback/oauth/checks.ts#L225

Added line #L225 was not covered by tests

// Clear the nonce cookie after use
resCookies.push({
Expand Down Expand Up @@ -294,7 +293,7 @@ export const webauthnChallenge = {
): Promise<WebAuthnChallengeCookie> {
const challenge = cookies?.[options.cookies.webauthnChallenge.name]

if (!challenge) throw new InvalidCheck("Challenge cookie missing.")
if (!challenge) throw new InvalidCheck("Challenge cookie missing")

Check warning on line 296 in packages/core/src/lib/actions/callback/oauth/checks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/actions/callback/oauth/checks.ts#L296

Added line #L296 was not covered by tests

const value = await decode<CheckPayload>({
...options.jwt,
Expand All @@ -303,7 +302,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")

Check warning on line 305 in packages/core/src/lib/actions/callback/oauth/checks.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/actions/callback/oauth/checks.ts#L305

Added line #L305 was not covered by tests

// Clear the pkce code verifier cookie after use
const cookie = {
Expand Down
17 changes: 8 additions & 9 deletions packages/core/src/lib/utils/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
)
}
}
Expand All @@ -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`

Check warning on line 168 in packages/core/src/lib/utils/assert.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/utils/assert.ts#L168

Added line #L168 was not covered by tests
)
}
hasConditionalUIProvider = true
Expand All @@ -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`

Check warning on line 180 in packages/core/src/lib/utils/assert.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/utils/assert.ts#L180

Added line #L180 was not covered by tests
)
}
}
Expand Down Expand Up @@ -216,12 +216,11 @@ export function assertConfig(
(!session?.strategy && adapter)
) {
if (hasEmail) {
if (!adapter)
return new MissingAdapter("Email login requires an adapter.")
if (!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)
}
}
Expand All @@ -232,11 +231,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"

Check warning on line 234 in packages/core/src/lib/utils/assert.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/utils/assert.ts#L234

Added line #L234 was not covered by tests
)
}

if (!adapter) return new MissingAdapter("WebAuthn requires an adapter.")
if (!adapter) return new MissingAdapter("WebAuthn requires an adapter")

Check warning on line 238 in packages/core/src/lib/utils/assert.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/utils/assert.ts#L238

Added line #L238 was not covered by tests
requiredMethods.push(...webauthnMethods)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/utils/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function toInternalRequest(
): Promise<RequestInternal | undefined> {
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"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/utils/webauthn-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/providers/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Check warning on line 244 in packages/core/src/providers/webauthn.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/providers/webauthn.ts#L244

Added line #L244 was not covered by tests
)

// Get email address from the query.
Expand Down
12 changes: 6 additions & 6 deletions packages/core/test/assert-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`")
)
})

Expand Down Expand Up @@ -92,11 +92,11 @@ describe("Assert user config correctness", () => {
it.each<[Provider, string]>([
[
() => ({ type: "oidc", id: "provider-id", name: "" }),
'Provider "provider-id" is missing both `issuer` and `authorization` endpoint config. At least one of them is required.',
'Provider "provider-id" is missing both `issuer` and `authorization` endpoint config. At least one of them is required',
],
[
{ type: "oidc", id: "provider-id", name: "" },
'Provider "provider-id" is missing both `issuer` and `authorization` endpoint config. At least one of them is required.',
'Provider "provider-id" is missing both `issuer` and `authorization` endpoint config. At least one of them is required',
],
[
{
Expand All @@ -105,7 +105,7 @@ describe("Assert user config correctness", () => {
id: "provider-id",
name: "",
},
'Provider "provider-id" is missing both `issuer` and `token` endpoint config. At least one of them is required.',
'Provider "provider-id" is missing both `issuer` and `token` endpoint config. At least one of them is required',
],
[
{
Expand All @@ -115,7 +115,7 @@ describe("Assert user config correctness", () => {
id: "provider-id",
name: "",
},
'Provider "provider-id" is missing both `issuer` and `userinfo` endpoint config. At least one of them is required.',
'Provider "provider-id" is missing both `issuer` and `userinfo` endpoint config. At least one of them is required',
],
[
{
Expand All @@ -127,7 +127,7 @@ describe("Assert user config correctness", () => {
id: "oauth-provider",
name: "",
},
'Provider "oauth-provider" is missing both `issuer` and `userinfo` endpoint config. At least one of them is required.',
'Provider "oauth-provider" is missing both `issuer` and `userinfo` endpoint config. At least one of them is required',
],
])("OAuth/OIDC: invalid endpoints %j", async (provider, error) => {
const { response, logger } = await makeAuthRequest({
Expand Down

0 comments on commit 668796b

Please sign in to comment.