Skip to content

Commit

Permalink
refactor: handle missing configuration error
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Aug 28, 2024
1 parent 4ad11a4 commit 5675aaf
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 160 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"ofetch": "^1.3.4",
"ohash": "^1.1.3",
"pathe": "^1.1.2",
"scule": "^1.3.0",
"uncrypto": "^0.1.3"
},
"devDependencies": {
Expand Down
188 changes: 135 additions & 53 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -72,12 +73,7 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }: OAuthConf
const { code } = getQuery(event)

if (!config.clientId || !config.clientSecret || !config.domain) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_AUTH0_CLIENT_ID or NUXT_OAUTH_AUTH0_CLIENT_SECRET or NUXT_OAUTH_AUTH0_DOMAIN env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'auth0', ['clientId', 'clientSecret', 'domain'], onError)
}
const authorizationURL = `https://${config.domain}/authorize`
const tokenURL = `https://${config.domain}/oauth/token`
Expand Down
9 changes: 3 additions & 6 deletions src/runtime/server/lib/oauth/battledotnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -75,12 +76,8 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }: OA
}

if (!config.clientId || !config.clientSecret) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_BATTLEDOTNET_CLIENT_ID or NUXT_OAUTH_BATTLEDOTNET_CLIENT_SECRET env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'battledotnet', ['clientId', 'clientSecret'], onError,
)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/cognito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -56,12 +57,7 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }: OAuthCo
const { code } = getQuery(event)

if (!config.clientId || !config.clientSecret || !config.userPoolId || !config.region) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_COGNITO_CLIENT_ID, NUXT_OAUTH_COGNITO_CLIENT_SECRET, NUXT_OAUTH_COGNITO_USER_POOL_ID, or NUXT_OAUTH_COGNITO_REGION env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'cognito', ['clientId', 'clientSecret', 'userPoolId', 'region'], onError)
}

const urlBase = config?.domain || `${config.userPoolId}.auth.${config.region}.amazoncognito.com`
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -69,12 +70,7 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }: OAuthCo
const { code } = getQuery(event)

if (!config.clientId || !config.clientSecret) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_DISCORD_CLIENT_ID or NUXT_OAUTH_DISCORD_CLIENT_SECRET env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'discord', ['clientId', 'clientSecret'], onError)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
9 changes: 2 additions & 7 deletions src/runtime/server/lib/oauth/facebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'h3'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -86,13 +87,7 @@ export function oauthFacebookEventHandler({
}

if (!config.clientId) {
const error = createError({
statusCode: 500,
message:
'Missing NUXT_OAUTH_FACEBOOK_CLIENT_ID or NUXT_OAUTH_FACEBOOK_CLIENT_SECRET env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'facebook', ['clientId'], onError)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -76,12 +77,7 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }: OAuthCon
}

if (!config.clientId || !config.clientSecret) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_GITHUB_CLIENT_ID or NUXT_OAUTH_GITHUB_CLIENT_SECRET env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'github', ['clientId', 'clientSecret'], onError)
}

if (!query.code) {
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -79,12 +80,7 @@ export function oauthGoogleEventHandler({
const { code } = getQuery(event)

if (!config.clientId) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_GOOGLE_CLIENT_ID env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'google', ['clientId'], onError)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
9 changes: 2 additions & 7 deletions src/runtime/server/lib/oauth/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -80,13 +81,7 @@ export function oauthKeycloakEventHandler({
|| !config.serverUrl
|| !config.realm
) {
const error = createError({
statusCode: 500,
message:
'Missing NUXT_OAUTH_KEYCLOAK_CLIENT_ID or NUXT_OAUTH_KEYCLOAK_CLIENT_SECRET or NUXT_OAUTH_KEYCLOAK_SERVER_URL or NUXT_OAUTH_KEYCLOAK_REALM env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'keycloak', ['clientId', 'clientSecret', 'serverUrl', 'realm'], onError)
}

const realmURL = `${config.serverUrl}/realms/${config.realm}`
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/linkedin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event, H3Error } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'

export interface OAuthLinkedInConfig {
Expand Down Expand Up @@ -66,12 +67,7 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }: OAuthC
const { code } = getQuery(event)

if (!config.clientId || !config.clientSecret) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_LINKEDIN_CLIENT_ID or NUXT_OAUTH_LINKEDIN_CLIENT_SECRET env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'linkedin', ['clientId', 'clientSecret'], onError)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/microsoft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event, H3Error } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'

export interface OAuthMicrosoftConfig {
Expand Down Expand Up @@ -72,12 +73,7 @@ export function oauthMicrosoftEventHandler({ config, onSuccess, onError }: OAuth
const { code } = getQuery(event)

if (!config.clientId || !config.clientSecret || !config.tenant) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_MICROSOFT_CLIENT_ID or NUXT_OAUTH_MICROSOFT_CLIENT_SECRET or NUXT_OAUTH_MICROSOFT_TENANT env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'microsoft', ['clientId', 'clientSecret', 'tenant'], onError)
}

const authorizationURL = config.authorizationURL || `https://login.microsoftonline.com/${config.tenant}/oauth2/v2.0/authorize`
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/paypal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -74,12 +75,7 @@ export function oauthPaypalEventHandler({ config, onSuccess, onError }: OAuthCon
const { code } = getQuery(event)

if (!config.clientId) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_PAYPAL_CLIENT_ID env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'paypal', ['clientId'], onError)
}

let paypalAPI = 'api-m.paypal.com'
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -64,12 +65,7 @@ export function oauthSpotifyEventHandler({ config, onSuccess, onError }: OAuthCo
const { code } = getQuery(event)

if (!config.clientId || !config.clientSecret) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_SPOTIFY_CLIENT_ID or NUXT_OAUTH_SPOTIFY_CLIENT_SECRET env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'spotify', ['clientId', 'clientSecret'], onError)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/steam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -34,12 +35,7 @@ export function oauthSteamEventHandler({ config, onSuccess, onError }: OAuthConf
const query: Record<string, string> = getQuery(event)

if (!config.apiKey) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_STEAM_API_KEY env variable.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'steam', ['apiKey'], onError)
}

if (!query['openid.claimed_id']) {
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -67,12 +68,7 @@ export function oauthTwitchEventHandler({ config, onSuccess, onError }: OAuthCon
const { code } = getQuery(event)

if (!config.clientId) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_TWITCH_CLIENT_ID env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'twitch', ['clientId'], onError)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -86,12 +87,7 @@ export function oauthXEventHandler({
const { code } = getQuery(event)

if (!config.clientId) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_X_CLIENT_ID env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'x', ['clientId'], onError)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/server/lib/oauth/xsuaa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -41,12 +42,7 @@ export function oauthXSUAAEventHandler({ config, onSuccess, onError }: OAuthConf
const { code } = getQuery(event)

if (!config.clientId || !config.clientSecret || !config.domain) {
const error = createError({
statusCode: 500,
message: 'Missing NUXT_OAUTH_XSUAA_CLIENT_ID or NUXT_OAUTH_XSUAA_CLIENT_SECRET or NUXT_OAUTH_XSUAA_DOMAIN env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'xsuaa', ['clientId', 'clientSecret', 'domain'], onError)
}
const authorizationURL = `https://${config.domain}/oauth/authorize`
const tokenURL = `https://${config.domain}/oauth/token`
Expand Down
9 changes: 2 additions & 7 deletions src/runtime/server/lib/oauth/yandex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'h3'
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -78,13 +79,7 @@ export function oauthYandexEventHandler({
const { code } = getQuery(event)

if (!config.clientId || !config.clientSecret) {
const error = createError({
statusCode: 500,
message:
'Missing NUXT_OAUTH_YANDEX_CLIENT_ID or NUXT_OAUTH_YANDEX_CLIENT_SECRET env variables.',
})
if (!onError) throw error
return onError(event, error)
return handleMissingConfiguration(event, 'yandex', ['clientId', 'clientSecret'], onError)
}

const redirectURL = config.redirectURL || getRequestURL(event).href
Expand Down
Loading

0 comments on commit 5675aaf

Please sign in to comment.